-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcontent.test.ts
More file actions
328 lines (302 loc) · 9.18 KB
/
Copy pathcontent.test.ts
File metadata and controls
328 lines (302 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { describe, expect, it } from "vitest";
import {
contentToXml,
type EditorContent,
extractFilePaths,
xmlToContent,
xmlToPlainText,
} from "./content";
import { buildGithubRefPlaceholderChip } from "./githubIssueChip";
describe("xmlToContent", () => {
it("parses a file tag into a file chip", () => {
const result = xmlToContent('<file path="src/foo/bar.ts" />');
expect(result).toEqual({
segments: [
{
type: "chip",
chip: { type: "file", id: "src/foo/bar.ts", label: "foo/bar.ts" },
},
],
});
});
it("derives file label from the final path segment when no parent", () => {
const result = xmlToContent('<file path="README.md" />');
expect(result.segments).toEqual([
{
type: "chip",
chip: { type: "file", id: "README.md", label: "README.md" },
},
]);
});
it("unescapes XML attributes", () => {
const result = xmlToContent('<file path="a/"weird".ts" />');
const segment = result.segments[0];
expect(segment.type).toBe("chip");
if (segment.type === "chip") {
expect(segment.chip.id).toBe('a/"weird".ts');
}
});
it("parses github_issue tags with title", () => {
const xml =
'<github_issue number="42" title="Fix bug" url="https://github.com/org/repo/issues/42" />';
expect(xmlToContent(xml).segments).toEqual([
{
type: "chip",
chip: {
type: "github_issue",
id: "https://github.com/org/repo/issues/42",
label: "#42 - Fix bug",
},
},
]);
});
it("parses github_issue tags without title", () => {
const xml =
'<github_issue number="7" url="https://github.com/org/repo/issues/7" />';
const segment = xmlToContent(xml).segments[0];
expect(segment.type).toBe("chip");
if (segment.type === "chip") {
expect(segment.chip.label).toBe("#7");
}
});
it("parses github_pr tags with title", () => {
const xml =
'<github_pr number="123" title="Ship it" url="https://github.com/org/repo/pull/123" />';
expect(xmlToContent(xml).segments).toEqual([
{
type: "chip",
chip: {
type: "github_pr",
id: "https://github.com/org/repo/pull/123",
label: "#123 - Ship it",
},
},
]);
});
it("serializes a fallback-labeled github_issue chip with an empty title", () => {
const content: EditorContent = {
segments: [
{
type: "chip",
chip: {
type: "github_issue",
id: "https://github.com/org/repo/issues/1454",
label: "#1454",
},
},
],
};
expect(contentToXml(content)).toBe(
'<github_issue number="1454" title="" url="https://github.com/org/repo/issues/1454" />',
);
});
it("serializes an unresolved github_pr placeholder chip with an empty title", () => {
const content: EditorContent = {
segments: [
{
type: "chip",
chip: buildGithubRefPlaceholderChip({
kind: "pr",
owner: "org",
repo: "repo",
number: 71827,
normalizedUrl: "https://github.com/org/repo/pull/71827",
}),
},
],
};
expect(contentToXml(content)).toBe(
'<github_pr number="71827" title="" url="https://github.com/org/repo/pull/71827" />',
);
});
it("round-trips a github_pr chip", () => {
const content: EditorContent = {
segments: [
{
type: "chip",
chip: {
type: "github_pr",
id: "https://github.com/org/repo/pull/42",
label: "#42 - Fix thing",
},
},
],
};
expect(xmlToContent(contentToXml(content)).segments).toEqual(
content.segments,
);
});
it.each([
["error", "err-1"],
["experiment", "exp-1"],
["insight", "ins-1"],
["feature_flag", "flag-1"],
])("parses %s tag into a chip with id as label", (type, id) => {
const xml = `<${type} id="${id}" />`;
expect(xmlToContent(xml).segments).toEqual([
{ type: "chip", chip: { type, id, label: id } },
]);
});
it("preserves surrounding text around chips", () => {
const result = xmlToContent(
'please review <file path="src/a.ts" /> and <file path="src/b.ts" />',
);
expect(result.segments).toEqual([
{ type: "text", text: "please review " },
{
type: "chip",
chip: { type: "file", id: "src/a.ts", label: "src/a.ts" },
},
{ type: "text", text: " and " },
{
type: "chip",
chip: { type: "file", id: "src/b.ts", label: "src/b.ts" },
},
]);
});
it("returns a single text segment when no tags are present", () => {
expect(xmlToContent("just plain text").segments).toEqual([
{ type: "text", text: "just plain text" },
]);
});
it("returns a single text segment for empty input", () => {
expect(xmlToContent("").segments).toEqual([{ type: "text", text: "" }]);
});
it("parses a folder tag into a folder chip", () => {
const result = xmlToContent('<folder path="src/foo" />');
expect(result.segments).toEqual([
{
type: "chip",
chip: { type: "folder", id: "src/foo", label: "src/foo" },
},
]);
});
it("round-trips a folder chip", () => {
const content: EditorContent = {
segments: [
{
type: "chip",
chip: { type: "folder", id: "src/foo", label: "src/foo" },
},
],
};
expect(contentToXml(content)).toBe('<folder path="src/foo" />');
expect(xmlToContent(contentToXml(content)).segments).toEqual(
content.segments,
);
});
it("round-trips a local skill command chip", () => {
const content: EditorContent = {
segments: [
{
type: "chip",
chip: {
type: "command",
id: "/Users/alessandro/.claude/skills/local-skill",
label: "local-skill",
skillName: "local-skill",
skillSource: "user",
skillPath: "/Users/alessandro/.claude/skills/local-skill",
},
},
],
};
expect(contentToXml(content)).toBe(
'<skill name="local-skill" source="user" path="/Users/alessandro/.claude/skills/local-skill" />',
);
expect(xmlToContent(contentToXml(content)).segments).toEqual(
content.segments,
);
});
it("extractFilePaths includes folder chips alongside file chips", () => {
const content: EditorContent = {
segments: [
{ type: "text", text: "see " },
{
type: "chip",
chip: { type: "folder", id: "src/sub", label: "src/sub" },
},
{
type: "chip",
chip: { type: "file", id: "src/a.ts", label: "a.ts" },
},
],
};
expect(extractFilePaths(content)).toEqual(["src/sub", "src/a.ts"]);
});
it("xmlToPlainText renders folder mentions as @mentions", () => {
expect(
xmlToPlainText('look at <folder path="products/agentic_tests" /> please'),
).toBe("look at @products/agentic_tests please");
});
it("xmlToPlainText renders file mentions as @mentions", () => {
expect(
xmlToPlainText('see <file path="src/foo/bar.ts" /> for details'),
).toBe("see @foo/bar.ts for details");
});
it("xmlToPlainText renders structured chip types as @label", () => {
expect(
xmlToPlainText(
'investigate <error id="err-1" /> and <feature_flag id="flag-2" />',
),
).toBe("investigate @err-1 and @flag-2");
});
it("xmlToPlainText leaves plain text untouched", () => {
expect(xmlToPlainText("ship the fix")).toBe("ship the fix");
});
it("xmlToPlainText renders github_pr and github_issue mentions", () => {
expect(
xmlToPlainText(
'<github_pr number="42" title="Add login" url="https://github.com/x/y/pull/42" />',
),
).toBe("@#42 - Add login");
expect(
xmlToPlainText(
'<github_issue number="7" title="" url="https://github.com/x/y/issues/7" />',
),
).toBe("@#7");
});
it("xmlToPlainText passes through non-chip XML-like text", () => {
expect(xmlToPlainText("use Array<string> and <div> tags")).toBe(
"use Array<string> and <div> tags",
);
});
it("round-trips contentToXml for a mix of text and chips", () => {
const content: EditorContent = {
segments: [
{ type: "text", text: "look at " },
{
type: "chip",
chip: { type: "file", id: "apps/code/src/a.ts", label: "src/a.ts" },
},
{ type: "text", text: " and " },
{
type: "chip",
chip: {
type: "github_issue",
id: "https://github.com/org/repo/issues/9",
label: "#9 - Thing",
},
},
],
};
const xml = contentToXml(content);
const parsed = xmlToContent(xml);
expect(parsed.segments).toEqual([
{ type: "text", text: "look at " },
{
type: "chip",
chip: { type: "file", id: "apps/code/src/a.ts", label: "src/a.ts" },
},
{ type: "text", text: " and " },
{
type: "chip",
chip: {
type: "github_issue",
id: "https://github.com/org/repo/issues/9",
label: "#9 - Thing",
},
},
]);
});
});