Files
OmniRoute/tests/unit/cli-version-fastpath.test.ts
Diego Rodrigues de Sa e Souza 205361a850 fix(cli): fast-path --version to skip full CLI bootstrap (#7208)
* fix(cli): fast-path --version to skip full CLI bootstrap

`omniroute --version` ran the entire CLI bootstrap before printing the
version: the tsx/esm + polyfill imports, env-file loading, and
Commander's ~70-command registration (importing DB, providers, OAuth,
and other heavy modules). That took ~1.5s just to print a version
string.

Add isVersionFastPath() (bin/cli/utils/versionFastPath.mjs) and check it
at the very top of bin/omniroute.mjs, before any of that work runs. It
only trips for an unambiguous bare `--version`/`-V` invocation (no other
args), so it never changes behavior for real commands or for `--help`
(whose output is generated dynamically from every registered
subcommand, so it still needs full registration and is deliberately not
fast-pathed).

`--version` now returns in ~0.3s instead of ~1.5s locally.

Co-authored-by: Sutarto Jordan Chrisfivo <Jordannst@users.noreply.github.com>
Inspired-by: https://github.com/decolua/9router/pull/2414

* chore(changelog): fragment for #7208

* fix(build): enforce bin/cli/utils/versionFastPath.mjs in the pack-artifact gate

bin/omniroute.mjs now imports ./cli/utils/versionFastPath.mjs on its boot path
(the --version fast-path). bin/cli/ is only an allowlist PREFIX, so the file
vanishing from the npm tarball would never fail the unexpected-paths check --
only PACK_ARTIFACT_REQUIRED_PATHS makes its absence loud (#7065 class).

Adds the required path and updates the hardcoded expectation in
pack-artifact-policy.test.ts, matching the existing data-dir.mjs /
storageKeyProvision.mjs entries. Fixes the red in
tests/unit/pack-artifact-entrypoint-closures.test.ts, which derives the
requirement from the entrypoint's own imports.

---------

Co-authored-by: Sutarto Jordan Chrisfivo <Jordannst@users.noreply.github.com>
2026-07-17 10:39:39 -03:00

57 lines
2.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { isVersionFastPath } from "../../bin/cli/utils/versionFastPath.mjs";
const execFileAsync = promisify(execFile);
// argv shape is [node, script, ...args]
const argv = (...args: string[]) => ["node", "omniroute", ...args];
test("fast-path selector: bare --version/-V select the fast path", () => {
assert.equal(isVersionFastPath(argv("--version")), true);
assert.equal(isVersionFastPath(argv("-V")), true);
});
test("fast-path selector: --help does NOT select the fast path (help text is dynamic)", () => {
assert.equal(isVersionFastPath(argv("--help")), false);
assert.equal(isVersionFastPath(argv("-h")), false);
});
test("fast-path selector: extra args or a subcommand alongside --version fall through", () => {
assert.equal(isVersionFastPath(argv("serve", "--version")), false);
assert.equal(isVersionFastPath(argv("--version", "extra")), false);
assert.equal(isVersionFastPath(argv("--lang", "en", "--version")), false);
});
test("fast-path selector: no args or a real command do not select the fast path", () => {
assert.equal(isVersionFastPath(argv()), false);
assert.equal(isVersionFastPath(argv("serve")), false);
});
test("fast-path selector: defensive on non-array input", () => {
// @ts-expect-error intentional bad input
assert.equal(isVersionFastPath(undefined), false);
});
test("omniroute CLI --version fast-path prints ONLY the version, skipping bootstrap output", async () => {
const pkg = JSON.parse(
readFileSync(join(process.cwd(), "package.json"), "utf8")
) as { version: string };
const { stdout } = await execFileAsync(process.execPath, ["bin/omniroute.mjs", "--version"], {
cwd: process.cwd(),
env: { ...process.env, DATA_DIR: "" },
});
// Before the fast-path, env-file loading (loadEnvFile) runs ahead of Commander and
// prints "Loaded env from ..." lines interleaved with the version — proving the full
// bootstrap (tsx/esm polyfill, env loading, ~70-command Commander registration) ran
// for a plain --version query. The fast-path must short-circuit before any of that,
// so stdout is EXACTLY the version string and nothing else.
assert.equal(stdout.trim(), pkg.version);
});