diff --git a/src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts b/src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts index f71e58dcc8b..c3f0e02f00c 100644 --- a/src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts +++ b/src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts @@ -280,29 +280,49 @@ describe('OrcaRuntimeService', () => { }) it('yields chunked terminal input through immediates between writes', async () => { - const immediate = vi.spyOn(globalThis, 'setImmediate') + const writes: string[] = [] + const bodyWritten = Promise.withResolvers() + let turnObserved = false + let secondWriteFollowedTurn = false + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: (_ptyId, data) => { + writes.push(data) + if (writes.length === 1) { + setImmediate(() => { + turnObserved = true + }) + } else if (writes.length === 2) { + secondWriteFollowedTurn = turnObserved + bodyWritten.resolve() + } + return true + }, + kill: () => true, + getForegroundProcess: async () => null + }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`) + + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'], shouldClearNativeTimers: true }) try { - const writes: string[] = [] - const runtime = new OrcaRuntimeService(store) - runtime.setPtyController({ - spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), - write: (_ptyId, data) => { - writes.push(data) - return true - }, - kill: () => true, - getForegroundProcess: async () => null - }) - const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`) - const text = `${'x'.repeat(TERMINAL_INPUT_CHUNK_MAX_BYTES)}\nline two\nline three` - await runtime.sendTerminal(handle, { text, enter: true }, { inputKind: 'driving' }) + await Promise.all([ + runtime.sendTerminal(handle, { text, enter: true }, { inputKind: 'driving' }), + (async () => { + await bodyWritten.promise + // Let the write continuation arm the separate Enter timeout. + await new Promise((resolve) => setImmediate(resolve)) + await vi.runAllTimersAsync() + })() + ]) + expect(secondWriteFollowedTurn).toBe(true) expect(writes.at(-1)).toBe('\r') expect(writes.slice(0, -1).join('')).toBe(text) - expect(immediate).toHaveBeenCalled() } finally { - immediate.mockRestore() + vi.clearAllTimers() + vi.useRealTimers() } })