|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import { makeSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 3 | +import { LINUX_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts'; |
| 4 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 5 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 6 | +import { createRequestExecutionScope } from '../request-execution-scope.ts'; |
| 7 | +import type { DaemonRequest } from '../daemon-request.ts'; |
| 8 | + |
| 9 | +function makeRequest(overrides: Partial<DaemonRequest>): DaemonRequest { |
| 10 | + return { |
| 11 | + token: 't', |
| 12 | + session: 'default', |
| 13 | + command: 'snapshot', |
| 14 | + positionals: [], |
| 15 | + flags: {}, |
| 16 | + ...overrides, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +test('expired leases remove owned sessions before the next command and free capacity', async () => { |
| 21 | + let now = 1_000; |
| 22 | + const sessionStore = makeSessionStore('agent-device-request-scope-'); |
| 23 | + const leaseRegistry = new LeaseRegistry({ |
| 24 | + maxActiveSimulatorLeases: 1, |
| 25 | + defaultLeaseTtlMs: 10, |
| 26 | + minLeaseTtlMs: 1, |
| 27 | + now: () => now, |
| 28 | + }); |
| 29 | + const lease = leaseRegistry.allocateLease({ tenantId: 'tenant-a', runId: 'run-1' }); |
| 30 | + sessionStore.set( |
| 31 | + 'default', |
| 32 | + makeSession('default', { |
| 33 | + device: LINUX_DEVICE, |
| 34 | + lease: { |
| 35 | + leaseId: lease.leaseId, |
| 36 | + tenantId: lease.tenantId, |
| 37 | + runId: lease.runId, |
| 38 | + leaseBackend: lease.backend, |
| 39 | + leaseProvider: 'proxy', |
| 40 | + deviceKey: 'ios:SIM-001', |
| 41 | + expiresAt: lease.expiresAt, |
| 42 | + }, |
| 43 | + }), |
| 44 | + ); |
| 45 | + now = 1_011; |
| 46 | + |
| 47 | + const scope = await createRequestExecutionScope({ |
| 48 | + req: makeRequest({ command: 'snapshot' }), |
| 49 | + sessionStore, |
| 50 | + leaseRegistry, |
| 51 | + }); |
| 52 | + await scope.runLocked(async () => 'ran'); |
| 53 | + |
| 54 | + expect(sessionStore.get('default')).toBeUndefined(); |
| 55 | + const nextLease = leaseRegistry.allocateLease({ tenantId: 'tenant-b', runId: 'run-2' }); |
| 56 | + expect(nextLease.tenantId).toBe('tenant-b'); |
| 57 | +}); |
| 58 | + |
| 59 | +test.each(['rebuild', 'retire'] as const)( |
| 60 | + 'scoped lease expiry preserves a public-name session and a %s during held teardown', |
| 61 | + async (change) => { |
| 62 | + let now = 1_000; |
| 63 | + const store = makeSessionStore('request-scope-lease-lifetime-'); |
| 64 | + const leases = new LeaseRegistry({ defaultLeaseTtlMs: 10, minLeaseTtlMs: 1, now: () => now }); |
| 65 | + const lease = leases.allocateLease({ tenantId: 'tenant-a', runId: 'run-1' }); |
| 66 | + const address = 'cwd:ownership:default'; |
| 67 | + const original = store.publish( |
| 68 | + address, |
| 69 | + makeSession('default', { |
| 70 | + device: LINUX_DEVICE, |
| 71 | + sessionScope: { kind: 'cwd', id: 'ownership' }, |
| 72 | + lease: { |
| 73 | + leaseId: lease.leaseId, |
| 74 | + tenantId: lease.tenantId, |
| 75 | + runId: lease.runId, |
| 76 | + leaseBackend: lease.backend, |
| 77 | + }, |
| 78 | + }), |
| 79 | + ); |
| 80 | + const decoy = store.publish( |
| 81 | + 'default', |
| 82 | + makeSession('default', { |
| 83 | + device: { ...LINUX_DEVICE, id: 'public-name-decoy' }, |
| 84 | + appName: 'decoy', |
| 85 | + }), |
| 86 | + ); |
| 87 | + let enter!: () => void; |
| 88 | + let resume!: () => void; |
| 89 | + const entered = new Promise<void>((resolve) => { |
| 90 | + enter = resolve; |
| 91 | + }); |
| 92 | + const release = new Promise<void>((resolve) => { |
| 93 | + resume = resolve; |
| 94 | + }); |
| 95 | + const stopSnapshotHelper = vi.fn(async () => { |
| 96 | + enter(); |
| 97 | + await release; |
| 98 | + }); |
| 99 | + now = 1_011; |
| 100 | + const scope = await createRequestExecutionScope({ |
| 101 | + req: makeRequest({ session: address, flags: { session: address } }), |
| 102 | + sessionStore: store, |
| 103 | + leaseRegistry: leases, |
| 104 | + platformResourceCleanup: { |
| 105 | + stopSnapshotHelper, |
| 106 | + closeManagedBrowser: async () => {}, |
| 107 | + cleanupSessionlessExecutionHost: async () => {}, |
| 108 | + retainExecutionHostAfterClose: () => false, |
| 109 | + }, |
| 110 | + }); |
| 111 | + expect(scope.sessionName).toBe(address); |
| 112 | + const running = scope.runLocked(async () => 'ran'); |
| 113 | + await entered; |
| 114 | + let successor: ReturnType<typeof store.publish> | undefined; |
| 115 | + if (change === 'retire') { |
| 116 | + store.retire(original); |
| 117 | + successor = store.publish( |
| 118 | + address, |
| 119 | + makeSession('default', { |
| 120 | + device: { ...LINUX_DEVICE, id: 'successor' }, |
| 121 | + appName: 'successor', |
| 122 | + }), |
| 123 | + ); |
| 124 | + } else store.update(original, { appName: 'latest' }); |
| 125 | + resume(); |
| 126 | + await expect(running).resolves.toBe('ran'); |
| 127 | + expect(stopSnapshotHelper).toHaveBeenCalledExactlyOnceWith(original.session.device); |
| 128 | + expect(store.requireCurrent(decoy)).toBe(decoy.session); |
| 129 | + if (successor) expect(store.requireCurrent(successor)).toBe(successor.session); |
| 130 | + else expect(store.lookup(address)).toBeUndefined(); |
| 131 | + }, |
| 132 | +); |
0 commit comments