forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixtures.ts
More file actions
174 lines (146 loc) · 4.49 KB
/
Copy pathfixtures.ts
File metadata and controls
174 lines (146 loc) · 4.49 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
import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { createLogger } from "../logger.js";
const log = createLogger({ component: "fixtures" });
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SHOWCASE_DIR = path.resolve(__dirname, "../../..");
const AIMOCK_DIR = path.join(SHOWCASE_DIR, "aimock");
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface FixtureEntry {
match?: Record<string, unknown>;
response?: Record<string, unknown>;
}
interface FixtureFile {
fixtures?: FixtureEntry[];
}
interface ValidationError {
file: string;
message: string;
}
export interface ValidationReport {
ok: number;
errors: ValidationError[];
}
// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------
/**
* Validate all fixture JSON files under showcase/aimock/.
*
* Checks:
* - Valid JSON parsing
* - Required top-level `fixtures` array
* - Each entry has `match` and `response` fields
* - No duplicate sequence keys (userMessage + turnIndex combos)
*/
export function fixturesValidate(): ValidationReport {
const report: ValidationReport = { ok: 0, errors: [] };
if (!fs.existsSync(AIMOCK_DIR)) {
report.errors.push({
file: AIMOCK_DIR,
message: "aimock directory not found",
});
return report;
}
const jsonFiles = fs
.readdirSync(AIMOCK_DIR)
.filter((f) => f.endsWith(".json"));
if (jsonFiles.length === 0) {
log.warn("no JSON fixture files found", { dir: AIMOCK_DIR });
return report;
}
for (const file of jsonFiles) {
const filePath = path.join(AIMOCK_DIR, file);
// 1. Valid JSON?
let parsed: FixtureFile;
try {
const raw = fs.readFileSync(filePath, "utf-8");
parsed = JSON.parse(raw) as FixtureFile;
} catch (err) {
report.errors.push({
file,
message: `Invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
});
continue;
}
// 2. Has fixtures array?
if (!Array.isArray(parsed.fixtures)) {
report.errors.push({
file,
message: 'Missing or non-array "fixtures" field',
});
continue;
}
// 3. Validate each entry and track sequence keys for duplicates
const seenKeys = new Set<string>();
let fileOk = true;
for (let i = 0; i < parsed.fixtures.length; i++) {
const entry = parsed.fixtures[i];
if (!entry.match || typeof entry.match !== "object") {
report.errors.push({
file,
message: `fixtures[${i}]: missing or invalid "match" field`,
});
fileOk = false;
continue;
}
if (!entry.response || typeof entry.response !== "object") {
report.errors.push({
file,
message: `fixtures[${i}]: missing or invalid "response" field`,
});
fileOk = false;
continue;
}
// 4. Check for duplicate sequence keys (userMessage + turnIndex)
const userMessage = entry.match.userMessage;
const turnIndex = entry.match.turnIndex;
const toolCallId = entry.match.toolCallId;
let seqKey: string | null = null;
if (typeof userMessage === "string") {
seqKey =
turnIndex !== undefined
? `msg:${userMessage}@turn:${turnIndex}`
: `msg:${userMessage}`;
} else if (typeof toolCallId === "string") {
seqKey = `toolCallId:${toolCallId}`;
}
if (seqKey !== null) {
if (seenKeys.has(seqKey)) {
report.errors.push({
file,
message: `fixtures[${i}]: duplicate sequence key "${seqKey}"`,
});
fileOk = false;
} else {
seenKeys.add(seqKey);
}
}
}
if (fileOk) {
report.ok++;
}
}
return report;
}
/**
* Format a validation report as a human-readable string.
*/
export function formatReport(report: ValidationReport): string {
const lines: string[] = [];
lines.push(
`\n Fixture validation: ${report.ok} OK, ${report.errors.length} error(s)`,
);
if (report.errors.length > 0) {
lines.push("");
for (const err of report.errors) {
lines.push(` \x1b[31m✗\x1b[0m ${err.file}: ${err.message}`);
}
} else {
lines.push(" \x1b[32m✓ All fixture files valid\x1b[0m");
}
return lines.join("\n");
}