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

enrichedLinkPresentation.test.ts

185 lines6.2 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { ComposerContextId, EnvironmentId, ThreadId } from "@t3tools/contracts";
3import {
4 enrichedLinkVariantPattern,
5 buildEnrichedModelReferences,
6 enrichedModelLinkRegex,
7 enrichedModelReference,
8 enrichedTextLinkRegex,
9 enrichedSkillLinkRegex,
10 enrichedThreadLinkPresentation,
11 ENRICHED_INLINE_FILE_LINK_REGEX,
12 threadChipTargets,
13} from "./enrichedLinkPresentation";
14​
15describe("Enriched link presentation", () => {
16 it("recognizes file-shaped code without turning commands or URLs into links", () => {
17 for (const path of [
18 "src/app.tsx",
19 "./README.md",
20 "/tmp/report.md",
21 "C:\\repo\\app.tsx",
22 "app.tsx:12:3",
23 "src/http.ts:42-64",
24 "http.ts:42-64",
25 "src/a.ts#L10-L20",
26 "a.ts#L10-L20",
27 "app/(auth)/login/page.tsx",
28 "src/routes/[id]/+page.svelte",
29 "app/routes/posts.$id.tsx",
30 "node_modules/@scope/pkg/index.js",
31 "+page.svelte:42",
32 "[id].tsx:10:2",
33 "posts.$id.tsx:18-24",
34 "(auth).tsx:5",
35 ]) {
36 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(path), path).toBe(true);
37 }
38 for (const text of [
39 "npm run dev",
40 "port:3000",
41 "port:3000-3010",
42 "TODO:12",
43 "example.com",
44 "https://example.com/app.tsx",
45 "x + y",
46 "README.md",
47 "Dockerfile",
48 "docker/Dockerfiles",
49 "/app/settings",
50 "src/a.ts#section",
51 "src/a.ts#L10-L20-extra",
52 "me@example.com:80",
53 "+page.svelte",
54 ]) {
55 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(text), text).toBe(false);
56 }
57 });
58​
59 it("only recognizes complete known skill names and escapes their punctuation", () => {
60 const regex = enrichedSkillLinkRegex([{ name: "check" }, { name: "check.mobile" }]);
61 expect(regex?.exec("Try $check.mobile next")?.[0]).toBe("$check.mobile");
62 expect(regex?.exec("Try $check-mobile next")).toBeNull();
63 expect(regex?.exec("Try $checkXmobile next")).toBeNull();
64 expect(regex?.exec("prefix$check next")).toBeNull();
65 expect(enrichedSkillLinkRegex([])).toBeNull();
66 });
67​
68 it("recognizes only complete known models in prose and resolves their display names", () => {
69 const models = buildEnrichedModelReferences([
70 {
71 instanceId: "codex",
72 driver: "codex",
73 models: [
74 { slug: "gpt-6.1", name: "OpenAI GPT-6.1", subProvider: "OpenAI" },
75 { slug: "gpt-6.1-sol[1m]", name: "GPT-6.1 Sol" },
76 ],
77 },
78 ]);
79 const regex = enrichedModelLinkRegex(models);
80 for (const text of [
81 "Try model:codex/gpt-6.1 next",
82 "Try (model:codex/gpt-6.1).",
83 "Try [model:codex/gpt-6.1] next",
84 '"model:codex/gpt-6.1"',
85 "‏model:codex/gpt-6.1 هذا شرح",
86 ]) {
87 const source = regex?.exec(text)?.[0];
88 expect(source, text).toBe("model:codex/gpt-6.1");
89 expect(enrichedModelReference(source!, models)?.label).toBe("GPT-6.1");
90 }
91 expect(regex?.exec("Try model:codex/gpt-6.1-sol[1m].")?.[0]).toBe(
92 "model:codex/gpt-6.1-sol[1m]",
93 );
94 for (const text of [
95 "model:openai/gpt-4o",
96 "model:codex/gpt-6.1-extra",
97 "model:codex/gpt-6.1.2",
98 "model:codex/gpt-6X1",
99 "premodel:codex/gpt-6.1",
100 ]) {
101 expect(regex?.exec(text), text).toBeNull();
102 }
103 expect(enrichedModelReference("model:openai/gpt-4o", models)).toBeUndefined();
104 expect(enrichedModelLinkRegex([])).toBeNull();
105 expect(enrichedTextLinkRegex([], [])).toBeNull();
106 const combined = enrichedTextLinkRegex([{ name: "review" }], models);
107 expect(
108 "Use $review with (model:codex/gpt-6.1).".match(new RegExp(combined!.source, "gu")),
109 ).toEqual(["$review", "model:codex/gpt-6.1"]);
110 });
111​
112 it("matches only the complete URL, including literal regex metacharacters", () => {
113 const url = "file:///workspace/app[1].tsx?line=3+4";
114 const pattern = new RegExp(enrichedLinkVariantPattern(url));
115 expect(pattern.test(url)).toBe(true);
116 expect(pattern.test(`${url}/child`)).toBe(false);
117 expect(pattern.test("file:///workspace/app1Xtsx?line=34")).toBe(false);
118 });
119​
120 it("presents thread links as thread chips and leaves other links alone", () => {
121 for (const href of ["t3-thread://v1/thread%3Aproject%3A1", "t3-thread://v2/env/thread_1"]) {
122 expect(enrichedThreadLinkPresentation(href), href).toEqual({
123 accent: "#009c96",
124 icon: { kind: "chip", name: "messages-square" },
125 });
126 }
127 for (const href of [
128 "https://github.com/example/repo/pull/7",
129 "https://example.com",
130 "t3-context://v1/thread/thread_1",
131 ]) {
132 expect(enrichedThreadLinkPresentation(href), href).toBeUndefined();
133 }
134 });
135​
136 it("finds each linked thread once and resolves thread records to their own environment", () => {
137 const here = "env-here" as EnvironmentId;
138 const there = "env-there" as EnvironmentId;
139 expect(
140 threadChipTargets(
141 here,
142 "See [A](t3-thread://v1/thread_a), [A again](t3-thread://v1/thread_a), [B](t3-thread://v1/thread_b) and [D](t3-thread://v2/env-there/thread_d).",
143 [
144 {
145 version: 1,
146 contextId: "thr1" as ComposerContextId,
147 kind: "thread",
148 label: "C",
149 environmentId: there,
150 threadId: "thread_c" as ThreadId,
151 title: "C",
152 },
153 ],
154 ),
155 ).toEqual([
156 { href: "t3-thread://v1/thread_a", environmentId: here, threadId: "thread_a", link: true },
157 { href: "t3-thread://v1/thread_b", environmentId: here, threadId: "thread_b", link: true },
158 {
159 href: "t3-thread://v2/env-there/thread_d",
160 environmentId: there,
161 threadId: "thread_d",
162 link: true,
163 },
164 {
165 href: "t3-context://v1/thread/thr1",
166 environmentId: there,
167 threadId: "thread_c",
168 link: false,
169 },
170 ]);
171 expect(threadChipTargets(here, "No links here.")).toEqual([]);
172 });
173​
174 it("finds thread links with angle-bracketed destinations", () => {
175 const here = "env-here" as EnvironmentId;
176 expect(threadChipTargets(here, "See [A](<t3-thread://v2/env-here/thread_a>).")).toEqual([
177 {
178 href: "t3-thread://v2/env-here/thread_a",
179 environmentId: here,
180 threadId: "thread_a",
181 link: true,
182 },
183 ]);
184 });
185});