|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | + |
| 7 | +import { |
| 8 | + computeTuiView, |
| 9 | + fuzzyScore, |
| 10 | + nextRecents, |
| 11 | + parseRecents, |
| 12 | + rankCandidates, |
| 13 | + recentCandidates, |
| 14 | + recordRecent, |
| 15 | + loadRecents, |
| 16 | + type TuiCandidate |
| 17 | +} from './persona-tui.js'; |
| 18 | + |
| 19 | +const CANDIDATES: TuiCandidate[] = [ |
| 20 | + { |
| 21 | + id: 'code-reviewer', |
| 22 | + description: 'Reviews pull requests for quality, correctness and security.', |
| 23 | + source: 'library' |
| 24 | + }, |
| 25 | + { |
| 26 | + id: 'fix-flaky', |
| 27 | + description: 'Repairs flaky tests across the test suite.', |
| 28 | + source: 'user' |
| 29 | + }, |
| 30 | + { |
| 31 | + id: 'persona-maker', |
| 32 | + description: 'Scaffolds a new persona via interactive Q&A.', |
| 33 | + source: 'library' |
| 34 | + }, |
| 35 | + { |
| 36 | + id: 'my-reviewer', |
| 37 | + description: 'Local reviewer override with team-specific style rules.', |
| 38 | + source: 'cwd' |
| 39 | + } |
| 40 | +]; |
| 41 | + |
| 42 | +test('fuzzyScore returns null when chars are absent or out of order', () => { |
| 43 | + assert.equal(fuzzyScore('zzz', 'code-reviewer'), null); |
| 44 | + assert.equal(fuzzyScore('reverse', 'reviewer'), null); |
| 45 | +}); |
| 46 | + |
| 47 | +test('fuzzyScore prefers prefix and dense matches', () => { |
| 48 | + const prefix = fuzzyScore('code', 'code-reviewer'); |
| 49 | + const scattered = fuzzyScore('code', 'committed-old-de'); |
| 50 | + assert.ok(prefix !== null && scattered !== null); |
| 51 | + assert.ok(prefix! < scattered!, `expected prefix=${prefix} < scattered=${scattered}`); |
| 52 | +}); |
| 53 | + |
| 54 | +test('rankCandidates surfaces name matches over description matches', () => { |
| 55 | + // Both reviewer ids match by name; "review" doesn't subsequence-match |
| 56 | + // anything else, so the two name matches are the only results and rank |
| 57 | + // by leading-offset (my-reviewer first because "r" appears earlier). |
| 58 | + const ranked = rankCandidates(CANDIDATES, 'review'); |
| 59 | + assert.deepEqual(ranked.map((c) => c.id), ['my-reviewer', 'code-reviewer']); |
| 60 | +}); |
| 61 | + |
| 62 | +test('rankCandidates returns empty array when nothing matches', () => { |
| 63 | + assert.deepEqual(rankCandidates(CANDIDATES, 'xxxxxxxx'), []); |
| 64 | +}); |
| 65 | + |
| 66 | +test('rankCandidates returns all candidates with empty query', () => { |
| 67 | + const ranked = rankCandidates(CANDIDATES, ' '); |
| 68 | + assert.equal(ranked.length, CANDIDATES.length); |
| 69 | +}); |
| 70 | + |
| 71 | +test('rankCandidates can match purely from description text', () => { |
| 72 | + const ranked = rankCandidates(CANDIDATES, 'flaky'); |
| 73 | + assert.equal(ranked[0].id, 'fix-flaky'); |
| 74 | +}); |
| 75 | + |
| 76 | +test('recentCandidates preserves order and drops unknown ids', () => { |
| 77 | + const recents = recentCandidates( |
| 78 | + CANDIDATES, |
| 79 | + ['fix-flaky', 'gone-persona', 'code-reviewer', 'my-reviewer'], |
| 80 | + 3 |
| 81 | + ); |
| 82 | + assert.deepEqual( |
| 83 | + recents.map((c) => c.id), |
| 84 | + ['fix-flaky', 'code-reviewer', 'my-reviewer'] |
| 85 | + ); |
| 86 | +}); |
| 87 | + |
| 88 | +test('nextRecents moves an existing id to the front and caps the list', () => { |
| 89 | + const result = nextRecents(['a', 'b', 'c', 'd', 'e'], 'c', 3); |
| 90 | + assert.deepEqual(result, ['c', 'a', 'b']); |
| 91 | +}); |
| 92 | + |
| 93 | +test('nextRecents prepends a new id', () => { |
| 94 | + assert.deepEqual(nextRecents(['a', 'b'], 'z'), ['z', 'a', 'b']); |
| 95 | +}); |
| 96 | + |
| 97 | +test('parseRecents tolerates garbage input', () => { |
| 98 | + assert.deepEqual(parseRecents('not json'), []); |
| 99 | + assert.deepEqual(parseRecents('null'), []); |
| 100 | + assert.deepEqual(parseRecents('{"ids": "nope"}'), []); |
| 101 | + assert.deepEqual(parseRecents('{"ids": [1, "ok", " ", "ok"]}'), ['ok']); |
| 102 | +}); |
| 103 | + |
| 104 | +test('recordRecent + loadRecents round-trip via the filesystem', () => { |
| 105 | + const dir = mkdtempSync(join(tmpdir(), 'aw-tui-')); |
| 106 | + const path = join(dir, 'nested', 'recents.json'); |
| 107 | + try { |
| 108 | + recordRecent('code-reviewer', path); |
| 109 | + recordRecent('fix-flaky', path); |
| 110 | + recordRecent('code-reviewer', path); |
| 111 | + assert.deepEqual(loadRecents(path), ['code-reviewer', 'fix-flaky']); |
| 112 | + const onDisk = JSON.parse(readFileSync(path, 'utf8')) as { version: number; ids: string[] }; |
| 113 | + assert.equal(onDisk.version, 1); |
| 114 | + assert.deepEqual(onDisk.ids, ['code-reviewer', 'fix-flaky']); |
| 115 | + } finally { |
| 116 | + rmSync(dir, { recursive: true, force: true }); |
| 117 | + } |
| 118 | +}); |
| 119 | + |
| 120 | +test('computeTuiView: empty query with resolved recents → recents mode', () => { |
| 121 | + const view = computeTuiView(CANDIDATES, ['fix-flaky', 'code-reviewer'], ''); |
| 122 | + assert.equal(view.mode, 'recents'); |
| 123 | + assert.deepEqual(view.items.map((c) => c.id), ['fix-flaky', 'code-reviewer']); |
| 124 | +}); |
| 125 | + |
| 126 | +test('computeTuiView: recents pointing only at unknown ids → all mode (regression)', () => { |
| 127 | + // Prior bug: header said "RECENT" because recentIds was non-empty even |
| 128 | + // though every id had been uninstalled/renamed and the full catalog was |
| 129 | + // being shown. |
| 130 | + const view = computeTuiView(CANDIDATES, ['ghost-persona', 'also-gone'], ''); |
| 131 | + assert.equal(view.mode, 'all'); |
| 132 | + assert.equal(view.items.length, CANDIDATES.length); |
| 133 | +}); |
| 134 | + |
| 135 | +test('computeTuiView: empty query with no recents → all mode', () => { |
| 136 | + const view = computeTuiView(CANDIDATES, [], ''); |
| 137 | + assert.equal(view.mode, 'all'); |
| 138 | +}); |
| 139 | + |
| 140 | +test('computeTuiView: non-empty query → matches mode', () => { |
| 141 | + const view = computeTuiView(CANDIDATES, ['fix-flaky'], 'review'); |
| 142 | + assert.equal(view.mode, 'matches'); |
| 143 | + assert.ok(view.items.length > 0); |
| 144 | + assert.ok(view.items.every((c) => c.id.includes('review'))); |
| 145 | +}); |
| 146 | + |
| 147 | +test('computeTuiView: matches mode honors visibleCap', () => { |
| 148 | + const view = computeTuiView(CANDIDATES, [], 'e', 2); |
| 149 | + assert.equal(view.mode, 'matches'); |
| 150 | + assert.ok(view.items.length <= 2); |
| 151 | +}); |
| 152 | + |
| 153 | +test('loadRecents returns [] when the file is absent or corrupt', () => { |
| 154 | + const dir = mkdtempSync(join(tmpdir(), 'aw-tui-')); |
| 155 | + const path = join(dir, 'recents.json'); |
| 156 | + try { |
| 157 | + assert.deepEqual(loadRecents(path), []); |
| 158 | + writeFileSync(path, '{ not json', 'utf8'); |
| 159 | + assert.deepEqual(loadRecents(path), []); |
| 160 | + } finally { |
| 161 | + rmSync(dir, { recursive: true, force: true }); |
| 162 | + } |
| 163 | +}); |
0 commit comments