ReviewOS

also looking at this

stacks/drone

feat: add shared-clock scoped vehicle simulation sessions

#98
Merged chris wants to merge codex/cargo-fleet-runtime into main
42 files +2764 -127
tests/unit/cargo-delivery-config.test.tsadded+92-0
Changes to tests/unit/cargo-delivery-config.test.ts
@@ -0,0 +1,92 @@
1import { expect, test } from 'bun:test'
2import { cargoDeliverySummary, simulateCargoDelivery } from '../../resources/functions/drone/cargo/delivery'
3import { replayVehicleMission } from '../../resources/functions/drone/missions/vehicle'
4import { planCargoDelivery } from '../../resources/functions/drone/cargo/plan'
5
6const configuration = {
7 version: 1,
8 id: 'operator-cargo-route',
9 delivery: { origin: { latitudeDeg: 0, longitudeDeg: 0, ellipsoidHeightM: 100 }, site: { id: 'declared-site', recipientId: 'declared-recipient', containerId: 'parcel-150g', positionM: [2, 1, 0], radiusM: .4 }, releaseHeightM: 1.2 },
10 load: { massKg: .15, forwardM: -.04 },
11 alternateM: [-1, 1, 0],
12 power: { availableWh: 50, maxW: 700, reservePercent: 35 },
13}
14
15// Existing public simulation seam: catches declared routes, actual loaded-body
16// and sensor/authority/payload integration; misses physical flight qualification.
17test('an explicit cargo configuration drives its own load, destination, release and return on the shared runtime', () => {
18 const run = simulateCargoDelivery({ configuration })
19 expect(run.status).toBe('completed')
20 expect(run.delivery).toMatchObject({ outcome: 'received', containerId: 'parcel-150g', siteId: 'declared-site', recipientId: 'declared-recipient' })
21 expect(run.initialLoad.properties.totalMassKg).toBe(1.65)
22 expect(run.initialLoad.properties.cgBodyM[0]).toBeLessThan(0)
23 expect(run.finalLoad.properties.totalMassKg).toBe(1.5)
24 expect(run.release?.outcome).toBe('separated')
25 expect(run.parcel?.massKg).toBe(.15)
26 expect(run.parcel?.onSurface).toBe(true)
27 expect(Math.hypot(run.parcel!.positionM[0] - 2, run.parcel!.positionM[1] - 1)).toBeLessThanOrEqual(.4)
28 expect(run.runtime.trace.plan.id).toBe('operator-cargo-route')
29 expect(run.runtime.trace.plan.power).toEqual(configuration.power)
30 expect(run.runtime.trace.plan.tasks.find(task => task.id === 'site')).toMatchObject({ positionM: [2, 1, -1.2] })
31 expect(run.runtime.trace.plan.tasks.find(task => task.id === 'alternate-land')).toMatchObject({ positionM: [-1, 1, 0] })
32 expect(replayVehicleMission(run.runtime.trace).snapshot()).toEqual(run.runtime.mission)
33 expect(run.runtime.board.actuators).toEqual([0, 0, 0, 0])
34 const summary = cargoDeliverySummary(run)
35 expect(summary.model).toMatchObject({ loadMassKg: .15, loadForwardM: -.04 })
36 expect(summary.energy).toMatchObject({ capacityWh: 50, maximumDeclaredW: 700, reservePercent: 35, measured: false, electricalModel: false })
37 expect(summary.demonstrationMatched).toBe(true)
38 expect(run.physicalFlightEnabled).toBe(false)
39 expect(run.identityAuthenticated).toBe(false)
40}, 30000)
41
42test('a blocked configured receiver retains its actual mass and lands at the explicitly declared alternate', () => {
43 const run = simulateCargoDelivery({ configuration, scenario: 'site-blocked' })
44 expect(run.status).toBe('diverted')
45 expect(run.delivery.outcome).toBe('not-released')
46 expect(run.release).toBeNull()
47 expect(run.finalLoad.properties.totalMassKg).toBe(1.65)
48 expect(run.runtime.mission.payloadFailure?.landingTaskId).toBe('alternate-land')
49 expect(run.runtime.trace.rows.at(-1)!.observation.onGround).toBe(true)
50 const landed = run.runtime.path.at(-1)!.truthPositionM
51 expect(Math.hypot(landed[0] + 1, landed[1] - 1)).toBeLessThan(.18)
52 expect(run.runtime.board.actuators).toEqual([0, 0, 0, 0])
53 expect(cargoDeliverySummary(run).demonstrationMatched).toBe(true)
54}, 30000)
55
56test('cargo planning captures a complete route and rejects unsupported geometry and unsafe budgets before execution', () => {
57 const input = structuredClone(configuration), planned = planCargoDelivery(input)
58 input.delivery.site.positionM[0] = 11; input.alternateM[0] = 10; input.load.massKg = .2
59 expect(planned.configuration.delivery.site.positionM).toEqual([2, 1, 0])
60 expect(planned.configuration.load.massKg).toBe(.15)
61 expect(planned.mission.tasks.find(task => task.id === 'alternate-land')).toMatchObject({ positionM: [-1, 1, 0] })
62 expect(planned.budget.worstCaseMs).toBe(81520)
63 expect(planned.physicalFlightEnabled).toBe(false)
64 expect(planned.identityAuthenticated).toBe(false)
65 const lowBudget = structuredClone(configuration); lowBudget.power.availableWh = 1
66 expect(() => planCargoDelivery(lowBudget)).toThrow()
67 const farSite = structuredClone(configuration); farSite.delivery.site.positionM = [13, 0, 0]
68 expect(() => planCargoDelivery(farSite)).toThrow()
69 const farAlternate = structuredClone(configuration); farAlternate.alternateM = [13, 0, 0]
70 expect(() => planCargoDelivery(farAlternate)).toThrow()
71 const unmodeledOrigin = structuredClone(configuration); unmodeledOrigin.delivery.origin.latitudeDeg = 50
72 expect(() => planCargoDelivery(unmodeledOrigin)).toThrow('cargo-delivery-configuration')
73 const unmodeledSurface = structuredClone(configuration); unmodeledSurface.delivery.site.positionM[2] = -.2
74 expect(() => planCargoDelivery(unmodeledSurface)).toThrow('cargo-delivery-configuration')
75 const understatedPower = structuredClone(configuration); understatedPower.power.maxW = 50
76 expect(() => planCargoDelivery(understatedPower)).toThrow('cargo-delivery-configuration')
77 expect(() => simulateCargoDelivery({ configuration: undefined })).toThrow('cargo-delivery-input')
78 let reads = 0
79 const getter = { ...configuration, get alternateM() { reads++; return [-1, 1, 0] } }
80 expect(() => planCargoDelivery(getter)).toThrow('cargo-delivery-configuration')
81 expect(reads).toBe(0)
82})
83
84test('a configured route that never reaches the selected containment failure retains its actual completed outcome', () => {
85 const near = structuredClone(configuration); near.delivery.site.positionM = [1, 0, 0]
86 const run = simulateCargoDelivery({ configuration: near, scenario: 'authority-containment' })
87 expect(run.status).toBe('completed')
88 expect(run.delivery.outcome).toBe('received')
89 expect(run.runtime.authority.events.some(event => event.reason === 'invalid-target-or-lease')).toBe(false)
90 expect(cargoDeliverySummary(run).demonstrationMatched).toBe(false)
91 expect(cargoDeliverySummary(run).landingContact).toBe(true)
92}, 30000)