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