test: dedupe is-main and shell coverage

This commit is contained in:
Peter Steinberger 2026-03-13 20:04:35 +00:00
parent 7771444725
commit bf6da81028
3 changed files with 115 additions and 109 deletions

View File

@ -1,116 +1,7 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { isMainModule } from "./is-main.js";
import { buildNodeShellCommand } from "./node-shell.js";
import { parseSshTarget } from "./ssh-tunnel.js"; import { parseSshTarget } from "./ssh-tunnel.js";
describe("infra parsing", () => { describe("infra parsing", () => {
describe("isMainModule", () => {
it("returns true when argv[1] matches current file", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/repo/dist/index.js"],
cwd: "/repo",
env: {},
}),
).toBe(true);
});
it("returns true under PM2 when pm_exec_path matches current file", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/pm2/lib/ProcessContainerFork.js"],
cwd: "/repo",
env: { pm_exec_path: "/repo/dist/index.js", pm_id: "0" },
}),
).toBe(true);
});
it("returns true for dist/entry.js when launched via openclaw.mjs wrapper", () => {
expect(
isMainModule({
currentFile: "/repo/dist/entry.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
wrapperEntryPairs: [{ wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" }],
}),
).toBe(true);
});
it("returns false for wrapper launches when wrapper pair is not configured", () => {
expect(
isMainModule({
currentFile: "/repo/dist/entry.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
}),
).toBe(false);
});
it("returns false when wrapper pair targets a different entry basename", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
wrapperEntryPairs: [{ wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" }],
}),
).toBe(false);
});
it("returns false when running under PM2 but this module is imported", () => {
expect(
isMainModule({
currentFile: "/repo/node_modules/openclaw/dist/index.js",
argv: ["node", "/repo/app.js"],
cwd: "/repo",
env: { pm_exec_path: "/repo/app.js", pm_id: "0" },
}),
).toBe(false);
});
});
describe("buildNodeShellCommand", () => {
it("uses cmd.exe for win32", () => {
expect(buildNodeShellCommand("echo hi", "win32")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
});
it("uses cmd.exe for windows labels", () => {
expect(buildNodeShellCommand("echo hi", "windows")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
expect(buildNodeShellCommand("echo hi", "Windows 11")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
});
it("uses /bin/sh for darwin", () => {
expect(buildNodeShellCommand("echo hi", "darwin")).toEqual(["/bin/sh", "-lc", "echo hi"]);
});
it("uses /bin/sh when platform missing", () => {
expect(buildNodeShellCommand("echo hi")).toEqual(["/bin/sh", "-lc", "echo hi"]);
});
});
describe("parseSshTarget", () => { describe("parseSshTarget", () => {
it("parses user@host:port targets", () => { it("parses user@host:port targets", () => {
expect(parseSshTarget("me@example.com:2222")).toEqual({ expect(parseSshTarget("me@example.com:2222")).toEqual({

80
src/infra/is-main.test.ts Normal file
View File

@ -0,0 +1,80 @@
import { describe, expect, it } from "vitest";
import { isMainModule } from "./is-main.js";
describe("isMainModule", () => {
it("returns true when argv[1] matches current file", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/repo/dist/index.js"],
cwd: "/repo",
env: {},
}),
).toBe(true);
});
it("returns true under PM2 when pm_exec_path matches current file", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/pm2/lib/ProcessContainerFork.js"],
cwd: "/repo",
env: { pm_exec_path: "/repo/dist/index.js", pm_id: "0" },
}),
).toBe(true);
});
it("returns true for configured wrapper-to-entry pairs", () => {
expect(
isMainModule({
currentFile: "/repo/dist/entry.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
wrapperEntryPairs: [{ wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" }],
}),
).toBe(true);
});
it("returns false for unmatched wrapper launches", () => {
expect(
isMainModule({
currentFile: "/repo/dist/entry.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
}),
).toBe(false);
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "/repo/openclaw.mjs"],
cwd: "/repo",
env: {},
wrapperEntryPairs: [{ wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" }],
}),
).toBe(false);
});
it("returns false when this module is only imported under PM2", () => {
expect(
isMainModule({
currentFile: "/repo/node_modules/openclaw/dist/index.js",
argv: ["node", "/repo/app.js"],
cwd: "/repo",
env: { pm_exec_path: "/repo/app.js", pm_id: "0" },
}),
).toBe(false);
});
it("falls back to basename matching for relative or symlinked entrypoints", () => {
expect(
isMainModule({
currentFile: "/repo/dist/index.js",
argv: ["node", "../other/index.js"],
cwd: "/repo/dist",
env: {},
}),
).toBe(true);
});
});

View File

@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { buildNodeShellCommand } from "./node-shell.js";
describe("buildNodeShellCommand", () => {
it("uses cmd.exe for win-prefixed platform labels", () => {
expect(buildNodeShellCommand("echo hi", "win32")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
expect(buildNodeShellCommand("echo hi", "windows")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
expect(buildNodeShellCommand("echo hi", " Windows 11 ")).toEqual([
"cmd.exe",
"/d",
"/s",
"/c",
"echo hi",
]);
});
it("uses /bin/sh for non-windows and missing platform values", () => {
expect(buildNodeShellCommand("echo hi", "darwin")).toEqual(["/bin/sh", "-lc", "echo hi"]);
expect(buildNodeShellCommand("echo hi", "linux")).toEqual(["/bin/sh", "-lc", "echo hi"]);
expect(buildNodeShellCommand("echo hi")).toEqual(["/bin/sh", "-lc", "echo hi"]);
expect(buildNodeShellCommand("echo hi", null)).toEqual(["/bin/sh", "-lc", "echo hi"]);
});
});