Open History to see the last commit
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.each(["$", "€", "𑿝"])("collects %s skill names that begin with a digit", (prefix) => {
35 expect(collectComposerInlineTokens(`Use ${prefix}2spec next`)).toEqual([
36 {
37 type: "skill",
38 value: "2spec",
39 source: `${prefix}2spec`,
40 start: 4,
41 end: 9 + prefix.length,
42 },
43 ]);
44 });
45
46 it("leaves digits-only dollar amounts and compact monetary expressions as text", () => {
47 expect(collectComposerInlineTokens("I'll pay $20 tomorrow")).toEqual([]);
48 expect(collectComposerInlineTokens("Budget is $1_000 total")).toEqual([]);
49 expect(collectComposerInlineTokens("Budget is $20k tomorrow")).toEqual([]);
50 expect(collectComposerInlineTokens("Cost is $100M total")).toEqual([]);
51 expect(collectComposerInlineTokens("Limit is $1e6 here")).toEqual([]);
52 });
53
54 it("does not convert incomplete trailing tokens", () => {
55 expect(collectComposerInlineTokens("Use $ui")).toEqual([]);
56 expect(collectComposerInlineTokens("Inspect @AGENTS.md")).toEqual([]);
57 });
58
59 it("preserves a confirmed pill when only its trailing delimiter is removed", () => {
60 const withDelimiter = "[package.json](package.json) ";
61 const confirmed = collectComposerInlineTokens(withDelimiter);
62
63 expect(
64 collectComposerInlineTokens(withDelimiter.trimEnd(), { preserveTrailingFrom: confirmed }),
65 ).toEqual([
66 {
67 type: "mention",
68 value: "package.json",
69 source: "[package.json](package.json)",
70 start: 0,
71 end: 28,
72 },
73 ]);
74 });
75
76 it("does not preserve a pill after its source is edited", () => {
77 const confirmed = collectComposerInlineTokens("[package.json](package.json) ");
78
79 expect(
80 collectComposerInlineTokens("[package.json](package-json)", {
81 preserveTrailingFrom: confirmed,
82 }),
83 ).toEqual([]);
84 });
85
86 it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
87 "keeps scoped package reference %s as plain text",
88 (reference) => {
89 expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
90 },
91 );
92
93 it.each(["@namespace.foo(bar)", "@écrire(option)", "@e\u0301crire(option)"])(
94 "keeps decorator call %s as plain text",
95 (reference) => {
96 expect(collectComposerInlineTokens(`${reference}\nclass Example {}`)).toEqual([]);
97 },
98 );
99
100 it.each(['@"foo(bar)"', "[foo(bar)](foo%28bar%29)"])(
101 "keeps an explicit decorator-shaped file reference as a mention: %s",
102 (reference) => {
103 expect(collectComposerInlineTokens(`${reference} `)).toEqual([
104 {
105 type: "mention",
106 value: "foo(bar)",
107 source: reference,
108 start: 0,
109 end: reference.length,
110 },
111 ]);
112 },
113 );
114
115 it("keeps bare file mentions before whitespace-separated parentheses", () => {
116 expect(collectComposerInlineTokens("Inspect @README.md (notes)")).toEqual([
117 {
118 type: "mention",
119 value: "README.md",
120 source: "@README.md",
121 start: 8,
122 end: 18,
123 },
124 ]);
125 });
126
127 it("keeps canonical file links for scoped paths as mentions", () => {
128 expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
129 {
130 type: "mention",
131 value: "@scope/pkg/sub",
132 source: "[sub](@scope/pkg/sub)",
133 start: 8,
134 end: 29,
135 },
136 ]);
137 });
138
139 it("allows ambiguous scoped paths through explicit quoted mentions", () => {
140 expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
141 {
142 type: "mention",
143 value: "expo/ui",
144 source: '@"expo/ui"',
145 start: 8,
146 end: 18,
147 },
148 ]);
149 });
150
151 it("still collects a file link whose label is at the length cap", () => {
152 const label = `${"a".repeat(508)}.tsx`;
153 const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
154
155 expect(tokens).toHaveLength(1);
156 expect(tokens[0]?.value).toBe(`src/${label}`);
157 });
158
159 it("leaves a file link past the label cap as plain text", () => {
160 const label = `${"a".repeat(509)}.tsx`;
161 expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
162 });
163
164 it("leaves a context reference link alone", () => {
165 expect(
166 collectComposerInlineTokens("see [checkout.png](t3-context://v1/image/ctx_abc) ok"),
167 ).toEqual([]);
168 expect(collectComposerInlineTokens("see  ok")).toEqual(
169 [],
170 );
171 });
172
173 it("keeps skill and mention sources inside TeX out of the context chip list", () => {
174 const source = "\\[\\text{use $browser and @README.md here}\\] then $real-skill ";
175 expect(collectComposerInlineTokens(source).map((token) => token.source)).toEqual([
176 "$real-skill",
177 ]);
178 });
179});