Files
OmniRoute/tests/unit/cli-storage-key-provision.test.ts
diegosouzapw e1007acb7e fix(cli): bare omniroute (default serve) must provision STORAGE_ENCRYPTION_KEY
My #3129 gate wrongly skipped provisioning for a bare `omniroute` invocation —
but `serve` is isDefault:true, so bare runs the server, which needs the key.
Only --version/--help/help/completion skip now. Realigns with #1622: its bootstrap
test invoked `--help` (now correctly skipped), so it's switched to `config list
--json` (a real, fast, offline command) to exercise the provisioning path.
2026-06-03 21:08:01 -03:00

43 lines
2.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { shouldProvisionStorageKey } from "../../bin/cli/utils/storageKeyProvision.mjs";
// argv shape is [node, script, ...args]
const argv = (...args: string[]) => ["node", "omniroute", ...args];
test("storage key: informational commands do NOT provision a key", () => {
assert.equal(shouldProvisionStorageKey(argv("--version")), false);
assert.equal(shouldProvisionStorageKey(argv("-V")), false);
assert.equal(shouldProvisionStorageKey(argv("--help")), false);
assert.equal(shouldProvisionStorageKey(argv("-h")), false);
assert.equal(shouldProvisionStorageKey(argv("help")), false);
assert.equal(shouldProvisionStorageKey(argv("completion")), false);
});
test("storage key: bare `omniroute` provisions (serve is the default command)", () => {
// No args → Commander runs the isDefault `serve` command, which needs the key.
assert.equal(shouldProvisionStorageKey(argv()), true);
});
test("storage key: --help/--version anywhere in the args still skips", () => {
assert.equal(shouldProvisionStorageKey(argv("serve", "--help")), false);
assert.equal(shouldProvisionStorageKey(argv("keys", "list", "-h")), false);
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "--version")), false);
});
test("storage key: real commands DO provision (preserves #1622 persistence)", () => {
assert.equal(shouldProvisionStorageKey(argv("serve")), true);
assert.equal(shouldProvisionStorageKey(argv("keys", "list")), true);
assert.equal(shouldProvisionStorageKey(argv("providers")), true);
assert.equal(shouldProvisionStorageKey(argv("serve", "--port", "20128")), true);
// global --lang option before a real command must not suppress provisioning
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "serve")), true);
});
test("storage key: defensive on non-array input → fail-safe to provisioning", () => {
// Non-array argv collapses to [] → treated as a bare invocation (default serve),
// so it provisions. Fail-safe: better to have the key than to skip it.
// @ts-expect-error intentional bad input
assert.equal(shouldProvisionStorageKey(undefined), true);
});