repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
scripts/e2e.test.ts
Rawimport { type ChildProcess, spawn } from "node:child_process";
import { EventEmitter } from "node:events";
import { existsSync } from "node:fs";
import { delimiter, join } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import {
internalTask,
isolatedEnvironment,
main,
selectedTasks,
within,
} from "./e2e.ts";
import { ROOT } from "./lib/common.ts";
vi.mock("node:child_process", async (original) => ({
...(await original<typeof import("node:child_process")>()),
spawn: vi.fn(),
}));
afterEach(() => {
vi.resetAllMocks();
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
it.each([0, 1])(
"provisions once, forwards runner args, and cleans up (install exit %i)",
async (installExit) => {
vi.stubEnv("CI", "false");
vi.stubEnv("PI_EXT_E2E_FILTER", "");
vi.mocked(spawn).mockImplementation(() => {
const child = new EventEmitter();
const code = vi.mocked(spawn).mock.calls.length === 1 ? installExit : 0;
queueMicrotask(() => child.emit("close", code, null));
return child as ChildProcess;
});
const result = await main([
"//extensions/the-system:e2e",
"--",
"--project=firefox",
]);
expect(result).toBe(installExit);
const calls = vi.mocked(spawn).mock.calls;
expect(calls).toHaveLength(installExit ? 1 : 2);
expect(calls[0]?.slice(0, 2)).toEqual([
"npm",
["exec", "--", "playwright", "install", "chromium", "firefox"],
]);
const env = calls[0]?.[2]?.env;
const root = env?.["PI_EXT_E2E_ROOT"];
expect(root).toBeTruthy();
expect(existsSync(root as string)).toBe(false);
if (!installExit) {
expect(calls[1]?.[0]).toBe("mise");
expect(calls[1]?.[1]).toEqual(
expect.arrayContaining([
"//extensions/the-system:e2e-run",
"--project=firefox",
]),
);
expect(calls[1]?.[2]?.env).toEqual(env);
}
},
);
it("shares one provisioning pass across multiple suites", async () => {
vi.stubEnv("PI_EXT_E2E_FILTER", "");
vi.mocked(spawn).mockImplementation(() => {
const child = new EventEmitter();
queueMicrotask(() => child.emit("close", 0, null));
return child as ChildProcess;
});
expect(
await main(["//extensions/mockup:e2e", "//extensions/the-system:e2e"]),
).toBe(0);
const calls = vi.mocked(spawn).mock.calls;
expect(calls.map((call) => call[0])).toEqual(["npm", "mise", "mise"]);
expect(calls[1]?.[1]).toContain("//extensions/mockup:e2e-run");
expect(calls[2]?.[1]).toContain("//extensions/the-system:e2e-run");
expect(calls[1]?.[2]?.env).toEqual(calls[0]?.[2]?.env);
expect(calls[2]?.[2]?.env).toEqual(calls[0]?.[2]?.env);
});
it.each([0, 1])(
"bounds suite concurrency, continues after failures, and awaits cleanup (suite exit %i)",
async (suiteExit) => {
vi.stubEnv("PI_EXT_E2E_JOBS", "2");
vi.stubEnv("PI_EXT_E2E_FILTER", "");
vi.spyOn(console, "error").mockImplementation(() => {});
const started = Promise.withResolvers<void>();
const third = Promise.withResolvers<void>();
const children: EventEmitter[] = [];
let root = "";
vi.mocked(spawn).mockImplementation((command, _args, options) => {
const child = new EventEmitter();
root = options?.env?.["PI_EXT_E2E_ROOT"] ?? "";
if (command === "npm") queueMicrotask(() => child.emit("close", 0, null));
else {
children.push(child);
if (children.length === 2) started.resolve();
if (children.length === 3) third.resolve();
}
return child as ChildProcess;
});
const result = main([
"//extensions/canvas:e2e",
"//extensions/mockup:e2e",
"//extensions/the-system:e2e",
]);
await started.promise;
expect(children).toHaveLength(2);
expect(existsSync(root)).toBe(true);
children[1]!.emit("close", suiteExit, null);
await third.promise;
expect(existsSync(root)).toBe(true);
children[2]!.emit("close", 0, null);
children[0]!.emit("close", 0, null);
expect(await result).toBe(suiteExit);
expect(existsSync(root)).toBe(false);
for (const call of vi.mocked(spawn).mock.calls)
expect(call[1]).not.toContain("--continue-on-error");
},
);
it("awaits active suites before cleanup after a sibling fails to spawn", async () => {
vi.stubEnv("PI_EXT_E2E_JOBS", "2");
vi.stubEnv("PI_EXT_E2E_FILTER", "");
const interrupted = Promise.withResolvers<void>();
const groupKill = vi.spyOn(process, "kill").mockImplementation(() => {
interrupted.resolve();
return true;
});
const active = Object.assign(new EventEmitter(), {
pid: 424242,
kill: () => {
interrupted.resolve();
return true;
},
});
let root = "";
vi.mocked(spawn)
.mockImplementationOnce(() => {
const child = new EventEmitter();
queueMicrotask(() => child.emit("close", 0, null));
return child as ChildProcess;
})
.mockImplementationOnce((_command, _args, options) => {
root = options?.env?.["PI_EXT_E2E_ROOT"] ?? "";
return active as ChildProcess;
})
.mockImplementationOnce(() => {
const child = new EventEmitter();
queueMicrotask(() =>
child.emit("error", new Error("cannot spawn suite")),
);
return child as ChildProcess;
});
const result = main(["//extensions/canvas:e2e", "//extensions/mockup:e2e"]);
const rejected = expect(result).rejects.toThrow("cannot spawn suite");
await interrupted.promise;
if (process.platform !== "win32")
expect(groupKill).toHaveBeenCalledWith(-424242, "SIGINT");
expect(existsSync(root)).toBe(true);
active.emit("close", 143, null);
await rejected;
expect(existsSync(root)).toBe(false);
});
it("reuses browser binaries while keeping agent, profiles, and test state disposable", () => {
const root = "/tmp/pi-ext-e2e-example";
const env = isolatedEnvironment(root, {
HOME: "/home/owner",
XDG_DATA_HOME: "/home/owner/.local/share",
XDG_CONFIG_HOME: "/home/owner/.config",
PLAYWRIGHT_BROWSERS_PATH: "/home/owner/.cache/ms-playwright",
PI_CODING_AGENT_DIR: "/home/owner/.pi/agent",
TMPDIR: "/home/owner/project/.pi/tmp",
PI_BAK_S3_ENDPOINT: "https://example.invalid",
});
for (const key of [
"HOME",
"USERPROFILE",
"XDG_CONFIG_HOME",
"XDG_CACHE_HOME",
"XDG_DATA_HOME",
"XDG_STATE_HOME",
"TMPDIR",
"TMP",
"TEMP",
"PI_CODING_AGENT_DIR",
"npm_config_cache",
"MISE_CACHE_DIR",
"MISE_STATE_DIR",
])
expect(within(root, env[key] ?? "/missing")).toBe(true);
expect(env["PLAYWRIGHT_BROWSERS_PATH"]).toBe(
"/home/owner/.cache/ms-playwright",
);
expect(env["MISE_DATA_DIR"]).toBe(join("/home/owner/.local/share", "mise"));
expect(env["MISE_CONFIG_DIR"]).toBe(join("/home/owner/.config", "mise"));
expect(env["PI_BAK_S3_ENDPOINT"]).toBeUndefined();
expect(env["PI_BAK_E2E_S3"]).toBe("0");
expect(env["PI_EXT_E2E_ROOT"]).toBe(root);
expect(env["MISE_AUTO_INSTALL"]).toBe("0");
expect(env["MISE_TRUSTED_CONFIG_PATHS"]).toBe(ROOT);
expect(
isolatedEnvironment(root, {
HOME: "/home/owner",
MISE_TRUSTED_CONFIG_PATHS: "/elsewhere",
})["MISE_TRUSTED_CONFIG_PATHS"],
).toBe(["/elsewhere", ROOT].join(delimiter));
});
it("preserves explicit CI caches without leaking CI download paths into local runs", () => {
const env = isolatedEnvironment("/tmp/isolated", {
HOME: "/tmp/ci-home",
CI: "true",
PI_EXT_CI_CACHE_ROOT: "/cache/toolchains",
NPM_CONFIG_CACHE: "/cache/npm",
});
expect(env["PLAYWRIGHT_BROWSERS_PATH"]).toBe(
join("/cache/toolchains", "browsers"),
);
expect(env["npm_config_cache"]).toBe("/cache/npm");
expect(env["MISE_CACHE_DIR"]).toBe(join("/cache/toolchains", "downloads"));
expect(env["MISE_STATE_DIR"]).toBe(join("/tmp/isolated", "mise-state"));
expect(env["HOME"]).toBe(join("/tmp/isolated", "home"));
const local = isolatedEnvironment(
"/tmp/isolated",
{
HOME: "/home/owner",
PI_EXT_CI_CACHE_ROOT: "/cache/toolchains",
},
"linux",
);
expect(local["PLAYWRIGHT_BROWSERS_PATH"]).toBe(
join("/home/owner/.cache", "ms-playwright"),
);
expect(within("/tmp/isolated", local["npm_config_cache"]!)).toBe(true);
});
it.each([
["linux", {}, [".cache"]],
["linux", { XDG_CACHE_HOME: "/xdg-cache" }, ["/xdg-cache"]],
["darwin", {}, ["Library", "Caches"]],
["win32", {}, ["AppData", "Local"]],
["win32", { LOCALAPPDATA: "/local-app-data" }, ["/local-app-data"]],
] as const)(
"reuses the caller browser cache across fresh %s sandboxes",
(platform, overrides, cacheParts) => {
const parent = { HOME: "/home/owner", ...overrides };
const first = isolatedEnvironment("/tmp/run-one", parent, platform);
const second = isolatedEnvironment("/tmp/run-two", parent, platform);
const cache = cacheParts[0].startsWith("/")
? join(...cacheParts)
: join(parent.HOME, ...cacheParts);
expect(first["PLAYWRIGHT_BROWSERS_PATH"]).toBe(
join(cache, "ms-playwright"),
);
expect(second["PLAYWRIGHT_BROWSERS_PATH"]).toBe(
first["PLAYWRIGHT_BROWSERS_PATH"],
);
expect(first["HOME"]).not.toBe(second["HOME"]);
expect(first["PI_CODING_AGENT_DIR"]).not.toBe(
second["PI_CODING_AGENT_DIR"],
);
},
);
it("preserves Playwright hermetic install mode", () => {
expect(
isolatedEnvironment("/tmp/run", {
HOME: "/home/owner",
PLAYWRIGHT_BROWSERS_PATH: "0",
})["PLAYWRIGHT_BROWSERS_PATH"],
).toBe("0");
});
it("preserves Windows Mise installations before sandboxing AppData", () => {
const parent = {
USERPROFILE: "C:/Users/owner",
LOCALAPPDATA: "D:/AppData/Local",
};
expect(
isolatedEnvironment("C:/Temp/run", parent, "win32")["MISE_DATA_DIR"],
).toBe(join(parent.LOCALAPPDATA, "mise"));
expect(
isolatedEnvironment(
"C:/Temp/run",
{ USERPROFILE: parent.USERPROFILE },
"win32",
)["MISE_DATA_DIR"],
).toBe(join(parent.USERPROFILE, "AppData", "Local", "mise"));
expect(
isolatedEnvironment(
"C:/Temp/run",
{ ...parent, XDG_DATA_HOME: "D:/xdg" },
"win32",
)["MISE_DATA_DIR"],
).toBe(join("D:/xdg", "mise"));
expect(
isolatedEnvironment(
"C:/Temp/run",
{ ...parent, XDG_DATA_HOME: "D:/xdg", MISE_DATA_DIR: "D:/explicit" },
"win32",
)["MISE_DATA_DIR"],
).toBe("D:/explicit");
});
it("filters only listed local E2E suites", () => {
const tasks = ["//extensions/mockup:e2e", "//extensions/chrome-cdp:e2e"];
expect(selectedTasks(tasks, "mockup")).toEqual([tasks[0]]);
expect(() => selectedTasks(tasks, "bak")).toThrow(/No local E2E task/);
expect(() => selectedTasks(tasks, "../bak")).toThrow(/extension slug/);
});
it("routes public suites to internal runners without recursive provisioning", () => {
for (const name of ["canvas", "chrome-cdp", "mockup", "the-system"])
expect(internalTask(`//extensions/${name}:e2e`)).toBe(
`//extensions/${name}:e2e-run`,
);
expect(() => internalTask("//extensions/canvas:e2e-run")).toThrow(/Expected/);
expect(() => internalTask("//:e2e")).toThrow(/Expected/);
});
it("rejects a temporary root inside the repository", () => {
const repo = "/home/owner/project";
expect(within(repo, join(repo, ".pi/tmp"))).toBe(true);
expect(within(repo, "/tmp/pi-ext-e2e-example")).toBe(false);
});