|
| 1 | +import { createTestDeviceInventoryGateways } from '../../__tests__/test-utils/device-inventory-gateways.ts'; |
| 2 | +/** |
| 3 | + * #2833: when a command finds no session because this daemon already expired it for idleness, the |
| 4 | + * answer stays `SESSION_NOT_FOUND` but carries the typed reason, the window it missed, and the device |
| 5 | + * it released — never a bare "Run open first" with nothing to reason about. The repair tombstone is |
| 6 | + * consulted first, so an abandoned repair transaction keeps its own, more specific guidance. |
| 7 | + */ |
| 8 | +import { test, expect } from 'vitest'; |
| 9 | +import path from 'node:path'; |
| 10 | +import fs from 'node:fs'; |
| 11 | + |
| 12 | +import { createRequestHandler } from './test-device-runtime-gateway.ts'; |
| 13 | +import { buildIdleExpiryTombstone } from '../session-idle-expiry.ts'; |
| 14 | +import type { DaemonRequest } from '../daemon-request.ts'; |
| 15 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 16 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 17 | +import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; |
| 18 | + |
| 19 | +function makeHandler(prefix: string) { |
| 20 | + const sessionStore = makeSessionStore(prefix); |
| 21 | + const handler = createRequestHandler({ |
| 22 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 23 | + token: 'test-token', |
| 24 | + sessionStore, |
| 25 | + leaseRegistry: new LeaseRegistry(), |
| 26 | + deviceInventoryGateways: createTestDeviceInventoryGateways(), |
| 27 | + trackDownloadableArtifact: () => 'artifact-id', |
| 28 | + }); |
| 29 | + return { sessionStore, handler }; |
| 30 | +} |
| 31 | + |
| 32 | +function closeRequest(session: string, tenant?: string): DaemonRequest { |
| 33 | + return { |
| 34 | + token: 'test-token', |
| 35 | + session, |
| 36 | + command: 'close', |
| 37 | + positionals: [], |
| 38 | + flags: {}, |
| 39 | + ...(tenant ? { meta: { tenantId: tenant, sessionIsolation: 'tenant' as const } } : {}), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function writeIdleMarker(sessionStore: ReturnType<typeof makeSessionStore>, session: string) { |
| 44 | + sessionStore.writeIdleExpiryTombstone( |
| 45 | + session, |
| 46 | + buildIdleExpiryTombstone(session, { |
| 47 | + expiredAtMs: Date.now(), |
| 48 | + idleExpiryMs: 1_500_000, |
| 49 | + deviceKey: 'ios:sim-1', |
| 50 | + }), |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +test('a command on an idle-expired session names the reason, window, and released device', async () => { |
| 55 | + const { sessionStore, handler } = makeHandler('agent-device-router-idle-expired-'); |
| 56 | + writeIdleMarker(sessionStore, 'idle-x'); |
| 57 | + |
| 58 | + const response = await handler(closeRequest('idle-x')); |
| 59 | + |
| 60 | + expect(response.ok).toBe(false); |
| 61 | + if (response.ok) return; |
| 62 | + // Still SESSION_NOT_FOUND: the session genuinely is gone. What changes is what it says why. |
| 63 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 64 | + expect(response.error.details?.reason).toBe('SESSION_IDLE_EXPIRED'); |
| 65 | + expect(response.error.details?.idleExpiryMs).toBe(1_500_000); |
| 66 | + expect(response.error.details?.deviceKey).toBe('ios:sim-1'); |
| 67 | + expect(response.error.hint).toMatch(/ios:sim-1/); |
| 68 | +}); |
| 69 | + |
| 70 | +test('an expired session with no marker keeps the plain SESSION_NOT_FOUND', async () => { |
| 71 | + const { handler } = makeHandler('agent-device-router-idle-no-marker-'); |
| 72 | + const response = await handler(closeRequest('never-existed')); |
| 73 | + |
| 74 | + expect(response.ok).toBe(false); |
| 75 | + if (response.ok) return; |
| 76 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 77 | + expect(response.error.details?.reason).toBeUndefined(); |
| 78 | +}); |
| 79 | + |
| 80 | +test('an expired marker that has aged out stops explaining the absence', async () => { |
| 81 | + const { sessionStore, handler } = makeHandler('agent-device-router-idle-stale-'); |
| 82 | + writeIdleMarker(sessionStore, 'idle-stale'); |
| 83 | + const markerPath = path.join(sessionStore.resolveSessionDir('idle-stale'), 'idle-expiry.json'); |
| 84 | + const marker = JSON.parse(fs.readFileSync(markerPath, 'utf8')) as { expiresAt: number }; |
| 85 | + marker.expiresAt = Date.now() - 1; |
| 86 | + fs.writeFileSync(markerPath, `${JSON.stringify(marker)}\n`); |
| 87 | + |
| 88 | + const response = await handler(closeRequest('idle-stale')); |
| 89 | + |
| 90 | + expect(response.ok).toBe(false); |
| 91 | + if (response.ok) return; |
| 92 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 93 | + expect(response.error.details?.reason).toBeUndefined(); |
| 94 | +}); |
| 95 | + |
| 96 | +test('an abandoned repair transaction outranks the idle-expiry marker', async () => { |
| 97 | + const { sessionStore, handler } = makeHandler('agent-device-router-idle-vs-repair-'); |
| 98 | + writeIdleMarker(sessionStore, 'repair-x'); |
| 99 | + sessionStore.writeRepairTombstone({ |
| 100 | + name: 'repair-x', |
| 101 | + device: { platform: 'apple', id: 'sim-1', name: 'iPhone', kind: 'simulator', booted: true }, |
| 102 | + createdAt: Date.now(), |
| 103 | + actions: [], |
| 104 | + scriptPublication: { |
| 105 | + kind: 'repair', |
| 106 | + status: 'armed', |
| 107 | + target: { kind: 'default', force: false }, |
| 108 | + boundary: 0, |
| 109 | + sourcePath: '/flows/login.ad', |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + const response = await handler(closeRequest('repair-x')); |
| 114 | + |
| 115 | + expect(response.ok).toBe(false); |
| 116 | + if (response.ok) return; |
| 117 | + expect(response.error.code).toBe('REPAIR_SESSION_EXPIRED'); |
| 118 | +}); |
| 119 | + |
| 120 | +test('a tenant-isolated request reads the marker for its own scoped session', async () => { |
| 121 | + const { sessionStore, handler } = makeHandler('agent-device-router-idle-tenant-'); |
| 122 | + // The request names `idle-x` and the daemon owns it as `<tenant>:idle-x`. Reading the raw name |
| 123 | + // would miss the marker entirely and return a bare "Run open first". |
| 124 | + writeIdleMarker(sessionStore, 'tenant-a:idle-x'); |
| 125 | + |
| 126 | + const response = await handler(closeRequest('idle-x', 'tenant-a')); |
| 127 | + |
| 128 | + expect(response.ok).toBe(false); |
| 129 | + if (response.ok) return; |
| 130 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 131 | + expect(response.error.details?.reason).toBe('SESSION_IDLE_EXPIRED'); |
| 132 | + expect(response.error.details?.deviceKey).toBe('ios:sim-1'); |
| 133 | +}); |
| 134 | + |
| 135 | +test('another tenant marker never explains this tenant absent session', async () => { |
| 136 | + const { sessionStore, handler } = makeHandler('agent-device-router-idle-tenant-leak-'); |
| 137 | + writeIdleMarker(sessionStore, 'tenant-b:idle-x'); |
| 138 | + |
| 139 | + const response = await handler(closeRequest('idle-x', 'tenant-a')); |
| 140 | + |
| 141 | + expect(response.ok).toBe(false); |
| 142 | + if (response.ok) return; |
| 143 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 144 | + // Reading the unscoped key would have reported tenant-b's device as the one tenant-a just lost. |
| 145 | + expect(response.error.details?.reason).toBeUndefined(); |
| 146 | + expect(response.error.details?.deviceKey).toBeUndefined(); |
| 147 | +}); |
0 commit comments