@@ -5,6 +5,28 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55import { clearConfigCache } from "../src/config" ;
66import plugin from "../src/index" ;
77
8+ // Hermetic stub: no child process ever spawns from this file. `setup()`
9+ // hardwires the real spawner (src/index.ts `run: runCommand`), so without
10+ // this every idle-triggered gate would shell out to real `git`/`false` —
11+ // the only test file in the suite that did. `false` mimics /bin/false
12+ // (exit 1, gate-failing); `git` mimics "not a repo" so gitChangedFiles
13+ // fails fast to [] exactly as it does in these temp dirs.
14+ vi . mock ( "../src/host" , async ( importOriginal ) => {
15+ const original = await importOriginal < typeof import ( "../src/host" ) > ( ) ;
16+ const runCommand : typeof original . runCommand = async ( command ) => {
17+ if ( command [ 0 ] === "false" ) {
18+ return { stdout : "" , stderr : "" , exitCode : 1 , timedOut : false } ;
19+ }
20+ if ( command [ 0 ] === "git" ) {
21+ return { stdout : "" , stderr : "not a git repo" , exitCode : 128 , timedOut : false } ;
22+ }
23+ return { stdout : "" , stderr : "" , exitCode : 0 , timedOut : false } ;
24+ } ;
25+ return { ...original , runCommand } ;
26+ } ) ;
27+
28+ type Cleanup = ( ) => Promise < void > | void ;
29+
830describe ( "issue #1: teardown must not brick tool calls" , ( ) => {
931 let directory : string ;
1032
@@ -54,11 +76,15 @@ describe("issue #1: teardown must not brick tool calls", () => {
5476 async function waitForPromptCalls (
5577 prompt : unknown ,
5678 expected : number ,
57- timeoutMs = 10000 ,
79+ // Below the default vitest 5s test timeout on purpose: on failure the
80+ // terminal expect below must be what reports, not a framework timeout.
81+ timeoutMs = 3000 ,
5882 ) : Promise < void > {
5983 const mock = prompt as { mock : { calls : unknown [ ] [ ] } } ;
6084 const deadline = Date . now ( ) + timeoutMs ;
61- while ( mock . mock . calls . length !== expected && Date . now ( ) < deadline ) {
85+ // `<` (not `!==`): an overshoot is already a failure — fail fast
86+ // instead of spinning the full timeout before the expect fires.
87+ while ( mock . mock . calls . length < expected && Date . now ( ) < deadline ) {
6288 await sleep ( 25 ) ;
6389 }
6490 expect ( mock . mock . calls . length ) . toBe ( expected ) ;
@@ -263,16 +289,15 @@ describe("issue #1: teardown must not brick tool calls", () => {
263289
264290 it ( "rapid setup/teardown cycles with in-flight streams leave no live stale loop (strain probe)" , async ( ) => {
265291 // Gate config that blocks on idle, so event-loop liveness is observable
266- // via ctx.session.prompt: `false` exits 1 instantly (hermetic, no real
267- // checks run) and skip_unchanged is off so the gate runs with no
268- // changed files.
292+ // via ctx.session.prompt: stubbed `false` exits 1 ( no child process
293+ // ever spawns — see the host mock at the top of this file) and
294+ // skip_unchanged is off so the gate runs with no changed files.
269295 writeFileSync (
270296 join ( directory , ".opencode-dev-framework.yml" ) ,
271297 'profile: standard\ncommands:\n typecheck: "false"\ngate:\n run_typecheck: true\n run_tests: false\n skip_unchanged: false\n block_on_failure: true\n' ,
272298 ) ;
273299 clearConfigCache ( ) ;
274300 const mod = plugin as unknown as { setup : ( c : unknown ) => Promise < unknown > } ;
275- type Cleanup = ( ) => Promise < void > | void ;
276301
277302 // Reload churn: five rapid setup/teardown pairs with hanging streams.
278303 const dead : Array < {
@@ -314,13 +339,16 @@ describe("issue #1: teardown must not brick tool calls", () => {
314339 expect ( live . ctx . session . prompt ) . toHaveBeenCalledWith (
315340 expect . objectContaining ( { sessionID : "stress-live" } ) ,
316341 ) ;
342+ // Quiescence: a double-firing loop would land its second prompt just
343+ // after the first — settle, then confirm the count is still exactly 1.
344+ await sleep ( 200 ) ;
345+ expect ( live . ctx . session . prompt ) . toHaveBeenCalledTimes ( 1 ) ;
317346
318347 await cleanupLive ( ) ;
319348 } ) ;
320349
321350 it ( "a throwing event subscriber neither breaks setup nor bricks the session" , async ( ) => {
322351 const mod = plugin as unknown as { setup : ( c : unknown ) => Promise < unknown > } ;
323- type Cleanup = ( ) => Promise < void > | void ;
324352
325353 // 1) subscribe() throws synchronously: the loop catches it, setup still
326354 // resolves, and the guardrail still works.
0 commit comments