|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 4 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 5 | +import { assertRequestLeaseAdmission } from '../request-admission.ts'; |
| 6 | +import type { DaemonRequest } from '../types.ts'; |
| 7 | + |
| 8 | +function makeRequest(overrides: Partial<DaemonRequest> = {}): DaemonRequest { |
| 9 | + return { |
| 10 | + token: 'token', |
| 11 | + session: 'default', |
| 12 | + command: 'close', |
| 13 | + positionals: [], |
| 14 | + flags: {}, |
| 15 | + ...overrides, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +// #2016: a tenant-isolated connection (e.g. BrowserStack) whose lease was |
| 20 | +// never allocated (`open` was never called) must not surface the generic |
| 21 | +// tenant/run/lease admission error when `close` tries to clean it up. |
| 22 | +test('close on a tenant-isolated session with no lease ever allocated admits without throwing', () => { |
| 23 | + const registry = new LeaseRegistry(); |
| 24 | + |
| 25 | + const result = assertRequestLeaseAdmission( |
| 26 | + makeRequest({ |
| 27 | + command: 'close', |
| 28 | + meta: { tenantId: 'tenant-a', runId: 'run-1', sessionIsolation: 'tenant' }, |
| 29 | + }), |
| 30 | + registry, |
| 31 | + undefined, |
| 32 | + ); |
| 33 | + |
| 34 | + assert.equal(result, undefined); |
| 35 | +}); |
| 36 | + |
| 37 | +test('close on a lease-less but stored tenant-isolated session still requires a lease id', () => { |
| 38 | + // #2016 follow-up: a *stored* session under tenant isolation is keyed by |
| 39 | + // tenant, not by run, so a lease-less stored session could belong to |
| 40 | + // another run in the same tenant. The bypass must not treat "this session |
| 41 | + // record has no lease field" as proof there's nothing to protect — only |
| 42 | + // "no session record exists at all" (the actual deferred-connect case) |
| 43 | + // qualifies. |
| 44 | + const registry = new LeaseRegistry(); |
| 45 | + const session = makeIosSession('default'); |
| 46 | + |
| 47 | + assert.throws( |
| 48 | + () => |
| 49 | + assertRequestLeaseAdmission( |
| 50 | + makeRequest({ |
| 51 | + command: 'close', |
| 52 | + meta: { tenantId: 'tenant-a', runId: 'run-1', sessionIsolation: 'tenant' }, |
| 53 | + }), |
| 54 | + registry, |
| 55 | + session, |
| 56 | + ), |
| 57 | + /tenant isolation requires lease id/, |
| 58 | + ); |
| 59 | +}); |
| 60 | + |
| 61 | +test('close with an app target still requires a lease id even with no lease anywhere', () => { |
| 62 | + // #2016 follow-up: `close <app>` with no session resolves its device |
| 63 | + // straight from flags (`closeWithoutSession`), so it must not bypass |
| 64 | + // tenant/lease admission the way a plain `close` (nothing to close) does — |
| 65 | + // otherwise any caller could close an arbitrary flag-selected device on a |
| 66 | + // tenant-isolated fleet without ever presenting a lease. |
| 67 | + const registry = new LeaseRegistry(); |
| 68 | + |
| 69 | + assert.throws( |
| 70 | + () => |
| 71 | + assertRequestLeaseAdmission( |
| 72 | + makeRequest({ |
| 73 | + command: 'close', |
| 74 | + positionals: ['com.example.app'], |
| 75 | + meta: { tenantId: 'tenant-a', runId: 'run-1', sessionIsolation: 'tenant' }, |
| 76 | + }), |
| 77 | + registry, |
| 78 | + undefined, |
| 79 | + ), |
| 80 | + /tenant isolation requires lease id/, |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +test('close with an explicit lease id but no session lease still requires an active lease', () => { |
| 85 | + const registry = new LeaseRegistry(); |
| 86 | + |
| 87 | + assert.throws( |
| 88 | + () => |
| 89 | + assertRequestLeaseAdmission( |
| 90 | + makeRequest({ |
| 91 | + command: 'close', |
| 92 | + meta: { |
| 93 | + tenantId: 'tenant-a', |
| 94 | + runId: 'run-1', |
| 95 | + leaseId: 'a'.repeat(32), |
| 96 | + sessionIsolation: 'tenant', |
| 97 | + }, |
| 98 | + }), |
| 99 | + registry, |
| 100 | + undefined, |
| 101 | + ), |
| 102 | + /Lease is not active/, |
| 103 | + ); |
| 104 | +}); |
| 105 | + |
| 106 | +test('non-close commands on a tenant-isolated session still require a lease id', () => { |
| 107 | + const registry = new LeaseRegistry(); |
| 108 | + |
| 109 | + assert.throws( |
| 110 | + () => |
| 111 | + assertRequestLeaseAdmission( |
| 112 | + makeRequest({ |
| 113 | + command: 'snapshot', |
| 114 | + meta: { tenantId: 'tenant-a', runId: 'run-1', sessionIsolation: 'tenant' }, |
| 115 | + }), |
| 116 | + registry, |
| 117 | + undefined, |
| 118 | + ), |
| 119 | + /tenant isolation requires lease id/, |
| 120 | + ); |
| 121 | +}); |
| 122 | + |
| 123 | +test('close still admits and heartbeats a real active lease', () => { |
| 124 | + let now = 1_000; |
| 125 | + const registry = new LeaseRegistry({ now: () => now }); |
| 126 | + const lease = registry.allocateLease({ tenantId: 'tenant-a', runId: 'run-1' }); |
| 127 | + const session = makeIosSession('default', { |
| 128 | + lease: { |
| 129 | + leaseId: lease.leaseId, |
| 130 | + tenantId: lease.tenantId, |
| 131 | + runId: lease.runId, |
| 132 | + leaseBackend: lease.backend, |
| 133 | + expiresAt: lease.expiresAt, |
| 134 | + }, |
| 135 | + }); |
| 136 | + now = 2_000; |
| 137 | + |
| 138 | + const result = assertRequestLeaseAdmission( |
| 139 | + makeRequest({ |
| 140 | + command: 'close', |
| 141 | + meta: { tenantId: 'tenant-a', runId: 'run-1', sessionIsolation: 'tenant' }, |
| 142 | + }), |
| 143 | + registry, |
| 144 | + session, |
| 145 | + ); |
| 146 | + |
| 147 | + assert.equal(result?.leaseId, lease.leaseId); |
| 148 | + assert.equal(result?.heartbeatAt, 2_000); |
| 149 | +}); |
0 commit comments