-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdrift-scripts.test.ts
More file actions
498 lines (442 loc) · 16.9 KB
/
Copy pathdrift-scripts.test.ts
File metadata and controls
498 lines (442 loc) · 16.9 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
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { writeFileSync, readFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
// ---------------------------------------------------------------------------
// drift-sync.ts exports under test (C3: retargeted from the deleted
// scripts/fix-drift.ts — the LLM freewriter path, including buildPrompt and
// the predicate-gated CLI's parseMode, has been removed entirely).
// ---------------------------------------------------------------------------
import {
readDriftReport,
patchBumpVersion,
addChangelogEntry,
parsePorcelainLine,
todayStamp,
} from "../../scripts/drift-sync.js";
import { summarizeDriftReport } from "../../scripts/drift-slack-summary.js";
import type { DriftEntry, DriftReport, QuarantineEntry } from "../../scripts/drift-types.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function makeReport(overrides?: Partial<DriftReport>): DriftReport {
return {
timestamp: "2024-01-01T00:00:00.000Z",
entries: [
{
provider: "OpenAI Chat",
scenario: "non-streaming text",
builderFile: "src/helpers.ts",
builderFunctions: ["buildTextCompletion"],
typesFile: "src/types.ts",
sdkShapesFile: "src/__tests__/drift/sdk-shapes.ts",
diffs: [
{
severity: "critical",
issue: "LLMOCK DRIFT — field in SDK + real API but missing from mock",
path: "choices[0].message.refusal",
expected: "null",
real: "null",
mock: "<absent>",
},
],
},
],
...overrides,
};
}
// ---------------------------------------------------------------------------
// readDriftReport
// ---------------------------------------------------------------------------
describe("readDriftReport", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = mkdtempSync(join(tmpdir(), "drift-test-"));
});
afterEach(() => {
rmSync(tmpDir, { recursive: true, force: true });
});
it("throws when file does not exist", () => {
expect(() => readDriftReport(join(tmpDir, "nonexistent.json"))).toThrow(
/Drift report not found/,
);
});
it("throws when file contains invalid JSON", () => {
const path = join(tmpDir, "bad.json");
writeFileSync(path, "{ not valid json ]", "utf-8");
expect(() => readDriftReport(path)).toThrow(/is not valid JSON/);
});
it("throws when top-level structure lacks entries array", () => {
const path = join(tmpDir, "missing-entries.json");
writeFileSync(path, JSON.stringify({ timestamp: "2024-01-01", foo: "bar" }), "utf-8");
expect(() => readDriftReport(path)).toThrow(/invalid structure.*entries/);
});
it("throws when an entry is missing provider", () => {
const path = join(tmpDir, "bad-entry.json");
writeFileSync(
path,
JSON.stringify({
timestamp: "2024-01-01T00:00:00Z",
entries: [{ scenario: "x", diffs: [] }],
}),
"utf-8",
);
expect(() => readDriftReport(path)).toThrow(/missing required "provider"/);
});
it("throws when an entry has invalid severity", () => {
const path = join(tmpDir, "bad-severity.json");
const report = makeReport();
report.entries[0].diffs[0].severity = "banana" as never;
writeFileSync(path, JSON.stringify(report), "utf-8");
expect(() => readDriftReport(path)).toThrow(/invalid severity "banana"/);
});
it("returns a valid report", () => {
const path = join(tmpDir, "valid.json");
const report = makeReport();
writeFileSync(path, JSON.stringify(report), "utf-8");
const result = readDriftReport(path);
expect(result.entries).toHaveLength(1);
expect(result.entries[0].provider).toBe("OpenAI Chat");
});
});
// ---------------------------------------------------------------------------
// patchBumpVersion
// ---------------------------------------------------------------------------
describe("patchBumpVersion", () => {
let tmpDir: string;
let origCwd: string;
beforeEach(() => {
tmpDir = mkdtempSync(join(tmpdir(), "drift-test-"));
origCwd = process.cwd();
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(origCwd);
rmSync(tmpDir, { recursive: true, force: true });
});
it("increments the patch version", () => {
writeFileSync(join(tmpDir, "package.json"), JSON.stringify({ version: "1.2.3" }), "utf-8");
const newVersion = patchBumpVersion();
expect(newVersion).toBe("1.2.4");
});
it("writes the new version to package.json", () => {
writeFileSync(join(tmpDir, "package.json"), JSON.stringify({ version: "2.0.0" }), "utf-8");
patchBumpVersion();
const pkg = JSON.parse(readFileSync(join(tmpDir, "package.json"), "utf-8")) as {
version: string;
};
expect(pkg.version).toBe("2.0.1");
});
it("throws for non-semver version", () => {
writeFileSync(join(tmpDir, "package.json"), JSON.stringify({ version: "bad" }), "utf-8");
expect(() => patchBumpVersion()).toThrow(/Cannot patch-bump non-standard version/);
});
});
// ---------------------------------------------------------------------------
// addChangelogEntry
// ---------------------------------------------------------------------------
describe("addChangelogEntry", () => {
let tmpDir: string;
let origCwd: string;
beforeEach(() => {
tmpDir = mkdtempSync(join(tmpdir(), "drift-test-"));
origCwd = process.cwd();
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(origCwd);
rmSync(tmpDir, { recursive: true, force: true });
});
it("inserts entry after title line in existing changelog", () => {
const existing = "# @copilotkit/aimock\n\n## 1.0.0\n\nOld entry\n";
writeFileSync(join(tmpDir, "CHANGELOG.md"), existing, "utf-8");
addChangelogEntry(makeReport(), "1.2.4");
const content = readFileSync(join(tmpDir, "CHANGELOG.md"), "utf-8");
expect(content).toContain("## 1.2.4");
expect(content.indexOf("## 1.2.4")).toBeLessThan(content.indexOf("## 1.0.0"));
});
it("creates entry even when changelog is missing", () => {
addChangelogEntry(makeReport(), "1.0.1");
const content = readFileSync(join(tmpDir, "CHANGELOG.md"), "utf-8");
expect(content).toContain("## 1.0.1");
});
it("includes provider summaries", () => {
writeFileSync(join(tmpDir, "CHANGELOG.md"), "# @copilotkit/aimock\n", "utf-8");
addChangelogEntry(makeReport(), "1.2.4");
const content = readFileSync(join(tmpDir, "CHANGELOG.md"), "utf-8");
expect(content).toContain("OpenAI Chat (non-streaming text)");
expect(content).toContain("choices[0].message.refusal");
});
});
// ---------------------------------------------------------------------------
// parsePorcelainLine
// ---------------------------------------------------------------------------
describe("parsePorcelainLine", () => {
it("parses a plain modified file", () => {
expect(parsePorcelainLine(" M src/helpers.ts")).toBe("src/helpers.ts");
});
it("unquotes paths with special characters", () => {
expect(parsePorcelainLine(' M "src/path with spaces.ts"')).toBe("src/path with spaces.ts");
});
it("handles rename notation by returning the new path", () => {
expect(parsePorcelainLine(" R src/old.ts -> src/new.ts")).toBe("src/new.ts");
});
it("handles added files", () => {
expect(parsePorcelainLine("?? src/new-file.ts")).toBe("src/new-file.ts");
});
});
// ---------------------------------------------------------------------------
// todayStamp
// ---------------------------------------------------------------------------
describe("todayStamp", () => {
it("returns an ISO date string", () => {
expect(todayStamp()).toMatch(/^\d{4}-\d{2}-\d{2}$/);
});
});
// ---------------------------------------------------------------------------
// summarizeDriftReport (Slack detail)
// ---------------------------------------------------------------------------
function makeEntry(overrides?: Partial<DriftEntry>): DriftEntry {
return {
provider: "OpenAI Chat",
scenario: "non-streaming text",
builderFile: "src/helpers.ts",
builderFunctions: ["buildTextCompletion"],
typesFile: "src/types.ts",
sdkShapesFile: "src/__tests__/drift/sdk-shapes.ts",
diffs: [
{
severity: "critical",
issue: "field missing from mock",
path: "choices[0].message.refusal",
expected: "null",
real: "null",
mock: "<absent>",
},
],
...overrides,
};
}
describe("summarizeDriftReport", () => {
it("returns an empty string when there are no entries", () => {
expect(summarizeDriftReport({ timestamp: "t", entries: [] })).toBe("");
});
it("names the drifted provider, severity tally, and a changed path", () => {
const summary = summarizeDriftReport({ timestamp: "t", entries: [makeEntry()] });
expect(summary).toContain("*OpenAI Chat*");
expect(summary).toContain("1 critical");
expect(summary).toContain("`choices[0].message.refusal`");
// The first line is now the classification header; bullet lines follow.
const bulletLines = summary.split("\n").filter((l) => l.startsWith("•"));
expect(bulletLines.length).toBeGreaterThan(0);
});
it("merges multiple entries for the same provider into one line with combined counts", () => {
const summary = summarizeDriftReport({
timestamp: "t",
entries: [
makeEntry(),
makeEntry({
scenario: "streaming text",
diffs: [
{
severity: "warning",
issue: "type changed",
path: "choices[0].delta.role",
expected: "string",
real: "string",
mock: "number",
},
],
}),
],
});
// One bullet line for the single provider
expect(summary.split("\n").filter((l) => l.startsWith("• *OpenAI Chat*"))).toHaveLength(1);
expect(summary).toContain("1 critical, 1 warning");
});
it("lists multiple providers on separate lines, critical-first", () => {
const summary = summarizeDriftReport({
timestamp: "t",
entries: [
makeEntry({
provider: "Anthropic",
diffs: [
{
severity: "warning",
issue: "x",
path: "content[0].type",
expected: "a",
real: "a",
mock: "b",
},
],
}),
makeEntry(), // OpenAI Chat with a critical
],
});
// First line is the classification header; provider bullets follow.
const bulletLines = summary.split("\n").filter((l) => l.startsWith("•"));
expect(bulletLines).toHaveLength(2);
// Provider with a critical diff sorts before the warning-only provider
expect(bulletLines[0]).toContain("*OpenAI Chat*");
expect(bulletLines[1]).toContain("*Anthropic*");
});
it("caps example paths per provider and reports the remainder", () => {
const manyPaths = Array.from({ length: 6 }, (_, i) => ({
severity: "critical" as const,
issue: "x",
path: `field.${i}`,
expected: "a",
real: "a",
mock: "b",
}));
const summary = summarizeDriftReport({
timestamp: "t",
entries: [makeEntry({ diffs: manyPaths })],
});
expect(summary).toContain("`field.0`");
expect(summary).toContain("`field.2`");
expect(summary).not.toContain("`field.3`");
expect(summary).toContain("+3 more");
});
it("uses real newlines (not literal backslash-n) between providers", () => {
const summary = summarizeDriftReport({
timestamp: "t",
entries: [makeEntry(), makeEntry({ provider: "Anthropic" })],
});
expect(summary).toContain("\n");
expect(summary).not.toContain("\\n");
});
});
// ---------------------------------------------------------------------------
// summarizeDriftReport — headline class + per-item detail (C5.2)
// ---------------------------------------------------------------------------
function makeQuarantineEntry(overrides?: Partial<QuarantineEntry>): QuarantineEntry {
return {
provider: "OpenAI Chat",
testName: "OpenAI Chat > non-streaming text > response shape",
rawLocation: "src/__tests__/drift/openai-chat.drift.ts:42",
message: "Cannot read properties of undefined (reading 'choices')",
...overrides,
};
}
describe("summarizeDriftReport — headline class", () => {
it("class: real-drift — report has entries with critical diffs", () => {
const report: DriftReport = {
timestamp: "t",
entries: [
makeEntry({
diffs: [
{
severity: "critical",
issue: "field missing from mock",
path: "choices[0].message.refusal",
expected: "null",
real: "null",
mock: "<absent>",
},
],
}),
],
};
const summary = summarizeDriftReport(report);
expect(summary).toContain("real-drift");
// per-item: provider name present
expect(summary).toContain("OpenAI Chat");
// per-item: offending path or id
expect(summary).toContain("choices[0].message.refusal");
// per-item: one-line issue
expect(summary).toContain("field missing from mock");
// per-item: file reference (builderFile)
expect(summary).toContain("src/helpers.ts");
});
it("class: quarantine — report has quarantine[] entries", () => {
const report: DriftReport = {
timestamp: "t",
entries: [],
quarantine: [makeQuarantineEntry()],
};
const summary = summarizeDriftReport(report);
expect(summary).toContain("quarantine");
// quarantine: provider
expect(summary).toContain("OpenAI Chat");
// quarantine: rawLocation (file:line)
expect(summary).toContain("src/__tests__/drift/openai-chat.drift.ts:42");
// quarantine: one-line message
expect(summary).toContain("Cannot read properties of undefined");
});
it("class: stale-key — InfraError status 401", () => {
const report: DriftReport = { timestamp: "t", entries: [] };
const summary = summarizeDriftReport(report, { infraErrorStatus: 401 });
expect(summary).toContain("stale-key");
});
it("class: stale-key — InfraError status 403", () => {
const report: DriftReport = { timestamp: "t", entries: [] };
const summary = summarizeDriftReport(report, { infraErrorStatus: 403 });
expect(summary).toContain("stale-key");
});
it("class: infra-transient — InfraError status 429", () => {
const report: DriftReport = { timestamp: "t", entries: [] };
const summary = summarizeDriftReport(report, { infraErrorStatus: 429 });
expect(summary).toContain("infra-transient");
});
it("class: infra-transient — InfraError status 503", () => {
const report: DriftReport = { timestamp: "t", entries: [] };
const summary = summarizeDriftReport(report, { infraErrorStatus: 503 });
expect(summary).toContain("infra-transient");
});
it("class: test-infra-false-positive — exit 0, empty entries, no infraError", () => {
const report: DriftReport = { timestamp: "t", entries: [] };
const summary = summarizeDriftReport(report, { exitCode: 0 });
expect(summary).toContain("test-infra-false-positive");
});
it("quarantine entries appear on their own distinct line with testName", () => {
const report: DriftReport = {
timestamp: "t",
entries: [],
quarantine: [
makeQuarantineEntry({ provider: "Anthropic", testName: "Anthropic > streaming > shape" }),
],
};
const summary = summarizeDriftReport(report);
const lines = summary.split("\n");
const quarantineLine = lines.find((l) => l.includes("Anthropic"));
expect(quarantineLine).toBeDefined();
expect(quarantineLine).toContain("Anthropic > streaming > shape");
});
it("mixed: real-drift entries + quarantine both appear in summary", () => {
const report: DriftReport = {
timestamp: "t",
entries: [makeEntry()],
quarantine: [makeQuarantineEntry({ provider: "Gemini" })],
};
const summary = summarizeDriftReport(report);
// Class is real-drift when entries have critical diffs (quarantine is secondary)
expect(summary).toContain("real-drift");
// Quarantine section still present
expect(summary).toContain("Gemini");
expect(summary).toContain("quarantine");
});
it("per-item id is used over path when present", () => {
const report: DriftReport = {
timestamp: "t",
entries: [
makeEntry({
diffs: [
{
severity: "critical",
issue: "model removed from mock",
path: "knownModels",
id: "gpt-4o-mini",
expected: "present",
real: "present",
mock: "<absent>",
},
],
}),
],
};
const summary = summarizeDriftReport(report);
expect(summary).toContain("gpt-4o-mini");
});
});