|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Unit tests for the orphan-ledger-entry scan (the reverse direction of the |
| 4 | +// liveness gate — see orphans.mts for why it exists). |
| 5 | +// |
| 6 | +// These matter more than a typical unit test: the tree was ORPHAN-FREE when the |
| 7 | +// check landed, so a green `check:liveness` proves nothing about whether the |
| 8 | +// scan can fire at all. That proof has to come from here. Real-ledger coverage |
| 9 | +// is the gate's own CI job (`Spec property liveness`), which walks the live Zod |
| 10 | +// schemas — this file owns the logic, including every case it must stay quiet on. |
| 11 | + |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { findOrphanEntries, ORPHAN_GUIDANCE } from './orphans.mts'; |
| 14 | + |
| 15 | +/** No property is a container unless a test says so. */ |
| 16 | +const noChildren = () => null; |
| 17 | + |
| 18 | +describe('findOrphanEntries — rows it must catch', () => { |
| 19 | + it('catches a top-level row whose key left the schema (the strict-removal shape)', () => { |
| 20 | + const orphans = findOrphanEntries({ |
| 21 | + type: 'tool', |
| 22 | + props: { name: { status: 'live' }, category: { status: 'dead' } }, |
| 23 | + shapeKeys: ['name'], // `category` was deleted from the `.strict()` ToolSchema |
| 24 | + childKeysOf: noChildren, |
| 25 | + }); |
| 26 | + expect(orphans).toEqual([{ key: 'tool/category', level: 'top' }]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('catches a drilled child row whose key left its container', () => { |
| 30 | + const orphans = findOrphanEntries({ |
| 31 | + type: 'report', |
| 32 | + props: { layout: { children: { columns: { status: 'live' }, aria: { status: 'dead' } } } }, |
| 33 | + shapeKeys: ['layout'], |
| 34 | + childKeysOf: (key) => (key === 'layout' ? ['columns'] : null), |
| 35 | + }); |
| 36 | + expect(orphans).toEqual([{ key: 'report/layout.aria', level: 'child' }]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('reports every orphan, not just the first', () => { |
| 40 | + const orphans = findOrphanEntries({ |
| 41 | + type: 'view', |
| 42 | + props: { |
| 43 | + gone: { status: 'dead' }, |
| 44 | + alsoGone: { status: 'live' }, |
| 45 | + list: { children: { type: { status: 'live' }, responsive: { status: 'dead' } } }, |
| 46 | + }, |
| 47 | + shapeKeys: ['list'], // `gone` and `alsoGone` both left the schema |
| 48 | + childKeysOf: (key) => (key === 'list' ? ['type'] : null), |
| 49 | + }); |
| 50 | + expect(orphans.map((o) => o.key)).toEqual(['view/gone', 'view/alsoGone', 'view/list.responsive']); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('findOrphanEntries — cases it must stay quiet on', () => { |
| 55 | + it('does NOT flag a `retiredKey()` tombstone — z.never() is still a property', () => { |
| 56 | + // The asymmetry this whole check exists to make legible: a tombstoned key |
| 57 | + // stays in the walked shape, so its row is correct and must stay. Flagging |
| 58 | + // it would tell the author to delete a row whose deletion fails CI as |
| 59 | + // UNCLASSIFIED — pushing them into an infinite loop between two gates. |
| 60 | + const orphans = findOrphanEntries({ |
| 61 | + type: 'action', |
| 62 | + props: { shortcut: { status: 'dead', note: 'REMOVED — tombstoned, row stays' } }, |
| 63 | + shapeKeys: ['shortcut'], |
| 64 | + childKeysOf: noChildren, |
| 65 | + }); |
| 66 | + expect(orphans).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('defers `children` declared on a non-container to the forward pass', () => { |
| 70 | + // The forward walk already reports this as UNCLASSIFIED with a more precise |
| 71 | + // message ("declared children but property is not a container"). Two |
| 72 | + // headings for one fix reads as two problems. |
| 73 | + const orphans = findOrphanEntries({ |
| 74 | + type: 'flow', |
| 75 | + props: { name: { children: { nope: { status: 'dead' } } } }, |
| 76 | + shapeKeys: ['name'], |
| 77 | + childKeysOf: noChildren, // `name` is a string, not a container |
| 78 | + }); |
| 79 | + expect(orphans).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('is quiet on a ledger that matches its schema exactly', () => { |
| 83 | + const orphans = findOrphanEntries({ |
| 84 | + type: 'skill', |
| 85 | + props: { name: { status: 'live' }, tools: { status: 'live' } }, |
| 86 | + shapeKeys: ['name', 'tools', 'label'], // an unclassified extra is the FORWARD pass's job |
| 87 | + childKeysOf: noChildren, |
| 88 | + }); |
| 89 | + expect(orphans).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('handles an absent or empty props block without throwing', () => { |
| 93 | + const base = { type: 'page', shapeKeys: ['name'], childKeysOf: noChildren }; |
| 94 | + expect(findOrphanEntries({ ...base, props: undefined })).toEqual([]); |
| 95 | + expect(findOrphanEntries({ ...base, props: {} })).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does not confuse an entry FIELD with a child prop', () => { |
| 99 | + // `status`/`evidence`/`verifiedAt`/`note`/`authorWarn` live on the row, not |
| 100 | + // under `children` — only a declared `children` map is drilled. |
| 101 | + const orphans = findOrphanEntries({ |
| 102 | + type: 'agent', |
| 103 | + props: { |
| 104 | + role: { status: 'live', evidence: 'x.ts:1', verifiedAt: '2026-07-30', authorWarn: true, note: 'n' }, |
| 105 | + }, |
| 106 | + shapeKeys: ['role'], |
| 107 | + childKeysOf: () => ['definitely', 'not', 'these'], |
| 108 | + }); |
| 109 | + expect(orphans).toEqual([]); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('ORPHAN_GUIDANCE', () => { |
| 114 | + it('names both causes and the asymmetry, so the wrong fix is not the obvious one', () => { |
| 115 | + const text = ORPHAN_GUIDANCE.join(' '); |
| 116 | + expect(text).toMatch(/STRICT-REMOVAL/); |
| 117 | + expect(text).toMatch(/Delete the row/); |
| 118 | + expect(text).toMatch(/retiredKey\(\).*KEEPS the key/); |
| 119 | + expect(text).toMatch(/UNCLASSIFIED/); |
| 120 | + expect(text).toMatch(/Fix the walk, not the row/); |
| 121 | + }); |
| 122 | +}); |
0 commit comments