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-cli.test.tsadded+95-0
Changes to tests/unit/cargo-delivery-cli.test.ts
@@ -0,0 +1,95 @@
1import { expect, test } from 'bun:test'
2import { command } from '@stacksjs/testing'
3import { spawn } from 'node:child_process'
4
5test('native Buddy delivery prints bounded modeled custody, landing and reserve facts', async () => {
6 const result = await command(['cargo:deliver']).withTimeout(30000)
7 expect(result.exitCode).toBe(0)
8 expect(result.timedOut).toBe(false)
9 const report = JSON.parse(result.stdout)
10 expect(report.evidence).toBe('owned-cargo-delivery-simulation')
11 expect(report.status).toBe('completed')
12 expect(report.demonstrationMatched).toBe(true)
13 expect(report.delivery.outcome).toBe('received')
14 expect(report.landingContact).toBe(true)
15 expect(report.load.final.totalMassKg).toBe(1.5)
16 expect(report.board.actuators).toEqual([0, 0, 0, 0])
17 expect(report.energy.evidence).toBe('declared-mission-power-budget')
18 expect(report.energy.worstCaseMs).toBe(81520)
19 expect(report.physicalFlightEnabled).toBe(false)
20 expect(report.identityAuthenticated).toBe(false)
21 expect(result.stdout.trim().split('\n')).toHaveLength(1)
22 expect(Buffer.byteLength(result.stdout)).toBeLessThanOrEqual(16384)
23}, 40000)
24
25
26test('native delivery keeps refused and uncertain custody distinct from its completed alternate', async () => {
27 for (const scenario of ['recipient-absent', 'site-blocked', 'mechanism-jam', 'receipt-loss', 'foreign-receipt']) {
28 const result = await command(['cargo:deliver', '--scenario', scenario]).withTimeout(30000)
29 expect(result.exitCode).toBe(0)
30 expect(result.timedOut).toBe(false)
31 const report = JSON.parse(result.stdout)
32 expect(report.status).toBe('diverted')
33 expect(report.demonstrationMatched).toBe(true)
34 expect(report.landingContact).toBe(true)
35 expect(report.delivery.outcome).toBe(scenario.includes('receipt') ? 'uncertain' : 'not-released')
36 expect(report.mission.payloadFailure.landingTaskId).toBe('alternate-land')
37 expect(report.board.actuators).toEqual([0, 0, 0, 0])
38 expect(Buffer.byteLength(result.stdout)).toBeLessThanOrEqual(16384)
39 }
40}, 170000)
41
42test('native delivery reports backup continuity and the actual authority failure outcomes', async () => {
43 const cases = [
44 ['primary-link-loss', 'completed'], ['both-links-loss', 'blocked'], ['position-loss', 'blocked'], ['imu-loss', 'blocked'], ['vehicle-sensor-loss', 'blocked'],
45 ['rc-override', 'blocked'], ['kill', 'blocked'], ['host-loss', 'host-lost'], ['authority-containment', 'blocked'], ['diversion-position-loss', 'blocked'], ['diversion-kill', 'blocked'],
46 ]
47 for (const [scenario, status] of cases) {
48 const result = await command(['cargo:deliver', '--scenario', scenario!]).withTimeout(30000)
49 expect(result.timedOut).toBe(false)
50 expect(result.exitCode).toBe(0)
51 const report = JSON.parse(result.stdout)
52 expect(report.status).toBe(status)
53 expect(report.demonstrationMatched).toBe(true)
54 expect(report.delivery.outcome).toBe(scenario === 'primary-link-loss' ? 'received' : 'not-released')
55 expect(report.landingContact).toBe(scenario === 'primary-link-loss')
56 expect(report.board.actuators).toEqual([0, 0, 0, 0])
57 expect(report.board.expiresAtMs).toBeNull()
58 expect(report.authority.hostAvailable).toBe(scenario !== 'host-loss')
59 if (scenario === 'host-loss') {
60 expect(report.authority.state).toBe('autonomous')
61 expect(report.board.fault).toBe('output-lease-expired')
62 }
63 expect(report.links.scope).toEqual({ vehicleId: 'carrier-a', bootId: 55, sessionId: 123 })
64 expect(report.physicalFlightEnabled).toBe(false)
65 expect(report.identityAuthenticated).toBe(false)
66 expect(Buffer.byteLength(result.stdout)).toBeLessThanOrEqual(16384)
67 }
68}, 340000)
69
70test('native delivery final options reject coercion without echoing caller details', async () => {
71 for (const flags of [['--scenario', '/private-delivery-sentinel'], ['--scenario', ''], ['--scenario=false'], ['--scenario', 'nominal', '--scenario', 'nominal']]) {
72 const result = await command(['cargo:deliver', ...flags]).withTimeout(30000)
73 expect(result.exitCode).toBe(1)
74 expect(result.stdout).toBe('')
75 expect(result.stderr).toBe('Cargo delivery simulation failed: invalid input, unavailable result or output failure\n')
76 expect(result.timedOut).toBe(false)
77 }
78}, 130000)
79
80test('a closed delivery stdout exits with an awaited fixed error and no success payload', async () => {
81 const child = spawn(process.execPath, ['bin/drone.ts', 'cargo:deliver'], { stdio: ['ignore', 'pipe', 'pipe'] })
82 child.stdout.destroy()
83 let stderr = '', timedOut = false
84 child.stderr.on('data', (chunk: Buffer) => {
85 stderr += chunk.toString('utf8')
86 if (Buffer.byteLength(stderr) > 8192) child.kill('SIGKILL')
87 })
88 const guard = setTimeout(() => { timedOut = true; child.kill('SIGKILL') }, 30000)
89 let code: number | null
90 try { code = await new Promise<number | null>((resolve, reject) => { child.once('error', reject); child.once('close', resolve) }) }
91 finally { clearTimeout(guard) }
92 expect(timedOut).toBe(false)
93 expect(code).toBe(1)
94 expect(stderr).toBe('Cargo delivery simulation failed: invalid input, unavailable result or output failure\n')
95}, 40000)