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

enrichedLinkPresentation.test.ts

98 lines3.4 KB

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 expect(enrichedThreadLinkPresentation("t3-thread://v1/thread%3Aproject%3A1")).toEqual({
55 accent: "#009c96",
56 icon: { kind: "chip", name: "messages-square" },
57 });
58 for (const href of [
59 "https://github.com/example/repo/pull/7",
60 "https://example.com",
61 "t3-context://v1/thread/thread_1",
62 ]) {
63 expect(enrichedThreadLinkPresentation(href), href).toBeUndefined();
64 }
65 });
66​
67 it("finds each linked thread once and resolves thread records to their own environment", () => {
68 const here = "env-here" as EnvironmentId;
69 const there = "env-there" as EnvironmentId;
70 expect(
71 threadChipTargets(
72 here,
73 "See [A](t3-thread://v1/thread_a), [A again](t3-thread://v1/thread_a) and [B](t3-thread://v1/thread_b).",
74 [
75 {
76 version: 1,
77 contextId: "thr1" as ComposerContextId,
78 kind: "thread",
79 label: "C",
80 environmentId: there,
81 threadId: "thread_c" as ThreadId,
82 title: "C",
83 },
84 ],
85 ),
86 ).toEqual([
87 { href: "t3-thread://v1/thread_a", environmentId: here, threadId: "thread_a", link: true },
88 { href: "t3-thread://v1/thread_b", environmentId: here, threadId: "thread_b", link: true },
89 {
90 href: "t3-context://v1/thread/thr1",
91 environmentId: there,
92 threadId: "thread_c",
93 link: false,
94 },
95 ]);
96 expect(threadChipTargets(here, "No links here.")).toEqual([]);
97 });
98});