Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3
4import {
5 formatThreadLink,
6 parseThreadLinkHref,
7 relabelThreadLinks,
8 resolveThreadLinkRef,
9} from "./threadLinks.ts";
10
11const ref = (environmentId: string, threadId = "chat-1") => ({
12 environmentId: EnvironmentId.make(environmentId),
13 threadId: ThreadId.make(threadId),
14});
15
16describe("thread link destinations", () => {
17 it("prefers the local literal id, then the local decoded id, over remote matches", () => {
18 const source = ref("local", "provider%3A1");
19 const decoded = ref("local", "provider:1");
20 const remote = ref("remote", "provider%3A1");
21 expect(resolveThreadLinkRef(source, [remote, decoded, source])).toEqual(source);
22 expect(resolveThreadLinkRef(source, [remote, decoded])).toEqual(decoded);
23 });
24
25 it("opens the unique matching chat in another environment", () => {
26 const destination = ref("remote");
27 expect(resolveThreadLinkRef(ref("local"), [ref("local", "other-chat"), destination])).toEqual(
28 destination,
29 );
30 });
31
32 it("leaves ambiguous or missing destinations unresolved", () => {
33 const source = ref("local", "provider%3A1");
34 const literal = ref("remote", "provider%3A1");
35 const decoded = ref("remote", "provider:1");
36 expect(
37 resolveThreadLinkRef(source, [literal, ref("other-remote", literal.threadId), decoded]),
38 ).toBeNull();
39 expect(
40 resolveThreadLinkRef(source, [decoded, ref("other-remote", decoded.threadId)]),
41 ).toBeNull();
42 expect(resolveThreadLinkRef(source, [ref("remote", "other-chat")])).toBeNull();
43 });
44});
45
46describe("thread links", () => {
47 it("takes the thread id verbatim, percent escapes included", () => {
48 expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toBe("mcp:1234");
49 expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toBe(
50 "thread:delegated-task:mcp%3A1",
51 );
52 });
53
54 it("rejects other links and an empty id", () => {
55 expect(parseThreadLinkHref("https://t3.codes")).toBeNull();
56 expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull();
57 expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
58 });
59
60 it("resolves a percent-encoded id when the id as written names no thread", () => {
61 const titles = new Map([
62 ["thread:project:1", "Decoded"],
63 ["provider%3A1", "Literal escape"],
64 ]);
65 expect(
66 relabelThreadLinks(
67 "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)",
68 (threadId) => titles.get(threadId),
69 ),
70 ).toBe(
71 "[Decoded](t3-thread://v1/thread:project:1) [Literal escape](t3-thread://v1/provider%3A1)",
72 );
73 const untitled = new Map([
74 ["a%3A1", ""],
75 ["a:1", "Other"],
76 ]);
77 expect(
78 relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", (threadId) => untitled.get(threadId)),
79 ).toBe("[Kept](t3-thread://v1/a%3A1)");
80 });
81
82 it("leaves links inside code spans and fences as written", () => {
83 const markdown = [
84 "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
85 "```md",
86 "[old](t3-thread://v1/t1)",
87 "```",
88 "After [old](t3-thread://v1/t1)",
89 ].join("\n");
90 expect(relabelThreadLinks(markdown, () => "New")).toBe(
91 [
92 "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
93 "```md",
94 "[old](t3-thread://v1/t1)",
95 "```",
96 "After [New](t3-thread://v1/t1)",
97 ].join("\n"),
98 );
99 });
100
101 it("writes the thread id verbatim so the link opens that exact thread", () => {
102 const link = formatThreadLink("thread:delegated-task:mcp%3A1", "Fix");
103 expect(link).toBe("[Fix](t3-thread://v1/thread:delegated-task:mcp%3A1)");
104 expect(parseThreadLinkHref(link.slice(link.indexOf("](") + 2, -1))).toBe(
105 "thread:delegated-task:mcp%3A1",
106 );
107 });
108
109 it("formats a label that would otherwise break the Markdown link", () => {
110 expect(formatThreadLink("t1", "Fix [ci] \\ build")).toBe("[Fix ci build](t3-thread://v1/t1)");
111 expect(formatThreadLink("t1", " ] ")).toBe("[t1](t3-thread://v1/t1)");
112 });
113});