|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { |
| 4 | + rankSearchableSessions, |
| 5 | + type SearchableSession, |
| 6 | +} from "../src/react-app/domains/session/search/session-search"; |
| 7 | +import { buildCommandPaletteSessions } from "../src/react-app/shell/command-palette-sessions"; |
| 8 | +import type { RouteSession, RouteWorkspace } from "../src/react-app/shell/route-workspaces"; |
| 9 | + |
| 10 | +const NOW = new Date(2026, 9, 29, 15, 0).getTime(); |
| 11 | +const MINUTE = 60 * 1000; |
| 12 | + |
| 13 | +function searchable(sessionId: string, title: string, updatedAt: number, workspaceTitle = "Lounge"): SearchableSession { |
| 14 | + return { workspaceId: "lounge", sessionId, title, workspaceTitle, updatedAt }; |
| 15 | +} |
| 16 | + |
| 17 | +function routeSession(id: string, updated: number, extra: Record<string, unknown> = {}): RouteSession { |
| 18 | + // Only the fields the builder reads; the rest of the shape is irrelevant here. |
| 19 | + return JSON.parse(JSON.stringify({ id, title: id, time: { created: updated, updated }, ...extra })); |
| 20 | +} |
| 21 | + |
| 22 | +function workspace(id: string): RouteWorkspace { |
| 23 | + return JSON.parse(JSON.stringify({ id, name: id, path: `/tmp/${id}` })); |
| 24 | +} |
| 25 | + |
| 26 | +const ids = (sessions: { sessionId: string }[]) => sessions.map((session) => session.sessionId); |
| 27 | + |
| 28 | +describe("rankSearchableSessions", () => { |
| 29 | + test("lists the newest message first with no query", () => { |
| 30 | + const sessions = [ |
| 31 | + searchable("old", "Quarterly report", NOW - 60 * MINUTE), |
| 32 | + searchable("new", "Launch plan", NOW - MINUTE), |
| 33 | + searchable("mid", "Hiring notes", NOW - 10 * MINUTE), |
| 34 | + ]; |
| 35 | + expect(ids(rankSearchableSessions(sessions, "", 15))).toEqual(["new", "mid", "old"]); |
| 36 | + expect(ids(rankSearchableSessions(sessions, "", 2))).toEqual(["new", "mid"]); |
| 37 | + }); |
| 38 | + |
| 39 | + test("orders title matches by newest message, not match quality", () => { |
| 40 | + const sessions = [ |
| 41 | + searchable("exact-old", "report", NOW - 60 * MINUTE), |
| 42 | + searchable("partial-new", "Weekly report draft", NOW - MINUTE), |
| 43 | + searchable("unrelated", "Launch plan", NOW), |
| 44 | + ]; |
| 45 | + expect(ids(rankSearchableSessions(sessions, "report", 50))).toEqual(["partial-new", "exact-old"]); |
| 46 | + }); |
| 47 | + |
| 48 | + test("matches every word across title and workspace", () => { |
| 49 | + const sessions = [ |
| 50 | + searchable("both", "Budget review", NOW - 5 * MINUTE, "Finance"), |
| 51 | + searchable("title-only", "Budget ideas", NOW - MINUTE, "Lounge"), |
| 52 | + ]; |
| 53 | + expect(ids(rankSearchableSessions(sessions, "budget finance", 50))).toEqual(["both"]); |
| 54 | + }); |
| 55 | + |
| 56 | + test("keeps looser fuzzy matches below every contained match", () => { |
| 57 | + const sessions = [ |
| 58 | + searchable("typo-new", "Reprot cleanup", NOW), |
| 59 | + searchable("contains-old", "Monthly report", NOW - 60 * MINUTE), |
| 60 | + ]; |
| 61 | + const ranked = ids(rankSearchableSessions(sessions, "report", 50)); |
| 62 | + expect(ranked[0]).toBe("contains-old"); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("buildCommandPaletteSessions", () => { |
| 67 | + test("leaves out sub-agent sessions", () => { |
| 68 | + const options = buildCommandPaletteSessions( |
| 69 | + [workspace("lounge")], |
| 70 | + { |
| 71 | + lounge: [ |
| 72 | + routeSession("parent", NOW - MINUTE), |
| 73 | + routeSession("child", NOW, { parentID: "parent" }), |
| 74 | + routeSession("blank-parent", NOW - 2 * MINUTE, { parentID: " " }), |
| 75 | + ], |
| 76 | + }, |
| 77 | + "lounge", |
| 78 | + ); |
| 79 | + expect(ids(options)).toEqual(["parent", "blank-parent"]); |
| 80 | + }); |
| 81 | + |
| 82 | + test("normalizes engines that report seconds", () => { |
| 83 | + const options = buildCommandPaletteSessions( |
| 84 | + [workspace("seconds"), workspace("millis")], |
| 85 | + { |
| 86 | + seconds: [routeSession("in-seconds", Math.floor(NOW / 1000))], |
| 87 | + millis: [routeSession("in-millis", NOW - MINUTE)], |
| 88 | + }, |
| 89 | + "", |
| 90 | + ); |
| 91 | + expect(ids(options)).toEqual(["in-seconds", "in-millis"]); |
| 92 | + expect(options[0]?.updatedAt).toBe(Math.floor(NOW / 1000) * 1000); |
| 93 | + }); |
| 94 | +}); |
0 commit comments