also looking at this
feat: model one point-load intake with conserved momentum
#99
45 files
+3133
-169
| @@ -0,0 +1,69 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { createRedundantVehicleLinks } from '../../resources/functions/drone/vehicles/links' | |
| 3 | ||
| 4 | // Public captured-link adapter: modeled scope, source time and latched loss. | |
| 5 | // This does not verify radios, clock synchronization or authenticated identity. | |
| 6 | test('a fresh backup preserves the same authority scope when the primary becomes stale', () => { | |
| 7 | const scope = { vehicleId: 'carrier-a', bootId: 55, sessionId: 123 } | |
| 8 | const links = createRedundantVehicleLinks(scope) | |
| 9 | expect(links.snapshot(0).selectedSource).toBeNull() | |
| 10 | expect(links.ingest({ ...scope, sourceId: 'primary', sequence: 1, capturedAtMs: 10, receivedAtMs: 30, clockUncertaintyMs: 2, connected: true }, 30)).toBe(true) | |
| 11 | expect(links.snapshot(30).selectedSource).toBe('primary') | |
| 12 | expect(links.ingest({ ...scope, sourceId: 'backup', sequence: 1, capturedAtMs: 100, receivedAtMs: 120, clockUncertaintyMs: 2, connected: true }, 120)).toBe(true) | |
| 13 | const failover = links.snapshot(162) | |
| 14 | expect(failover.selectedSource).toBe('backup') | |
| 15 | expect(failover.authority).toEqual({ capturedAtMs: 98, connected: true }) | |
| 16 | expect(failover.scope).toEqual(scope) | |
| 17 | expect(failover.latchedLoss).toBe(false) | |
| 18 | }) | |
| 19 | ||
| 20 | test('fresh packets cannot silently restore a scope after both sources have expired', () => { | |
| 21 | const scope = { vehicleId: 'carrier-a', bootId: 55, sessionId: 123 } | |
| 22 | const links = createRedundantVehicleLinks(scope) | |
| 23 | const first = { ...scope, sourceId: 'primary', sequence: 1, capturedAtMs: 10, receivedAtMs: 30, clockUncertaintyMs: 2, connected: true } | |
| 24 | expect(links.ingest(first, 30)).toBe(true) | |
| 25 | expect(links.snapshot(30).authority.connected).toBe(true) | |
| 26 | // No intervening snapshot is required to notice the lost interval. | |
| 27 | expect(links.ingest({ ...first, sequence: 2, capturedAtMs: 190, receivedAtMs: 210 }, 210)).toBe(false) | |
| 28 | expect(links.snapshot(210).latchedLoss).toBe(true) | |
| 29 | expect(links.snapshot(210).authority.connected).toBe(false) | |
| 30 | }) | |
| 31 | ||
| 32 | test('foreign, replayed, future, uncertain and executable link envelopes cannot refresh the captured source', () => { | |
| 33 | const scope = { vehicleId: 'carrier-a', bootId: 55, sessionId: 123 }, links = createRedundantVehicleLinks(scope) | |
| 34 | const packet = { ...scope, sourceId: 'primary', sequence: 1, capturedAtMs: 20, receivedAtMs: 40, clockUncertaintyMs: 2, connected: true } | |
| 35 | expect(links.ingest(packet, 40)).toBe(true) | |
| 36 | let accessed = false | |
| 37 | const getter = { ...packet, sequence: 2 } | |
| 38 | Object.defineProperty(getter, 'capturedAtMs', { enumerable: true, get() { accessed = true; return 60 } }) | |
| 39 | for (const invalid of [packet, { ...packet, vehicleId: 'carrier-b', sequence: 2 }, { ...packet, bootId: 56, sequence: 2 }, { ...packet, sessionId: 124, sequence: 2 }, | |
| 40 | { ...packet, sourceId: 'third', sequence: 2 }, { ...packet, sequence: 2, capturedAtMs: 60, receivedAtMs: 80 }, { ...packet, sequence: 2, receivedAtMs: 70 }, | |
| 41 | { ...packet, sequence: 2, clockUncertaintyMs: 11 }, { ...packet, sequence: 2, unknown: true }, getter]) { | |
| 42 | expect(links.ingest(invalid, 60)).toBe(false) | |
| 43 | } | |
| 44 | expect(accessed).toBe(false) | |
| 45 | expect(links.snapshot(60).authority).toEqual({ capturedAtMs: 18, connected: true }) | |
| 46 | expect(links.snapshot(168).authority.connected).toBe(true) | |
| 47 | expect(links.snapshot(169).latchedLoss).toBe(true) | |
| 48 | }) | |
| 49 | ||
| 50 | test('a disconnected primary uses the backup without taking a new authority session', () => { | |
| 51 | const scope = { vehicleId: 'carrier-a', bootId: 55, sessionId: 123 }, links = createRedundantVehicleLinks(scope) | |
| 52 | for (const sourceId of ['primary', 'backup']) expect(links.ingest({ ...scope, sourceId, sequence: 1, capturedAtMs: 20, receivedAtMs: 40, clockUncertaintyMs: 2, connected: true }, 40)).toBe(true) | |
| 53 | expect(links.snapshot(40).selectedSource).toBe('primary') | |
| 54 | expect(links.ingest({ ...scope, sourceId: 'primary', sequence: 2, capturedAtMs: 60, receivedAtMs: 80, clockUncertaintyMs: 2, connected: false }, 80)).toBe(true) | |
| 55 | const result = links.snapshot(80) | |
| 56 | expect(result.selectedSource).toBe('backup') | |
| 57 | expect(result.authority.capturedAtMs).toBe(18) | |
| 58 | expect(result.scope).toEqual(scope) | |
| 59 | expect(result.identityAuthenticated).toBe(false) | |
| 60 | expect(() => links.snapshot(79)).toThrow('Invalid vehicle link clock') | |
| 61 | }) | |
| 62 | ||
| 63 | test('link ownership requires captured scalar vehicle, boot and session identities', () => { | |
| 64 | expect(() => createRedundantVehicleLinks({ vehicleId: undefined as unknown as string, bootId: 55, sessionId: 123 })).toThrow('Invalid vehicle link scope') | |
| 65 | let accessed = false | |
| 66 | const input = { bootId: 55, sessionId: 123, get vehicleId() { accessed = true; return 'carrier-a' } } | |
| 67 | expect(() => createRedundantVehicleLinks(input)).toThrow('Invalid vehicle link scope') | |
| 68 | expect(accessed).toBe(false) | |
| 69 | }) | |