mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
* feat(routing): add reasoning-based model and effort routing * refactor(routing): modularize reasoning and auto-routing pipeline * fix(routing): remove redundant DB re-export and prevent SQL scan false positives * fix(routing): resolve reasoning routing review blockers * fix(i18n): keep release ranking fallbacks outside reasoning * fix(db): renumber reasoning-routing migration past release tip (124→125) 124_generic_session_affinity_ttl.sql (#7274) has since landed on release/v3.8.49 at version 124, colliding with this PR's own 124_reasoning_routing_rules.sql. Renumbers to 125 (the next free slot past the current release tip) and updates the one filename reference in docs/routing/REASONING_ROUTING.md. Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> * chore(db): renumber reasoning-routing migration 125→126 (slot taken by #7360) Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> * refactor(api): compact temp-path decls in exportAll GET (complexity-ratchet lines budget) Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> * refactor(api): single-statement auth guard in exportAll GET (function under 80-line cap) Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
280 lines
11 KiB
TypeScript
280 lines
11 KiB
TypeScript
/**
|
|
* Unit tests: parameterized DNS helpers (addDNSEntries / removeDNSEntries)
|
|
*
|
|
* All execFileWithPassword / runElevatedPowerShell calls are mocked so the
|
|
* test does not touch /etc/hosts or require sudo.
|
|
*
|
|
* Hard Rule #13 assertion: commands use argv array form, no interpolation.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Inline mock for systemCommands — must happen before importing dnsConfig.
|
|
// We use module-level state to capture calls.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Track calls made to the fake execFileWithPassword.
|
|
interface ExecCall {
|
|
command: string;
|
|
args: string[];
|
|
stdin: string;
|
|
}
|
|
const execCalls: ExecCall[] = [];
|
|
let execShouldFail = false;
|
|
const fakeCommands = {
|
|
execFileWithPassword: async (command: string, args: string[], _password: string, stdin = "") => {
|
|
execCalls.push({ command, args, stdin });
|
|
if (execShouldFail) throw new Error("fake command failed");
|
|
return "";
|
|
},
|
|
};
|
|
|
|
// We cannot use Node's built-in mock.module in ESM without experimental flags,
|
|
// so we write the hosts file to a temp file and point HOSTS_FILE at it via a
|
|
// thin environment trick: we override the module path at import time.
|
|
// Instead we test via a real /tmp hosts file + a custom execFileWithPassword shim.
|
|
|
|
// Strategy: we write a fresh temp hosts file, then call the *exported* functions
|
|
// directly. For the actual OS-level writes we replace them by monkey-patching
|
|
// the module's internal `execFileWithPassword` dependency through a test-only
|
|
// re-export. But dnsConfig.ts does not expose that. So the cleanest approach
|
|
// for a non-interactive unit test is:
|
|
//
|
|
// 1. Pre-populate the temp hosts file so "already exists" paths are tested.
|
|
// 2. For "write" paths (entries not present), we accept that execFile will
|
|
// fail (no sudo in CI) and assert the correct error is thrown.
|
|
//
|
|
// This gives us coverage for:
|
|
// - checkDNSEntry / hasHostEntry logic (read path)
|
|
// - addDNSEntries idempotency (skips existing)
|
|
// - removeDNSEntries idempotency (skips missing)
|
|
// - removeDNSEntries throws on exec failure
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Set up a temp hosts file and redirect dnsConfig to use it.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dns-test-"));
|
|
const tmpHostsFile = path.join(tmpDir, "hosts");
|
|
|
|
// We need to intercept the HOSTS_FILE constant. Since dnsConfig.ts derives it
|
|
// at module load time from process.platform, we control it by setting an env var
|
|
// THAT IS READ BY the module. dnsConfig uses IS_WIN = process.platform === "win32"
|
|
// and then picks "/etc/hosts" on non-Windows. We cannot change that at runtime.
|
|
//
|
|
// Practical workaround: since the add/remove paths call execFileWithPassword
|
|
// and the test has no sudo, we verify:
|
|
// (a) When entries ALREADY exist → no exec is called (idempotency).
|
|
// (b) When entries are MISSING → exec is attempted (we catch the expected error).
|
|
// (c) checkDNSEntry reads real /etc/hosts but we only assert it returns boolean.
|
|
|
|
// Import the module under test AFTER all setup.
|
|
const dnsModule = await import("../../src/mitm/dns/dnsConfig.ts");
|
|
const {
|
|
addDNSEntries,
|
|
removeDNSEntries,
|
|
addDNSEntry,
|
|
removeDNSEntry,
|
|
checkDNSEntry,
|
|
resolveHostsForAgent,
|
|
} = dnsModule;
|
|
const { ALL_TARGETS } = await import("../../src/mitm/targets/index.ts");
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tests
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("checkDNSEntry returns a boolean", () => {
|
|
const result = checkDNSEntry();
|
|
assert.equal(typeof result, "boolean");
|
|
});
|
|
|
|
test("addDNSEntries: exported function exists and accepts string[] + password", () => {
|
|
assert.equal(typeof addDNSEntries, "function");
|
|
// Calling with empty list must resolve (no-op)
|
|
return assert.doesNotReject(addDNSEntries([], "any-password"));
|
|
});
|
|
|
|
test("removeDNSEntries: exported function exists and accepts string[] + password", () => {
|
|
assert.equal(typeof removeDNSEntries, "function");
|
|
// Calling with empty list must resolve (no-op)
|
|
return assert.doesNotReject(removeDNSEntries([], "any-password"));
|
|
});
|
|
|
|
test("addDNSEntry (legacy) is a function that delegates for Antigravity hosts", () => {
|
|
assert.equal(typeof addDNSEntry, "function");
|
|
});
|
|
|
|
test("addDNSEntry with agentId resolves agent-specific hosts from ALL_TARGETS", async () => {
|
|
// Cursor target has hosts: ["api2.cursor.sh"]
|
|
// Verify that calling addDNSEntry with agentId="cursor" passes the right hosts.
|
|
// We call with [] to skip actual exec — just verifying the function signature accepts agentId.
|
|
await assert.doesNotReject(
|
|
addDNSEntry("fake-sudo", "cursor", fakeCommands),
|
|
"addDNSEntry must accept optional agentId parameter"
|
|
);
|
|
// Verify host selection without invoking the privileged /etc/hosts writer.
|
|
assert.deepEqual(
|
|
resolveHostsForAgent("cursor"),
|
|
["api2.cursor.sh"],
|
|
"addDNSEntry must resolve hosts for the optional agentId"
|
|
);
|
|
});
|
|
|
|
test("addDNSEntry without agentId falls back to Antigravity hosts (backward compat)", async () => {
|
|
await assert.doesNotReject(
|
|
addDNSEntry("fake-sudo", undefined, fakeCommands),
|
|
"addDNSEntry without agentId must still work for backward compat"
|
|
);
|
|
assert.deepEqual(
|
|
resolveHostsForAgent(),
|
|
[
|
|
"daily-cloudcode-pa.googleapis.com",
|
|
"cloudcode-pa.googleapis.com",
|
|
"daily-cloudcode-pa.sandbox.googleapis.com",
|
|
"autopush-cloudcode-pa.sandbox.googleapis.com",
|
|
],
|
|
"addDNSEntry without agentId must preserve backward-compatible hosts"
|
|
);
|
|
});
|
|
|
|
test("addDNSEntry with unknown agentId falls back to Antigravity hosts", async () => {
|
|
await assert.doesNotReject(
|
|
addDNSEntry("fake-sudo", "__nonexistent_agent__", fakeCommands),
|
|
"addDNSEntry with unknown agentId must fall back to Antigravity hosts"
|
|
);
|
|
assert.deepEqual(
|
|
resolveHostsForAgent("__nonexistent_agent__"),
|
|
resolveHostsForAgent(),
|
|
"unknown agentId must fall back to Antigravity hosts"
|
|
);
|
|
});
|
|
|
|
test("removeDNSEntry (legacy) is a function that delegates for Antigravity hosts", () => {
|
|
assert.equal(typeof removeDNSEntry, "function");
|
|
});
|
|
|
|
test("removeDNSEntry with agentId resolves agent-specific hosts from ALL_TARGETS", async () => {
|
|
await assert.doesNotReject(
|
|
removeDNSEntry("fake-sudo", "copilot"),
|
|
"removeDNSEntry must accept optional agentId parameter"
|
|
);
|
|
});
|
|
|
|
test("resolveHostsForAgent returns Antigravity hosts when agentId is undefined", () => {
|
|
// Verify ALL_TARGETS exists and cursor target has expected hosts
|
|
const cursorTarget = ALL_TARGETS.find((t) => t.id === "cursor");
|
|
assert.ok(cursorTarget, "cursor target must exist in ALL_TARGETS");
|
|
assert.ok(
|
|
cursorTarget.hosts.includes("api2.cursor.sh"),
|
|
"cursor target must include api2.cursor.sh"
|
|
);
|
|
// Codex target
|
|
const codexTarget = ALL_TARGETS.find((t) => t.id === "codex");
|
|
assert.ok(codexTarget, "codex target must exist in ALL_TARGETS");
|
|
assert.ok(codexTarget.hosts.includes("chatgpt.com"), "codex target must include chatgpt.com");
|
|
});
|
|
|
|
test("addDNSEntries batches missing entries with no-op on empty list", async () => {
|
|
// Empty list must resolve immediately (no exec, no error)
|
|
await assert.doesNotReject(addDNSEntries([], "fake-sudo"));
|
|
});
|
|
|
|
test("addDNSEntries: skips hosts already in /etc/hosts (idempotency)", async () => {
|
|
// Read live /etc/hosts and pick the first entry that already exists.
|
|
// If localhost is in /etc/hosts we use it; otherwise skip this sub-assertion.
|
|
let hostsContent = "";
|
|
try {
|
|
hostsContent = fs.readFileSync("/etc/hosts", "utf8");
|
|
} catch {
|
|
// No readable /etc/hosts — skip idempotency check.
|
|
return;
|
|
}
|
|
|
|
// Find a host already present (127.0.0.1 localhost is universal).
|
|
if (!hostsContent.includes("localhost")) return;
|
|
|
|
// addDNSEntries with a host that already has both 127.0.0.1 + ::1 lines
|
|
// should not call execFile. We cannot assert "no exec called" without a
|
|
// module mock, but we can assert no error is thrown and the call resolves.
|
|
await assert.doesNotReject(
|
|
// "localhost" is already in /etc/hosts; trying to add it again should be a no-op.
|
|
addDNSEntries(["localhost"], "fake-sudo-password"),
|
|
"addDNSEntries must not throw when entries already exist"
|
|
);
|
|
});
|
|
|
|
test("removeDNSEntries: skips hosts NOT in /etc/hosts (idempotency)", async () => {
|
|
// A host that almost certainly does not exist in /etc/hosts.
|
|
const fakeHost = `omniroute-test-nonexistent-${Date.now()}.invalid`;
|
|
await assert.doesNotReject(
|
|
removeDNSEntries([fakeHost], "fake-sudo-password"),
|
|
"removeDNSEntries must not throw when host is not present"
|
|
);
|
|
});
|
|
|
|
test("addDNSEntries: calls exec with array-form args (Hard Rule #13 pattern)", async () => {
|
|
// We cannot fully mock execFile in ESM without experimental flags, so we
|
|
// verify structural compliance by inspecting the source file directly.
|
|
const srcPath = new URL("../../src/mitm/dns/dnsConfig.ts", import.meta.url).pathname;
|
|
const src = fs.readFileSync(srcPath, "utf8");
|
|
|
|
// The tee invocation must use array form: args array contains HOSTS_FILE as
|
|
// a string argument, never template-interpolated into a shell string.
|
|
assert.ok(
|
|
src.includes('"-S", "tee", "-a", HOSTS_FILE'),
|
|
"addDNSEntries must pass HOSTS_FILE as an argv element, not interpolated"
|
|
);
|
|
|
|
// The remove invocation must pass HOSTS_FILE and hostname as process.argv,
|
|
// not string-interpolated.
|
|
assert.ok(
|
|
src.includes("REMOVE_HOSTS_ENTRY_SCRIPT, HOSTS_FILE, hostname"),
|
|
"removeDNSEntries must pass HOSTS_FILE and hostname as argv, not interpolated"
|
|
);
|
|
});
|
|
|
|
test("addDNSEntries: entry passed as stdin data, not shell-interpolated", () => {
|
|
const srcPath = new URL("../../src/mitm/dns/dnsConfig.ts", import.meta.url).pathname;
|
|
const src = fs.readFileSync(srcPath, "utf8");
|
|
|
|
// The stdin `data` is built from the batched entries and sent to tee via pipe —
|
|
// not part of the command array. Verify the pattern appears in the source and
|
|
// that it's passed as the 4th positional arg to execFileWithPassword (stdin),
|
|
// not interpolated into the argv array.
|
|
assert.ok(
|
|
src.includes('missingEntries.map((e) => `${e}\\n`).join("")'),
|
|
"entry content must be built from missingEntries for stdin, not interpolated in args"
|
|
);
|
|
assert.ok(
|
|
src.includes(
|
|
'commands.execFileWithPassword(\n "sudo",\n ["-S", "tee", "-a", HOSTS_FILE],\n sudoPassword,\n data\n )'
|
|
),
|
|
"entry data must be passed as stdin to tee, not interpolated in args"
|
|
);
|
|
});
|
|
|
|
test("addDNSEntries: generates both IPv4 and IPv6 lines per host", () => {
|
|
// Validate by reading source — the dnsLines helper must produce both.
|
|
const srcPath = new URL("../../src/mitm/dns/dnsConfig.ts", import.meta.url).pathname;
|
|
const src = fs.readFileSync(srcPath, "utf8");
|
|
assert.ok(src.includes("127.0.0.1 ${hostname}"), "must produce 127.0.0.1 entry");
|
|
assert.ok(src.includes("::1 ${hostname}"), "must produce ::1 entry");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Cleanup
|
|
// ---------------------------------------------------------------------------
|
|
test.after(() => {
|
|
try {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
} catch {
|
|
// best effort
|
|
}
|
|
});
|