|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { makeSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 5 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 6 | + |
| 7 | +const ADDRESS = 'cwd:worktree:default'; |
| 8 | + |
| 9 | +function ended(error: unknown): boolean { |
| 10 | + return error instanceof AppError && error.details?.reason === 'session_lifetime_ended'; |
| 11 | +} |
| 12 | + |
| 13 | +test('refs capture records while resolving rebuilds from the same lifetime', () => { |
| 14 | + const store = makeSessionStore(); |
| 15 | + const session = makeSession('default'); |
| 16 | + const initial = store.publish(ADDRESS, session); |
| 17 | + const lookup = store.lookup(ADDRESS)!; |
| 18 | + const listed = store.listRefs()[0]!; |
| 19 | + const byDevice = store.findByDevice(session.device.id)!; |
| 20 | + for (const ref of [lookup, listed, byDevice]) { |
| 21 | + assert.notEqual(ref, initial); |
| 22 | + assert.equal(ref.lifetime, initial.lifetime); |
| 23 | + assert.equal(ref.session, session); |
| 24 | + assert.equal(Object.isFrozen(ref), true); |
| 25 | + } |
| 26 | + const rebuilt = store.update(initial, { appName: 'Reopened' }); |
| 27 | + assert.equal(initial.session, session); |
| 28 | + assert.equal(initial.session.appName, undefined); |
| 29 | + for (const ref of [initial, lookup, listed, byDevice]) { |
| 30 | + assert.equal(store.resolveCurrent(ref), rebuilt); |
| 31 | + } |
| 32 | + assert.equal(store.lookup(ADDRESS)?.session, rebuilt); |
| 33 | + assert.equal(store.get('default'), undefined); |
| 34 | +}); |
| 35 | + |
| 36 | +test('updates derive from the latest matching record and preserve intervening fields', () => { |
| 37 | + const store = makeSessionStore(); |
| 38 | + const ref = store.publish(ADDRESS, makeSession('default', { createdAt: 10 })); |
| 39 | + store.update(ref, { appBundleId: 'com.example.updated' }); |
| 40 | + store.update(ref, (current) => ({ |
| 41 | + appName: current.appBundleId, |
| 42 | + createdAt: current.createdAt + 1, |
| 43 | + })); |
| 44 | + assert.equal(store.get(ADDRESS)?.appBundleId, 'com.example.updated'); |
| 45 | + assert.equal(store.get(ADDRESS)?.appName, 'com.example.updated'); |
| 46 | + assert.equal(store.get(ADDRESS)?.createdAt, 11); |
| 47 | + assert.equal(store.get(ADDRESS)?.actions, ref.session.actions); |
| 48 | +}); |
| 49 | + |
| 50 | +test('address reuse with the same record still starts a different lifetime', () => { |
| 51 | + const store = makeSessionStore(); |
| 52 | + const session = makeSession('default'); |
| 53 | + const old = store.publish(ADDRESS, session); |
| 54 | + assert.equal(store.retire(old), true); |
| 55 | + const successor = store.publish(ADDRESS, session); |
| 56 | + store.setRuntimeHints(ADDRESS, { metroPort: 8082 }); |
| 57 | + assert.notEqual(successor.lifetime, old.lifetime); |
| 58 | + assert.equal(store.resolveCurrent(old), undefined); |
| 59 | + assert.throws(() => store.requireCurrent(old), ended); |
| 60 | + assert.throws(() => store.update(old, { appName: 'Stale' }), ended); |
| 61 | + assert.equal(store.retire(old), false); |
| 62 | + assert.equal(store.get(ADDRESS), session); |
| 63 | + assert.equal(store.getRuntimeHints(ADDRESS)?.metroPort, 8082); |
| 64 | + assert.equal(store.retire(successor), true); |
| 65 | + assert.equal(store.getRuntimeHints(ADDRESS), undefined); |
| 66 | +}); |
| 67 | + |
| 68 | +test('retired updates cannot run their derivation or resurrect a record', () => { |
| 69 | + const store = makeSessionStore(); |
| 70 | + const ref = store.publish(ADDRESS, makeSession('default')); |
| 71 | + store.retire(ref); |
| 72 | + let ran = false; |
| 73 | + assert.throws( |
| 74 | + () => |
| 75 | + store.update(ref, () => { |
| 76 | + ran = true; |
| 77 | + return { appName: 'Late' }; |
| 78 | + }), |
| 79 | + ended, |
| 80 | + ); |
| 81 | + assert.equal(ran, false); |
| 82 | + assert.equal(store.lookup(ADDRESS), undefined); |
| 83 | +}); |
| 84 | + |
| 85 | +test('an occupied address cannot be published again', () => { |
| 86 | + const store = makeSessionStore(); |
| 87 | + const ref = store.publish(ADDRESS, makeSession('default')); |
| 88 | + assert.throws( |
| 89 | + () => store.publish(ADDRESS, makeSession('default')), |
| 90 | + (error) => error instanceof AppError && error.details?.reason === 'session_address_occupied', |
| 91 | + ); |
| 92 | + assert.equal(store.requireCurrent(ref), ref.session); |
| 93 | +}); |
| 94 | + |
| 95 | +test('shutdown closes draft admission while allowing the current lifetime to settle', () => { |
| 96 | + const store = makeSessionStore(); |
| 97 | + const ref = store.publish(ADDRESS, makeSession('default')); |
| 98 | + store.closeAdmission(); |
| 99 | + assert.throws( |
| 100 | + () => store.publish('late-draft', makeSession('late-draft')), |
| 101 | + (error) => error instanceof AppError && error.details?.reason === 'daemon_shutting_down', |
| 102 | + ); |
| 103 | + store.update(ref, { appName: 'Settled' }); |
| 104 | + assert.equal(store.requireCurrent(ref).appName, 'Settled'); |
| 105 | + assert.equal(store.retire(ref), true); |
| 106 | + assert.equal(store.lookup('late-draft'), undefined); |
| 107 | +}); |
| 108 | + |
| 109 | +test('a ref from another store has no authority over the same address', () => { |
| 110 | + const source = makeSessionStore(); |
| 111 | + const target = makeSessionStore(); |
| 112 | + const foreign = source.publish(ADDRESS, makeSession('default')); |
| 113 | + const local = target.publish(ADDRESS, foreign.session); |
| 114 | + assert.equal(target.resolveCurrent(foreign), undefined); |
| 115 | + assert.throws(() => target.update(foreign, { appName: 'Foreign' }), ended); |
| 116 | + assert.equal(target.retire(foreign), false); |
| 117 | + assert.equal(target.requireCurrent(local), foreign.session); |
| 118 | +}); |
0 commit comments