forked from rockcarver/frodo-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-e2e-audit.mjs
More file actions
219 lines (192 loc) · 6.91 KB
/
Copy pathagent-e2e-audit.mjs
File metadata and controls
219 lines (192 loc) · 6.91 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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
const ROOT = process.cwd();
const TEST_DIR = path.join(ROOT, 'test/e2e');
const SNAP_DIR = path.join(ROOT, 'test/e2e/__snapshots__');
const MOCKS_DIR = path.join(ROOT, 'test/e2e/mocks');
const TEST_FILES = fs
.readdirSync(TEST_DIR)
.filter((f) => (f.includes('agent') || f === 'config-manager-export-oauth2-agents.e2e.test.js') && f.endsWith('.e2e.test.js'))
.sort();
const ERROR_PATTERNS = [
/\[Polly\].*Recording for the following request is not found/i,
/Error getting tokens/i,
/Command failed:/i,
/TypeError:/,
/ReferenceError:/,
/SyntaxError:/,
/ENOENT:/,
/Error exporting/i,
];
function walkDirs(dir, predicate) {
const out = [];
if (!fs.existsSync(dir)) return out;
const stack = [dir];
while (stack.length) {
const cur = stack.pop();
for (const entry of fs.readdirSync(cur)) {
const full = path.join(cur, entry);
const st = fs.statSync(full);
if (st.isDirectory()) {
if (predicate(full, entry)) out.push(full);
stack.push(full);
}
}
}
return out;
}
function countHarFiles(dir) {
let count = 0;
const stack = [dir];
while (stack.length) {
const cur = stack.pop();
for (const entry of fs.readdirSync(cur)) {
const full = path.join(cur, entry);
const st = fs.statSync(full);
if (st.isDirectory()) stack.push(full);
else if (entry.endsWith('.har')) count += 1;
}
}
return count;
}
function parseCommands(testText) {
return [...testText.matchAll(/const\s+CMD\s*=\s*`([^`]+)`/g)].map((m) => m[1]);
}
function getSuiteStem(testFile) {
return testFile.replace('.e2e.test.js', '');
}
function findMatchingMockDirs(stem, commands) {
if (stem.startsWith('agent-')) {
const agentRoot = walkDirs(MOCKS_DIR, (_, entry) => /^agent_\d+$/.test(entry))[0];
if (!agentRoot) return [];
const entries = fs.readdirSync(agentRoot).filter((d) => fs.statSync(path.join(agentRoot, d)).isDirectory());
const op = stem.replace('agent-', '');
if (!op.includes('-')) {
return entries.filter((d) => d.startsWith(`${op}_`)).map((d) => path.join(agentRoot, d));
}
if (op.startsWith('web-')) return entries.filter((d) => d.startsWith(`web-${op.replace('web-', '')}`)).map((d) => path.join(agentRoot, d));
if (op.startsWith('java-')) return entries.filter((d) => d.startsWith(`java-${op.replace('java-', '')}`)).map((d) => path.join(agentRoot, d));
if (op.startsWith('gateway-')) return entries.filter((d) => d.startsWith(`gateway-${op.replace('gateway-', '')}`)).map((d) => path.join(agentRoot, d));
if (op.startsWith('ai-')) return entries.filter((d) => d.startsWith(`ai-${op.replace('ai-', '')}`)).map((d) => path.join(agentRoot, d));
return [];
}
const cmdLine = commands.join('\n');
if (cmdLine.includes('config-manager pull oauth2-agents')) {
return walkDirs(MOCKS_DIR, (_, entry) => /^oauth2-agents_\d+$/.test(entry));
}
return [];
}
function snapshotHealth(snapshotText) {
const hits = [];
for (const re of ERROR_PATTERNS) {
if (re.test(snapshotText)) hits.push(re.source);
}
return hits;
}
function parseSnapshotEntries(snapshotText) {
const entries = [];
const re = /exports\[`([^`]+)`\]\s*=\s*`([\s\S]*?)`;/g;
for (const m of snapshotText.matchAll(re)) {
entries.push({
name: m[1],
body: m[2],
});
}
return entries;
}
function findSuspiciousSnapshotTests(snapshotText) {
const suspiciousTests = [];
const entries = parseSnapshotEntries(snapshotText);
for (const entry of entries) {
const matchedPatterns = [];
for (const pattern of ERROR_PATTERNS) {
if (pattern.test(entry.body)) matchedPatterns.push(pattern.source);
}
if (matchedPatterns.length > 0) {
suspiciousTests.push({
testName: entry.name,
matchedPatterns,
});
}
}
return suspiciousTests;
}
function classifySuite({ testFile, commands, snapshotPath, mockDirs }) {
const reasons = [];
let verdict = 'GOOD';
if (!fs.existsSync(snapshotPath)) {
verdict = 'BAD';
reasons.push('missing snapshot file');
}
if (mockDirs.length === 0) {
verdict = 'BAD';
reasons.push('recording directory missing');
}
let snapshotCount = 0;
let suspicious = [];
let suspiciousTests = [];
if (fs.existsSync(snapshotPath)) {
const snapshotText = fs.readFileSync(snapshotPath, 'utf8');
snapshotCount = (snapshotText.match(/exports\[`/g) || []).length;
suspicious = snapshotHealth(snapshotText);
suspiciousTests = findSuspiciousSnapshotTests(snapshotText);
if (suspicious.length > 0) {
verdict = 'BAD';
reasons.push('snapshot contains explicit error signature');
}
}
const harCount = mockDirs.reduce((sum, dir) => sum + countHarFiles(dir), 0);
if (mockDirs.length > 0 && harCount === 0) {
verdict = 'BAD';
reasons.push('recording directory has no .har files');
}
return {
suite: testFile,
verdict,
reasons,
commands: commands.length,
snapshots: snapshotCount,
mockDirs: mockDirs.length,
harFiles: harCount,
suspicious,
suspiciousTests,
};
}
const results = [];
for (const testFile of TEST_FILES) {
const testPath = path.join(TEST_DIR, testFile);
const testText = fs.readFileSync(testPath, 'utf8');
const commands = parseCommands(testText);
const stem = getSuiteStem(testFile);
const snapshotPath = path.join(SNAP_DIR, `${testFile}.snap`);
const mockDirs = findMatchingMockDirs(stem, commands);
results.push(
classifySuite({
testFile,
commands,
snapshotPath,
mockDirs,
}),
);
}
const bad = results.filter((r) => r.verdict === 'BAD');
const good = results.length - bad.length;
console.log(`Audited suites: ${results.length}`);
console.log(`GOOD: ${good}`);
console.log(`BAD: ${bad.length}`);
console.log('');
for (const r of results) {
const prefix = r.verdict === 'GOOD' ? '[GOOD]' : '[BAD] ';
console.log(`${prefix} ${r.suite}`);
if (r.reasons.length > 0) console.log(` reasons: ${r.reasons.join('; ')}`);
if (r.suspicious.length > 0) console.log(` suspicious: ${r.suspicious.join(', ')}`);
if (r.suspiciousTests.length > 0) {
for (const t of r.suspiciousTests) {
console.log(` suspicious_test: ${t.testName}`);
console.log(` matched: ${t.matchedPatterns.join(', ')}`);
}
}
console.log(` cmds=${r.commands} snapshots=${r.snapshots} mockDirs=${r.mockDirs} harFiles=${r.harFiles}`);
}
process.exit(bad.length > 0 ? 1 : 0);