Files
OmniRoute/tests/unit/cli-contexts.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

180 lines
6.5 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
let tmpDir: string;
let origDataDir: string | undefined;
test.before(() => {
tmpDir = mkdtempSync(join(tmpdir(), "omniroute-ctx-test-"));
origDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = tmpDir;
});
test.after(() => {
if (origDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = origDataDir;
try {
rmSync(tmpDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {}
});
test("contexts.mjs pode ser importado sem erro", async () => {
const mod = await import("../../bin/cli/contexts.mjs");
assert.equal(typeof mod.loadContexts, "function");
assert.equal(typeof mod.saveContexts, "function");
assert.equal(typeof mod.resolveActiveContext, "function");
assert.equal(typeof mod.configPath, "function");
});
test("loadContexts retorna config padrão quando arquivo não existe", async () => {
const { loadContexts } = await import("../../bin/cli/contexts.mjs");
const cfg = loadContexts();
assert.ok(cfg.contexts);
assert.ok(cfg.contexts.default);
assert.equal(typeof cfg.contexts.default.baseUrl, "string");
assert.equal(cfg.currentContext, "default");
});
test("saveContexts persiste e loadContexts relê", async () => {
const { loadContexts, saveContexts } = await import("../../bin/cli/contexts.mjs");
const cfg = loadContexts();
cfg.contexts.test = { baseUrl: "http://test:9999", apiKey: null };
cfg.currentContext = "test";
saveContexts(cfg);
const cfg2 = loadContexts();
assert.equal(cfg2.currentContext, "test");
assert.equal(cfg2.contexts.test?.baseUrl, "http://test:9999");
});
test("resolveActiveContext retorna contexto ativo", async () => {
const { resolveActiveContext, loadContexts, saveContexts } =
await import("../../bin/cli/contexts.mjs");
const cfg = loadContexts();
cfg.contexts.prod = { baseUrl: "https://prod.example.com", apiKey: "sk-prod" };
cfg.currentContext = "prod";
saveContexts(cfg);
const ctx = resolveActiveContext(undefined);
assert.equal(ctx.baseUrl, "https://prod.example.com");
});
test("resolveActiveContext aceita override pontual", async () => {
const { resolveActiveContext, loadContexts, saveContexts } =
await import("../../bin/cli/contexts.mjs");
const cfg = loadContexts();
cfg.contexts.staging = { baseUrl: "http://staging:20128", apiKey: null };
saveContexts(cfg);
const ctx = resolveActiveContext("staging");
assert.equal(ctx.baseUrl, "http://staging:20128");
});
test("saveContextsSecure guarda tokens no keychain e resolve pela referência", async () => {
const {
loadContexts,
saveContextsSecure,
resolveActiveContext,
setContextKeychainBackendForTests,
} = await import("../../bin/cli/contexts.mjs");
const entries = new Map<string, string>();
const fakeKeychain = {
async getPassword(_service: string, account: string) {
return entries.get(account) || null;
},
async setPassword(_service: string, account: string, value: string) {
entries.set(account, value);
},
async deletePassword(_service: string, account: string) {
entries.delete(account);
return true;
},
};
await setContextKeychainBackendForTests(fakeKeychain);
const cfg = loadContexts();
cfg.contexts.secure = {
baseUrl: "https://secure.example.com",
accessToken: "oma_test_secret",
scope: "write",
};
await saveContextsSecure(cfg);
const persisted = JSON.parse(readFileSync(join(tmpDir, "config.json"), "utf8"));
assert.equal(persisted.contexts.secure.accessToken, undefined);
assert.match(persisted.contexts.secure.credentialRef, /^omniroute-cli:context:/);
assert.equal(resolveActiveContext("secure").accessToken, "oma_test_secret");
assert.ok(entries.size >= 1);
await setContextKeychainBackendForTests(null);
});
test("contexts.mjs (commands) pode ser importado sem erro", async () => {
const mod = await import("../../bin/cli/commands/contexts.mjs");
assert.equal(typeof mod.registerContexts, "function");
});
test("context export redaction covers canonical and legacy profile schemas", async () => {
const { redactContextSecrets } = await import("../../bin/cli/commands/contexts.mjs");
const redacted = redactContextSecrets({
contexts: { remote: { accessToken: "oma-secret", apiKey: "sk-secret" } },
profiles: { legacy: { accessToken: "legacy-secret", apiKey: "legacy-key" } },
});
assert.deepEqual(redacted.contexts.remote, { apiKey: null });
assert.deepEqual(redacted.profiles.legacy, { apiKey: null });
});
test("confirm() declines cleanly on non-interactive stdin (no hung await)", async () => {
// Regression: `contexts remove` without --yes used to prompt even when stdin
// could not answer (pipe/CI/EOF), leaving the readline question pending and
// triggering Node's "unsettled top-level await" warning. With a non-TTY stdin
// confirm() must resolve to false immediately (decline) without touching
// readline — `--yes` remains the way to proceed non-interactively.
const { confirm } = await import("../../bin/cli/commands/contexts.mjs");
const desc = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
Object.defineProperty(process.stdin, "isTTY", { value: false, configurable: true });
try {
const result = await confirm("Remove context 'x'?");
assert.equal(result, false);
} finally {
if (desc) Object.defineProperty(process.stdin, "isTTY", desc);
else delete (process.stdin as { isTTY?: boolean }).isTTY;
}
});
test("registerContexts registers the singular `context` alias", async () => {
// The connect output and older docs say `omniroute context current` (singular);
// the command is `contexts`. An alias keeps the singular muscle-memory working.
const { registerContexts } = await import("../../bin/cli/commands/contexts.mjs");
let aliasName: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fakeCtx: any = {
command() {
return this;
},
alias(a: string) {
aliasName = a;
return this;
},
description() {
return this;
},
requiredOption() {
return this;
},
option() {
return this;
},
action() {
return this;
},
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fakeProgram: any = {
command() {
return fakeCtx;
},
};
registerContexts(fakeProgram);
assert.equal(aliasName, "context");
});