1- // Vitest supplies the BDD helpers plus the mock factory we rely on in this file.
21import { beforeEach , describe , expect , it , vi } from "vitest" ;
3- // Import the concrete handlers so we can exercise their logic in isolation.
42import { ActHandler } from "../lib/v3/handlers/actHandler" ;
53import { ExtractHandler } from "../lib/v3/handlers/extractHandler" ;
64import { ObserveHandler } from "../lib/v3/handlers/observeHandler" ;
7- // Static Page type to satisfy handler constructor contracts without a real page.
85import type { Page } from "../lib/v3/understudy/page" ;
9- // Pull in the client option type so we can build a minimal config object.
106import type { ClientOptions } from "../lib/v3/types/public/model" ;
11- // Reference to the abstract client type for our fake LLM client implementation.
127import type { LLMClient } from "../lib/v3/llm/LLMClient" ;
13- // We stub the timeout guard to control when ensureTimeRemaining throws.
148import { createTimeoutGuard } from "../lib/v3/handlers/handlerUtils/timeoutGuard" ;
15- // This helper is a heavy async call we expect to run before the timeout trips.
169import { waitForDomNetworkQuiet } from "../lib/v3/handlers/handlerUtils/actHandlerUtils" ;
17- // Snapshotting should never occur after the guard fires, so we spy on it as well.
1810import { captureHybridSnapshot } from "../lib/v3/understudy/a11y/snapshot" ;
19- // Import the timeout error types for instanceof checks.
2011import {
2112 ActTimeoutError ,
2213 ExtractTimeoutError ,
2314 ObserveTimeoutError ,
2415} from "../lib/v3/types/public/sdkErrors" ;
25- // Import inference functions so we can mock them
2616import {
2717 act as actInference ,
2818 extract as extractInference ,
2919 observe as observeInference ,
3020} from "../lib/inference" ;
31- // Import V3FunctionName for metrics validation
3221import { V3FunctionName } from "../lib/v3/types/public/methods" ;
3322
34- // Replace the timeout guard factory with a mock so we can inject a deterministic guard.
3523vi . mock ( "../lib/v3/handlers/handlerUtils/timeoutGuard" , ( ) => ( {
3624 createTimeoutGuard : vi . fn ( ) ,
3725} ) ) ;
3826
39- // Mock out the rest of the handler utils to keep the test from touching real browser logic.
4027vi . mock ( "../lib/v3/handlers/handlerUtils/actHandlerUtils" , ( ) => ( {
4128 waitForDomNetworkQuiet : vi . fn ( ) ,
4229 performUnderstudyMethod : vi . fn ( ) ,
4330} ) ) ;
4431
45- // Snapshot helpers are also mocked; we just care about whether they are invoked or not.
4632vi . mock ( "../lib/v3/understudy/a11y/snapshot" , ( ) => ( {
4733 captureHybridSnapshot : vi . fn ( ) ,
4834 diffCombinedTrees : vi . fn ( ) ,
4935} ) ) ;
5036
51- // Mock inference calls
5237vi . mock ( "../lib/inference" , ( ) => ( {
5338 act : vi . fn ( ) ,
5439 extract : vi . fn ( ) ,
5540 observe : vi . fn ( ) ,
5641} ) ) ;
5742
58- // ============================================================================
59- // ActHandler Timeout Tests
60- // ============================================================================
6143describe ( "ActHandler timeout guard" , ( ) => {
6244 beforeEach ( ( ) => {
6345 vi . clearAllMocks ( ) ;
@@ -188,9 +170,6 @@ describe("ActHandler timeout guard", () => {
188170 } ) ;
189171} ) ;
190172
191- // ============================================================================
192- // ActHandler Two-Step Timeout Tests
193- // ============================================================================
194173describe ( "ActHandler two-step timeout" , ( ) => {
195174 beforeEach ( ( ) => {
196175 vi . clearAllMocks ( ) ;
@@ -271,9 +250,6 @@ describe("ActHandler two-step timeout", () => {
271250 } ) ;
272251} ) ;
273252
274- // ============================================================================
275- // ActHandler Self-Heal Timeout Tests
276- // ============================================================================
277253describe ( "ActHandler self-heal timeout" , ( ) => {
278254 beforeEach ( ( ) => {
279255 vi . clearAllMocks ( ) ;
@@ -420,9 +396,6 @@ describe("ActHandler self-heal timeout", () => {
420396 } ) ;
421397} ) ;
422398
423- // ============================================================================
424- // ExtractHandler Timeout Tests
425- // ============================================================================
426399describe ( "ExtractHandler timeout guard" , ( ) => {
427400 beforeEach ( ( ) => {
428401 vi . clearAllMocks ( ) ;
@@ -586,9 +559,6 @@ describe("ExtractHandler timeout guard", () => {
586559 } ) ;
587560} ) ;
588561
589- // ============================================================================
590- // ObserveHandler Timeout Tests
591- // ============================================================================
592562describe ( "ObserveHandler timeout guard" , ( ) => {
593563 beforeEach ( ( ) => {
594564 vi . clearAllMocks ( ) ;
@@ -752,9 +722,6 @@ describe("ObserveHandler timeout guard", () => {
752722 } ) ;
753723} ) ;
754724
755- // ============================================================================
756- // No-Timeout Success Path Tests
757- // ============================================================================
758725describe ( "No-timeout success paths" , ( ) => {
759726 beforeEach ( ( ) => {
760727 vi . clearAllMocks ( ) ;
@@ -815,11 +782,11 @@ describe("No-timeout success paths", () => {
815782 expect ( result . success ) . toBe ( true ) ;
816783 expect ( metricsCallback ) . toHaveBeenCalledWith (
817784 V3FunctionName . ACT ,
818- 100 , // prompt_tokens
819- 50 , // completion_tokens
820- 10 , // reasoning_tokens
821- 5 , // cached_input_tokens
822- 500 , // inference_time_ms
785+ 100 ,
786+ 50 ,
787+ 10 ,
788+ 5 ,
789+ 500 ,
823790 ) ;
824791 } ) ;
825792
@@ -866,11 +833,11 @@ describe("No-timeout success paths", () => {
866833 expect ( result ) . toHaveProperty ( "title" , "Test Title" ) ;
867834 expect ( metricsCallback ) . toHaveBeenCalledWith (
868835 V3FunctionName . EXTRACT ,
869- 200 , // prompt_tokens
870- 100 , // completion_tokens
871- 20 , // reasoning_tokens
872- 10 , // cached_input_tokens
873- 800 , // inference_time_ms
836+ 200 ,
837+ 100 ,
838+ 20 ,
839+ 10 ,
840+ 800 ,
874841 ) ;
875842 } ) ;
876843
@@ -922,11 +889,11 @@ describe("No-timeout success paths", () => {
922889 expect ( result [ 0 ] ) . toHaveProperty ( "description" , "Submit button" ) ;
923890 expect ( metricsCallback ) . toHaveBeenCalledWith (
924891 V3FunctionName . OBSERVE ,
925- 150 , // prompt_tokens
926- 75 , // completion_tokens
927- 15 , // reasoning_tokens
928- 8 , // cached_input_tokens
929- 600 , // inference_time_ms
892+ 150 ,
893+ 75 ,
894+ 15 ,
895+ 8 ,
896+ 600 ,
930897 ) ;
931898 } ) ;
932899
@@ -1044,10 +1011,6 @@ describe("No-timeout success paths", () => {
10441011 } ) ;
10451012} ) ;
10461013
1047- // ============================================================================
1048- // Helper Functions
1049- // ============================================================================
1050-
10511014interface BuildActHandlerOptions {
10521015 selfHeal ?: boolean ;
10531016 onMetrics ?: (
@@ -1074,11 +1037,11 @@ function buildActHandler(options: BuildActHandlerOptions = {}): ActHandler {
10741037 "gpt-4o" ,
10751038 defaultClientOptions ,
10761039 resolveLlmClient ,
1077- undefined , // systemPrompt
1078- false , // logInferenceToFile
1040+ undefined ,
1041+ false ,
10791042 options . selfHeal ?? false ,
10801043 options . onMetrics ,
1081- undefined , // defaultDomSettleTimeoutMs
1044+ undefined ,
10821045 ) ;
10831046}
10841047
@@ -1109,9 +1072,9 @@ function buildExtractHandler(
11091072 "gpt-4o" ,
11101073 defaultClientOptions ,
11111074 resolveLlmClient ,
1112- undefined , // systemPrompt
1113- false , // logInferenceToFile
1114- false , // experimental
1075+ undefined ,
1076+ false ,
1077+ false ,
11151078 options . onMetrics ,
11161079 ) ;
11171080}
@@ -1143,9 +1106,9 @@ function buildObserveHandler(
11431106 "gpt-4o" ,
11441107 defaultClientOptions ,
11451108 resolveLlmClient ,
1146- undefined , // systemPrompt
1147- false , // logInferenceToFile
1148- false , // experimental
1109+ undefined ,
1110+ false ,
1111+ false ,
11491112 options . onMetrics ,
11501113 ) ;
11511114}
0 commit comments