Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadLinks.test.ts

123 lines4.5 KB

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("preserves a remote literal id and decodes only when that id is absent", () => {
33 const source = ref("local", "provider%3A1");
34 const literal = ref("remote", "provider%3A1");
35 const decoded = ref("other-remote", "provider:1");
36 expect(resolveThreadLinkRef(source, [decoded, literal])).toEqual(literal);
37 expect(resolveThreadLinkRef(source, [decoded])).toEqual(decoded);
38 });
39​
40 it("leaves ambiguous or missing destinations unresolved", () => {
41 const source = ref("local", "provider%3A1");
42 const literal = ref("remote", "provider%3A1");
43 const decoded = ref("remote", "provider:1");
44 expect(
45 resolveThreadLinkRef(source, [literal, ref("other-remote", literal.threadId), decoded]),
46 ).toBeNull();
47 expect(
48 resolveThreadLinkRef(source, [decoded, ref("other-remote", decoded.threadId)]),
49 ).toBeNull();
50 expect(resolveThreadLinkRef(source, [ref("remote", "other-chat")])).toBeNull();
51 });
52});
53​
54describe("thread links", () => {
55 it("takes the thread id verbatim, percent escapes included", () => {
56 expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toBe("mcp:1234");
57 expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toBe(
58 "thread:delegated-task:mcp%3A1",
59 );
60 });
61​
62 it("rejects other links and an empty id", () => {
63 expect(parseThreadLinkHref("https://t3.codes")).toBeNull();
64 expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull();
65 expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
66 });
67​
68 it("resolves a percent-encoded id when the id as written names no thread", () => {
69 const titles = new Map([
70 ["thread:project:1", "Decoded"],
71 ["provider%3A1", "Literal escape"],
72 ]);
73 expect(
74 relabelThreadLinks(
75 "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)",
76 (threadId) => titles.get(threadId),
77 ),
78 ).toBe(
79 "[Decoded](t3-thread://v1/thread:project:1) [Literal escape](t3-thread://v1/provider%3A1)",
80 );
81 const untitled = new Map([
82 ["a%3A1", ""],
83 ["a:1", "Other"],
84 ]);
85 expect(
86 relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", (threadId) => untitled.get(threadId)),
87 ).toBe("[Kept](t3-thread://v1/a%3A1)");
88 });
89​
90 it("leaves links inside code spans and fences as written", () => {
91 const markdown = [
92 "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
93 "```md",
94 "[old](t3-thread://v1/t1)",
95 "```",
96 "After [old](t3-thread://v1/t1)",
97 ].join("\n");
98 expect(relabelThreadLinks(markdown, () => "New")).toBe(
99 [
100 "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
101 "```md",
102 "[old](t3-thread://v1/t1)",
103 "```",
104 "After [New](t3-thread://v1/t1)",
105 ].join("\n"),
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​
114 it("relabels links with the current title and leaves unknown threads alone", () => {
115 const titles = new Map([["renamed", "Fix [the] build\nnow"]]);
116 expect(
117 relabelThreadLinks(
118 "See [Old name](t3-thread://v1/renamed) and [Gone](t3-thread://v1/deleted).",
119 (threadId) => titles.get(threadId),
120 ),
121 ).toBe("See [Fix the build now](t3-thread://v1/renamed) and [Gone](t3-thread://v1/deleted).");
122 });
123});