@shubh000/macrc 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -2
- package/cli/lib/context-verifier.mjs +247 -0
- package/cli/lib/heuristic.mjs +440 -0
- package/cli/lib/output.mjs +109 -0
- package/cli/lib/policy.mjs +104 -0
- package/cli/lib/remediator.mjs +656 -0
- package/cli/lib/syntax-queries.mjs +169 -0
- package/cli/lib/targeted-llm.mjs +233 -0
- package/cli/macrc.js +2 -0
- package/cli/macrc.mjs +299 -0
- package/demo/bad-express-app/package.json +14 -0
- package/demo/bad-express-app/src/index.js +19 -0
- package/demo/bad-express-app/src/routes/users.js +16 -0
- package/demo/bad-express-app/src/services/api.js +21 -0
- package/package.json +42 -4
- package/tests/context-verifier.test.mjs +184 -0
- package/tests/fixtures/diff-after.json +36 -0
- package/tests/fixtures/diff-before.json +36 -0
- package/tests/integration.test.mjs +82 -0
- package/tests/remediator.test.mjs +181 -0
- package/tests/syntax-queries.test.mjs +98 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema_version": "1.0",
|
|
3
|
+
"repo_id": "fixture",
|
|
4
|
+
"audited_at": "2026-01-01",
|
|
5
|
+
"auditor": "macrc-heuristic",
|
|
6
|
+
"layers": {
|
|
7
|
+
"security": {
|
|
8
|
+
"auth": { "state": "absent", "severity": "high", "notes": "fixture: no auth", "snippets": [] },
|
|
9
|
+
"input_validation": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
10
|
+
"transport_security": { "state": "absent", "severity": null, "notes": "fixture", "snippets": [] },
|
|
11
|
+
"dependency_hygiene": { "state": "pending", "severity": null, "notes": "fixture", "snippets": [] },
|
|
12
|
+
"secrets_management": { "state": "pending", "severity": null, "notes": "fixture", "snippets": [] }
|
|
13
|
+
},
|
|
14
|
+
"reliability": {
|
|
15
|
+
"error_handling": { "state": "absent", "severity": "critical", "notes": "fixture", "snippets": [] },
|
|
16
|
+
"retry_logic": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
17
|
+
"circuit_breaking": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
18
|
+
"graceful_degradation": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
19
|
+
"timeout_handling": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] }
|
|
20
|
+
},
|
|
21
|
+
"observability": {
|
|
22
|
+
"logging": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
23
|
+
"metrics_emission": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
24
|
+
"tracing": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
25
|
+
"alerting": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
26
|
+
"health_checks": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] }
|
|
27
|
+
},
|
|
28
|
+
"scale_contract": {
|
|
29
|
+
"statelessness": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
30
|
+
"load_balancing_awareness": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
31
|
+
"distributed_assumptions": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
32
|
+
"rate_limiting": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
33
|
+
"caching_strategy": { "state": "absent", "severity": "low", "notes": "fixture", "snippets": [] }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
import { spawnSync } from 'child_process';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { runHeuristic } from '../cli/lib/heuristic.mjs';
|
|
7
|
+
import { buildPatches } from '../cli/lib/remediator.mjs';
|
|
8
|
+
|
|
9
|
+
const __dir = fileURLToPath(new URL('.', import.meta.url));
|
|
10
|
+
const DEMO = resolve(__dir, '../demo/bad-express-app');
|
|
11
|
+
|
|
12
|
+
// ── Absent-dim assertions ──────────────────────────────────────────────────────
|
|
13
|
+
// bad-express-app:
|
|
14
|
+
// no auth packages/patterns → secEnhancement.auth absent
|
|
15
|
+
// no validation packages/patterns → secEnhancement.input_validation absent
|
|
16
|
+
// no retry packages/patterns → reliability.retry_logic absent
|
|
17
|
+
// no timeout patterns → reliability.timeout_handling absent
|
|
18
|
+
// no health route → observability.health_checks absent
|
|
19
|
+
// express-session, no redis → scale_contract.statelessness absent
|
|
20
|
+
// localhost URLs in api.js → scale_contract.distributed_assumptions absent
|
|
21
|
+
// no rate-limit packages → scale_contract.rate_limiting absent
|
|
22
|
+
|
|
23
|
+
const ABSENT = [
|
|
24
|
+
['secEnhancement', 'auth'],
|
|
25
|
+
['secEnhancement', 'input_validation'],
|
|
26
|
+
['reliability', 'retry_logic'],
|
|
27
|
+
['reliability', 'timeout_handling'],
|
|
28
|
+
['observability', 'health_checks'],
|
|
29
|
+
['scale_contract', 'statelessness'],
|
|
30
|
+
['scale_contract', 'distributed_assumptions'],
|
|
31
|
+
['scale_contract', 'rate_limiting'],
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// Run once; reuse across all tests
|
|
35
|
+
const heuristicResult = runHeuristic(DEMO, 'ai-web-app', { verified: true });
|
|
36
|
+
|
|
37
|
+
for (const [layer, dim] of ABSENT) {
|
|
38
|
+
test(`${layer}.${dim} is absent in bad-express-app`, () => {
|
|
39
|
+
const dimResult = heuristicResult[layer]?.[dim];
|
|
40
|
+
// secEnhancement only records present/partial — undefined means absent
|
|
41
|
+
const state = dimResult?.state ?? 'absent';
|
|
42
|
+
assert.equal(state, 'absent', `expected absent, got "${state}"`);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ── Patch count test ───────────────────────────────────────────────────────────
|
|
47
|
+
// buildPatches expects auditResult.layers with 'security' (not 'secEnhancement')
|
|
48
|
+
|
|
49
|
+
test('buildPatches produces ≥7 patches on bad-express-app audit', () => {
|
|
50
|
+
const security = {
|
|
51
|
+
auth: heuristicResult.secEnhancement.auth || { state: 'absent', severity: null, notes: '', snippets: [] },
|
|
52
|
+
input_validation: heuristicResult.secEnhancement.input_validation || { state: 'absent', severity: null, notes: '', snippets: [] },
|
|
53
|
+
transport_security: heuristicResult.secEnhancement.transport_security || { state: 'absent', severity: null, notes: '', snippets: [] },
|
|
54
|
+
dependency_hygiene: { state: 'pending', severity: null, notes: '', snippets: [] },
|
|
55
|
+
secrets_management: { state: 'pending', severity: null, notes: '', snippets: [] },
|
|
56
|
+
};
|
|
57
|
+
const auditResult = {
|
|
58
|
+
layers: {
|
|
59
|
+
security,
|
|
60
|
+
reliability: heuristicResult.reliability,
|
|
61
|
+
observability: heuristicResult.observability,
|
|
62
|
+
scale_contract: heuristicResult.scale_contract,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const patches = buildPatches(auditResult, DEMO);
|
|
66
|
+
assert.ok(patches.length >= 7, `expected ≥7 patches, got ${patches.length}: ${patches.map(p => p._dim).join(', ')}`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ── Diff regression test ───────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
test('diff detects auth absent→present as IMPROVED', () => {
|
|
72
|
+
const FIX_BEFORE = resolve(__dir, 'fixtures/diff-before.json');
|
|
73
|
+
const FIX_AFTER = resolve(__dir, 'fixtures/diff-after.json');
|
|
74
|
+
const MACRC = resolve(__dir, '../cli/macrc.mjs');
|
|
75
|
+
|
|
76
|
+
const r = spawnSync(process.execPath, [MACRC, 'diff', FIX_BEFORE, FIX_AFTER], { encoding: 'utf8' });
|
|
77
|
+
assert.equal(r.status, 0, `CLI exited ${r.status}: ${r.stderr}`);
|
|
78
|
+
assert.ok(
|
|
79
|
+
r.stdout.includes('IMPROVED') && r.stdout.includes('auth'),
|
|
80
|
+
`expected "IMPROVED" and "auth" in diff output, got:\n${r.stdout}`
|
|
81
|
+
);
|
|
82
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for cli/lib/remediator.mjs
|
|
3
|
+
* Run: node --test tests/remediator.test.mjs
|
|
4
|
+
*/
|
|
5
|
+
import { test } from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { mkdirSync, writeFileSync, readFileSync, rmSync } from 'fs';
|
|
8
|
+
import { join, resolve } from 'path';
|
|
9
|
+
import { tmpdir } from 'os';
|
|
10
|
+
import { randomBytes } from 'crypto';
|
|
11
|
+
import { buildPatches, applyPatches } from '../cli/lib/remediator.mjs';
|
|
12
|
+
|
|
13
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function makeTmp() {
|
|
16
|
+
const dir = join(tmpdir(), 'macrc-test-' + randomBytes(4).toString('hex'));
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
mkdirSync(join(dir, 'src'), { recursive: true });
|
|
19
|
+
// Minimal package.json so detectLanguage returns 'js'
|
|
20
|
+
writeFileSync(join(dir, 'package.json'), JSON.stringify({
|
|
21
|
+
name: 'test-app', type: 'module', dependencies: { express: '^4', axios: '^1' }
|
|
22
|
+
}));
|
|
23
|
+
return dir;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function cleanup(dir) {
|
|
27
|
+
try { rmSync(dir, { recursive: true, force: true }); } catch {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function fakeAudit(absentDims) {
|
|
31
|
+
const layers = {
|
|
32
|
+
security: { auth: absent(), input_validation: absent(), transport_security: present(), dependency_hygiene: pending(), secrets_management: pending() },
|
|
33
|
+
reliability: { error_handling: present(), retry_logic: present(), circuit_breaking: present(), graceful_degradation: present(), timeout_handling: present() },
|
|
34
|
+
observability: { logging: present(), metrics_emission: present(), tracing: present(), alerting: present(), health_checks: present() },
|
|
35
|
+
scale_contract: { statelessness: present(), load_balancing_awareness: present(), distributed_assumptions: present(), rate_limiting: present(), caching_strategy: present() },
|
|
36
|
+
};
|
|
37
|
+
for (const dim of absentDims) {
|
|
38
|
+
for (const layer of Object.values(layers)) {
|
|
39
|
+
if (dim in layer) layer[dim] = absent();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { schema_version: '1.0', layers };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const absent = () => ({ state: 'absent', severity: 'high', notes: '', snippets: [] });
|
|
46
|
+
const present = () => ({ state: 'present', severity: null, notes: '', snippets: [] });
|
|
47
|
+
const pending = () => ({ state: 'pending', severity: null, notes: '', snippets: [] });
|
|
48
|
+
|
|
49
|
+
// ── health_checks ─────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
test('health_checks: injects /health endpoint before app.listen', () => {
|
|
52
|
+
const dir = makeTmp();
|
|
53
|
+
try {
|
|
54
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
55
|
+
"import express from 'express';\nconst app = express();\napp.listen(3000);\n");
|
|
56
|
+
const audit = fakeAudit(['health_checks']);
|
|
57
|
+
const patches = buildPatches(audit, dir);
|
|
58
|
+
applyPatches(patches, dir, false);
|
|
59
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
60
|
+
assert.ok(result.includes("app.get('/health'"), "health route injected");
|
|
61
|
+
assert.ok(result.includes('app.listen('), 'listen call preserved');
|
|
62
|
+
} finally { cleanup(dir); }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ── timeout_handling ──────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
test('timeout_handling: adds axios instance with timeout when bare axios calls found', () => {
|
|
68
|
+
const dir = makeTmp();
|
|
69
|
+
try {
|
|
70
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
71
|
+
"import axios from 'axios';\nexport async function fetch(url) { return axios.get(url); }\n");
|
|
72
|
+
const audit = fakeAudit(['timeout_handling']);
|
|
73
|
+
const patches = buildPatches(audit, dir);
|
|
74
|
+
applyPatches(patches, dir, false);
|
|
75
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
76
|
+
assert.ok(result.includes('axios.create({ timeout: 10000 })'), 'timeout instance created');
|
|
77
|
+
} finally { cleanup(dir); }
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// ── retry_logic ───────────────────────────────────────────────────────────────
|
|
81
|
+
|
|
82
|
+
test('retry_logic: adds axios-retry import and wires it', () => {
|
|
83
|
+
const dir = makeTmp();
|
|
84
|
+
try {
|
|
85
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
86
|
+
"import axios from 'axios';\nexport async function call(url) { return axios.get(url); }\n");
|
|
87
|
+
const audit = fakeAudit(['retry_logic']);
|
|
88
|
+
const patches = buildPatches(audit, dir);
|
|
89
|
+
applyPatches(patches, dir, false);
|
|
90
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
91
|
+
assert.ok(result.includes("from 'axios-retry'"), 'axios-retry imported');
|
|
92
|
+
assert.ok(result.includes('axiosRetry(axios'), 'axiosRetry wired');
|
|
93
|
+
} finally { cleanup(dir); }
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// ── input_validation ──────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
test('input_validation: injects validateBody factory after last import in route file', () => {
|
|
99
|
+
const dir = makeTmp();
|
|
100
|
+
try {
|
|
101
|
+
mkdirSync(join(dir, 'src', 'routes'), { recursive: true });
|
|
102
|
+
writeFileSync(join(dir, 'src', 'routes', 'users.js'),
|
|
103
|
+
"import { Router } from 'express';\nimport { db } from '../db.js';\nconst router = Router();\n");
|
|
104
|
+
const audit = fakeAudit(['input_validation']);
|
|
105
|
+
const patches = buildPatches(audit, dir);
|
|
106
|
+
applyPatches(patches, dir, false);
|
|
107
|
+
const result = readFileSync(join(dir, 'src', 'routes', 'users.js'), 'utf8');
|
|
108
|
+
assert.ok(result.includes('function validateBody(schema)'), 'validateBody injected');
|
|
109
|
+
// Must appear after the import block
|
|
110
|
+
const importEnd = result.lastIndexOf("import { db }");
|
|
111
|
+
const fnStart = result.indexOf('function validateBody');
|
|
112
|
+
assert.ok(fnStart > importEnd, 'validateBody placed after imports');
|
|
113
|
+
} finally { cleanup(dir); }
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ── distributed_assumptions ───────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
test('distributed_assumptions: replaces localhost URLs with env vars', () => {
|
|
119
|
+
const dir = makeTmp();
|
|
120
|
+
try {
|
|
121
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
122
|
+
"import axios from 'axios';\nconst res = await axios.get('http://localhost:8080/items');\n");
|
|
123
|
+
const audit = fakeAudit(['distributed_assumptions']);
|
|
124
|
+
const patches = buildPatches(audit, dir);
|
|
125
|
+
applyPatches(patches, dir, false);
|
|
126
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
127
|
+
// localhost is kept only as a fallback value inside the env expression
|
|
128
|
+
assert.ok(result.includes('process.env.SERVICE_URL_8080'), 'env var substituted');
|
|
129
|
+
assert.ok(!result.includes("axios.get('http://localhost"), 'bare localhost call replaced');
|
|
130
|
+
} finally { cleanup(dir); }
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// ── auth ──────────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
test('auth: injects requireAuth stub after last import in router file', () => {
|
|
136
|
+
const dir = makeTmp();
|
|
137
|
+
try {
|
|
138
|
+
mkdirSync(join(dir, 'src', 'routes'), { recursive: true });
|
|
139
|
+
writeFileSync(join(dir, 'src', 'routes', 'api.js'),
|
|
140
|
+
"import { Router } from 'express';\nimport { db } from '../db.js';\nconst router = Router();\nrouter.get('/', (req, res) => res.json([]));\n");
|
|
141
|
+
const audit = fakeAudit(['auth']);
|
|
142
|
+
const patches = buildPatches(audit, dir);
|
|
143
|
+
applyPatches(patches, dir, false);
|
|
144
|
+
const result = readFileSync(join(dir, 'src', 'routes', 'api.js'), 'utf8');
|
|
145
|
+
assert.ok(result.includes('function requireAuth(req, res, next)'), 'requireAuth injected');
|
|
146
|
+
const lastImport = result.lastIndexOf("import { db }");
|
|
147
|
+
const fnStart = result.indexOf('function requireAuth');
|
|
148
|
+
assert.ok(fnStart > lastImport, 'requireAuth placed after imports');
|
|
149
|
+
} finally { cleanup(dir); }
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ── statelessness ─────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
test('statelessness: adds connect-redis import for ESM express-session', () => {
|
|
155
|
+
const dir = makeTmp();
|
|
156
|
+
try {
|
|
157
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
158
|
+
"import express from 'express';\nimport session from 'express-session';\nconst app = express();\napp.use(session({ secret: 'x', resave: false, saveUninitialized: true }));\napp.listen(3000);\n");
|
|
159
|
+
const audit = fakeAudit(['statelessness']);
|
|
160
|
+
const patches = buildPatches(audit, dir);
|
|
161
|
+
applyPatches(patches, dir, false);
|
|
162
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
163
|
+
assert.ok(result.includes("from 'connect-redis'"), 'connect-redis imported');
|
|
164
|
+
assert.ok(result.includes('RedisStore'), 'RedisStore defined');
|
|
165
|
+
} finally { cleanup(dir); }
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// ── load_balancing_awareness ──────────────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
test('load_balancing_awareness: injects /health endpoint (prerequisite for LB probes)', () => {
|
|
171
|
+
const dir = makeTmp();
|
|
172
|
+
try {
|
|
173
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
174
|
+
"import express from 'express';\nconst app = express();\napp.listen(8080);\n");
|
|
175
|
+
const audit = fakeAudit(['load_balancing_awareness']);
|
|
176
|
+
const patches = buildPatches(audit, dir);
|
|
177
|
+
applyPatches(patches, dir, false);
|
|
178
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
179
|
+
assert.ok(result.includes("app.get('/health'"), 'health probe endpoint injected');
|
|
180
|
+
} finally { cleanup(dir); }
|
|
181
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { writeFileSync, unlinkSync } from 'fs';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { queryTimeoutHandling, queryDistributedAssumptions } from '../cli/lib/syntax-queries.mjs';
|
|
7
|
+
|
|
8
|
+
const TMP_DIR = fileURLToPath(new URL('.', import.meta.url));
|
|
9
|
+
|
|
10
|
+
function tmp(name, src) {
|
|
11
|
+
const p = resolve(TMP_DIR, `_tmp_sq_${name}.js`);
|
|
12
|
+
writeFileSync(p, src);
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
function rm(p) { try { unlinkSync(p); } catch {} }
|
|
16
|
+
|
|
17
|
+
// ── queryTimeoutHandling ───────────────────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
test('timeout_handling: absent when axios.get has no timeout', () => {
|
|
20
|
+
const p = tmp('t1', `import axios from 'axios';\naxios.get('http://api.example.com/data');`);
|
|
21
|
+
const r = queryTimeoutHandling(p);
|
|
22
|
+
rm(p);
|
|
23
|
+
assert.equal(r?.state, 'absent');
|
|
24
|
+
assert.equal(r?.callsFound, 1);
|
|
25
|
+
assert.equal(r?.callsWithTimeout, 0);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('timeout_handling: present when axios.get has { timeout }', () => {
|
|
29
|
+
const p = tmp('t2', `import axios from 'axios';\naxios.get('http://api.example.com/data', { timeout: 5000 });`);
|
|
30
|
+
const r = queryTimeoutHandling(p);
|
|
31
|
+
rm(p);
|
|
32
|
+
assert.equal(r?.state, 'present');
|
|
33
|
+
assert.equal(r?.callsWithTimeout, 1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('timeout_handling: present when fetch has AbortSignal in { signal }', () => {
|
|
37
|
+
const p = tmp('t3', `fetch('http://api.example.com', { signal: AbortSignal.timeout(5000) });`);
|
|
38
|
+
const r = queryTimeoutHandling(p);
|
|
39
|
+
rm(p);
|
|
40
|
+
assert.equal(r?.state, 'present');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('timeout_handling: null for file with no HTTP calls', () => {
|
|
44
|
+
const p = tmp('t4', `const x = 1 + 2;\nconsole.log(x);`);
|
|
45
|
+
const r = queryTimeoutHandling(p);
|
|
46
|
+
rm(p);
|
|
47
|
+
assert.equal(r, null);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('timeout_handling: partial when mixed (some with, some without)', () => {
|
|
51
|
+
const p = tmp('t5', [
|
|
52
|
+
`import axios from 'axios';`,
|
|
53
|
+
`axios.get('/a');`,
|
|
54
|
+
`axios.get('/b', { timeout: 3000 });`,
|
|
55
|
+
].join('\n'));
|
|
56
|
+
const r = queryTimeoutHandling(p);
|
|
57
|
+
rm(p);
|
|
58
|
+
assert.equal(r?.state, 'partial');
|
|
59
|
+
assert.equal(r?.callsFound, 2);
|
|
60
|
+
assert.equal(r?.callsWithTimeout, 1);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// ── queryDistributedAssumptions ────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
test('distributed_assumptions: absent when localhost in call argument', () => {
|
|
66
|
+
const p = tmp('d1', `fetch('http://localhost:3000/api');`);
|
|
67
|
+
const r = queryDistributedAssumptions(p);
|
|
68
|
+
rm(p);
|
|
69
|
+
assert.equal(r?.state, 'absent');
|
|
70
|
+
assert.ok(r?.hardcodedUrls.length >= 1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('distributed_assumptions: null when localhost not in call argument', () => {
|
|
74
|
+
const p = tmp('d2', `const URL = 'http://localhost:3000';`);
|
|
75
|
+
const r = queryDistributedAssumptions(p);
|
|
76
|
+
rm(p);
|
|
77
|
+
assert.equal(r, null);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('distributed_assumptions: null when localhost in test block', () => {
|
|
81
|
+
const p = tmp('d3', [
|
|
82
|
+
`describe('test', () => {`,
|
|
83
|
+
` it('calls local', () => {`,
|
|
84
|
+
` fetch('http://localhost:3000/api');`,
|
|
85
|
+
` });`,
|
|
86
|
+
`});`,
|
|
87
|
+
].join('\n'));
|
|
88
|
+
const r = queryDistributedAssumptions(p);
|
|
89
|
+
rm(p);
|
|
90
|
+
assert.equal(r, null);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('distributed_assumptions: absent for 127.0.0.1 in call argument', () => {
|
|
94
|
+
const p = tmp('d4', `axios.get('http://127.0.0.1:8080/health');`);
|
|
95
|
+
const r = queryDistributedAssumptions(p);
|
|
96
|
+
rm(p);
|
|
97
|
+
assert.equal(r?.state, 'absent');
|
|
98
|
+
});
|