Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
3
4import {
5 formatHostedThreadLink,
6 formatThreadLink,
7 guessThreadLinkRef,
8 hostedThreadLink,
9 parseThreadLinkHref,
10 relabelThreadLinks,
11 threadLinkCandidates,
12 threadLinkRefs,
13} from "./threadLinks.ts";
14
15const ref = (environmentId: string, threadId = "chat-1") => ({
16 environmentId: EnvironmentId.make(environmentId),
17 threadId: ThreadId.make(threadId),
18});
19const v1 = (threadId: string) => ({ environmentId: null, threadId: ThreadId.make(threadId) });
20const local = EnvironmentId.make("local");
21const titlesIn =
22 (titles: Record<string, Record<string, string>>) =>
23 ({ environmentId, threadId }: ScopedThreadRef) =>
24 titles[environmentId]?.[threadId];
25
26describe("thread link destinations", () => {
27 it("names a v1 link's thread in the message's environment, as written and then decoded", () => {
28 expect(threadLinkCandidates(v1("provider%3A1"), local)).toEqual([
29 ref("local", "provider%3A1"),
30 ref("local", "provider:1"),
31 ]);
32 });
33
34 it("opens the unique matching chat in another environment for a v1 link", () => {
35 const destination = ref("remote");
36 expect(guessThreadLinkRef(v1("chat-1"), [ref("local", "other-chat"), destination])).toEqual(
37 destination,
38 );
39 });
40
41 it("opens a v2 link only in its own environment", () => {
42 const there = ref("there", "a%3A1");
43 expect(threadLinkCandidates(there, local)).toEqual([there, ref("there", "a:1")]);
44 expect(guessThreadLinkRef(there, [ref("elsewhere", "a%3A1"), ref("elsewhere", "a:1")])).toBe(
45 null,
46 );
47 });
48
49 it("leaves ambiguous or missing destinations unresolved", () => {
50 const link = v1("provider%3A1");
51 const literal = ref("remote", "provider%3A1");
52 const decoded = ref("remote", "provider:1");
53 expect(
54 guessThreadLinkRef(link, [literal, ref("other-remote", literal.threadId), decoded]),
55 ).toBeNull();
56 expect(guessThreadLinkRef(link, [decoded, ref("other-remote", decoded.threadId)])).toBeNull();
57 expect(guessThreadLinkRef(link, [ref("remote", "other-chat")])).toBeNull();
58 });
59});
60
61describe("thread links", () => {
62 it("takes the ids verbatim, percent escapes included", () => {
63 expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toEqual(v1("mcp:1234"));
64 expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toEqual(
65 v1("thread:delegated-task:mcp%3A1"),
66 );
67 expect(parseThreadLinkHref("t3-thread://v2/env-1/thread:a/b%3A1")).toEqual(
68 ref("env-1", "thread:a/b%3A1"),
69 );
70 });
71
72 it("rejects other links and an empty id", () => {
73 expect(parseThreadLinkHref("https://t3.codes")).toBeNull();
74 expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull();
75 expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
76 expect(parseThreadLinkHref("t3-thread://v2/env-1")).toBeNull();
77 expect(parseThreadLinkHref("t3-thread://v2/env-1/")).toBeNull();
78 expect(parseThreadLinkHref("t3-thread://v2//chat-1")).toBeNull();
79 });
80
81 it("resolves a percent-encoded id when the id as written names no thread", () => {
82 const title = titlesIn({ local: { "thread:project:1": "Decoded", "provider%3A1": "Literal" } });
83 expect(
84 relabelThreadLinks(
85 "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)",
86 local,
87 title,
88 ),
89 ).toBe("[Decoded](t3-thread://v1/thread:project:1) [Literal](t3-thread://v1/provider%3A1)");
90 const untitled = titlesIn({ local: { "a%3A1": "", "a:1": "Other" } });
91 expect(relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", local, untitled)).toBe(
92 "[Kept](t3-thread://v1/a%3A1)",
93 );
94 });
95
96 it("titles a v2 link from its own environment and writes it with that title", () => {
97 const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } });
98 expect(
99 relabelThreadLinks(
100 "<t3-thread://v2/there/t1> and [old](t3-thread://v2/local/t1)",
101 local,
102 title,
103 ),
104 ).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
105 });
106
107 it("turns a v2 link it cannot resolve into plain text, never the raw link", () => {
108 expect(
109 relabelThreadLinks(
110 "<t3-thread://v2/there/t1>, <t3-thread://v2/local/t1>, [Old title](t3-thread://v2/there/t1)",
111 local,
112 () => undefined,
113 ),
114 ).toBe("Thread on another machine, Thread no longer available, Old title");
115 expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe(
116 "[Old](t3-thread://v1/t1)",
117 );
118 });
119
120 it("leaves links inside code spans and fences as written", () => {
121 const markdown = [
122 "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
123 "```md",
124 "<t3-thread://v2/local/t1>",
125 "```",
126 "After <t3-thread://v2/local/t1>",
127 ].join("\n");
128 expect(relabelThreadLinks(markdown, local, () => "New")).toBe(
129 [
130 "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
131 "```md",
132 "<t3-thread://v2/local/t1>",
133 "```",
134 "After [New](t3-thread://v2/local/t1)",
135 ].join("\n"),
136 );
137 });
138
139 it("lists the threads a message's links can name, outside code", () => {
140 expect(
141 threadLinkRefs(
142 "<t3-thread://v2/there/t1> [a](t3-thread://v1/t%3A2) `<t3-thread://v2/code/t3>`",
143 local,
144 ),
145 ).toEqual([ref("there", "t1"), ref("local", "t%3A2"), ref("local", "t:2")]);
146 });
147
148 it("lists a message's thread links on runtimes whose matchAll omits named groups, such as Hermes", () => {
149 const matchAll = String.prototype.matchAll;
150 using _hermes = vi.spyOn(String.prototype, "matchAll").mockImplementation(function (
151 this: string,
152 regexp: RegExp,
153 ) {
154 return matchAll.call(this, regexp).map((match) => {
155 delete match.groups;
156 return match;
157 });
158 });
159 expect(threadLinkRefs("<t3-thread://v2/local/t1> [a](t3-thread://v2/there/t2)", local)).toEqual(
160 [ref("local", "t1"), ref("there", "t2")],
161 );
162 });
163
164 it("writes a titleless autolink that opens that exact thread", () => {
165 const link = formatThreadLink(ref("env-1", "thread:delegated-task:mcp%3A1"));
166 expect(link).toBe("<t3-thread://v2/env-1/thread:delegated-task:mcp%3A1>");
167 expect(parseThreadLinkHref(link.slice(1, -1))).toEqual(
168 ref("env-1", "thread:delegated-task:mcp%3A1"),
169 );
170 });
171
172 it("formats a label that would otherwise break the Markdown link", () => {
173 expect(formatThreadLink(ref("e", "t1"), "Fix [ci] \\ build")).toBe(
174 "[Fix ci build](t3-thread://v2/e/t1)",
175 );
176 expect(formatThreadLink(ref("e", "t1"), " ] ")).toBe("<t3-thread://v2/e/t1>");
177 });
178});
179
180describe("hosted thread links", () => {
181 it("escapes every id character a sentence or path could claim, and reads it back", () => {
182 const thread = ref("env.1", "thread:delegated/mcp%3A1.");
183 const link = hostedThreadLink(thread);
184 expect(link).toBe("https://app.t3.codes/threads/env%2E1/thread%3Adelegated%2Fmcp%253A1%2E");
185 expect(parseThreadLinkHref(link)).toEqual(thread);
186 expect(hostedThreadLink(thread, "https://preview.t3.codes/")).toMatch(
187 /^https:\/\/preview\.t3\.codes\/threads\//,
188 );
189 });
190
191 it("reads only a bare thread path on the hosted app", () => {
192 expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1")).toEqual(
193 ref("env-1", "thread-1"),
194 );
195 expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1?prompt=go")).toBeNull();
196 expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1/extra")).toBeNull();
197 expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1")).toBeNull();
198 expect(parseThreadLinkHref("https://app.t3.codes/env-1/thread-1")).toBeNull();
199 expect(parseThreadLinkHref("https://evil.test/threads/env-1/thread-1")).toBeNull();
200 });
201
202 it("writes a copied link a reader can follow, labeled when it has a title", () => {
203 expect(formatHostedThreadLink(ref("e", "t1"), "Fix [ci]")).toBe(
204 "[Fix ci](https://app.t3.codes/threads/e/t1)",
205 );
206 expect(formatHostedThreadLink(ref("e", "t1"), " ")).toBe("https://app.t3.codes/threads/e/t1");
207 });
208
209 it("titles hosted links in every Markdown form and writes them as v2 links", () => {
210 const title = titlesIn({ there: { t1: "There" } });
211 expect(
212 relabelThreadLinks(
213 [
214 "See https://app.t3.codes/threads/there/t1.",
215 "[old](https://app.t3.codes/threads/there/t1)",
216 "<https://app.t3.codes/threads/there/t1>",
217 "`https://app.t3.codes/threads/there/t1`",
218 "https://app.t3.codes/threads/there/t1?prompt=go",
219 ].join("\n"),
220 local,
221 title,
222 ),
223 ).toBe(
224 [
225 "See [There](t3-thread://v2/there/t1).",
226 "[There](t3-thread://v2/there/t1)",
227 "[There](t3-thread://v2/there/t1)",
228 "`https://app.t3.codes/threads/there/t1`",
229 "https://app.t3.codes/threads/there/t1?prompt=go",
230 ].join("\n"),
231 );
232 expect(
233 relabelThreadLinks("https://app.t3.codes/threads/there/t1", local, () => undefined),
234 ).toBe("Thread on another machine");
235 expect(threadLinkRefs("Open https://app.t3.codes/threads/there/t%3A1", local)).toEqual([
236 ref("there", "t:1"),
237 ]);
238 });
239});