|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { VmAgentContainer } from '../../../src/durable-objects/vm-agent-container'; |
| 4 | + |
| 5 | +// Regression test for the restored-session prompt failure: `proxyHttp` read the |
| 6 | +// container state ONCE before `wakeFromSnapshot()`, then applied the |
| 7 | +// `stopped`/`stopped_with_code` -> 410 guard using that stale pre-wake state. |
| 8 | +// A freshly-woken, restored container was therefore rejected with 410 (surfaced |
| 9 | +// by the Worker as a generic 500), even though restore succeeded. The fix |
| 10 | +// re-reads the container state after a successful wake. |
| 11 | + |
| 12 | +interface FakeState { |
| 13 | + status: string; |
| 14 | +} |
| 15 | + |
| 16 | +function makeFake(opts: { |
| 17 | + statuses: string[]; // sequence returned by getState() |
| 18 | + lifecycleStatus: string; |
| 19 | + wakeOk: boolean; |
| 20 | +}) { |
| 21 | + const getState = vi.fn<[], Promise<FakeState>>(); |
| 22 | + for (const s of opts.statuses) { |
| 23 | + getState.mockResolvedValueOnce({ status: s }); |
| 24 | + } |
| 25 | + getState.mockResolvedValue({ status: opts.statuses[opts.statuses.length - 1] }); |
| 26 | + |
| 27 | + const containerFetch = vi |
| 28 | + .fn() |
| 29 | + .mockResolvedValue(new Response('proxied', { status: 200 })); |
| 30 | + const wakeFromSnapshot = vi |
| 31 | + .fn() |
| 32 | + .mockResolvedValue(opts.wakeOk ? { ok: true } : { ok: false, message: 'degraded' }); |
| 33 | + |
| 34 | + const fake = { |
| 35 | + getState, |
| 36 | + containerFetch, |
| 37 | + wakeFromSnapshot, |
| 38 | + defaultPort: 8080, |
| 39 | + ctx: { storage: { get: vi.fn().mockResolvedValue(opts.lifecycleStatus) } }, |
| 40 | + }; |
| 41 | + return { fake, getState, containerFetch, wakeFromSnapshot }; |
| 42 | +} |
| 43 | + |
| 44 | +function callProxyHttp(fake: unknown, request: Request): Promise<Response> { |
| 45 | + return (VmAgentContainer.prototype as unknown as { |
| 46 | + proxyHttp: (this: unknown, request: Request, port?: number) => Promise<Response>; |
| 47 | + }).proxyHttp.call(fake, request); |
| 48 | +} |
| 49 | + |
| 50 | +describe('VmAgentContainer.proxyHttp wake state re-read', () => { |
| 51 | + it('proxies the prompt after a successful wake even though the pre-wake state was stopped', async () => { |
| 52 | + // Pre-wake getState -> stopped; post-wake getState -> running (fresh container). |
| 53 | + const { fake, getState, containerFetch, wakeFromSnapshot } = makeFake({ |
| 54 | + statuses: ['stopped', 'running'], |
| 55 | + lifecycleStatus: 'sleeping', |
| 56 | + wakeOk: true, |
| 57 | + }); |
| 58 | + |
| 59 | + const res = await callProxyHttp(fake, new Request('http://container/prompt', { method: 'POST' })); |
| 60 | + |
| 61 | + expect(wakeFromSnapshot).toHaveBeenCalledTimes(1); |
| 62 | + // State must be re-read after wake (once before, once after) so the stopped |
| 63 | + // guard sees the now-running container. |
| 64 | + expect(getState).toHaveBeenCalledTimes(2); |
| 65 | + // The request is proxied to the running container, NOT rejected with 410. |
| 66 | + expect(containerFetch).toHaveBeenCalledTimes(1); |
| 67 | + expect(res.status).toBe(200); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns 503 (not 410/proxy) when wake fails', async () => { |
| 71 | + const { fake, containerFetch } = makeFake({ |
| 72 | + statuses: ['stopped', 'stopped'], |
| 73 | + lifecycleStatus: 'sleeping', |
| 74 | + wakeOk: false, |
| 75 | + }); |
| 76 | + |
| 77 | + const res = await callProxyHttp(fake, new Request('http://container/prompt', { method: 'POST' })); |
| 78 | + |
| 79 | + expect(res.status).toBe(503); |
| 80 | + expect(containerFetch).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('still returns 410 for a genuinely stopped, non-sleeping container', async () => { |
| 84 | + const { fake, containerFetch, wakeFromSnapshot } = makeFake({ |
| 85 | + statuses: ['stopped'], |
| 86 | + lifecycleStatus: 'running', |
| 87 | + wakeOk: true, |
| 88 | + }); |
| 89 | + |
| 90 | + const res = await callProxyHttp(fake, new Request('http://container/prompt', { method: 'POST' })); |
| 91 | + |
| 92 | + expect(wakeFromSnapshot).not.toHaveBeenCalled(); |
| 93 | + expect(containerFetch).not.toHaveBeenCalled(); |
| 94 | + expect(res.status).toBe(410); |
| 95 | + }); |
| 96 | +}); |
0 commit comments