| 1 | // Run: tsx src/__tests__/session-title-contract.test.ts |
| 2 | |
| 3 | import { |
| 4 | historySessionDisplayTitle, |
| 5 | paletteSessionDisplayTitle, |
| 6 | paletteSessionHint, |
| 7 | paletteSessionKeywords, |
| 8 | } from "../lib/session"; |
| 9 | import type { SessionMeta } from "../lib/types"; |
| 10 | |
| 11 | let passed = 0; |
| 12 | let failed = 0; |
| 13 | |
| 14 | function eq<T>(actual: T, expected: T, label: string) { |
| 15 | if (Object.is(actual, expected)) { |
| 16 | process.stdout.write(` PASS ${label}\n`); |
| 17 | passed += 1; |
| 18 | } else { |
| 19 | process.stdout.write(` FAIL ${label}\n expected: ${String(expected)}\n actual: ${String(actual)}\n`); |
| 20 | failed += 1; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | function deepEq<T>(actual: T, expected: T, label: string) { |
| 25 | const actualJson = JSON.stringify(actual); |
| 26 | const expectedJson = JSON.stringify(expected); |
| 27 | if (actualJson === expectedJson) { |
| 28 | process.stdout.write(` PASS ${label}\n`); |
| 29 | passed += 1; |
| 30 | } else { |
| 31 | process.stdout.write(` FAIL ${label}\n expected: ${expectedJson}\n actual: ${actualJson}\n`); |
| 32 | failed += 1; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | function session(overrides: Partial<SessionMeta> = {}): SessionMeta { |
| 37 | return { |
| 38 | path: "/sessions/topic-backed.jsonl", |
| 39 | preview: "first saved prompt preview", |
| 40 | title: "Renamed saved session", |
| 41 | turns: 3, |
| 42 | createdAt: 100, |
| 43 | lastActivityAt: 200, |
| 44 | modTime: 200, |
| 45 | current: false, |
| 46 | open: false, |
| 47 | workspaceRoot: "/work/project-alpha", |
| 48 | topicId: "topic-1", |
| 49 | topicTitle: "Shared topic", |
| 50 | ...overrides, |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | console.log("\nsession title contracts"); |
| 55 | |
| 56 | { |
| 57 | const item = session(); |
| 58 | eq( |
| 59 | paletteSessionDisplayTitle(item, "Untitled"), |
| 60 | "Shared topic", |
| 61 | "Cmd+K displays the topic title for topic-backed sessions", |
| 62 | ); |
| 63 | deepEq( |
| 64 | paletteSessionKeywords(item), |
| 65 | ["Renamed saved session", "first saved prompt preview"], |
| 66 | "Cmd+K still searches the session rename and preview text", |
| 67 | ); |
| 68 | eq( |
| 69 | paletteSessionHint(item), |
| 70 | "Renamed saved session · /work/project-alpha", |
| 71 | "Cmd+K shows the session rename in the hint so hidden search hits are visible", |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | const item = session(); |
| 77 | eq( |
| 78 | historySessionDisplayTitle(item, "Untitled"), |
| 79 | "Renamed saved session", |
| 80 | "History displays an explicit session rename before the topic title", |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | { |
| 85 | const item = session({ title: " " }); |
| 86 | eq( |
| 87 | historySessionDisplayTitle(item, "Untitled"), |
| 88 | "Shared topic", |
| 89 | "Unrenamed topic-backed history sessions fall back to topic title", |
| 90 | ); |
| 91 | deepEq( |
| 92 | paletteSessionKeywords(item), |
| 93 | ["first saved prompt preview"], |
| 94 | "Blank session titles are not added as Cmd+K keywords", |
| 95 | ); |
| 96 | eq( |
| 97 | paletteSessionHint(item), |
| 98 | "first saved prompt preview · /work/project-alpha", |
| 99 | "Cmd+K falls back to preview text in the hint when there is no custom session title", |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | const item = session({ title: "Shared topic", preview: "first saved prompt preview" }); |
| 105 | eq( |
| 106 | paletteSessionHint(item), |
| 107 | "first saved prompt preview · /work/project-alpha", |
| 108 | "Cmd+K skips duplicate title hints that repeat the visible topic title", |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | console.log(`\n${passed} passed, ${failed} failed`); |
| 113 | if (failed > 0) process.exit(1); |
| 114 |