|
| 1 | +/** |
| 2 | + * Pure-logic tests for share-URL helpers. URL construction and |
| 3 | + * sim_saved event scanning live here so they run under node:test |
| 4 | + * without a browser shim. |
| 5 | + */ |
| 6 | +import test from 'node:test'; |
| 7 | +import assert from 'node:assert/strict'; |
| 8 | +import { |
| 9 | + buildReplayShareUrl, |
| 10 | + findLatestSavedSessionId, |
| 11 | +} from './shareUrl.helpers.js'; |
| 12 | +import type { SimEvent } from './useSSE'; |
| 13 | + |
| 14 | +// -- buildReplayShareUrl -------------------------------------------------- |
| 15 | + |
| 16 | +test('buildReplayShareUrl: defaults to viz tab', () => { |
| 17 | + const url = buildReplayShareUrl('https://paracosm.agentos.sh', 'abc123'); |
| 18 | + assert.equal(url, 'https://paracosm.agentos.sh/sim?replay=abc123&tab=viz'); |
| 19 | +}); |
| 20 | + |
| 21 | +test('buildReplayShareUrl: honors explicit sim tab', () => { |
| 22 | + const url = buildReplayShareUrl('https://paracosm.agentos.sh', 'abc123', 'sim'); |
| 23 | + assert.equal(url, 'https://paracosm.agentos.sh/sim?replay=abc123&tab=sim'); |
| 24 | +}); |
| 25 | + |
| 26 | +test('buildReplayShareUrl: honors reports tab', () => { |
| 27 | + const url = buildReplayShareUrl('https://paracosm.agentos.sh', 'abc123', 'reports'); |
| 28 | + assert.equal(url, 'https://paracosm.agentos.sh/sim?replay=abc123&tab=reports'); |
| 29 | +}); |
| 30 | + |
| 31 | +test('buildReplayShareUrl: handles origin with trailing slash', () => { |
| 32 | + const url = buildReplayShareUrl('https://paracosm.agentos.sh/', 'abc123'); |
| 33 | + assert.equal(url, 'https://paracosm.agentos.sh/sim?replay=abc123&tab=viz'); |
| 34 | +}); |
| 35 | + |
| 36 | +test('buildReplayShareUrl: encodes session id with special chars', () => { |
| 37 | + const url = buildReplayShareUrl('https://paracosm.agentos.sh', 'id with spaces'); |
| 38 | + assert.match(url, /replay=id\+with\+spaces|replay=id%20with%20spaces/); |
| 39 | +}); |
| 40 | + |
| 41 | +test('buildReplayShareUrl: works for localhost dev origin', () => { |
| 42 | + const url = buildReplayShareUrl('http://localhost:3456', 'abc123'); |
| 43 | + assert.equal(url, 'http://localhost:3456/sim?replay=abc123&tab=viz'); |
| 44 | +}); |
| 45 | + |
| 46 | +// -- findLatestSavedSessionId -------------------------------------------- |
| 47 | + |
| 48 | +function ev(type: string, data: Record<string, unknown> = {}): SimEvent { |
| 49 | + return { type, leader: '', data } as SimEvent; |
| 50 | +} |
| 51 | + |
| 52 | +test('findLatestSavedSessionId: returns id from sim_saved with status=saved', () => { |
| 53 | + const events = [ |
| 54 | + ev('turn_start'), |
| 55 | + ev('sim_saved', { status: 'saved', id: 'sess_abc' }), |
| 56 | + ]; |
| 57 | + assert.equal(findLatestSavedSessionId(events), 'sess_abc'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('findLatestSavedSessionId: returns null when no sim_saved event', () => { |
| 61 | + const events = [ev('turn_start'), ev('decision')]; |
| 62 | + assert.equal(findLatestSavedSessionId(events), null); |
| 63 | +}); |
| 64 | + |
| 65 | +test('findLatestSavedSessionId: returns null when sim_saved status is failed', () => { |
| 66 | + const events = [ev('sim_saved', { status: 'failed', error: 'disk full' })]; |
| 67 | + assert.equal(findLatestSavedSessionId(events), null); |
| 68 | +}); |
| 69 | + |
| 70 | +test('findLatestSavedSessionId: returns null when sim_saved status is skipped', () => { |
| 71 | + const events = [ev('sim_saved', { status: 'skipped', reason: 'below_min_turns' })]; |
| 72 | + assert.equal(findLatestSavedSessionId(events), null); |
| 73 | +}); |
| 74 | + |
| 75 | +test('findLatestSavedSessionId: returns newest id when multiple saves present', () => { |
| 76 | + const events = [ |
| 77 | + ev('sim_saved', { status: 'saved', id: 'sess_old' }), |
| 78 | + ev('turn_start'), |
| 79 | + ev('sim_saved', { status: 'saved', id: 'sess_new' }), |
| 80 | + ]; |
| 81 | + assert.equal(findLatestSavedSessionId(events), 'sess_new'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('findLatestSavedSessionId: ignores sim_saved without id', () => { |
| 85 | + const events = [ev('sim_saved', { status: 'saved' })]; |
| 86 | + assert.equal(findLatestSavedSessionId(events), null); |
| 87 | +}); |
| 88 | + |
| 89 | +test('findLatestSavedSessionId: ignores sim_saved with non-string id', () => { |
| 90 | + const events = [ev('sim_saved', { status: 'saved', id: 12345 })]; |
| 91 | + assert.equal(findLatestSavedSessionId(events), null); |
| 92 | +}); |
| 93 | + |
| 94 | +test('findLatestSavedSessionId: ignores sim_saved with empty string id', () => { |
| 95 | + const events = [ev('sim_saved', { status: 'saved', id: '' })]; |
| 96 | + assert.equal(findLatestSavedSessionId(events), null); |
| 97 | +}); |
| 98 | + |
| 99 | +test('findLatestSavedSessionId: empty event list -> null', () => { |
| 100 | + assert.equal(findLatestSavedSessionId([]), null); |
| 101 | +}); |
| 102 | + |
| 103 | +test('findLatestSavedSessionId: skips failed save and returns prior saved id', () => { |
| 104 | + const events = [ |
| 105 | + ev('sim_saved', { status: 'saved', id: 'sess_first' }), |
| 106 | + ev('sim_saved', { status: 'failed', error: 'retry' }), |
| 107 | + ]; |
| 108 | + // Newest-wins iteration: failed save short-circuits the continue, |
| 109 | + // then the earlier saved entry is returned. |
| 110 | + assert.equal(findLatestSavedSessionId(events), 'sess_first'); |
| 111 | +}); |
0 commit comments