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

promptLinks.test.ts

118 lines4.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 buildNewThreadLink,
5 parseNewThreadLink,
6 parseThreadLinkPrompt,
7 promptLinkAppPath,
8 sanitizeLinkedPrompt,
9 withoutPromptLinkParams,
10} from "./promptLinks.ts";
11​
12describe("sanitizeLinkedPrompt", () => {
13 it("removes characters a reader cannot see and counts them", () => {
14 const tagged = "rate me" + String.fromCodePoint(0xe0041, 0xe0042);
15 expect(sanitizeLinkedPrompt(`a\u200Bb\u202Ec\uFEFF${tagged}`)).toEqual({
16 text: "abcrate me",
17 hiddenCharacters: 5,
18 });
19 expect(sanitizeLinkedPrompt("a\u2028b\u2029c")).toEqual({
20 text: "a\nb\nc",
21 hiddenCharacters: 0,
22 });
23 });
24​
25 it("keeps emoji sequences intact", () => {
26 const family = "\u{1F468}\u200D\u{1F469}\u200D\u{1F467}";
27 const heart = "\u2764\uFE0F";
28 expect(sanitizeLinkedPrompt(`${family} ${heart}`)).toEqual({
29 text: `${family} ${heart}`,
30 hiddenCharacters: 0,
31 });
32 expect(sanitizeLinkedPrompt("a\u200Db").hiddenCharacters).toBe(1);
33 expect(sanitizeLinkedPrompt("\u{1F468}\u200D").hiddenCharacters).toBe(1);
34 expect(sanitizeLinkedPrompt("\u{20000}\u200D\u{20001}").hiddenCharacters).toBe(1);
35 });
36});
37​
38describe("parseNewThreadLink", () => {
39 it("reads a shared prompt with its optional repository and model", () => {
40 const result = parseNewThreadLink(
41 "?prompt=Rate+me&repo=https://github.com/pingdotgg/t3code.git&provider=Codex&model=gpt-5",
42 );
43 expect(result).toEqual({
44 ok: true,
45 value: {
46 kind: "new",
47 prompt: { text: "Rate me", hiddenCharacters: 0 },
48 repository: { owner: "pingdotgg", name: "t3code" },
49 provider: "codex",
50 model: "gpt-5",
51 },
52 });
53 });
54​
55 it("reads a pull request link, whose prompt is optional", () => {
56 for (const pr of [
57 "pingdotgg/t3code/42",
58 "pingdotgg/t3code%2342",
59 "https://github.com/pingdotgg/t3code/pull/42/files",
60 ]) {
61 expect(parseNewThreadLink(`?pr=${pr}`)).toEqual({
62 ok: true,
63 value: {
64 kind: "pull-request",
65 pullRequest: { owner: "pingdotgg", name: "t3code", number: 42 },
66 prompt: null,
67 },
68 });
69 }
70 });
71​
72 it("refuses links it cannot open safely", () => {
73 expect(parseNewThreadLink("?repo=a/b").ok).toBe(false);
74 expect(parseNewThreadLink("?prompt=%E2%80%8B").ok).toBe(false);
75 expect(parseThreadLinkPrompt("?prompt=%E2%80%8B").ok).toBe(false);
76 expect(parseNewThreadLink("?prompt=x&repo=../etc").ok).toBe(false);
77 expect(parseNewThreadLink(`?prompt=${"x".repeat(8_001)}`).ok).toBe(false);
78 });
79});
80​
81describe("thread links", () => {
82 it("reads the prompt and leaves other parameters in place", () => {
83 expect(parseThreadLinkPrompt("?prompt=Fix+it")).toEqual({
84 ok: true,
85 value: { text: "Fix it", hiddenCharacters: 0 },
86 });
87 expect(parseThreadLinkPrompt("?panel=diff")).toEqual({ ok: true, value: null });
88 expect(withoutPromptLinkParams("?prompt=x&panel=diff")).toBe("?panel=diff");
89 });
90});
91​
92describe("link round trips", () => {
93 it("builds a link that parses back to the same prompt", () => {
94 const link = buildNewThreadLink({
95 prompt: "Swear count & rating?",
96 repository: { owner: "pingdotgg", name: "t3code" },
97 provider: "claudeAgent",
98 model: "opus",
99 });
100 const path = promptLinkAppPath(link);
101 expect(path).toBe(new URL(link).pathname + new URL(link).search);
102 const parsed = parseNewThreadLink(new URL(link).search);
103 expect(parsed.ok && parsed.value.kind === "new" && parsed.value.prompt.text).toBe(
104 "Swear count & rating?",
105 );
106 });
107​
108 it("maps app scheme and hosted links to in-app paths, and nothing else", () => {
109 expect(promptLinkAppPath("t3code://new?prompt=hi")).toBe("/new?prompt=hi");
110 expect(promptLinkAppPath("t3code-dev://threads/env-1/thread-1")).toBe("/env-1/thread-1");
111 expect(promptLinkAppPath("https://app.t3.codes/env-1/thread-1?prompt=go")).toBe(
112 "/env-1/thread-1?prompt=go",
113 );
114 expect(promptLinkAppPath("https://app.t3.codes/settings/general")).toBeNull();
115 expect(promptLinkAppPath("https://evil.test/new?prompt=hi")).toBeNull();
116 expect(promptLinkAppPath("t3code://app/index.html")).toBeNull();
117 });
118});