Files
OmniRoute/tests/unit/cli/tray-runtime-windows-esm-import.test.ts
Paco Cartones 644dd32d3f fix(cli): resolve tray runtime import to a file:// URL so --tray works on Windows (#11332)
Merged via consolidated batch validation. Fixes omniroute server --tray on Windows: absolute paths passed to dynamic import() are parsed as URLs, and a Windows drive letter (C:) isn't a supported URL scheme. Resolves via pathToFileURL. Own regression test passes.
2026-08-24 12:11:49 -03:00

49 lines
2.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import path from "node:path";
import os from "node:os";
import { pathToFileURL } from "node:url";
import {
systrayModuleSpecifier,
SYSTRAY_PACKAGE,
} from "../../../bin/cli/runtime/trayRuntime.ts";
// Regression guard for the Windows-only ESM loader failure at the lazy tray
// import in bin/cli/runtime/trayRuntime.ts (loadSystray):
//
// Error: Only URLs with a scheme in: file, data, and node are supported by
// the default ESM loader. On Windows, absolute paths must be valid file://
// URLs. Received protocol 'c:'
//
// `import()` resolves its specifier as a URL. A POSIX absolute path like
// /home/x/.omniroute/runtime/node_modules/systray2 doubles as a valid relative
// URL, so passing it works by accident on Linux/macOS (and CI stays green). A
// Windows absolute path is C:\Users\x\.omniroute\runtime\node_modules\systray2,
// whose leading drive letter the loader parses as the URL scheme `c:` and
// rejects — so `omniroute server --tray` never loads the tray on Windows.
// This is the same defect as #11238 (CLI db-fallback imports), which missed
// this call site. The specifier must be a file:// URL.
test("systrayModuleSpecifier returns a file:// URL, not a raw absolute path", () => {
const runtimeDir = path.join(os.homedir(), ".omniroute", "runtime");
const spec = systrayModuleSpecifier(runtimeDir);
assert.match(
spec,
/^file:\/\//,
"dynamic import() of a raw absolute path fails on Windows (drive letter " +
"parsed as a URL scheme); wrap the path in pathToFileURL(...).href",
);
assert.ok(spec.includes(SYSTRAY_PACKAGE), "specifier must target the systray2 package");
// A file:// URL is a loader-acceptable specifier on every platform.
assert.doesNotThrow(() => new URL(spec));
});
test("systrayModuleSpecifier matches pathToFileURL of the module directory", () => {
const runtimeDir = path.join(os.tmpdir(), "omniroute-tray-spec-test");
const expected = pathToFileURL(
path.join(runtimeDir, "node_modules", SYSTRAY_PACKAGE),
).href;
assert.equal(systrayModuleSpecifier(runtimeDir), expected);
});