@@ -7,13 +7,15 @@ import assert from 'node:assert/strict';
77import fs from 'node:fs' ;
88import path from 'node:path' ;
99import { test } from 'node:test' ;
10+ import { runCmdSync } from '../../src/utils/exec.ts' ;
1011import { runWithContentionRetry , type TestRun } from './contention-retry-lane.ts' ;
1112import { processBlockers } from './contention-retry-blockers.ts' ;
1213import contentionRetryReporter , {
1314 failedTestCase ,
1415 runBlockers ,
1516 writeFailureReport ,
1617} from './contention-retry-reporter.ts' ;
18+ import { RUNNER_TIMEOUT_META } from './runner-timeout-meta.ts' ;
1719import {
1820 CONTENTION_RETRY_FILES ,
1921 expiredRetryEntries ,
@@ -26,6 +28,7 @@ import {
2628} from './contention-retry.ts' ;
2729
2830const repoRoot = path . resolve ( import . meta. dirname , '../..' ) ;
31+ const FIXTURE_CONFIG = 'test/contention-retry-fixtures/vitest.fixture.config.ts' ;
2932
3033const LISTED = 'src/daemon/__tests__/request-router-open.test.ts' ;
3134// Vitest's own timeout error, verbatim (@vitest/runner `makeTimeoutError`).
@@ -51,23 +54,21 @@ function assertionFailure(overrides: Partial<TestFailure> = {}): TestFailure {
5154 return failure ( { message : ASSERTION_MESSAGE , timeout : false , ...overrides } ) ;
5255}
5356
54- const TIMEOUT_MS = 5000 ;
57+ /** What the runner-timeout setup file writes on a test the runner aborted. */
58+ const RUNNER_ABORTED = { [ RUNNER_TIMEOUT_META ] : true } ;
5559
5660function testCaseStub (
5761 errors : ReadonlyArray < Record < string , unknown > > ,
58- overrides : { state ?: string ; timeoutMs ?: number ; durationMs ?: number } = { } ,
62+ overrides : { state ?: string ; meta ?: Record < string , unknown > } = { } ,
5963) : unknown {
6064 return {
6165 fullName : 'opens a session' ,
6266 module : { moduleId : `${ repoRoot } /${ LISTED } ` } ,
63- options : { timeout : overrides . timeoutMs ?? TIMEOUT_MS } ,
64- diagnostic : ( ) => ( { duration : overrides . durationMs ?? TIMEOUT_MS + 4 } ) ,
67+ meta : ( ) => overrides . meta ?? RUNNER_ABORTED ,
6568 result : ( ) => ( { state : overrides . state ?? 'failed' , errors } ) ,
6669 } ;
6770}
6871
69- const RAN_TO_BUDGET = { timeoutMs : TIMEOUT_MS , durationMs : TIMEOUT_MS + 4 } ;
70-
7172function lane ( runs : { first : TestRun ; retry ?: TestRun ; today ?: Date } ) : {
7273 result : ReturnType < typeof runWithContentionRetry > ;
7374 rerun : string [ ] [ ] ;
@@ -150,57 +151,61 @@ test('the expiry gate fails the run before any test executes', async () => {
150151 assert . deepEqual ( rerun , [ ] ) ;
151152} ) ;
152153
153- test ( 'only a test the runner aborted at its own budget classifies as a timeout' , ( ) => {
154- assert . ok ( isRunnerTimeout ( [ VITEST_TIMEOUT ] , RAN_TO_BUDGET ) ) ;
155- assert . ok (
156- isRunnerTimeout (
157- [
158- {
159- name : 'Error' ,
160- message :
161- 'Hook timed out in 10000ms.\nIf this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".' ,
162- } ,
163- ] ,
164- RAN_TO_BUDGET ,
165- ) ,
166- ) ;
167- // Runner metadata is the primary evidence: test code cannot make its own
168- // failure consume the whole configured budget and still choose the error.
169- assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , { timeoutMs : TIMEOUT_MS , durationMs : 12 } ) ) ;
170- assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , { timeoutMs : undefined , durationMs : 99_999 } ) ) ;
171- assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , { timeoutMs : 0 , durationMs : 0 } ) ) ;
172- assert . ok ( ! isRunnerTimeout ( [ ] , RAN_TO_BUDGET ) ) ;
173- // An assertion must not be able to talk its way into a retry, whatever it says.
154+ test ( 'only the runner-owned abort mark classifies a failure as a timeout' , ( ) => {
155+ assert . ok ( isRunnerTimeout ( [ VITEST_TIMEOUT ] , RUNNER_ABORTED ) ) ;
156+ // Without the runner's mark, no message and no error shape can earn a retry.
174157 for ( const impostor of [
158+ { name : 'Error' , message : VITEST_TIMEOUT . message } ,
175159 { name : 'AssertionError' , message : VITEST_TIMEOUT . message } ,
176- { name : 'AssertionError' , message : 'expected ETIMEDOUT, got ECONNRESET' } ,
177- { name : 'Error' , message : `saw: ${ VITEST_TIMEOUT . message } ` } ,
178160 { name : 'Error' , message : 'connect ETIMEDOUT 127.0.0.1:8080' } ,
179161 { name : 'Error' , message : 'Closing timeout while tearing down the daemon' } ,
180- { name : 'Error' , message : VITEST_TIMEOUT . message , expected : 'OK' , actual : 'DEVICE_IN_USE' } ,
181162 ] ) {
182- assert . ok ( ! isRunnerTimeout ( [ impostor ] , RAN_TO_BUDGET ) , `${ impostor . name } : ${ impostor . message } ` ) ;
163+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , { } ) , `${ impostor . name } : ${ impostor . message } ` ) ;
164+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , undefined ) ) ;
165+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , { [ RUNNER_TIMEOUT_META ] : 'yes' } ) ) ;
183166 }
167+ // An aborted test that also produced an assertion diff is a regression.
168+ assert . ok (
169+ ! isRunnerTimeout (
170+ [ VITEST_TIMEOUT , { name : 'AssertionError' , message : 'x' , expected : 1 , actual : 2 } ] ,
171+ RUNNER_ABORTED ,
172+ ) ,
173+ ) ;
174+ assert . ok ( ! isRunnerTimeout ( [ ] , RUNNER_ABORTED ) ) ;
184175} ) ;
185176
186- test ( 'an exact-template Error thrown by the test itself never retries' , async ( ) => {
187- // The forgery the classifier must refuse: right name, right message, right
188- // shape — but the test returned long before its budget was up.
189- const forged = failedTestCase (
190- testCaseStub ( [ { name : 'Error' , message : VITEST_TIMEOUT . message } ] , { durationMs : 7 } ) as never ,
191- ) ;
192- assert . ok ( forged ) ;
193- assert . equal ( forged . timeout , false , 'a hand-thrown timeout message is not a runner timeout' ) ;
194- const { result, rerun } = lane ( { first : { ok : false , failures : [ { ...forged , file : LISTED } ] } } ) ;
195- const resolved = await result ;
196- assert . equal ( resolved . ok , false ) ;
197- assert . deepEqual ( rerun , [ ] ) ;
198- assert . equal ( resolved . envelope . data . retryCount , 0 ) ;
177+ test ( 'a real Vitest run marks only runner-aborted tests as retry-eligible' , ( ) => {
178+ // The classifier's inputs come from the runner, so this case drives a real
179+ // child Vitest run over test/contention-retry-fixtures/ rather than stubs.
180+ const report = path . join ( repoRoot , '.tmp/contention-retry/fixture-gate.json' ) ;
181+ fs . rmSync ( report , { force : true } ) ;
182+ runCmdSync ( 'pnpm' , [ 'exec' , 'vitest' , 'run' , '--config' , FIXTURE_CONFIG ] , {
183+ cwd : repoRoot ,
184+ env : { ...process . env , CONTENTION_RETRY_FAILURES : report } ,
185+ allowFailure : true ,
186+ } ) ;
187+ const { failures } = parseFailureReport ( JSON . parse ( fs . readFileSync ( report , 'utf8' ) ) , repoRoot ) ;
188+ assert . deepEqual ( Object . fromEntries ( failures . map ( ( entry ) => [ entry . testName , entry . timeout ] ) ) , {
189+ 'genuine runner timeout' : true ,
190+ 'timeout message thrown immediately' : false ,
191+ // The forgery this gate exists for: blocking the event loop past the
192+ // budget does not abort the signal, so the throw stays a plain failure.
193+ 'timeout message thrown after blocking the event loop past the budget' : false ,
194+ 'assertion failure after blocking the event loop past the budget' : false ,
195+ 'plain assertion failure' : false ,
196+ } ) ;
197+ // Only the genuine timeout may reach a rerun.
198+ const listed = failures . map ( ( entry ) => ( { ...entry , file : LISTED } ) ) ;
199+ const plan = planContentionRetry ( listed ) ;
200+ assert . equal ( plan . retry , false ) ;
201+ assert . equal ( planContentionRetry ( listed . filter ( ( entry ) => entry . timeout ) ) . retry , true ) ;
199202} ) ;
200203
201204test ( 'an assertion message quoting a timeout still fails on the first run' , async ( ) => {
202205 const impostor = failedTestCase (
203- testCaseStub ( [ { name : 'AssertionError' , message : VITEST_TIMEOUT . message } ] ) as never ,
206+ testCaseStub ( [ { name : 'AssertionError' , message : VITEST_TIMEOUT . message } ] , {
207+ meta : { } ,
208+ } ) as never ,
204209 ) ;
205210 assert . ok ( impostor ) ;
206211 assert . equal ( impostor . timeout , false ) ;
0 commit comments