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

threadLinks.test.ts

176 lines6.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
3​
4import {
5 formatThreadLink,
6 guessThreadLinkRef,
7 parseThreadLinkHref,
8 relabelThreadLinks,
9 threadLinkCandidates,
10 threadLinkRefs,
11} from "./threadLinks.ts";
12​
13const ref = (environmentId: string, threadId = "chat-1") => ({
14 environmentId: EnvironmentId.make(environmentId),
15 threadId: ThreadId.make(threadId),
16});
17const v1 = (threadId: string) => ({ environmentId: null, threadId: ThreadId.make(threadId) });
18const local = EnvironmentId.make("local");
19const titlesIn =
20 (titles: Record<string, Record<string, string>>) =>
21 ({ environmentId, threadId }: ScopedThreadRef) =>
22 titles[environmentId]?.[threadId];
23​
24describe("thread link destinations", () => {
25 it("names a v1 link's thread in the message's environment, as written and then decoded", () => {
26 expect(threadLinkCandidates(v1("provider%3A1"), local)).toEqual([
27 ref("local", "provider%3A1"),
28 ref("local", "provider:1"),
29 ]);
30 });
31​
32 it("opens the unique matching chat in another environment for a v1 link", () => {
33 const destination = ref("remote");
34 expect(guessThreadLinkRef(v1("chat-1"), [ref("local", "other-chat"), destination])).toEqual(
35 destination,
36 );
37 });
38​
39 it("opens a v2 link only in its own environment", () => {
40 const there = ref("there", "a%3A1");
41 expect(threadLinkCandidates(there, local)).toEqual([there, ref("there", "a:1")]);
42 expect(guessThreadLinkRef(there, [ref("elsewhere", "a%3A1"), ref("elsewhere", "a:1")])).toBe(
43 null,
44 );
45 });
46​
47 it("leaves ambiguous or missing destinations unresolved", () => {
48 const link = v1("provider%3A1");
49 const literal = ref("remote", "provider%3A1");
50 const decoded = ref("remote", "provider:1");
51 expect(
52 guessThreadLinkRef(link, [literal, ref("other-remote", literal.threadId), decoded]),
53 ).toBeNull();
54 expect(guessThreadLinkRef(link, [decoded, ref("other-remote", decoded.threadId)])).toBeNull();
55 expect(guessThreadLinkRef(link, [ref("remote", "other-chat")])).toBeNull();
56 });
57});
58​
59describe("thread links", () => {
60 it("takes the ids verbatim, percent escapes included", () => {
61 expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toEqual(v1("mcp:1234"));
62 expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toEqual(
63 v1("thread:delegated-task:mcp%3A1"),
64 );
65 expect(parseThreadLinkHref("t3-thread://v2/env-1/thread:a/b%3A1")).toEqual(
66 ref("env-1", "thread:a/b%3A1"),
67 );
68 });
69​
70 it("rejects other links and an empty id", () => {
71 expect(parseThreadLinkHref("https://t3.codes")).toBeNull();
72 expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull();
73 expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
74 expect(parseThreadLinkHref("t3-thread://v2/env-1")).toBeNull();
75 expect(parseThreadLinkHref("t3-thread://v2/env-1/")).toBeNull();
76 expect(parseThreadLinkHref("t3-thread://v2//chat-1")).toBeNull();
77 });
78​
79 it("resolves a percent-encoded id when the id as written names no thread", () => {
80 const title = titlesIn({ local: { "thread:project:1": "Decoded", "provider%3A1": "Literal" } });
81 expect(
82 relabelThreadLinks(
83 "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)",
84 local,
85 title,
86 ),
87 ).toBe("[Decoded](t3-thread://v1/thread:project:1) [Literal](t3-thread://v1/provider%3A1)");
88 const untitled = titlesIn({ local: { "a%3A1": "", "a:1": "Other" } });
89 expect(relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", local, untitled)).toBe(
90 "[Kept](t3-thread://v1/a%3A1)",
91 );
92 });
93​
94 it("titles a v2 link from its own environment and writes it with that title", () => {
95 const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } });
96 expect(
97 relabelThreadLinks(
98 "<t3-thread://v2/there/t1> and [old](t3-thread://v2/local/t1)",
99 local,
100 title,
101 ),
102 ).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
103 });
104​
105 it("turns a v2 link it cannot resolve into plain text, never the raw link", () => {
106 expect(
107 relabelThreadLinks(
108 "<t3-thread://v2/there/t1>, <t3-thread://v2/local/t1>, [Old title](t3-thread://v2/there/t1)",
109 local,
110 () => undefined,
111 ),
112 ).toBe("Thread on another machine, Thread no longer available, Old title");
113 expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe(
114 "[Old](t3-thread://v1/t1)",
115 );
116 });
117​
118 it("leaves links inside code spans and fences as written", () => {
119 const markdown = [
120 "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
121 "```md",
122 "<t3-thread://v2/local/t1>",
123 "```",
124 "After <t3-thread://v2/local/t1>",
125 ].join("\n");
126 expect(relabelThreadLinks(markdown, local, () => "New")).toBe(
127 [
128 "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
129 "```md",
130 "<t3-thread://v2/local/t1>",
131 "```",
132 "After [New](t3-thread://v2/local/t1)",
133 ].join("\n"),
134 );
135 });
136​
137 it("lists the threads a message's links can name, outside code", () => {
138 expect(
139 threadLinkRefs(
140 "<t3-thread://v2/there/t1> [a](t3-thread://v1/t%3A2) `<t3-thread://v2/code/t3>`",
141 local,
142 ),
143 ).toEqual([ref("there", "t1"), ref("local", "t%3A2"), ref("local", "t:2")]);
144 });
145​
146 it("lists a message's thread links on runtimes whose matchAll omits named groups, such as Hermes", () => {
147 const matchAll = String.prototype.matchAll;
148 using _hermes = vi.spyOn(String.prototype, "matchAll").mockImplementation(function (
149 this: string,
150 regexp: RegExp,
151 ) {
152 return matchAll.call(this, regexp).map((match) => {
153 delete match.groups;
154 return match;
155 });
156 });
157 expect(threadLinkRefs("<t3-thread://v2/local/t1> [a](t3-thread://v2/there/t2)", local)).toEqual(
158 [ref("local", "t1"), ref("there", "t2")],
159 );
160 });
161​
162 it("writes a titleless autolink that opens that exact thread", () => {
163 const link = formatThreadLink(ref("env-1", "thread:delegated-task:mcp%3A1"));
164 expect(link).toBe("<t3-thread://v2/env-1/thread:delegated-task:mcp%3A1>");
165 expect(parseThreadLinkHref(link.slice(1, -1))).toEqual(
166 ref("env-1", "thread:delegated-task:mcp%3A1"),
167 );
168 });
169​
170 it("formats a label that would otherwise break the Markdown link", () => {
171 expect(formatThreadLink(ref("e", "t1"), "Fix [ci] \\ build")).toBe(
172 "[Fix ci build](t3-thread://v2/e/t1)",
173 );
174 expect(formatThreadLink(ref("e", "t1"), " ] ")).toBe("<t3-thread://v2/e/t1>");
175 });
176});