Files
kandev/apps/web/e2e/helpers/docker.ts
T
José Almeida 538251d995 refactor(e2e): extract docker + session helpers from docker-launch.spec
Pull the inline docker CLI helpers (`dockerInspectExists`, `dockerStop`,
`dockerCurrentBranch`, etc.) and the generic session-state pollers
(`waitForLatestSessionDone`, `waitForSessionDone`,
`waitForSessionEnvironment`) into `e2e/helpers/docker.ts` and
`e2e/helpers/session.ts`. The spec drops from 763 to 647 lines and the
helpers can be reused by future docker / session tests.
2026-05-07 00:52:25 +01:00

68 lines
2.0 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { expect } from "@playwright/test";
export function dockerInspectExists(containerID: string): boolean {
const res = spawnSync("docker", ["inspect", containerID], { stdio: "ignore" });
return res.status === 0;
}
export function dockerRemove(containerID: string): void {
spawnSync("docker", ["rm", "-f", containerID], { stdio: "ignore" });
}
export function dockerStop(containerID: string): void {
const res = spawnSync("docker", ["stop", containerID], { stdio: "ignore" });
if (res.status !== 0) {
throw new Error(`failed to stop Docker container ${containerID}`);
}
}
export function dockerState(containerID: string): string {
const res = spawnSync("docker", ["inspect", "-f", "{{.State.Status}}", containerID], {
encoding: "utf8",
});
if (res.status !== 0) return "missing";
return res.stdout.trim();
}
export function dockerCurrentBranch(containerID: string): string {
const res = spawnSync(
"docker",
["exec", containerID, "git", "-C", "/workspace", "branch", "--show-current"],
{ encoding: "utf8" },
);
if (res.status !== 0) {
const diag = spawnSync(
"docker",
[
"exec",
containerID,
"sh",
"-lc",
"ls -la /workspace; git -C /workspace status --short --branch",
],
{ encoding: "utf8" },
);
const logs = spawnSync("docker", ["logs", "--tail", "40", containerID], { encoding: "utf8" });
return [
`ERR status=${res.status} state=${dockerState(containerID)}`,
`stderr=${res.stderr.trim()}`,
`diag=${diag.stdout.trim()} ${diag.stderr.trim()}`,
`logs=${logs.stdout.trim()} ${logs.stderr.trim()}`,
].join("\n");
}
return res.stdout.trim();
}
export async function waitForDockerContainerRemoved(
containerID: string,
message: string,
): Promise<void> {
await expect
.poll(() => dockerInspectExists(containerID), {
timeout: 60_000,
message,
})
.toBe(false);
}