|
| 1 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 2 | +import type { AppLogLiveHandle } from '@agent-device/contracts/app-log-runtime'; |
| 3 | +import { createDurableResourceEnvelope } from '@agent-device/capture-kit'; |
| 4 | +import { appLogResourceStore } from '../../../app-log-resource-store.ts'; |
| 5 | +import { |
| 6 | + sessionCloseShutdownFixture, |
| 7 | + type SessionState, |
| 8 | +} from './session-close-shutdown.fixtures.ts'; |
| 9 | +import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts'; |
| 10 | + |
| 11 | +const { |
| 12 | + handleSessionCommands, |
| 13 | + localRuntimeOwner, |
| 14 | + makeSession, |
| 15 | + makeSessionStore, |
| 16 | + noopInvoke, |
| 17 | + path, |
| 18 | + resetSessionCloseShutdownMocks, |
| 19 | +} = sessionCloseShutdownFixture; |
| 20 | + |
| 21 | +beforeEach(resetSessionCloseShutdownMocks); |
| 22 | + |
| 23 | +// An implicitly cwd-scoped session is *named* `default` but *stored* under |
| 24 | +// `cwd:<hash>:default` (see `SessionRef`). Its durable app-log record lives in |
| 25 | +// the address's directory, so teardown must address it by `sessionName`, not |
| 26 | +// `session.name`. When it used `session.name`, `close` reported |
| 27 | +// "App-log resource record is missing", left the record `open`/`active`, and |
| 28 | +// leaked the `log stream` child; the next `logs start` on that device then |
| 29 | +// failed with "has not reached a confirmed terminal state". |
| 30 | +test('close stops the app log of a cwd-scoped session whose name differs from its store address', async () => { |
| 31 | + const sessionStore = makeSessionStore(); |
| 32 | + const sessionAddress = 'cwd:0f803c4542a46e92:default'; |
| 33 | + const sessionName = 'default'; |
| 34 | + const device: SessionState['device'] = { |
| 35 | + platform: 'apple', |
| 36 | + appleOs: 'ios', |
| 37 | + id: '9105AAA0-3184-40BC-A9FC-46634C90DFFB', |
| 38 | + name: 'iPhone 16', |
| 39 | + kind: 'simulator', |
| 40 | + booted: true, |
| 41 | + }; |
| 42 | + const sessionDir = sessionStore.resolveSessionDir(sessionAddress); |
| 43 | + const outputPath = path.join(sessionDir, 'app.log'); |
| 44 | + const forceCleanup = vi.fn(async () => ({ status: 'cleaned' }) as const); |
| 45 | + const handle: AppLogLiveHandle = { |
| 46 | + inspect: () => ({ backend: 'ios-simulator', state: 'active', startedAt: Date.now() - 1000 }), |
| 47 | + finish: async () => ({ |
| 48 | + status: 'completed' as const, |
| 49 | + result: { backend: 'ios-simulator', outputPath, completedAt: Date.now() }, |
| 50 | + }), |
| 51 | + forceCleanup, |
| 52 | + [Symbol.asyncDispose]: async () => {}, |
| 53 | + }; |
| 54 | + const envelope = createDurableResourceEnvelope({ |
| 55 | + resourceKind: 'app-log', |
| 56 | + sessionId: sessionAddress, |
| 57 | + device: { id: device.id, family: 'apple', appleOs: 'ios', kind: 'simulator', target: 'mobile' }, |
| 58 | + owner: localRuntimeOwner('apple'), |
| 59 | + fence: { token: 'app-log-fence', generation: 1 }, |
| 60 | + lifecycle: 'open', |
| 61 | + descriptor: { |
| 62 | + version: 1, |
| 63 | + body: { |
| 64 | + transport: 'apple-log-stream', |
| 65 | + backend: 'ios-simulator', |
| 66 | + outputPath, |
| 67 | + pidPath: path.join(sessionDir, 'app-log.pid'), |
| 68 | + }, |
| 69 | + }, |
| 70 | + metadata: { phase: 'active' }, |
| 71 | + }); |
| 72 | + const session: SessionState = { |
| 73 | + ...makeSession(sessionName, device), |
| 74 | + appBundleId: 'com.apple.Preferences', |
| 75 | + appLog: { handle, envelope }, |
| 76 | + }; |
| 77 | + sessionStore.set(sessionAddress, session); |
| 78 | + const resourcePath = appLogResourceStore.resolvePath(sessionDir); |
| 79 | + appLogResourceStore.write(resourcePath, envelope); |
| 80 | + // The record must NOT exist under the bare name: that directory is a different session. |
| 81 | + expect( |
| 82 | + appLogResourceStore.read( |
| 83 | + appLogResourceStore.resolvePath(sessionStore.resolveSessionDir(sessionName)), |
| 84 | + ).status, |
| 85 | + ).toBe('missing'); |
| 86 | + |
| 87 | + const response = await handleSessionCommands({ |
| 88 | + req: { |
| 89 | + token: 't', |
| 90 | + session: sessionAddress, |
| 91 | + command: 'close', |
| 92 | + positionals: [], |
| 93 | + flags: {}, |
| 94 | + }, |
| 95 | + sessionName: sessionAddress, |
| 96 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 97 | + sessionStore, |
| 98 | + invoke: noopInvoke, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(response?.ok).toBe(true); |
| 102 | + expect(forceCleanup).toHaveBeenCalledOnce(); |
| 103 | + const record = appLogResourceStore.read(resourcePath); |
| 104 | + expect(record.status).toBe('decoded'); |
| 105 | + if (record.status === 'decoded') expect(record.envelope.lifecycle).toBe('completed'); |
| 106 | + expect(sessionStore.get(sessionAddress)).toBeUndefined(); |
| 107 | +}); |
0 commit comments