-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput.test.ts
More file actions
733 lines (644 loc) · 29.6 KB
/
output.test.ts
File metadata and controls
733 lines (644 loc) · 29.6 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import { describe, expect, it } from "bun:test";
import { segmentLineCol } from "./api.ts";
import {
buildJsonOutput,
buildMarkdownOutput,
buildOutput,
buildQueryTitle,
buildReplayCommand,
buildReplayDetails,
shortExtractRef,
shortRepo,
} from "./output.ts";
import type { ReplayOptions } from "./output.ts";
import type { RepoGroup } from "./types.ts";
const ORG = "myorg";
const QUERY = "useFlag";
function makeGroup(
repo: string,
paths: string[],
{
repoSelected = true,
extractSelected,
}: { repoSelected?: boolean; extractSelected?: boolean[] } = {},
): RepoGroup {
return {
repoFullName: repo,
matches: paths.map((p) => ({
path: p,
repoFullName: repo,
htmlUrl: `https://github.com/${repo}/blob/main/${p}`,
archived: false,
textMatches: [],
})),
folded: true,
repoSelected,
extractSelected: extractSelected ?? paths.map(() => true),
};
}
/** Helper: group where matches include text-match location info. */
function makeGroupWithMatches(
repo: string,
files: Array<{ path: string; line: number; col: number }>,
): RepoGroup {
return {
repoFullName: repo,
matches: files.map(({ path, line, col }) => ({
path,
repoFullName: repo,
htmlUrl: `https://github.com/${repo}/blob/main/${path}`,
archived: false,
textMatches: [
{
fragment: "some code snippet",
matches: [{ text: "snippet", indices: [10, 17], line, col }],
},
],
})),
folded: true,
repoSelected: true,
extractSelected: files.map(() => true),
};
}
describe("shortRepo", () => {
it("strips org prefix", () => {
expect(shortRepo("myorg/repoA", ORG)).toBe("repoA");
});
it("leaves other orgs untouched", () => {
expect(shortRepo("otherorg/repoA", ORG)).toBe("otherorg/repoA");
});
});
describe("shortExtractRef", () => {
it("strips org prefix from extract ref", () => {
expect(shortExtractRef("myorg/repoA:src/foo.ts:0", ORG)).toBe("repoA:src/foo.ts:0");
});
});
describe("buildReplayCommand", () => {
it("generates base command", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
expect(cmd).toContain(`github-code-search`);
expect(cmd).toContain(`--org '${ORG}'`);
expect(cmd).toContain(`--no-interactive`);
});
it("starts with replay comment header", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
expect(cmd).toContain("# Replay:");
});
it("command lines are directly runnable (no # prefix)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
const lines = cmd.split("\n");
// Second line onward should NOT start with "# " (only first is a comment)
const commandLines = lines.filter((l) => !l.startsWith("#"));
expect(commandLines.length).toBeGreaterThan(0);
expect(commandLines[0]).not.toMatch(/^#\s/);
});
it("includes deselected repo in --exclude-repositories", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { repoSelected: false })];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
expect(cmd).toContain("--exclude-repositories repoA");
});
it("includes deselected extract in --exclude-extracts", () => {
const groups = [
makeGroup("myorg/repoA", ["a.ts", "b.ts"], {
extractSelected: [true, false],
}),
];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
expect(cmd).toContain("--exclude-extracts 'repoA:b.ts:1'");
});
it("does not double-add pre-existing exclusions", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { repoSelected: false })];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(["myorg/repoA"]), new Set());
const count = (cmd.match(/repoA/g) ?? []).length;
expect(count).toBe(1);
});
it("includes --format json when format is json", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { format: "json" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--format json");
});
it("does not include --format when format is markdown (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { format: "markdown" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).not.toContain("--format");
});
it("includes --output-type repo-only when outputType is repo-only", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { outputType: "repo-only" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--output-type repo-only");
});
it("does not include --output-type when outputType is repo-and-matches (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { outputType: "repo-and-matches" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).not.toContain("--output-type");
});
it("includes --include-archived when includeArchived is true", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { includeArchived: true };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--include-archived");
});
it("does not include --include-archived when includeArchived is false (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { includeArchived: false };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).not.toContain("--include-archived");
});
it("includes --exclude-template-repositories when excludeTemplates is true", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { excludeTemplates: true };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--exclude-template-repositories");
});
it("does not include --exclude-template-repositories when excludeTemplates is false (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { excludeTemplates: false };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).not.toContain("--exclude-template-repositories");
});
it("includes --group-by-team-prefix when groupByTeamPrefix is set", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { groupByTeamPrefix: "squad-,chapter-" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--group-by-team-prefix 'squad-,chapter-'");
});
it("does not include --group-by-team-prefix when groupByTeamPrefix is empty (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { groupByTeamPrefix: "" };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).not.toContain("--group-by-team-prefix");
});
it("includes --regex-hint when regexHint is set", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = { regexHint: '"axios"' };
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--regex-hint");
expect(cmd).toContain("axios");
// Must use single-quote shell escaping, not JSON.stringify
expect(cmd).toContain(`--regex-hint '"`);
});
it("does not include --regex-hint when regexHint is not set (default)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set());
expect(cmd).not.toContain("--regex-hint");
});
it("emits --pick-team for each entry in pickTeams", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = {
groupByTeamPrefix: "squad-",
pickTeams: { "squad-frontend + squad-mobile": "squad-frontend" },
};
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--pick-team 'squad-frontend + squad-mobile=squad-frontend'");
});
it("emits multiple --pick-team flags when pickTeams has multiple entries", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const opts: ReplayOptions = {
groupByTeamPrefix: "squad-",
pickTeams: {
"squad-a + squad-b": "squad-a",
"squad-c + squad-d": "squad-c",
},
};
const cmd = buildReplayCommand(groups, QUERY, ORG, new Set(), new Set(), opts);
expect(cmd).toContain("--pick-team 'squad-a + squad-b=squad-a'");
expect(cmd).toContain("--pick-team 'squad-c + squad-d=squad-c'");
});
});
describe("buildReplayDetails", () => {
it("returns a <details> block with bash code fence", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const out = buildReplayDetails(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("<details>");
expect(out).toContain(
'<summary><a href="https://fulll.github.io/github-code-search/">github-code-search</a> replay command</summary>',
);
expect(out).toContain("```bash");
expect(out).toContain("</details>");
});
it("contains the runnable shell command (no # Replay: header)", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const out = buildReplayDetails(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("github-code-search");
expect(out).toContain("--no-interactive");
// The "# Replay:" comment header should NOT appear in the details block
expect(out).not.toContain("# Replay:");
});
it("includes exclusion flags inside the code fence", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { repoSelected: false })];
const out = buildReplayDetails(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("--exclude-repositories");
});
});
describe("buildQueryTitle", () => {
it("wraps a plain query in double quotes", () => {
expect(buildQueryTitle("useFlag")).toBe('# Results for "useFlag"');
});
it("wraps a regex query in backticks", () => {
expect(buildQueryTitle("/useFlag/i")).toBe("# Results for `/useFlag/i`");
});
it("appends · including archived when includeArchived is true", () => {
expect(buildQueryTitle("useFlag", { includeArchived: true })).toBe(
'# Results for "useFlag" · including archived',
);
});
it("appends · excluding templates when excludeTemplates is true", () => {
expect(buildQueryTitle("useFlag", { excludeTemplates: true })).toBe(
'# Results for "useFlag" · excluding templates',
);
});
it("appends both qualifiers in order when both are set", () => {
expect(buildQueryTitle("useFlag", { includeArchived: true, excludeTemplates: true })).toBe(
'# Results for "useFlag" · including archived · excluding templates',
);
});
it("omits qualifiers when neither option is set", () => {
expect(buildQueryTitle("useFlag", { includeArchived: false, excludeTemplates: false })).toBe(
'# Results for "useFlag"',
);
});
it("handles a plain query with embedded double quotes without breaking the heading", () => {
const title = buildQueryTitle('from "axios"');
// JSON.stringify escapes the embedded quotes — heading stays on one line
expect(title.startsWith("# Results for ")).toBe(true);
expect(title).not.toContain("\n");
expect(title).toContain("axios");
});
it("handles a plain query with a newline without breaking the heading", () => {
const title = buildQueryTitle("line1\nline2");
// JSON.stringify converts \n to \\n — no actual newline in the heading
expect(title.startsWith("# Results for ")).toBe(true);
expect(title).not.toContain("\n");
});
it("handles a regex query with an embedded backtick without producing malformed inline code", () => {
const title = buildQueryTitle("/foo`bar/i");
expect(title.startsWith("# Results for ")).toBe(true);
expect(title).not.toContain("\n");
// Variable-length fence: at least two consecutive backticks used as delimiter
expect(title).toContain("``");
});
});
describe("buildMarkdownOutput", () => {
it("includes selected repo and file link", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("myorg/repoA");
expect(out).toContain("[src/foo.ts](https://github.com/myorg/repoA/blob/main/src/foo.ts)");
});
it("starts with # Results for query H1 heading", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out.split("\n")[0]).toBe(`# Results for "${QUERY}"`);
});
it("renders repo as bold bullet", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("- **myorg/repoA**");
expect(out).toContain("(1 match)");
});
it("renders plural match count in repo bullet", () => {
const groups = [makeGroup("myorg/repoA", ["src/a.ts", "src/b.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("(2 matches)");
});
it("emits ## section header when sectionLabel is set on the first repo of a section", () => {
const groups = [
{
...makeGroup("myorg/repoA", ["src/foo.ts"]),
sectionLabel: "squad-frontend",
},
makeGroup("myorg/repoB", ["src/bar.ts"]),
];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("## squad-frontend");
// repoB has no sectionLabel — no duplicate header
const headerCount = (out.match(/^## /gm) ?? []).length;
expect(headerCount).toBe(1);
});
it("emits a ## header per section when multiple sections are defined", () => {
const groups = [
{ ...makeGroup("myorg/repoA", ["a.ts"]), sectionLabel: "squad-frontend" },
{ ...makeGroup("myorg/repoB", ["b.ts"]), sectionLabel: "squad-mobile" },
];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("## squad-frontend");
expect(out).toContain("## squad-mobile");
const headerCount = (out.match(/^## /gm) ?? []).length;
expect(headerCount).toBe(2);
});
it("renders each file as an indented sub-bullet", () => {
const groups = [makeGroup("myorg/repoA", ["src/a.ts", "src/b.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain(" - [ ] [src/a.ts](https://github.com/myorg/repoA/blob/main/src/a.ts)");
expect(out).toContain(" - [ ] [src/b.ts](https://github.com/myorg/repoA/blob/main/src/b.ts)");
});
it("renders file links as markdown TODO list items (- [ ] [...])", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain(" - [ ] [src/foo.ts](");
});
it("renders file links with location as markdown TODO items", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 3, col: 5 }])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain(" - [ ] [src/foo.ts:3:5](");
});
it("omits deselected repos", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { repoSelected: false })];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).not.toContain("**myorg/repoA**");
});
it("omits repos where all extracts are deselected", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { extractSelected: [false] })];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).not.toContain("**myorg/repoA**");
});
it("always appends a replay command in a <details> block", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("<details>");
expect(out).toContain(
'<summary><a href="https://fulll.github.io/github-code-search/">github-code-search</a> replay command</summary>',
);
expect(out).toContain("```bash");
expect(out).toContain("</details>");
});
it("repo-only mode outputs only repo names without file links", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).toContain("myorg/repoA");
expect(out).not.toContain("[src/foo.ts]");
});
it("repo-only mode appends a replay command in a <details> block", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).toContain("<details>");
expect(out).toContain(
'<summary><a href="https://fulll.github.io/github-code-search/">github-code-search</a> replay command</summary>',
);
});
it("repo-only mode returns newline-terminated list of repo names followed by replay", () => {
const groups = [makeGroup("myorg/repoA", ["src/a.ts"]), makeGroup("myorg/repoB", ["src/b.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).toContain("myorg/repoA\nmyorg/repoB\n");
expect(out).toContain("<details>");
expect(out).toContain(
'<summary><a href="https://fulll.github.io/github-code-search/">github-code-search</a> replay command</summary>',
);
});
it("repo-only mode returns empty string when no groups are selected", () => {
const groups = [makeGroup("myorg/repoA", ["src/a.ts"], { repoSelected: false })];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).toBe("");
});
it("repo-only mode prepends H1 query title before repo list", () => {
const groups = [makeGroup("myorg/repoA", ["src/a.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).toContain(`# Results for "${QUERY}"`);
const lines = out.split("\n");
expect(lines[0]).toBe(`# Results for "${QUERY}"`);
expect(out.indexOf(`# Results for "${QUERY}"`)).toBeLessThan(out.indexOf("myorg/repoA"));
});
it("prepends selection summary line in repo-and-matches mode", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("selected");
const lines = out.split("\n");
// first line is the H1 query title
expect(lines[0]).toMatch(/^# Results for /);
// third line (index 2) is the selection summary
expect(lines[2]).toContain("selected");
});
it("does not prepend selection summary in repo-only mode", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).not.toContain("selected");
});
});
describe("buildJsonOutput", () => {
it("returns valid JSON", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildJsonOutput(groups, QUERY, ORG, new Set(), new Set());
expect(() => JSON.parse(out)).not.toThrow();
});
it("contains results array", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results).toHaveLength(1);
expect(parsed.results[0].repo).toBe("myorg/repoA");
expect(parsed.results[0].matches[0].path).toBe("src/foo.ts");
});
it("omits deselected repos from results", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts"], { repoSelected: false })];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results).toHaveLength(0);
});
it("includes query and org fields", () => {
const groups: RepoGroup[] = [];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.query).toBe(QUERY);
expect(parsed.org).toBe(ORG);
});
it("includes selection field with repo and match counts", () => {
const groups = [makeGroup("myorg/repoA", ["a.ts", "b.ts"])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.selection).toBeDefined();
expect(parsed.selection.repos).toBe(1);
expect(parsed.selection.matches).toBe(2);
});
it("selection field reflects partial deselections", () => {
const groups = [
makeGroup("myorg/repoA", ["a.ts", "b.ts"], {
extractSelected: [true, false],
}),
];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.selection.repos).toBe(1);
expect(parsed.selection.matches).toBe(1);
});
it("repo-only mode omits matches field", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const parsed = JSON.parse(
buildJsonOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only"),
);
expect(parsed.results[0].repo).toBe("myorg/repoA");
expect(parsed.results[0].matches).toBeUndefined();
});
});
// ─── segmentLineCol ───────────────────────────────────────────────────────────
describe("segmentLineCol", () => {
it("returns line 1 col 1 for offset 0", () => {
expect(segmentLineCol("hello world", 0)).toEqual({ line: 1, col: 1 });
});
it("counts lines correctly across newlines", () => {
const fragment = "line1\nline2\nline3";
// 'line2' starts at offset 6
expect(segmentLineCol(fragment, 6)).toEqual({ line: 2, col: 1 });
});
it("computes column within a line", () => {
const fragment = "abc\ndef";
// 'e' is at offset 5 → line 2, col 2
expect(segmentLineCol(fragment, 5)).toEqual({ line: 2, col: 2 });
});
it("handles offset at end of fragment", () => {
const fragment = "abc";
expect(segmentLineCol(fragment, 3)).toEqual({ line: 1, col: 4 });
});
});
// ─── VS Code path:line:col annotation in markdown ────────────────────────────
describe("buildMarkdownOutput — line/col annotation", () => {
it("formats file link as [path:line:col](url#Lline) when location is available", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 3, col: 5 }])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("[src/foo.ts:3:5](");
expect(out).toContain("#L3)");
});
it("uses plain [path](url) link when no text matches", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("[src/foo.ts](");
expect(out).not.toMatch(/\[src\/foo\.ts:\d+:\d+\]/);
});
it("appends matched token in backticks when location is available", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 3, col: 5 }])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set());
expect(out).toContain("`snippet`");
expect(out).toContain("#L3): `snippet`");
});
it("does not add location suffix in repo-only mode", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 3, col: 5 }])];
const out = buildMarkdownOutput(groups, QUERY, ORG, new Set(), new Set(), "repo-only");
expect(out).not.toContain("[src/foo.ts:3:5]");
});
});
// ─── JSON line/col fields ─────────────────────────────────────────────────────
describe("buildJsonOutput — line/col fields", () => {
it("includes line and col in match when text match is present", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 2, col: 8 }])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results[0].matches[0].line).toBe(2);
expect(parsed.results[0].matches[0].col).toBe(8);
});
it("omits line and col when no text matches", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results[0].matches[0].line).toBeUndefined();
expect(parsed.results[0].matches[0].col).toBeUndefined();
});
it("includes matchedText in match when text match is present", () => {
const groups = [makeGroupWithMatches("myorg/repoA", [{ path: "src/foo.ts", line: 2, col: 8 }])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results[0].matches[0].matchedText).toBe("snippet");
});
it("omits matchedText when no text matches", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results[0].matches[0].matchedText).toBeUndefined();
});
it("omits matchedText when seg.text is an empty string", () => {
// api.ts normalizes missing segment text to "" — matchedText must not be emitted
const groups: RepoGroup[] = [
{
repoFullName: "myorg/repoA",
matches: [
{
path: "src/foo.ts",
repoFullName: "myorg/repoA",
htmlUrl: "https://github.com/myorg/repoA/blob/main/src/foo.ts",
archived: false,
textMatches: [
{
fragment: "some snippet",
matches: [{ text: "", indices: [0, 0], line: 1, col: 1 }],
},
],
},
],
folded: true,
repoSelected: true,
extractSelected: [true],
},
];
const parsed = JSON.parse(buildJsonOutput(groups, QUERY, ORG, new Set(), new Set()));
expect(parsed.results[0].matches[0].matchedText).toBeUndefined();
});
});
// ─── buildOutput dispatcher ──────────────────────────────────────────────────
describe("buildOutput", () => {
it("dispatches to JSON when format is json", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "json");
expect(() => JSON.parse(out)).not.toThrow();
const parsed = JSON.parse(out);
expect(parsed.query).toBe(QUERY);
expect(parsed.results).toBeDefined();
});
it("dispatches to markdown when format is text", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "text");
expect(out).toContain("- **myorg/repoA**");
});
it("passes repo-only outputType to JSON dispatcher", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "json", "repo-only");
const parsed = JSON.parse(out);
expect(parsed.results[0].matches).toBeUndefined();
});
it("passes repo-only outputType to text dispatcher", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "text", "repo-only");
expect(out).not.toContain("[src/foo.ts]");
expect(out).toContain("myorg/repoA");
});
it("threads --format json into replay command", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(
groups,
QUERY,
ORG,
new Set(),
new Set(),
"markdown",
"repo-and-matches",
{ includeArchived: false, groupByTeamPrefix: "" },
);
expect(out).not.toContain("--format");
});
it("threads --output-type repo-only into markdown replay command", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "markdown", "repo-only", {
includeArchived: false,
groupByTeamPrefix: "",
});
expect(out).toContain("--output-type repo-only");
});
it("threads --include-archived into markdown replay command", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(
groups,
QUERY,
ORG,
new Set(),
new Set(),
"markdown",
"repo-and-matches",
{ includeArchived: true, groupByTeamPrefix: "" },
);
expect(out).toContain("--include-archived");
});
it("threads --group-by-team-prefix into json replay command", () => {
const groups = [makeGroup("myorg/repoA", ["src/foo.ts"])];
const out = buildOutput(groups, QUERY, ORG, new Set(), new Set(), "json", "repo-and-matches", {
includeArchived: false,
groupByTeamPrefix: "squad-",
});
const parsed = JSON.parse(out);
expect(parsed.replayCommand).toContain("--group-by-team-prefix 'squad-'");
});
});