|
| 1 | +import { beforeEach, afterEach, expect, test, vi } from 'vitest'; |
| 2 | +import type { |
| 3 | + LeaseRequestStatus, |
| 4 | + ManagedLease, |
| 5 | +} from '@agent-device/contracts/managed-device-allocation'; |
| 6 | +import { |
| 7 | + NOW, |
| 8 | + controller, |
| 9 | + deferred, |
| 10 | + granted, |
| 11 | + horizon, |
| 12 | + renewedLease, |
| 13 | + setupAdmission, |
| 14 | + unknownStatus, |
| 15 | +} from './lease-admission.fixtures.ts'; |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + vi.useFakeTimers(); |
| 19 | + vi.setSystemTime(NOW); |
| 20 | +}); |
| 21 | +afterEach(() => vi.useRealTimers()); |
| 22 | + |
| 23 | +test('reuses only an allocator-confirmed horizon outside the safety window', async () => { |
| 24 | + const { admission, allocator } = setupAdmission({ grant: granted() }); |
| 25 | + const task = vi.fn(async () => 'value'); |
| 26 | + expect(await admission.run(horizon(), controller().signal, task)).toMatchObject({ |
| 27 | + status: 'admitted', |
| 28 | + ttlDeadline: NOW + 100_000, |
| 29 | + value: 'value', |
| 30 | + }); |
| 31 | + expect(allocator.calls).toEqual([]); |
| 32 | + const near = setupAdmission({ |
| 33 | + grant: granted(), |
| 34 | + safetyWindowMs: 100_000, |
| 35 | + script: { renewLease: [renewedLease({ ttlDeadline: NOW + 200_000 })] }, |
| 36 | + }); |
| 37 | + expect((await near.admission.run(horizon(), controller().signal, task)).status).toBe('admitted'); |
| 38 | + expect(near.allocator.calls).toHaveLength(1); |
| 39 | +}); |
| 40 | + |
| 41 | +test('same-lease waiters share renewal while a different lease proceeds independently', async () => { |
| 42 | + const pending = deferred<ManagedLease>(); |
| 43 | + const first = setupAdmission({ script: { renewLease: [pending.promise] } }); |
| 44 | + const task = vi.fn(async () => {}); |
| 45 | + const a = first.admission.run(horizon(), controller().signal, task); |
| 46 | + const b = first.admission.run(horizon(), controller().signal, task); |
| 47 | + const other = setupAdmission({ |
| 48 | + grant: granted({ lease: renewedLease({ id: 'lease-2', ttlDeadline: NOW + 1_000 }) }), |
| 49 | + script: { renewLease: [renewedLease({ id: 'lease-2' })] }, |
| 50 | + }); |
| 51 | + expect((await other.admission.run(horizon(), controller().signal, async () => {})).status).toBe( |
| 52 | + 'admitted', |
| 53 | + ); |
| 54 | + expect(first.allocator.calls).toHaveLength(1); |
| 55 | + expect(task).not.toHaveBeenCalled(); |
| 56 | + pending.resolve(renewedLease()); |
| 57 | + expect((await Promise.all([a, b])).map((result) => result.status)).toEqual([ |
| 58 | + 'admitted', |
| 59 | + 'admitted', |
| 60 | + ]); |
| 61 | +}); |
| 62 | + |
| 63 | +test('a longer-horizon waiter obtains its own confirmation after joining a shorter renewal', async () => { |
| 64 | + const short = deferred<ManagedLease>(); |
| 65 | + const long = deferred<ManagedLease>(); |
| 66 | + const { admission, allocator } = setupAdmission({ |
| 67 | + script: { renewLease: [short.promise, long.promise] }, |
| 68 | + }); |
| 69 | + const longTask = vi.fn(async () => {}); |
| 70 | + const a = admission.run(horizon(), controller().signal, async () => {}); |
| 71 | + const b = admission.run(horizon(200_000), controller().signal, longTask); |
| 72 | + short.resolve(renewedLease()); |
| 73 | + expect((await a).status).toBe('admitted'); |
| 74 | + expect(longTask).not.toHaveBeenCalled(); |
| 75 | + expect(allocator.calls[1]).toEqual({ |
| 76 | + method: 'renewLease', |
| 77 | + input: { leaseId: 'lease-1', ttlDeadline: NOW + 210_000 }, |
| 78 | + }); |
| 79 | + long.resolve(renewedLease({ ttlDeadline: NOW + 205_000 })); |
| 80 | + expect((await b).status).toBe('admitted'); |
| 81 | +}); |
| 82 | + |
| 83 | +test.each([ |
| 84 | + { ttlDeadline: NOW - 1 }, |
| 85 | + { ttlDeadline: NOW + 14_999 }, |
| 86 | + { ttlDeadline: Number.NaN }, |
| 87 | + { ttlDeadline: Number.POSITIVE_INFINITY }, |
| 88 | + { id: 'wrong' }, |
| 89 | + { device: { address: 'wrong' } }, |
| 90 | + { environment: { SIMLOCK_IOS_DEVICE_SET: '/foreign' } }, |
| 91 | +])('invalid renewal %j never becomes local authority', async (overrides) => { |
| 92 | + const { admission, allocator } = setupAdmission({ |
| 93 | + script: { renewLease: [renewedLease(overrides)], getLeaseRequestStatus: [unknownStatus] }, |
| 94 | + }); |
| 95 | + const task = vi.fn(async () => {}); |
| 96 | + expect(await admission.run(horizon(), controller().signal, task)).toMatchObject({ |
| 97 | + status: 'teardown-required', |
| 98 | + reason: 'authority-unconfirmed', |
| 99 | + }); |
| 100 | + expect((await admission.run(horizon(), controller().signal, task)).status).toBe( |
| 101 | + 'teardown-required', |
| 102 | + ); |
| 103 | + expect(task).not.toHaveBeenCalled(); |
| 104 | + expect(allocator.calls.map((call) => call.method)).toEqual([ |
| 105 | + 'renewLease', |
| 106 | + 'getLeaseRequestStatus', |
| 107 | + ]); |
| 108 | +}); |
| 109 | + |
| 110 | +test('a lost renewal response is reconciled through the exact durable attempt', async () => { |
| 111 | + const { admission, allocator } = setupAdmission({ |
| 112 | + script: { renewLease: [new Error('lost response')], getLeaseRequestStatus: [granted()] }, |
| 113 | + }); |
| 114 | + expect( |
| 115 | + await admission.run(horizon(), controller().signal, async () => 'confirmed'), |
| 116 | + ).toMatchObject({ status: 'admitted', ttlDeadline: NOW + 100_000 }); |
| 117 | + expect(allocator.calls[1]).toEqual({ |
| 118 | + method: 'getLeaseRequestStatus', |
| 119 | + input: { requesterId: 'requester-a', attemptKey: 'attempt-1' }, |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +test.each([ |
| 124 | + unknownStatus, |
| 125 | + granted({ state: 'cancelled' }), |
| 126 | + granted({ state: 'superseded' }), |
| 127 | + granted({ state: 'pending' }), |
| 128 | + granted({ state: 'refused' }), |
| 129 | + granted({ requesterId: 'other' }), |
| 130 | + granted({ attemptKey: 'other' }), |
| 131 | + granted({ requestGeneration: 2 }), |
| 132 | + granted({ identityIncarnationId: 'other' }), |
| 133 | + granted({ lease: renewedLease({ ttlDeadline: NOW + 14_999 }) }), |
| 134 | + new Error('lookup unavailable'), |
| 135 | +])('unconfirmed lookup %j requires canonical teardown', async (status) => { |
| 136 | + const { admission } = setupAdmission({ |
| 137 | + script: { renewLease: [new Error('renewal failed')], getLeaseRequestStatus: [status] }, |
| 138 | + }); |
| 139 | + const task = vi.fn(async () => {}); |
| 140 | + expect((await admission.run(horizon(), controller().signal, task)).status).toBe( |
| 141 | + 'teardown-required', |
| 142 | + ); |
| 143 | + expect(task).not.toHaveBeenCalled(); |
| 144 | +}); |
| 145 | + |
| 146 | +test('abort abandons one caller and does not cancel renewal or another waiter', async () => { |
| 147 | + const pending = deferred<ManagedLease>(); |
| 148 | + const { admission, allocator } = setupAdmission({ script: { renewLease: [pending.promise] } }); |
| 149 | + const abort = controller(); |
| 150 | + const task = vi.fn(async () => {}); |
| 151 | + const a = admission.run(horizon(), abort.signal, task); |
| 152 | + const b = admission.run(horizon(), controller().signal, async () => {}); |
| 153 | + abort.abort(); |
| 154 | + expect(await a).toEqual({ status: 'abandoned' }); |
| 155 | + pending.resolve(renewedLease()); |
| 156 | + expect((await b).status).toBe('admitted'); |
| 157 | + expect(task).not.toHaveBeenCalled(); |
| 158 | + expect(allocator.calls).toHaveLength(1); |
| 159 | +}); |
| 160 | + |
| 161 | +test('caller deadline ends the wait while a late renewal can still confirm authority', async () => { |
| 162 | + const pending = deferred<ManagedLease>(); |
| 163 | + const { admission, allocator } = setupAdmission({ script: { renewLease: [pending.promise] } }); |
| 164 | + const a = admission.run(horizon(), controller().signal, async () => { |
| 165 | + throw new Error('expired operation'); |
| 166 | + }); |
| 167 | + await vi.advanceTimersByTimeAsync(10_000); |
| 168 | + expect(await a).toEqual({ status: 'deadline-exceeded' }); |
| 169 | + pending.resolve(renewedLease()); |
| 170 | + expect((await admission.run(horizon(), controller().signal, async () => {})).status).toBe( |
| 171 | + 'admitted', |
| 172 | + ); |
| 173 | + expect(allocator.calls).toHaveLength(1); |
| 174 | +}); |
| 175 | + |
| 176 | +test.each(['released', 'replaced', 'superseded', 'fenced'] as const)( |
| 177 | + '%s binding cannot be revived by a late renewal or lookup', |
| 178 | + async (reason) => { |
| 179 | + for (const recovering of [false, true]) { |
| 180 | + const pending = deferred<ManagedLease | LeaseRequestStatus>(); |
| 181 | + const script = recovering |
| 182 | + ? { renewLease: [new Error('lost')], getLeaseRequestStatus: [pending.promise] } |
| 183 | + : { renewLease: [pending.promise] }; |
| 184 | + const { admission, allocator } = setupAdmission({ script }); |
| 185 | + const task = vi.fn(async () => {}); |
| 186 | + const a = admission.run(horizon(), controller().signal, task); |
| 187 | + if (recovering) await vi.waitFor(() => expect(allocator.calls).toHaveLength(2)); |
| 188 | + admission.fenceBinding(reason); |
| 189 | + pending.resolve(recovering ? granted() : renewedLease()); |
| 190 | + expect(await a).toMatchObject({ status: 'teardown-required', reason }); |
| 191 | + expect((await admission.run(horizon(), controller().signal, task)).status).toBe( |
| 192 | + 'teardown-required', |
| 193 | + ); |
| 194 | + expect(task).not.toHaveBeenCalled(); |
| 195 | + } |
| 196 | + }, |
| 197 | +); |
| 198 | + |
| 199 | +test('an already aborted caller starts no allocator work and an operation failure stays primary', async () => { |
| 200 | + const abort = controller(); |
| 201 | + abort.abort(); |
| 202 | + const { admission, allocator } = setupAdmission({ grant: granted() }); |
| 203 | + expect(await admission.run(horizon(), abort.signal, async () => {})).toEqual({ |
| 204 | + status: 'abandoned', |
| 205 | + }); |
| 206 | + const failure = new Error('operation failed'); |
| 207 | + await expect( |
| 208 | + admission.run(horizon(), controller().signal, async () => { |
| 209 | + throw failure; |
| 210 | + }), |
| 211 | + ).rejects.toBe(failure); |
| 212 | + expect(allocator.calls).toEqual([]); |
| 213 | +}); |
| 214 | + |
| 215 | +test('fencing wakes a waiter even when durable renewal never responds', async () => { |
| 216 | + const { admission } = setupAdmission({ |
| 217 | + script: { renewLease: [deferred<ManagedLease>().promise] }, |
| 218 | + }); |
| 219 | + const waiter = admission.run(horizon(), controller().signal, async () => { |
| 220 | + throw new Error('fenced operation'); |
| 221 | + }); |
| 222 | + admission.fenceBinding('released'); |
| 223 | + expect(await waiter).toEqual({ status: 'teardown-required', reason: 'released' }); |
| 224 | +}); |
0 commit comments