-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindingsView.test.ts
More file actions
490 lines (449 loc) · 17.6 KB
/
findingsView.test.ts
File metadata and controls
490 lines (449 loc) · 17.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
import { describe, it, expect, vi, beforeEach } from "vitest";
// The shared vscode stub in src/__testStubs__/vscode.ts covers the
// surface findingsView reaches into. The async factory below is the
// only safe way to share it: vi.mock hoists above imports and the
// factory cannot reference outer-scope bindings synchronously.
vi.mock("vscode", async () => {
const { vscodeStub } = await import("./__testStubs__/vscode");
return vscodeStub();
});
// Import after the mock is registered.
import { FindingsTreeProvider } from "./findingsView";
const ctx = {
subscriptions: [] as Array<{ dispose: () => void }>,
} as unknown as import("vscode").ExtensionContext;
// `vscode.languages.getDiagnostics()` returns `[uri, diagnostic[]][]`.
// We build that shape from a compact (severity, file, rule) triple so
// every test stays readable.
type FakeFinding = {
file: string;
rule: string;
severity?: string;
line?: number;
docsUrl?: string;
};
function setStubDiagnostics(findings: FakeFinding[]): void {
const grouped = new Map<string, unknown[]>();
for (const f of findings) {
const key = `file:///${f.file}`;
const arr = grouped.get(key) ?? [];
arr.push({
source: "pipeline-check",
message: `${f.rule} title\n\nThe long description.\n\nFix: do X.`,
code: {
value: f.rule,
target: { toString: () => f.docsUrl ?? "" },
},
range: {
start: { line: f.line ?? 0, character: 0 },
end: { line: f.line ?? 0, character: 0 },
},
severity: 0,
data: f.severity ? { severity: f.severity } : undefined,
});
grouped.set(key, arr);
}
const out: Array<[unknown, unknown[]]> = [];
for (const [uri, diags] of grouped) {
// Match the shape returned by `languages.getDiagnostics()`: a list
// of [Uri, Diagnostic[]] pairs.
out.push([
{
toString: () => uri,
path: `/${uri.split("///")[1]}`,
fsPath: `/${uri.split("///")[1]}`,
},
diags,
]);
}
(globalThis as { __stubDiagnostics?: unknown }).__stubDiagnostics = out;
}
beforeEach(() => {
(globalThis as { __stubDiagnostics?: unknown }).__stubDiagnostics = [];
});
describe("FindingsTreeProvider — collection from diagnostics", () => {
it("ignores diagnostics whose source is not pipeline-check", () => {
const noise = {
toString: () => "file:///workflows/ci.yml",
path: "/workflows/ci.yml",
fsPath: "/workflows/ci.yml",
};
(globalThis as { __stubDiagnostics?: unknown }).__stubDiagnostics = [
[
noise,
[
{
source: "eslint",
message: "not us",
code: "ESLINT-001",
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
},
severity: 0,
},
],
],
];
const p = new FindingsTreeProvider(ctx);
const roots = p.getChildren();
expect(roots).toEqual([]);
});
it("returns an empty tree when there are no findings", () => {
setStubDiagnostics([]);
const p = new FindingsTreeProvider(ctx);
expect(p.getChildren()).toEqual([]);
});
});
describe("FindingsTreeProvider — group by severity", () => {
it("orders buckets CRITICAL → HIGH → MEDIUM → LOW → INFO and reports counts", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "LOW" },
{ file: "a.yml", rule: "GHA-002", severity: "CRITICAL" },
{ file: "b.yml", rule: "GHA-003", severity: "HIGH" },
{ file: "b.yml", rule: "GHA-004", severity: "CRITICAL" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const labels = roots.map((n) => (n.kind === "group" ? n.label : ""));
expect(labels).toEqual(["CRITICAL", "HIGH", "LOW"]);
const counts = roots.map((n) => (n.kind === "group" ? n.description : ""));
expect(counts).toEqual(["2", "1", "1"]);
});
it("falls back to INFO for missing/unknown severity", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001" }, // missing
{ file: "a.yml", rule: "GHA-002", severity: "BOGUS" }, // unknown
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const labels = roots.map((n) => (n.kind === "group" ? n.label : ""));
expect(labels).toEqual(["INFO"]);
expect(roots[0].kind === "group" && roots[0].children).toHaveLength(2);
});
it("normalises lowercase data.severity to uppercase", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "high" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
expect(roots.map((n) => (n.kind === "group" ? n.label : ""))).toEqual([
"HIGH",
]);
});
});
describe("FindingsTreeProvider — group by file", () => {
it("buckets findings by URI, sorted alphabetically", () => {
setStubDiagnostics([
{ file: "z/last.yml", rule: "X", severity: "LOW" },
{ file: "a/first.yml", rule: "Y", severity: "LOW" },
{ file: "a/first.yml", rule: "Z", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("file");
const roots = p.getChildren();
const labels = roots.map((n) => (n.kind === "group" ? n.label : ""));
expect(labels).toEqual(["first.yml", "last.yml"]);
expect(roots[0].kind === "group" && roots[0].description).toBe("2");
expect(roots[1].kind === "group" && roots[1].description).toBe("1");
});
it("carries the workspace-relative path on the group tooltip", () => {
// U11: description is count-only so the right edge of the tree
// scans uniformly. The disambiguator for same-basename files in
// different directories (workflows/release.yml vs
// pipelines/release.yml) moves to the tooltip.
setStubDiagnostics([
{ file: "workflows/release.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("file");
const roots = p.getChildren();
const item = p.getTreeItem(roots[0]);
expect(item.tooltip).toBe("/workflows/release.yml");
});
});
describe("FindingsTreeProvider — group by rule", () => {
it("buckets findings by rule id, sorted alphabetically", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-002", severity: "LOW" },
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
{ file: "b.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("rule");
const roots = p.getChildren();
const labels = roots.map((n) => (n.kind === "group" ? n.label : ""));
expect(labels).toEqual(["GHA-001", "GHA-002"]);
expect(roots[0].kind === "group" && roots[0].description).toBe("2");
expect(roots[1].kind === "group" && roots[1].description).toBe("1");
});
it("places missing rule ids under '(unknown rule)'", () => {
setStubDiagnostics([{ file: "a.yml", rule: "", severity: "LOW" }]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("rule");
const roots = p.getChildren();
expect(roots.map((n) => (n.kind === "group" ? n.label : ""))).toEqual([
"(unknown rule)",
]);
});
it("picks the icon from the maximum severity in the bucket", () => {
// U6: rule groups previously took items[0].severity after a sort
// that ordered by file path + line — totally unrelated to
// severity. A rule with one CRITICAL and four LOW findings could
// render as the LOW icon if the CRITICAL was on the lexicographically-
// last file. maxSeverity aggregation pins the icon to the worst case
// so the tree's at-every-depth severity signal stays honest.
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "LOW" },
{ file: "z.yml", rule: "GHA-001", severity: "CRITICAL" },
{ file: "a.yml", rule: "GHA-001", severity: "LOW" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("rule");
const roots = p.getChildren();
const icon = roots[0].kind === "group" ? roots[0].icon : undefined;
expect((icon as { id: string }).id).toBe("flame");
});
});
describe("FindingsTreeProvider — leaf items", () => {
it("getTreeItem on a group node sets the expected description and contextValue", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
{ file: "a.yml", rule: "GHA-002", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
expect(roots.length).toBe(1);
const item = p.getTreeItem(roots[0]);
expect(item.description).toBe("2");
expect(item.contextValue).toBe("pipelineCheck.group");
});
it("leaf label is the title (first line); rule id and location ride on the description", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH", line: 22 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(leaves.length).toBe(1);
const item = p.getTreeItem(leaves[0]);
expect(item.label).toBe("GHA-001 title");
expect(item.contextValue).toBe("pipelineCheck.finding");
expect(item.description).toBe("GHA-001 · a.yml:23");
const cmd = (item as { command?: { command: string } }).command;
expect(cmd?.command).toBe("vscode.open");
});
});
describe("FindingsTreeProvider — adaptive leaf description", () => {
// The description column avoids repeating information the parent
// group already carries. Keeps the visual rhythm consistent across
// group modes.
it("severity-mode shows 'RULE · file:line' (group carries the severity)", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH", line: 4 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(p.getTreeItem(leaves[0]).description).toBe("GHA-001 · a.yml:5");
});
it("file-mode shows 'RULE · Lline' (group carries the file)", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH", line: 4 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("file");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(p.getTreeItem(leaves[0]).description).toBe("GHA-001 · L5");
});
it("rule-mode shows 'file:line' (group carries the rule)", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH", line: 4 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("rule");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(p.getTreeItem(leaves[0]).description).toBe("a.yml:5");
});
it("converts the 0-based LSP line to 1-based display", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH", line: 0 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("file");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(p.getTreeItem(leaves[0]).description).toBe("GHA-001 · L1");
});
it("omits the rule id from the description when none is present", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "", severity: "HIGH", line: 4 },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
expect(p.getTreeItem(leaves[0]).description).toBe("a.yml:5");
});
});
describe("FindingsTreeProvider — activity-bar badge", () => {
// setTreeView wires the badge; refresh() and the diagnostic-change
// listener both drive updates. Tests pin down the contract that the
// badge tracks the visible-finding count and clears to undefined
// when the workspace is clean.
function fakeTreeView(): { badge: unknown } & object {
return { badge: undefined };
}
it("setTreeView seeds the badge with the current finding count", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
{ file: "a.yml", rule: "GHA-002", severity: "LOW" },
]);
const p = new FindingsTreeProvider(ctx);
const view = fakeTreeView();
p.setTreeView(view as unknown as Parameters<typeof p.setTreeView>[0]);
expect((view.badge as { value: number }).value).toBe(2);
});
it("clears the badge to undefined when there are no findings", () => {
setStubDiagnostics([]);
const p = new FindingsTreeProvider(ctx);
const view = fakeTreeView();
p.setTreeView(view as unknown as Parameters<typeof p.setTreeView>[0]);
expect(view.badge).toBeUndefined();
});
it("singular/plural form of the badge tooltip", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
const view = fakeTreeView();
p.setTreeView(view as unknown as Parameters<typeof p.setTreeView>[0]);
expect((view.badge as { tooltip: string }).tooltip).toBe(
"1 Pipeline-Check finding",
);
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
{ file: "a.yml", rule: "GHA-002", severity: "LOW" },
]);
p.refresh();
expect((view.badge as { tooltip: string }).tooltip).toBe(
"2 Pipeline-Check findings",
);
});
it("refresh() before setTreeView is a no-op (does not throw)", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
// No setTreeView call here — the pre-wiring window must be safe.
expect(() => p.refresh()).not.toThrow();
});
});
describe("FindingsTreeProvider — group mode behaviour", () => {
it("setGroupMode of the same mode is a no-op (no refresh storm)", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
let fires = 0;
p.onDidChangeTreeData(() => {
fires += 1;
});
p.setGroupMode("severity");
p.setGroupMode("severity");
expect(fires).toBe(0);
p.setGroupMode("file");
expect(fires).toBe(1);
});
it("getGroupMode reflects the most recent setGroupMode call", () => {
// The Quick Pick prompt in extension.ts reads getGroupMode() to
// mark the active row with $(check). If the getter ever drifted
// from the field, the Quick Pick would lie. Pinned here.
const p = new FindingsTreeProvider(ctx);
expect(p.getGroupMode()).toBe("severity"); // default
p.setGroupMode("rule");
expect(p.getGroupMode()).toBe("rule");
p.setGroupMode("file");
expect(p.getGroupMode()).toBe("file");
});
});
describe("FindingsTreeProvider — rule docs link in tooltip", () => {
// When the server publishes ``Diagnostic.code.target`` (the rule's
// documentation URL), the leaf tooltip should carry a clickable
// "Read more" link below the message body. When the URL is absent
// or empty, the tooltip is just the message. The link target is
// exactly what the server published — we don't synthesise URLs.
it("appends a docs link when the server publishes one", () => {
setStubDiagnostics([
{
file: "a.yml",
rule: "GHA-001",
severity: "HIGH",
docsUrl: "https://example.com/rules/gha-001",
},
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
const item = p.getTreeItem(leaves[0]);
const tip = item.tooltip as { value: string; isTrusted: boolean };
expect(tip.value).toContain("GHA-001 title");
expect(tip.value).toContain(
"[$(book) GHA-001 documentation](https://example.com/rules/gha-001)",
);
expect(tip.isTrusted).toBe(true);
});
it("leaves the tooltip clean when the server publishes no URL", () => {
setStubDiagnostics([
{
file: "a.yml",
rule: "GHA-001",
severity: "HIGH",
// no docsUrl
},
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
const roots = p.getChildren();
const leaves = p.getChildren(roots[0]);
const item = p.getTreeItem(leaves[0]);
const tip = item.tooltip as { value: string };
expect(tip.value).toContain("GHA-001 title");
expect(tip.value).not.toContain("documentation");
});
});
describe("FindingsTreeProvider — findings cache invalidation", () => {
// refresh() drops the cached findings list so the next render sees
// any new diagnostic publishes. Without invalidation, a freshly
// published finding wouldn't appear in the tree until the user
// toggled the group mode or restarted VS Code. The test exercises
// the path end-to-end: render once, swap the stub data, refresh,
// render again.
it("refresh() picks up newly-published diagnostics", () => {
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
]);
const p = new FindingsTreeProvider(ctx);
p.setGroupMode("severity");
expect(p.getChildren()).toHaveLength(1);
setStubDiagnostics([
{ file: "a.yml", rule: "GHA-001", severity: "HIGH" },
{ file: "b.yml", rule: "GHA-002", severity: "CRITICAL" },
]);
// Without refresh() the second publish would be invisible.
p.refresh();
const roots = p.getChildren();
expect(roots).toHaveLength(2);
// CRITICAL is leftmost in the sort.
expect(roots[0].kind === "group" && roots[0].label).toBe("CRITICAL");
});
});