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

enrichedLinkPresentation.test.ts

170 lines5.8 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 ]) {
26 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(path), path).toBe(true);
27 }
28 for (const text of [
29 "npm run dev",
30 "port:3000",
31 "port:3000-3010",
32 "TODO:12",
33 "example.com",
34 "https://example.com/app.tsx",
35 "x + y",
36 "README.md",
37 "Dockerfile",
38 "docker/Dockerfiles",
39 "/app/settings",
40 ]) {
41 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(text), text).toBe(false);
42 }
43 });
44​
45 it("only recognizes complete known skill names and escapes their punctuation", () => {
46 const regex = enrichedSkillLinkRegex([{ name: "check" }, { name: "check.mobile" }]);
47 expect(regex?.exec("Try $check.mobile next")?.[0]).toBe("$check.mobile");
48 expect(regex?.exec("Try $check-mobile next")).toBeNull();
49 expect(regex?.exec("Try $checkXmobile next")).toBeNull();
50 expect(regex?.exec("prefix$check next")).toBeNull();
51 expect(enrichedSkillLinkRegex([])).toBeNull();
52 });
53​
54 it("recognizes only complete known models in prose and resolves their display names", () => {
55 const models = buildEnrichedModelReferences([
56 {
57 instanceId: "codex",
58 driver: "codex",
59 models: [
60 { slug: "gpt-6.1", name: "OpenAI GPT-6.1", subProvider: "OpenAI" },
61 { slug: "gpt-6.1-sol[1m]", name: "GPT-6.1 Sol" },
62 ],
63 },
64 ]);
65 const regex = enrichedModelLinkRegex(models);
66 for (const text of [
67 "Try model:codex/gpt-6.1 next",
68 "Try (model:codex/gpt-6.1).",
69 "Try [model:codex/gpt-6.1] next",
70 '"model:codex/gpt-6.1"',
71 ]) {
72 const source = regex?.exec(text)?.[0];
73 expect(source, text).toBe("model:codex/gpt-6.1");
74 expect(enrichedModelReference(source!, models)?.label).toBe("GPT-6.1");
75 }
76 expect(regex?.exec("Try model:codex/gpt-6.1-sol[1m].")?.[0]).toBe(
77 "model:codex/gpt-6.1-sol[1m]",
78 );
79 for (const text of [
80 "model:openai/gpt-4o",
81 "model:codex/gpt-6.1-extra",
82 "model:codex/gpt-6.1.2",
83 "model:codex/gpt-6X1",
84 "premodel:codex/gpt-6.1",
85 ]) {
86 expect(regex?.exec(text), text).toBeNull();
87 }
88 expect(enrichedModelReference("model:openai/gpt-4o", models)).toBeUndefined();
89 expect(enrichedModelLinkRegex([])).toBeNull();
90 expect(enrichedTextLinkRegex([], [])).toBeNull();
91 const combined = enrichedTextLinkRegex([{ name: "review" }], models);
92 expect(
93 "Use $review with (model:codex/gpt-6.1).".match(new RegExp(combined!.source, "gu")),
94 ).toEqual(["$review", "model:codex/gpt-6.1"]);
95 });
96​
97 it("matches only the complete URL, including literal regex metacharacters", () => {
98 const url = "file:///workspace/app[1].tsx?line=3+4";
99 const pattern = new RegExp(enrichedLinkVariantPattern(url));
100 expect(pattern.test(url)).toBe(true);
101 expect(pattern.test(`${url}/child`)).toBe(false);
102 expect(pattern.test("file:///workspace/app1Xtsx?line=34")).toBe(false);
103 });
104​
105 it("presents thread links as thread chips and leaves other links alone", () => {
106 for (const href of ["t3-thread://v1/thread%3Aproject%3A1", "t3-thread://v2/env/thread_1"]) {
107 expect(enrichedThreadLinkPresentation(href), href).toEqual({
108 accent: "#009c96",
109 icon: { kind: "chip", name: "messages-square" },
110 });
111 }
112 for (const href of [
113 "https://github.com/example/repo/pull/7",
114 "https://example.com",
115 "t3-context://v1/thread/thread_1",
116 ]) {
117 expect(enrichedThreadLinkPresentation(href), href).toBeUndefined();
118 }
119 });
120​
121 it("finds each linked thread once and resolves thread records to their own environment", () => {
122 const here = "env-here" as EnvironmentId;
123 const there = "env-there" as EnvironmentId;
124 expect(
125 threadChipTargets(
126 here,
127 "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).",
128 [
129 {
130 version: 1,
131 contextId: "thr1" as ComposerContextId,
132 kind: "thread",
133 label: "C",
134 environmentId: there,
135 threadId: "thread_c" as ThreadId,
136 title: "C",
137 },
138 ],
139 ),
140 ).toEqual([
141 { href: "t3-thread://v1/thread_a", environmentId: here, threadId: "thread_a", link: true },
142 { href: "t3-thread://v1/thread_b", environmentId: here, threadId: "thread_b", link: true },
143 {
144 href: "t3-thread://v2/env-there/thread_d",
145 environmentId: there,
146 threadId: "thread_d",
147 link: true,
148 },
149 {
150 href: "t3-context://v1/thread/thr1",
151 environmentId: there,
152 threadId: "thread_c",
153 link: false,
154 },
155 ]);
156 expect(threadChipTargets(here, "No links here.")).toEqual([]);
157 });
158​
159 it("finds thread links with angle-bracketed destinations", () => {
160 const here = "env-here" as EnvironmentId;
161 expect(threadChipTargets(here, "See [A](<t3-thread://v2/env-here/thread_a>).")).toEqual([
162 {
163 href: "t3-thread://v2/env-here/thread_a",
164 environmentId: here,
165 threadId: "thread_a",
166 link: true,
167 },
168 ]);
169 });
170});