The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; |
| 3 | import { mFallback } from "gt-i18n"; |
| 4 | import type { MFunctionType } from "gt-i18n/types"; |
| 5 | |
| 6 | import { |
| 7 | formatHostedThreadLink, |
| 8 | formatThreadLink, |
| 9 | guessThreadLinkRef, |
| 10 | hostedThreadLink, |
| 11 | parseThreadLinkHref, |
| 12 | threadLinkCandidates, |
| 13 | threadLinkRefs, |
| 14 | } from "./threadLinks.ts"; |
| 15 | import { relabelThreadLinks } from "./threadLinksCopy.ts"; |
| 16 | |
| 17 | const ref = (environmentId: string, threadId = "chat-1") => ({ |
| 18 | environmentId: EnvironmentId.make(environmentId), |
| 19 | threadId: ThreadId.make(threadId), |
| 20 | }); |
| 21 | const v1 = (threadId: string) => ({ environmentId: null, threadId: ThreadId.make(threadId) }); |
| 22 | const local = EnvironmentId.make("local"); |
| 23 | const titlesIn = |
| 24 | (titles: Record<string, Record<string, string>>) => |
| 25 | ({ environmentId, threadId }: ScopedThreadRef) => |
| 26 | titles[environmentId]?.[threadId]; |
| 27 | const japanese: MFunctionType = (encodedMsg, options) => { |
| 28 | const english = mFallback(encodedMsg, options); |
| 29 | return ( |
| 30 | english === "Thread no longer available" ? "スレッドは利用できなくなりました" : english |
| 31 | ) as typeof english; |
| 32 | }; |
| 33 | |
| 34 | describe("thread link destinations", () => { |
| 35 | it("names a v1 link's thread in the message's environment, as written and then decoded", () => { |
| 36 | expect(threadLinkCandidates(v1("provider%3A1"), local)).toEqual([ |
| 37 | ref("local", "provider%3A1"), |
| 38 | ref("local", "provider:1"), |
| 39 | ]); |
| 40 | }); |
| 41 | |
| 42 | it("opens the unique matching chat in another environment for a v1 link", () => { |
| 43 | const destination = ref("remote"); |
| 44 | expect(guessThreadLinkRef(v1("chat-1"), [ref("local", "other-chat"), destination])).toEqual( |
| 45 | destination, |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | it("opens a v2 link only in its own environment", () => { |
| 50 | const there = ref("there", "a%3A1"); |
| 51 | expect(threadLinkCandidates(there, local)).toEqual([there, ref("there", "a:1")]); |
| 52 | expect(guessThreadLinkRef(there, [ref("elsewhere", "a%3A1"), ref("elsewhere", "a:1")])).toBe( |
| 53 | null, |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | it("leaves ambiguous or missing destinations unresolved", () => { |
| 58 | const link = v1("provider%3A1"); |
| 59 | const literal = ref("remote", "provider%3A1"); |
| 60 | const decoded = ref("remote", "provider:1"); |
| 61 | expect( |
| 62 | guessThreadLinkRef(link, [literal, ref("other-remote", literal.threadId), decoded]), |
| 63 | ).toBeNull(); |
| 64 | expect(guessThreadLinkRef(link, [decoded, ref("other-remote", decoded.threadId)])).toBeNull(); |
| 65 | expect(guessThreadLinkRef(link, [ref("remote", "other-chat")])).toBeNull(); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe("thread links", () => { |
| 70 | it("takes the ids verbatim, percent escapes included", () => { |
| 71 | expect(parseThreadLinkHref("t3-thread://v1/mcp:1234")).toEqual(v1("mcp:1234")); |
| 72 | expect(parseThreadLinkHref("t3-thread://v1/thread:delegated-task:mcp%3A1")).toEqual( |
| 73 | v1("thread:delegated-task:mcp%3A1"), |
| 74 | ); |
| 75 | expect(parseThreadLinkHref("t3-thread://v2/env-1/thread:a/b%3A1")).toEqual( |
| 76 | ref("env-1", "thread:a/b%3A1"), |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | it("rejects other links and an empty id", () => { |
| 81 | expect(parseThreadLinkHref("https://t3.codes")).toBeNull(); |
| 82 | expect(parseThreadLinkHref("t3-thread://v1/")).toBeNull(); |
| 83 | expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull(); |
| 84 | expect(parseThreadLinkHref("t3-thread://v2/env-1")).toBeNull(); |
| 85 | expect(parseThreadLinkHref("t3-thread://v2/env-1/")).toBeNull(); |
| 86 | expect(parseThreadLinkHref("t3-thread://v2//chat-1")).toBeNull(); |
| 87 | }); |
| 88 | |
| 89 | it("resolves a percent-encoded id when the id as written names no thread", () => { |
| 90 | const title = titlesIn({ local: { "thread:project:1": "Decoded", "provider%3A1": "Literal" } }); |
| 91 | expect( |
| 92 | relabelThreadLinks( |
| 93 | "[a](t3-thread://v1/thread%3Aproject%3A1) [b](t3-thread://v1/provider%3A1)", |
| 94 | local, |
| 95 | title, |
| 96 | ), |
| 97 | ).toBe("[Decoded](t3-thread://v1/thread:project:1) [Literal](t3-thread://v1/provider%3A1)"); |
| 98 | const untitled = titlesIn({ local: { "a%3A1": "", "a:1": "Other" } }); |
| 99 | expect(relabelThreadLinks("[Kept](t3-thread://v1/a%3A1)", local, untitled)).toBe( |
| 100 | "[Kept](t3-thread://v1/a%3A1)", |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | it("titles a v2 link from its own environment and writes it with that title", () => { |
| 105 | const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } }); |
| 106 | expect( |
| 107 | relabelThreadLinks( |
| 108 | "<t3-thread://v2/there/t1> and [old](t3-thread://v2/local/t1)", |
| 109 | local, |
| 110 | title, |
| 111 | ), |
| 112 | ).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)"); |
| 113 | }); |
| 114 | |
| 115 | it("reads an angle-bracketed destination as the labeled link, not an autolink", () => { |
| 116 | const title = titlesIn({ local: { t1: "Here" }, there: { t1: "There" } }); |
| 117 | expect( |
| 118 | relabelThreadLinks( |
| 119 | "[old](<t3-thread://v2/there/t1>) and [older](<t3-thread://v2/local/t1>)", |
| 120 | local, |
| 121 | title, |
| 122 | ), |
| 123 | ).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)"); |
| 124 | expect( |
| 125 | relabelThreadLinks("[Kept label](<t3-thread://v2/local/t1>)", local, () => undefined), |
| 126 | ).toBe("Kept label"); |
| 127 | expect(threadLinkRefs("[a](<t3-thread://v2/there/t2>)", local)).toEqual([ref("there", "t2")]); |
| 128 | for (const threadId of ["mcp:launch:qa(1)", "mcp:launch:qa)"]) { |
| 129 | const thread = ref("there", threadId); |
| 130 | const link = formatThreadLink(thread, "Original"); |
| 131 | expect(link).toBe(`[Original](<t3-thread://v2/there/${threadId}>)`); |
| 132 | expect(threadLinkRefs(link, local)).toEqual([thread]); |
| 133 | expect(relabelThreadLinks(link, local, () => "Renamed")).toBe( |
| 134 | `[Renamed](<t3-thread://v2/there/${threadId}>)`, |
| 135 | ); |
| 136 | expect(relabelThreadLinks(link, local, () => undefined)).toBe("Original"); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | it("turns a v2 link it cannot resolve into plain text, never the raw link", () => { |
| 141 | expect( |
| 142 | relabelThreadLinks( |
| 143 | "<t3-thread://v2/there/t1>, <t3-thread://v2/local/t1>, [Old title](t3-thread://v2/there/t1)", |
| 144 | local, |
| 145 | () => undefined, |
| 146 | ), |
| 147 | ).toBe("Thread on another machine, Thread no longer available, Old title"); |
| 148 | expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe( |
| 149 | "[Old](t3-thread://v1/t1)", |
| 150 | ); |
| 151 | expect(relabelThreadLinks("See <t3-thread://v1/gone>", local, () => undefined, japanese)).toBe( |
| 152 | "See スレッドは利用できなくなりました", |
| 153 | ); |
| 154 | }); |
| 155 | |
| 156 | it("leaves links inside code spans and fences as written", () => { |
| 157 | const markdown = [ |
| 158 | "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.", |
| 159 | "```md", |
| 160 | "<t3-thread://v2/local/t1>", |
| 161 | "```", |
| 162 | "After <t3-thread://v2/local/t1>", |
| 163 | ].join("\n"); |
| 164 | expect(relabelThreadLinks(markdown, local, () => "New")).toBe( |
| 165 | [ |
| 166 | "Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.", |
| 167 | "```md", |
| 168 | "<t3-thread://v2/local/t1>", |
| 169 | "```", |
| 170 | "After [New](t3-thread://v2/local/t1)", |
| 171 | ].join("\n"), |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | it("lists the threads a message's links can name, outside code", () => { |
| 176 | expect( |
| 177 | threadLinkRefs( |
| 178 | "<t3-thread://v2/there/t1> [a](t3-thread://v1/t%3A2) `<t3-thread://v2/code/t3>`", |
| 179 | local, |
| 180 | ), |
| 181 | ).toEqual([ref("there", "t1"), ref("local", "t%3A2"), ref("local", "t:2")]); |
| 182 | }); |
| 183 | |
| 184 | it("lists a message's thread links on runtimes whose matchAll omits named groups, such as Hermes", () => { |
| 185 | const matchAll = String.prototype.matchAll; |
| 186 | using _hermes = vi.spyOn(String.prototype, "matchAll").mockImplementation(function ( |
| 187 | this: string, |
| 188 | regexp: RegExp, |
| 189 | ) { |
| 190 | return matchAll.call(this, regexp).map((match) => { |
| 191 | delete match.groups; |
| 192 | return match; |
| 193 | }); |
| 194 | }); |
| 195 | expect(threadLinkRefs("<t3-thread://v2/local/t1> [a](t3-thread://v2/there/t2)", local)).toEqual( |
| 196 | [ref("local", "t1"), ref("there", "t2")], |
| 197 | ); |
| 198 | }); |
| 199 | |
| 200 | it("writes a titleless autolink that opens that exact thread", () => { |
| 201 | const link = formatThreadLink(ref("env-1", "thread:delegated-task:mcp%3A1")); |
| 202 | expect(link).toBe("<t3-thread://v2/env-1/thread:delegated-task:mcp%3A1>"); |
| 203 | expect(parseThreadLinkHref(link.slice(1, -1))).toEqual( |
| 204 | ref("env-1", "thread:delegated-task:mcp%3A1"), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | it("formats a label that would otherwise break the Markdown link", () => { |
| 209 | expect(formatThreadLink(ref("e", "t1"), "Fix [ci] \\ build")).toBe( |
| 210 | "[Fix ci build](t3-thread://v2/e/t1)", |
| 211 | ); |
| 212 | expect(formatThreadLink(ref("e", "t1"), " ] ")).toBe("<t3-thread://v2/e/t1>"); |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | describe("hosted thread links", () => { |
| 217 | it("escapes every id character a sentence or path could claim, and reads it back", () => { |
| 218 | const thread = ref("env.1", "thread:delegated/mcp%3A1."); |
| 219 | const link = hostedThreadLink(thread); |
| 220 | expect(link).toBe("https://app.t3.codes/threads/env%2E1/thread%3Adelegated%2Fmcp%253A1%2E"); |
| 221 | expect(parseThreadLinkHref(link)).toEqual(thread); |
| 222 | expect(hostedThreadLink(thread, "https://preview.t3.codes/")).toMatch( |
| 223 | /^https:\/\/preview\.t3\.codes\/threads\//, |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | it("reads only a bare thread path on the hosted app", () => { |
| 228 | expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1")).toEqual( |
| 229 | ref("env-1", "thread-1"), |
| 230 | ); |
| 231 | expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1?prompt=go")).toBeNull(); |
| 232 | expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1/thread-1/extra")).toBeNull(); |
| 233 | expect(parseThreadLinkHref("https://app.t3.codes/threads/env-1")).toBeNull(); |
| 234 | expect(parseThreadLinkHref("https://app.t3.codes/env-1/thread-1")).toBeNull(); |
| 235 | expect(parseThreadLinkHref("https://evil.test/threads/env-1/thread-1")).toBeNull(); |
| 236 | }); |
| 237 | |
| 238 | it("writes a copied link a reader can follow, labeled when it has a title", () => { |
| 239 | expect(formatHostedThreadLink(ref("e", "t1"), "Fix [ci]")).toBe( |
| 240 | "[Fix ci](https://app.t3.codes/threads/e/t1)", |
| 241 | ); |
| 242 | expect(formatHostedThreadLink(ref("e", "t1"), " ")).toBe("https://app.t3.codes/threads/e/t1"); |
| 243 | }); |
| 244 | |
| 245 | it("titles hosted links in every Markdown form and writes them as v2 links", () => { |
| 246 | const title = titlesIn({ there: { t1: "There" } }); |
| 247 | expect( |
| 248 | relabelThreadLinks( |
| 249 | [ |
| 250 | "See https://app.t3.codes/threads/there/t1.", |
| 251 | "[old](https://app.t3.codes/threads/there/t1)", |
| 252 | "<https://app.t3.codes/threads/there/t1>", |
| 253 | "`https://app.t3.codes/threads/there/t1`", |
| 254 | "https://app.t3.codes/threads/there/t1?prompt=go", |
| 255 | ].join("\n"), |
| 256 | local, |
| 257 | title, |
| 258 | ), |
| 259 | ).toBe( |
| 260 | [ |
| 261 | "See [There](t3-thread://v2/there/t1).", |
| 262 | "[There](t3-thread://v2/there/t1)", |
| 263 | "[There](t3-thread://v2/there/t1)", |
| 264 | "`https://app.t3.codes/threads/there/t1`", |
| 265 | "https://app.t3.codes/threads/there/t1?prompt=go", |
| 266 | ].join("\n"), |
| 267 | ); |
| 268 | expect( |
| 269 | relabelThreadLinks("https://app.t3.codes/threads/there/t1", local, () => undefined), |
| 270 | ).toBe("Thread on another machine"); |
| 271 | expect(threadLinkRefs("Open https://app.t3.codes/threads/there/t%3A1", local)).toEqual([ |
| 272 | ref("there", "t:1"), |
| 273 | ]); |
| 274 | }); |
| 275 | |
| 276 | it("leaves a hosted link that is part of another link as written", () => { |
| 277 | const hosted = "https://app.t3.codes/threads/there/t1"; |
| 278 | const relabeled = "[There](t3-thread://v2/there/t1)"; |
| 279 | const title = titlesIn({ there: { t1: "There" } }); |
| 280 | for (const [markdown, expected] of [ |
| 281 | [`https://example.com/?next=${hosted}`], |
| 282 | [`[Example](https://example.com/?next=${hosted})`], |
| 283 | [`[ ${hosted} ](https://example.com)`], |
| 284 | [`[${hosted}](<https://example.com/a b> "Title")`], |
| 285 | [`[a](https://example.com/x_(y)) ${hosted}`, `[a](https://example.com/x_(y)) ${relabeled}`], |
| 286 | [`(${hosted}) **${hosted}**`, `(${relabeled}) **${relabeled}**`], |
| 287 | [`[[[ [old](${hosted})`, `[[[ ${relabeled}`], |
| 288 | [`\\\\[old](${hosted})`, `\\\\${relabeled}`], |
| 289 | [`\\\\\\\\[old](${hosted})`, `\\\\\\\\${relabeled}`], |
| 290 | [`\\[old](${hosted})`, `\\[old](${relabeled})`], |
| 291 | [`\\[ ${hosted} ](https://example.com)`, `\\[ ${relabeled} ](https://example.com)`], |
| 292 | ["\\[old](t3-thread://v2/there/t1) \\<t3-thread://v2/there/t1>"], |
| 293 | ] as const) { |
| 294 | expect(relabelThreadLinks(markdown, local, title)).toBe(expected ?? markdown); |
| 295 | expect(threadLinkRefs(markdown, local)).toEqual( |
| 296 | expected === undefined ? [] : [ref("there", "t1")], |
| 297 | ); |
| 298 | } |
| 299 | }); |
| 300 | }); |