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

composerInlineTokens.test.ts

153 lines4.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { collectComposerInlineTokens } from "./composerInlineTokens.ts";
4​
5describe("collectComposerInlineTokens", () => {
6 it("collects file links, mentions, and skills with source ranges", () => {
7 const text = "Use $ui and inspect [Chat.tsx](src/Chat.tsx) with @AGENTS.md please";
8​
9 expect(collectComposerInlineTokens(text)).toEqual([
10 {
11 type: "skill",
12 value: "ui",
13 source: "$ui",
14 start: 4,
15 end: 7,
16 },
17 {
18 type: "mention",
19 value: "src/Chat.tsx",
20 source: "[Chat.tsx](src/Chat.tsx)",
21 start: 20,
22 end: 44,
23 },
24 {
25 type: "mention",
26 value: "AGENTS.md",
27 source: "@AGENTS.md",
28 start: 50,
29 end: 60,
30 },
31 ]);
32 });
33​
34 it("does not convert incomplete trailing tokens", () => {
35 expect(collectComposerInlineTokens("Use $ui")).toEqual([]);
36 expect(collectComposerInlineTokens("Inspect @AGENTS.md")).toEqual([]);
37 });
38​
39 it("keeps the delimiter after a token outside its source range", () => {
40 const text = "Inspect [package.json](package.json) next";
41​
42 expect(collectComposerInlineTokens(text)).toEqual([
43 {
44 type: "mention",
45 value: "package.json",
46 source: "[package.json](package.json)",
47 start: 8,
48 end: 36,
49 },
50 ]);
51 expect(text.slice(36)).toBe(" next");
52 });
53​
54 it("preserves a confirmed pill when only its trailing delimiter is removed", () => {
55 const withDelimiter = "[package.json](package.json) ";
56 const confirmed = collectComposerInlineTokens(withDelimiter);
57​
58 expect(
59 collectComposerInlineTokens(withDelimiter.trimEnd(), { preserveTrailingFrom: confirmed }),
60 ).toEqual([
61 {
62 type: "mention",
63 value: "package.json",
64 source: "[package.json](package.json)",
65 start: 0,
66 end: 28,
67 },
68 ]);
69 });
70​
71 it("does not preserve a pill after its source is edited", () => {
72 const confirmed = collectComposerInlineTokens("[package.json](package.json) ");
73​
74 expect(
75 collectComposerInlineTokens("[package.json](package-json)", {
76 preserveTrailingFrom: confirmed,
77 }),
78 ).toEqual([]);
79 });
80​
81 it("ignores normal web links", () => {
82 expect(collectComposerInlineTokens("Read [docs](https://example.com) first")).toEqual([]);
83 });
84​
85 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
86 "keeps scoped package reference %s as plain text",
87 (reference) => {
88 expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
89 },
90 );
91​
92 it("keeps scoped package references plain across incomplete input and IME whitespace", () => {
93 expect(collectComposerInlineTokens("Install @expo/ui")).toEqual([]);
94 expect(collectComposerInlineTokens("入力 @expo/ui を追加")).toEqual([]);
95 });
96​
97 it("keeps bare non-scoped file paths as mentions", () => {
98 expect(collectComposerInlineTokens("Inspect @README.md next")).toEqual([
99 {
100 type: "mention",
101 value: "README.md",
102 source: "@README.md",
103 start: 8,
104 end: 18,
105 },
106 ]);
107 });
108​
109 it("keeps canonical file links for scoped paths as mentions", () => {
110 expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
111 {
112 type: "mention",
113 value: "@scope/pkg/sub",
114 source: "[sub](@scope/pkg/sub)",
115 start: 8,
116 end: 29,
117 },
118 ]);
119 });
120​
121 it("allows ambiguous scoped paths through explicit quoted mentions", () => {
122 expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
123 {
124 type: "mention",
125 value: "expo/ui",
126 source: '@"expo/ui"',
127 start: 8,
128 end: 18,
129 },
130 ]);
131 });
132​
133 it("still collects a file link whose label is at the length cap", () => {
134 const label = `${"a".repeat(508)}.tsx`;
135 const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
136​
137 expect(tokens).toHaveLength(1);
138 expect(tokens[0]?.value).toBe(`src/${label}`);
139 });
140​
141 it("leaves a file link past the label cap as plain text", () => {
142 const label = `${"a".repeat(509)}.tsx`;
143 expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
144 });
145​
146 it("stays fast on unterminated bracket runs", () => {
147 // Unbounded, the label body rescanned the rest of the text from every
148 // whitespace: this input took seconds.
149 const started = performance.now();
150 expect(collectComposerInlineTokens(" [[".repeat(40_000))).toEqual([]);
151 expect(performance.now() - started).toBeLessThan(1_000);
152 });
153});