|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { newAllocationOperation } from '../allocation-operation-record-factory.ts'; |
| 4 | +import { applyAllocationTransition } from '../allocation-operation-record-transition.ts'; |
| 5 | +import { decideAllocationAction } from '../allocation-operation-decision.ts'; |
| 6 | +import { ALLOCATION_LEASE, ALLOCATION_REQUEST } from './allocation-operation-fixtures.ts'; |
| 7 | + |
| 8 | +const NOW = 1_700_000_000_000; |
| 9 | + |
| 10 | +function requested() { |
| 11 | + return newAllocationOperation({ |
| 12 | + ...ALLOCATION_REQUEST, |
| 13 | + allocatorInstanceId: 'allocator-1', |
| 14 | + nowMs: NOW, |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +function granted() { |
| 19 | + const pending = applyAllocationTransition(requested(), { kind: 'request-dispatched' }, NOW + 1); |
| 20 | + assert.equal(pending.status, 'applied'); |
| 21 | + const result = applyAllocationTransition( |
| 22 | + pending.record, |
| 23 | + { |
| 24 | + kind: 'allocator-outcome', |
| 25 | + outcome: { |
| 26 | + status: 'granted', |
| 27 | + lease: ALLOCATION_LEASE, |
| 28 | + identityIncarnationId: 'incarnation-1', |
| 29 | + }, |
| 30 | + }, |
| 31 | + NOW + 2, |
| 32 | + ); |
| 33 | + assert.equal(result.status, 'applied'); |
| 34 | + return result.record; |
| 35 | +} |
| 36 | + |
| 37 | +test('new work may request once, while recovery of intent only looks up the allocator', () => { |
| 38 | + assert.equal(decideAllocationAction(requested(), 'new').kind, 'request'); |
| 39 | + assert.equal(decideAllocationAction(requested(), 'recover').kind, 'lookup'); |
| 40 | + const pending = applyAllocationTransition(requested(), { kind: 'request-dispatched' }, NOW + 1); |
| 41 | + assert.equal(pending.status, 'applied'); |
| 42 | + assert.equal(decideAllocationAction(pending.record, 'recover').kind, 'lookup'); |
| 43 | +}); |
| 44 | + |
| 45 | +test('an allocator-unknown record never gets an implicit second request', () => { |
| 46 | + const record = applyAllocationTransition( |
| 47 | + requested(), |
| 48 | + { kind: 'allocator-unknown', message: 'response lost' }, |
| 49 | + NOW + 1, |
| 50 | + ); |
| 51 | + assert.equal(record.status, 'applied'); |
| 52 | + const action = decideAllocationAction(record.record, 'recover'); |
| 53 | + assert.deepEqual(action, { |
| 54 | + kind: 'lookup', |
| 55 | + ref: { requesterId: 'requester-a', attemptKey: 'attempt-1' }, |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +test('grants publish first, and release cleans the binding before calling the allocator', () => { |
| 60 | + const record = granted(); |
| 61 | + assert.equal(decideAllocationAction(record, 'continue').kind, 'publish'); |
| 62 | + const published = applyAllocationTransition(record, { kind: 'binding-published' }, NOW + 3); |
| 63 | + assert.equal(published.status, 'applied'); |
| 64 | + const cleanup = decideAllocationAction(published.record, 'release'); |
| 65 | + assert.equal(cleanup.kind, 'cleanup'); |
| 66 | + const cleaned = applyAllocationTransition(published.record, { kind: 'binding-cleaned' }, NOW + 4); |
| 67 | + assert.equal(cleaned.status, 'applied'); |
| 68 | + assert.deepEqual(decideAllocationAction(cleaned.record, 'release'), { |
| 69 | + kind: 'release', |
| 70 | + leaseId: 'lease-1', |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +test('cancel and explicit supersession are allocator actions, not local terminal guesses', () => { |
| 75 | + assert.equal(decideAllocationAction(requested(), 'cancel').kind, 'cancel'); |
| 76 | + assert.equal(decideAllocationAction(requested(), 'supersede').kind, 'supersede'); |
| 77 | + assert.equal(decideAllocationAction(granted(), 'cancel').kind, 'blocked'); |
| 78 | +}); |
0 commit comments