|
| 1 | +import path from 'node:path'; |
| 2 | +import { afterEach, expect, test, vi } from 'vitest'; |
| 3 | + |
| 4 | +vi.mock('../../utils/exec.ts', async (importOriginal) => { |
| 5 | + const actual = await importOriginal<typeof import('../../utils/exec.ts')>(); |
| 6 | + return { ...actual, runCmd: vi.fn() }; |
| 7 | +}); |
| 8 | + |
| 9 | +import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; |
| 10 | +import { WEB_DESKTOP_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts'; |
| 11 | +import { AGENT_BROWSER_TIMEOUT_MS } from '../../platforms/web/agent-browser-provider.ts'; |
| 12 | +import { installFakeManagedAgentBrowser } from '../../platforms/web/__tests__/test-utils.ts'; |
| 13 | +import { runCmd } from '../../utils/exec.ts'; |
| 14 | +import { SessionStore } from '../session-store.ts'; |
| 15 | +import type { SessionState } from '../types.ts'; |
| 16 | +import { |
| 17 | + resolveDaemonSessionTeardownTimeoutMs, |
| 18 | + teardownDaemonSessionForShutdown, |
| 19 | + WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS, |
| 20 | +} from './daemon-runtime.ts'; |
| 21 | + |
| 22 | +const mockRunCmd = vi.mocked(runCmd); |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + vi.useRealTimers(); |
| 26 | + vi.clearAllMocks(); |
| 27 | +}); |
| 28 | + |
| 29 | +function makeWebSession(name: string): SessionState { |
| 30 | + return { name, device: WEB_DESKTOP_DEVICE, createdAt: Date.now(), actions: [] }; |
| 31 | +} |
| 32 | + |
| 33 | +test('daemon session teardown budget extends for an open web session', () => { |
| 34 | + const webSession = makeWebSession('budget-web-session'); |
| 35 | + const androidSession: SessionState = { |
| 36 | + name: 'budget-android-session', |
| 37 | + device: { |
| 38 | + platform: 'android', |
| 39 | + id: 'emulator-5554', |
| 40 | + name: 'Pixel', |
| 41 | + kind: 'emulator', |
| 42 | + booted: true, |
| 43 | + }, |
| 44 | + createdAt: Date.now(), |
| 45 | + actions: [], |
| 46 | + }; |
| 47 | + |
| 48 | + expect( |
| 49 | + resolveDaemonSessionTeardownTimeoutMs(webSession) - |
| 50 | + resolveDaemonSessionTeardownTimeoutMs(androidSession), |
| 51 | + ).toBe(WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS); |
| 52 | +}); |
| 53 | + |
| 54 | +// Pins the "mirrors AGENT_BROWSER_TIMEOUT_MS" comment on WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS |
| 55 | +// as a checked invariant rather than a claim nothing enforces: if agent-browser-provider.ts's own |
| 56 | +// per-call timeout changes, this budget must move with it in the same PR. |
| 57 | +test('the web-close teardown budget stays pinned to one agent-browser CLI call timeout', () => { |
| 58 | + expect(WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS).toBe(AGENT_BROWSER_TIMEOUT_MS); |
| 59 | +}); |
| 60 | + |
| 61 | +// The agent-browser CLI call this step makes has its own 30s internal timeout |
| 62 | +// (AGENT_BROWSER_TIMEOUT_MS in agent-browser-provider.ts), well past the 5s base teardown |
| 63 | +// budget every other resource shares; without the extended budget above, a close that takes |
| 64 | +// longer than 5s (closing ~15 Chrome processes under load is plausible) would be abandoned by |
| 65 | +// the daemon's own race before agent-browser ever got to finish it. |
| 66 | +test('daemon shutdown awaits a slow web close inside its extended budget', async () => { |
| 67 | + vi.useFakeTimers(); |
| 68 | + const root = mkdtempForTestSync('agent-device-shutdown-web-close-slow-'); |
| 69 | + installFakeManagedAgentBrowser(root); |
| 70 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 71 | + const session = makeWebSession('shutdown-slow-web-session'); |
| 72 | + sessionStore.set(session.name, session); |
| 73 | + mockRunCmd.mockImplementation( |
| 74 | + async () => |
| 75 | + await new Promise((resolve) => { |
| 76 | + setTimeout( |
| 77 | + () => |
| 78 | + resolve({ |
| 79 | + stdout: JSON.stringify({ success: true, data: {} }), |
| 80 | + stderr: '', |
| 81 | + exitCode: 0, |
| 82 | + }), |
| 83 | + 20_000, |
| 84 | + ); |
| 85 | + }), |
| 86 | + ); |
| 87 | + const stderrChunks: string[] = []; |
| 88 | + |
| 89 | + const teardown = teardownDaemonSessionForShutdown({ |
| 90 | + session, |
| 91 | + sessionStore, |
| 92 | + stateDir: root, |
| 93 | + stderr: { write: (chunk) => stderrChunks.push(chunk) }, |
| 94 | + }); |
| 95 | + await vi.advanceTimersByTimeAsync(20_000); |
| 96 | + await teardown; |
| 97 | + |
| 98 | + expect(stderrChunks.join('')).not.toMatch(/timed out/); |
| 99 | + expect(sessionStore.get(session.name)).toBeUndefined(); |
| 100 | +}); |
| 101 | + |
| 102 | +// #1868: SIGTERM (or any other daemon-shutdown teardown) must tell agent-browser to close its |
| 103 | +// fleet right away instead of leaving the ~15 Chrome processes for agent-browser's own multi- |
| 104 | +// minute idle timer. This is the daemon-shutdown-entry-point counterpart to |
| 105 | +// daemon-runtime-recording-teardown.test.ts's coverage of the #1325 recording step. |
| 106 | +test('daemon shutdown closes an open web session immediately, without waiting for close', async () => { |
| 107 | + const root = mkdtempForTestSync('agent-device-shutdown-web-close-'); |
| 108 | + installFakeManagedAgentBrowser(root); |
| 109 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 110 | + const session = makeWebSession('shutdown-web-session'); |
| 111 | + // Teardown runs while the session it is tearing down is still in the store (session deletion |
| 112 | + // happens after), which is also what keeps agent-browser's provider-startup orphan sweep from |
| 113 | + // treating this session's own browser as an orphan and scanning real host processes for it. |
| 114 | + sessionStore.set(session.name, session); |
| 115 | + mockRunCmd.mockResolvedValue({ |
| 116 | + stdout: JSON.stringify({ success: true, data: {} }), |
| 117 | + stderr: '', |
| 118 | + exitCode: 0, |
| 119 | + }); |
| 120 | + const stderrChunks: string[] = []; |
| 121 | + |
| 122 | + await teardownDaemonSessionForShutdown({ |
| 123 | + session, |
| 124 | + sessionStore, |
| 125 | + stateDir: root, |
| 126 | + stderr: { write: (chunk) => stderrChunks.push(chunk) }, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(stderrChunks.join('')).toBe(''); |
| 130 | + expect(sessionStore.get(session.name)).toBeUndefined(); |
| 131 | + const closeCall = mockRunCmd.mock.calls.find(([, args]) => (args as string[]).includes('close')); |
| 132 | + expect(closeCall?.[1]).toEqual(['close', '--json', '--session', session.name]); |
| 133 | +}); |
| 134 | + |
| 135 | +test('daemon shutdown reports a web close failure on stderr instead of losing it silently', async () => { |
| 136 | + const root = mkdtempForTestSync('agent-device-shutdown-web-close-failure-'); |
| 137 | + installFakeManagedAgentBrowser(root); |
| 138 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 139 | + const session = makeWebSession('shutdown-web-close-failure-session'); |
| 140 | + sessionStore.set(session.name, session); |
| 141 | + mockRunCmd.mockResolvedValue({ |
| 142 | + stdout: JSON.stringify({ success: false, error: 'no active browser session' }), |
| 143 | + stderr: '', |
| 144 | + exitCode: 1, |
| 145 | + }); |
| 146 | + const stderrChunks: string[] = []; |
| 147 | + |
| 148 | + await teardownDaemonSessionForShutdown({ |
| 149 | + session, |
| 150 | + sessionStore, |
| 151 | + stateDir: root, |
| 152 | + stderr: { write: (chunk) => stderrChunks.push(chunk) }, |
| 153 | + }); |
| 154 | + |
| 155 | + // Best-effort: the failure is surfaced, but it never blocks the session from being torn down. |
| 156 | + expect(stderrChunks.join('')).toMatch(/web_browser/); |
| 157 | + expect(sessionStore.get(session.name)).toBeUndefined(); |
| 158 | +}); |
0 commit comments