Files
OmniRoute/tests/unit/mitm-detection.test.ts
diegosouzapw 1196d08aad test(mitm): handlers + targets + detection unit tests (F3)
- mitm-handler-base: hookBufferStart returns InterceptedRequest with
  sanitized headers, extractSourceModel parses body.model, writeError
  emits JSON with sanitized message (Hard Rule #12).
- mitm-handler-<id>: nine per-agent happy-path tests (antigravity,
  kiro, copilot, codex, cursor, zed, claude-code, open-code) exercise
  full intercept() with mocked fetch via _mitmHandlerHarness.ts;
  asserts mapped model is forwarded and AgentBridge correlation
  headers are present. Trae test confirms intercept() rejects with a
  structured error.
- mitm-targets-resolve: ALL_TARGETS contains 9 entries; resolveTarget
  is case-insensitive and returns null for unknown hosts.
- mitm-targets-route: bypass > target > passthrough precedence
  validated against representative hostnames.
- mitm-detection: DETECTORS covers every AgentId; detectAgent never
  throws and returns DetectionResult-shaped objects for all probes.
2026-05-27 22:33:56 -03:00

78 lines
2.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import { detectAgent, DETECTORS } from "../../src/mitm/detection/index.ts";
import type { AgentId } from "../../src/mitm/types.ts";
test("DETECTORS — provides an entry for every AgentId", () => {
const ids: AgentId[] = [
"antigravity",
"kiro",
"copilot",
"codex",
"cursor",
"zed",
"claude-code",
"open-code",
"trae",
];
for (const id of ids) {
assert.equal(typeof DETECTORS[id], "function", `missing detector for ${id}`);
}
});
test("detectAgent — trae always reports not installed (investigating)", () => {
const r = detectAgent("trae");
assert.equal(r.installed, false);
});
test("detectAgent — returns installed=true when fs.existsSync hits a known path", () => {
// Inject a mock existsSync that returns true for every probed path so the
// dispatch path is exercised regardless of host environment.
const original = fs.existsSync;
let called = 0;
(fs as unknown as { existsSync: (p: fs.PathLike) => boolean }).existsSync = () => {
called++;
return true;
};
try {
// antigravity uses pure existsSync probes — first hit wins.
const r = detectAgent("antigravity");
assert.equal(r.installed, true);
assert.equal(typeof r.path, "string");
} finally {
(fs as unknown as { existsSync: typeof fs.existsSync }).existsSync = original;
}
assert.ok(called >= 1);
});
test("detectAgent — antigravity probe returns DetectionResult shape", () => {
const r = detectAgent("antigravity");
assert.equal(typeof r.installed, "boolean");
if (r.installed) assert.equal(typeof r.path, "string");
});
test("detectAgent — gracefully handles thrown detectors", () => {
// Unknown id falls through to default false branch.
const r = detectAgent("nonexistent" as AgentId);
assert.equal(r.installed, false);
});
test("detectAgent — runs all detectors without throwing", () => {
const ids: AgentId[] = [
"antigravity",
"kiro",
"copilot",
"codex",
"cursor",
"zed",
"claude-code",
"open-code",
"trae",
];
for (const id of ids) {
const r = detectAgent(id);
assert.equal(typeof r.installed, "boolean");
}
});