-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.test.ts
More file actions
164 lines (146 loc) · 5.54 KB
/
formatting.test.ts
File metadata and controls
164 lines (146 loc) · 5.54 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
import { describe, it, expect, vi, beforeEach } from "vitest";
import { formatAnalysisStatus, resolveToolUuids } from "./formatting";
// Mock ansis to return raw text for easier testing
vi.mock("ansis", () => ({
default: {
dim: (s: string) => s,
blueBright: (s: string) => s,
yellow: (s: string) => s,
bold: (s: string) => s,
red: (s: string) => s,
green: (s: string) => s,
blue: (s: string) => s,
hex: () => (s: string) => s,
white: (s: string) => s,
magenta: (s: string) => s,
},
}));
describe("formatAnalysisStatus", () => {
it("should show 'Finished' when analysis is complete and no coverage expected", () => {
const result = formatAnalysisStatus({
commitSha: "abc1234567890",
startedAnalysis: "2025-06-15T10:00:00Z",
endedAnalysis: "2025-06-15T10:05:00Z",
expectsCoverage: false,
hasCoverageData: false,
});
expect(result).toContain("Finished");
expect(result).toContain("abc1234");
});
it("should show 'In progress...' for first analysis", () => {
const result = formatAnalysisStatus({
commitSha: "def5678901234",
startedAnalysis: "2025-06-15T10:00:00Z",
endedAnalysis: undefined,
expectsCoverage: false,
hasCoverageData: false,
});
expect(result).toContain("In progress...");
expect(result).toContain("def5678");
});
it("should show 'Reanalysis in progress...' when reanalysis is running", () => {
const result = formatAnalysisStatus({
commitSha: "abc1234567890",
startedAnalysis: "2025-06-15T12:00:00Z",
endedAnalysis: "2025-06-15T10:05:00Z",
expectsCoverage: false,
hasCoverageData: false,
});
expect(result).toContain("Reanalysis in progress...");
expect(result).toContain("Finished");
expect(result).toContain("abc1234");
});
it("should show 'Waiting for coverage reports...' when coverage expected within 3h", () => {
const recentEnd = new Date(Date.now() - 60 * 60 * 1000).toISOString(); // 1h ago
const result = formatAnalysisStatus({
commitSha: "cov1234567890",
startedAnalysis: "2025-06-15T10:00:00Z",
endedAnalysis: recentEnd,
expectsCoverage: true,
hasCoverageData: false,
});
expect(result).toContain("Waiting for coverage reports...");
expect(result).toContain("cov1234");
});
it("should show 'Missing coverage reports' when coverage expected after 3h", () => {
const oldEnd = new Date(Date.now() - 4 * 60 * 60 * 1000).toISOString(); // 4h ago
const result = formatAnalysisStatus({
commitSha: "old1234567890",
startedAnalysis: "2025-06-15T10:00:00Z",
endedAnalysis: oldEnd,
expectsCoverage: true,
hasCoverageData: false,
});
expect(result).toContain("Missing coverage reports");
expect(result).toContain("old1234");
});
it("should show 'Never' when no analysis data", () => {
const result = formatAnalysisStatus({
commitSha: "abc1234567890",
startedAnalysis: undefined,
endedAnalysis: undefined,
expectsCoverage: false,
hasCoverageData: false,
});
expect(result).toBe("Never");
});
});
describe("resolveToolUuids", () => {
const mockTools = [
{ uuid: "uuid-eslint", name: "ESLint", shortName: "eslint", prefix: "ESLint_" },
{ uuid: "uuid-eslint9", name: "ESLint 9", shortName: "eslint9", prefix: "ESLint9_" },
{ uuid: "uuid-semgrep", name: "Semgrep", shortName: "semgrep", prefix: "Semgrep_" },
{ uuid: "uuid-markdownlint", name: "Markdownlint", shortName: "markdownlint", prefix: "Markdownlint_" },
{ uuid: "uuid-remarklint", name: "Remarklint", shortName: "remarklint", prefix: "Remarklint_" },
] as any[];
const fetchTools = vi.fn(async () => mockTools);
beforeEach(() => {
fetchTools.mockClear();
});
it("should pass UUIDs through without fetching tools", async () => {
const result = await resolveToolUuids(
["a1b2c3d4-e5f6-7890-abcd-ef1234567890"],
fetchTools,
);
expect(result).toEqual(["a1b2c3d4-e5f6-7890-abcd-ef1234567890"]);
expect(fetchTools).not.toHaveBeenCalled();
});
it("should resolve exact name match (case-insensitive)", async () => {
const result = await resolveToolUuids(["eslint"], fetchTools);
expect(result).toEqual(["uuid-eslint"]);
});
it("should resolve exact shortName match (case-insensitive)", async () => {
const result = await resolveToolUuids(["eslint9"], fetchTools);
expect(result).toEqual(["uuid-eslint9"]);
});
it("should resolve a unique substring match via name", async () => {
const result = await resolveToolUuids(["semgr"], fetchTools);
expect(result).toEqual(["uuid-semgrep"]);
});
it("should error on ambiguous substring match", async () => {
await expect(resolveToolUuids(["mark"], fetchTools)).rejects.toThrow(
/ambiguous.*Markdownlint.*Remarklint/,
);
});
it("should error when tool is not found", async () => {
await expect(resolveToolUuids(["zzz"], fetchTools)).rejects.toThrow(
'Tool "zzz" not found',
);
});
it("should deduplicate resolved UUIDs", async () => {
const result = await resolveToolUuids(["eslint", "eslint"], fetchTools);
expect(result).toEqual(["uuid-eslint"]);
});
it("should handle mixed UUIDs and names, fetching tools only once", async () => {
const result = await resolveToolUuids(
["a1b2c3d4-e5f6-7890-abcd-ef1234567890", "semgrep", "eslint"],
fetchTools,
);
expect(result).toEqual([
"a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"uuid-semgrep",
"uuid-eslint",
]);
expect(fetchTools).toHaveBeenCalledTimes(1);
});
});