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 enrichedSkillLinkRegex,
6 enrichedThreadLinkPresentation,
7 ENRICHED_INLINE_FILE_LINK_REGEX,
8 threadChipTargets,
9} from "./enrichedLinkPresentation";
10
11describe("Enriched link presentation", () => {
12 it("recognizes file-shaped code without turning commands or URLs into links", () => {
13 for (const path of [
14 "src/app.tsx",
15 "./README.md",
16 "/tmp/report.md",
17 "C:\\repo\\app.tsx",
18 "app.tsx:12:3",
19 ]) {
20 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(path), path).toBe(true);
21 }
22 for (const text of [
23 "npm run dev",
24 "port:3000",
25 "TODO:12",
26 "example.com",
27 "https://example.com/app.tsx",
28 "x + y",
29 "README.md",
30 "/app/settings",
31 ]) {
32 expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(text), text).toBe(false);
33 }
34 });
35
36 it("only recognizes complete known skill names and escapes their punctuation", () => {
37 const regex = enrichedSkillLinkRegex([{ name: "check" }, { name: "check.mobile" }]);
38 expect(regex?.exec("Try $check.mobile next")?.[0]).toBe("$check.mobile");
39 expect(regex?.exec("Try $check-mobile next")).toBeNull();
40 expect(regex?.exec("Try $checkXmobile next")).toBeNull();
41 expect(regex?.exec("prefix$check next")).toBeNull();
42 expect(enrichedSkillLinkRegex([])).toBeNull();
43 });
44
45 it("matches only the complete URL, including literal regex metacharacters", () => {
46 const url = "file:///workspace/app[1].tsx?line=3+4";
47 const pattern = new RegExp(enrichedLinkVariantPattern(url));
48 expect(pattern.test(url)).toBe(true);
49 expect(pattern.test(`${url}/child`)).toBe(false);
50 expect(pattern.test("file:///workspace/app1Xtsx?line=34")).toBe(false);
51 });
52
53 it("presents thread links as thread chips and leaves other links alone", () => {
54 for (const href of ["t3-thread://v1/thread%3Aproject%3A1", "t3-thread://v2/env/thread_1"]) {
55 expect(enrichedThreadLinkPresentation(href), href).toEqual({
56 accent: "#009c96",
57 icon: { kind: "chip", name: "messages-square" },
58 });
59 }
60 for (const href of [
61 "https://github.com/example/repo/pull/7",
62 "https://example.com",
63 "t3-context://v1/thread/thread_1",
64 ]) {
65 expect(enrichedThreadLinkPresentation(href), href).toBeUndefined();
66 }
67 });
68
69 it("finds each linked thread once and resolves thread records to their own environment", () => {
70 const here = "env-here" as EnvironmentId;
71 const there = "env-there" as EnvironmentId;
72 expect(
73 threadChipTargets(
74 here,
75 "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).",
76 [
77 {
78 version: 1,
79 contextId: "thr1" as ComposerContextId,
80 kind: "thread",
81 label: "C",
82 environmentId: there,
83 threadId: "thread_c" as ThreadId,
84 title: "C",
85 },
86 ],
87 ),
88 ).toEqual([
89 { href: "t3-thread://v1/thread_a", environmentId: here, threadId: "thread_a", link: true },
90 { href: "t3-thread://v1/thread_b", environmentId: here, threadId: "thread_b", link: true },
91 {
92 href: "t3-thread://v2/env-there/thread_d",
93 environmentId: there,
94 threadId: "thread_d",
95 link: true,
96 },
97 {
98 href: "t3-context://v1/thread/thr1",
99 environmentId: there,
100 threadId: "thread_c",
101 link: false,
102 },
103 ]);
104 expect(threadChipTargets(here, "No links here.")).toEqual([]);
105 });
106});