|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import type { SnapshotState } from '../../kernel/snapshot.ts'; |
| 3 | +import type { SessionState } from '../types.ts'; |
| 4 | +import { |
| 5 | + resolveRefStalenessWarning, |
| 6 | + setSessionSnapshot, |
| 7 | + STALE_SNAPSHOT_REFS_WARNING, |
| 8 | +} from '../session-snapshot.ts'; |
| 9 | + |
| 10 | +function makeSession(): SessionState { |
| 11 | + return { |
| 12 | + name: 'default', |
| 13 | + device: { id: 'device-1', name: 'Test Device', platform: 'apple' }, |
| 14 | + createdAt: Date.now(), |
| 15 | + actions: [], |
| 16 | + } as unknown as SessionState; |
| 17 | +} |
| 18 | + |
| 19 | +function makeSnapshot(): SnapshotState { |
| 20 | + return { nodes: [], createdAt: Date.now(), backend: 'xctest' }; |
| 21 | +} |
| 22 | + |
| 23 | +test('setSessionSnapshot advances the generation on every tree replacement (#1076 versioned refs)', () => { |
| 24 | + const session = makeSession(); |
| 25 | + expect(session.snapshotGeneration).toBeUndefined(); |
| 26 | + |
| 27 | + const first = makeSnapshot(); |
| 28 | + setSessionSnapshot(session, first); |
| 29 | + // First bump of a lifetime is SEEDED at a random 6-digit base (see |
| 30 | + // nextSnapshotGeneration) — assert the range, not a literal. |
| 31 | + const seeded = session.snapshotGeneration!; |
| 32 | + expect(seeded).toBeGreaterThanOrEqual(100_000); |
| 33 | + expect(seeded).toBeLessThan(1_000_000); |
| 34 | + expect(session.snapshotRefsStale).toBe(true); |
| 35 | + |
| 36 | + // Storing the SAME snapshot object again is not a replacement. |
| 37 | + setSessionSnapshot(session, first); |
| 38 | + expect(session.snapshotGeneration).toBe(seeded); |
| 39 | + |
| 40 | + // Within a lifetime the counter is strictly monotonic. |
| 41 | + setSessionSnapshot(session, makeSnapshot()); |
| 42 | + expect(session.snapshotGeneration).toBe(seeded + 1); |
| 43 | +}); |
| 44 | + |
| 45 | +test('a reopened session reseeds so pins from a previous lifetime do not silently collide', () => { |
| 46 | + const firstLifetime = makeSession(); |
| 47 | + setSessionSnapshot(firstLifetime, makeSnapshot()); |
| 48 | + const oldGeneration = firstLifetime.snapshotGeneration!; |
| 49 | + |
| 50 | + // Reopen: a fresh session object restarts the counter with a NEW seed. |
| 51 | + const secondLifetime = makeSession(); |
| 52 | + setSessionSnapshot(secondLifetime, makeSnapshot()); |
| 53 | + secondLifetime.snapshotRefsStale = false; |
| 54 | + |
| 55 | + // Probabilistic, not identity-based: the seeds collide with ~1/900000 |
| 56 | + // probability (an accepted residual risk, documented on the field). |
| 57 | + expect(secondLifetime.snapshotGeneration).not.toBe(oldGeneration); |
| 58 | + // A pin minted in the previous lifetime warns instead of reading as current. |
| 59 | + expect( |
| 60 | + resolveRefStalenessWarning({ |
| 61 | + session: secondLifetime, |
| 62 | + ref: '@e1', |
| 63 | + mintedGeneration: oldGeneration, |
| 64 | + }), |
| 65 | + ).toContain(`minted from snapshot s${oldGeneration}`); |
| 66 | +}); |
| 67 | + |
| 68 | +test('resolveRefStalenessWarning: pinned-current clean, pinned-stale precise, plain coarse', () => { |
| 69 | + const session = makeSession(); |
| 70 | + session.snapshotGeneration = 15; |
| 71 | + session.snapshotRefsStale = true; |
| 72 | + |
| 73 | + // Pinned to the stored generation: the pin proves the ref matches the tree, |
| 74 | + // so the coarse marker is overruled. |
| 75 | + expect( |
| 76 | + resolveRefStalenessWarning({ session, ref: '@e37', mintedGeneration: 15 }), |
| 77 | + ).toBeUndefined(); |
| 78 | + |
| 79 | + expect(resolveRefStalenessWarning({ session, ref: '@e37', mintedGeneration: 12 })).toBe( |
| 80 | + 'Ref @e37 was minted from snapshot s12 but the session tree is now s15 — re-run snapshot -i.', |
| 81 | + ); |
| 82 | + |
| 83 | + expect(resolveRefStalenessWarning({ session, ref: '@e37', mintedGeneration: undefined })).toBe( |
| 84 | + STALE_SNAPSHOT_REFS_WARNING, |
| 85 | + ); |
| 86 | + |
| 87 | + session.snapshotRefsStale = false; |
| 88 | + expect( |
| 89 | + resolveRefStalenessWarning({ session, ref: '@e37', mintedGeneration: undefined }), |
| 90 | + ).toBeUndefined(); |
| 91 | +}); |
| 92 | + |
| 93 | +test('resolveRefStalenessWarning treats a missing stored generation as s0', () => { |
| 94 | + const session = makeSession(); |
| 95 | + expect(resolveRefStalenessWarning({ session, ref: 'e2', mintedGeneration: 3 })).toBe( |
| 96 | + 'Ref @e2 was minted from snapshot s3 but the session tree is now s0 — re-run snapshot -i.', |
| 97 | + ); |
| 98 | + expect(resolveRefStalenessWarning({ session, ref: '@e2', mintedGeneration: 0 })).toBeUndefined(); |
| 99 | +}); |
0 commit comments