1import { describe, expect, it } from "vite-plus/test";
2
3import { serializeComposerFileLink } from "./composerTrigger.ts";
4import { isTriagePrompt, replaceTriagePlaybookLink } from "./triagePrompt.ts";
5
6const appPrompt = (playbookPath: string) =>
7 `Read ${serializeComposerFileLink(playbookPath)} and follow it. `;
8
9const MACHINE_A = "/Users/a/.t3/triage/1/triage-playbook.md";
10const MACHINE_B = "/home/b/.t3/triage/2/triage-playbook.md";
11
12describe("replaceTriagePlaybookLink", () => {
13 it("replaces only the playbook link and keeps the text the user typed", () => {
14 const other = serializeComposerFileLink("/Users/a/notes (old)/log.md");
15 const prompt = `Before. ${appPrompt(MACHINE_A)}The app crashes, see ${other} (twice).`;
16
17 expect(replaceTriagePlaybookLink(prompt, appPrompt(MACHINE_B))).toBe(
18 `Before. ${appPrompt(MACHINE_B)}The app crashes, see ${other} (twice).`,
19 );
20 });
21
22 it.each([
23 ["spaces", "/Users/a b/My Data/triage-playbook.md"],
24 ["parentheses", "/Users/a/t3 (dev)/triage-playbook.md"],
25 ["hash and question mark", "/Users/a/#1/what?/triage-playbook.md"],
26 ["a Windows path", "C:\\Users\\a b\\t3 (dev)\\triage-playbook.md"],
27 ])("replaces a link whose path has %s", (_name, path) => {
28 expect(replaceTriagePlaybookLink(`${appPrompt(path)}It is slow.`, appPrompt(MACHINE_B))).toBe(
29 `${appPrompt(MACHINE_B)}It is slow.`,
30 );
31 expect(replaceTriagePlaybookLink(`${appPrompt(MACHINE_A)}It is slow.`, appPrompt(path))).toBe(
32 `${appPrompt(path)}It is slow.`,
33 );
34 });
35
36 it("replaces a link at the end of the prompt", () => {
37 const link = serializeComposerFileLink(MACHINE_A);
38 expect(replaceTriagePlaybookLink(`Follow ${link}`, appPrompt(MACHINE_B))).toBe(
39 `Follow ${serializeComposerFileLink(MACHINE_B)}`,
40 );
41 });
42
43 it("returns the prompt unchanged when it has no playbook link", () => {
44 const prompt = `The app crashes, see ${serializeComposerFileLink("/Users/a/log.md")} please.`;
45 expect(isTriagePrompt(prompt)).toBe(false);
46 expect(replaceTriagePlaybookLink(prompt, appPrompt(MACHINE_B))).toBe(prompt);
47 });
48
49 it("returns the prompt unchanged when the prepared prompt has no playbook link", () => {
50 expect(replaceTriagePlaybookLink(appPrompt(MACHINE_A), "Read the playbook.")).toBe(
51 appPrompt(MACHINE_A),
52 );
53 });
54});