From afda294639af9d1b4f36b1cbc3943fa60b239de0 Mon Sep 17 00:00:00 2001 From: Alassane <79718216+yattdev@users.noreply.github.com> Date: Thu, 27 Aug 2026 10:09:18 +0000 Subject: [PATCH] fix(e2e): preserve runner container mode (#2800) Co-authored-by: ayattara Co-authored-by: Claude Sonnet 5 Co-authored-by: Carlos Florencio --- apps/web/e2e/global-setup.test.ts | 11 ++- apps/web/e2e/global-setup.ts | 5 +- apps/web/e2e/scripts/run-e2e.sh | 20 +++- apps/web/e2e/scripts/run-e2e.test.ts | 132 +++++++++++++++++++++++++++ apps/web/e2e/scripts/run-raw-e2e.sh | 31 +++++++ apps/web/package.json | 2 +- 6 files changed, 195 insertions(+), 6 deletions(-) create mode 100755 apps/web/e2e/scripts/run-raw-e2e.sh diff --git a/apps/web/e2e/global-setup.test.ts b/apps/web/e2e/global-setup.test.ts index fb91d8c4f0..c2a3ca2169 100644 --- a/apps/web/e2e/global-setup.test.ts +++ b/apps/web/e2e/global-setup.test.ts @@ -2,7 +2,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import globalSetup, { assertBackendArtifactsFresh } from "./global-setup"; +import globalSetup, { assertBackendArtifactsFresh, isContainerRun } from "./global-setup"; let backendDir: string; let binPath: string; @@ -244,6 +244,15 @@ describe("assertBackendBinaryFresh", () => { }); describe("globalSetup", () => { + it.each([ + ["containers", ["node", "playwright", "test", "--project=containers"]], + ["docker", ["node", "playwright", "test", "--project=docker"]], + ["containers", ["node", "playwright", "test", "--project", "containers"]], + ["docker", ["node", "playwright", "test", "--project", "docker"]], + ])("recognizes the direct %s project selection", (_project, argv) => { + expect(isContainerRun({}, argv)).toBe(true); + }); + it("checks the selected custom backend path instead of the default backend", () => { const customBackend = path.join(backendDir, "custom", "kandev"); vi.stubEnv("KANDEV_E2E_BIN", customBackend); diff --git a/apps/web/e2e/global-setup.ts b/apps/web/e2e/global-setup.ts index b09a5e674a..031078845b 100644 --- a/apps/web/e2e/global-setup.ts +++ b/apps/web/e2e/global-setup.ts @@ -77,7 +77,10 @@ export function isContainerRun(env: NodeJS.ProcessEnv, argv = process.argv): boo return []; }); - return selectedProjects.some((projects) => projects.split(",").includes("containers")); + return selectedProjects.some((projects) => { + const names = projects.split(","); + return names.includes("containers") || names.includes("docker"); + }); } function backendMakeCommand(backendDir: string, target: string): string { diff --git a/apps/web/e2e/scripts/run-e2e.sh b/apps/web/e2e/scripts/run-e2e.sh index 6ef5b513dc..c1fc79e567 100755 --- a/apps/web/e2e/scripts/run-e2e.sh +++ b/apps/web/e2e/scripts/run-e2e.sh @@ -106,7 +106,7 @@ build_fe() { build_backend_host() { log "building backend (host)" local targets=(build) - # The Playwright project is `containers`; `KANDEV_E2E_DOCKER` remains only an env alias. + # PROJECT is normalized above, so this also covers the deprecated `docker` alias. [[ "$PROJECT" == containers ]] && targets+=(build-agentctl-linux build-mock-agent-linux) make -C "$BACKEND_DIR" "${targets[@]}" >/dev/null || die "backend build failed" } @@ -174,6 +174,7 @@ while [[ $# -gt 0 ]]; do --no-build) DO_BUILD=0 ;; --no-strict) STRICT=0 ;; --project) [[ "${2:-}" =~ ^[a-zA-Z0-9_-]+$ ]] || die "invalid --project name: '${2:-}'"; PROJECT="$2"; shift ;; + --project=*) PROJECT="${1#--project=}"; [[ "$PROJECT" =~ ^[a-zA-Z0-9_-]+$ ]] || die "invalid --project name: '$PROJECT'" ;; --) shift; PW_ARGS+=("$@"); break ;; *) PW_ARGS+=("$1") ;; esac @@ -182,6 +183,15 @@ done if [[ "$SUBCMD" == clean ]]; then clean_artifacts; log "clean done"; exit 0; fi +# `docker` is a deprecated alias for the `containers` Playwright project +# (playwright.config.ts only defines `containers`); normalize once so every +# downstream check — build targets, env export, and the `--project` value +# handed to Playwright itself — only ever has to know about `containers`. +if [[ "$PROJECT" == docker ]]; then + log "--project docker is deprecated; using containers" + PROJECT=containers +fi + # Resolve mode if [[ "$MODE" == auto ]]; then if docker_up && { [[ -n "$RUNTIME_IMAGE" ]] || docker image inspect kandev-ci:runtime-local >/dev/null 2>&1 || docker image inspect ghcr.io/kdlbs/kandev-ci:runtime-latest >/dev/null 2>&1; }; then @@ -204,14 +214,18 @@ run_host() { prepare_pr_assets [[ "$DO_BUILD" == 1 ]] && { build_backend_host; build_fe; build_plugin_package; } local base_args=(playwright test --config e2e/playwright.config.ts --project="$PROJECT") + # `env FOO=bar cmd` inherits the rest of the parent shell's environment as-is, + # so a KANDEV_E2E_CONTAINERS/KANDEV_E2E_DOCKER left over from an earlier + # containers run in the same shell would otherwise leak into an ordinary + # run and make global setup demand Linux helper binaries it never built. if [[ "$SHARDS" -le 1 ]]; then - ( cd "$WEB_DIR" && env ${STRICT_ENV[@]+"${STRICT_ENV[@]}"} ${CONTAINER_ENV[@]+"${CONTAINER_ENV[@]}"} pnpm exec "${base_args[@]}" ${PW_ARGS[@]+"${PW_ARGS[@]}"} ) + ( cd "$WEB_DIR" && env -u KANDEV_E2E_CONTAINERS -u KANDEV_E2E_DOCKER ${STRICT_ENV[@]+"${STRICT_ENV[@]}"} ${CONTAINER_ENV[@]+"${CONTAINER_ENV[@]}"} pnpm exec "${base_args[@]}" ${PW_ARGS[@]+"${PW_ARGS[@]}"} ) return $? fi log "running $SHARDS host shards (distinct E2E_PORT_OFFSET + output dirs)" local pids=() rc=0 i for ((i=1; i<=SHARDS; i++)); do - ( cd "$WEB_DIR" && env ${STRICT_ENV[@]+"${STRICT_ENV[@]}"} ${CONTAINER_ENV[@]+"${CONTAINER_ENV[@]}"} E2E_PORT_OFFSET=$((i-1)) \ + ( cd "$WEB_DIR" && env -u KANDEV_E2E_CONTAINERS -u KANDEV_E2E_DOCKER ${STRICT_ENV[@]+"${STRICT_ENV[@]}"} ${CONTAINER_ENV[@]+"${CONTAINER_ENV[@]}"} E2E_PORT_OFFSET=$((i-1)) \ pnpm exec "${base_args[@]}" --shard="$i/$SHARDS" --output="e2e/test-results-shard-$i" ${PW_ARGS[@]+"${PW_ARGS[@]}"} \ > "/tmp/e2e-host-shard-$i.log" 2>&1 ) & pids+=("$!") diff --git a/apps/web/e2e/scripts/run-e2e.test.ts b/apps/web/e2e/scripts/run-e2e.test.ts index def10deb63..0c7da10ca6 100644 --- a/apps/web/e2e/scripts/run-e2e.test.ts +++ b/apps/web/e2e/scripts/run-e2e.test.ts @@ -5,6 +5,7 @@ import { spawnSync } from "node:child_process"; import { afterEach, describe, expect, it } from "vitest"; const scriptPath = path.resolve(__dirname, "run-e2e.sh"); +const rawScriptPath = path.resolve(__dirname, "run-raw-e2e.sh"); const tempDirs: string[] = []; const tempFiles: string[] = []; @@ -108,4 +109,135 @@ describe("run-e2e.sh", () => { true, ); }); + + it("treats the deprecated docker project name as an alias for containers", () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-e2e-runner-")); + tempDirs.push(binDir); + const pnpmPath = path.join(binDir, "pnpm"); + fs.writeFileSync(pnpmPath, "#!/usr/bin/env sh\nprintf '%s' \"${KANDEV_E2E_CONTAINERS:-}\"\n"); + fs.chmodSync(pnpmPath, 0o755); + + const result = spawnSync( + "bash", + [scriptPath, "--host", "--no-build", "--project", "docker", "--", "--help"], + { + encoding: "utf8", + env: runnerEnv(binDir), + }, + ); + + expect(result.status).toBe(0); + expect(result.stdout).toBe("1"); + }); + + it("accepts the deprecated docker project alias with equals syntax", () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-e2e-runner-")); + tempDirs.push(binDir); + const pnpmPath = path.join(binDir, "pnpm"); + fs.writeFileSync(pnpmPath, "#!/usr/bin/env sh\nprintf '%s' \"${KANDEV_E2E_CONTAINERS:-}\"\n"); + fs.chmodSync(pnpmPath, 0o755); + + const result = spawnSync( + "bash", + [scriptPath, "--host", "--no-build", "--project=docker", "--", "--help"], + { + encoding: "utf8", + env: runnerEnv(binDir), + }, + ); + + expect(result.status).toBe(0); + expect(result.stdout).toBe("1"); + }); + + it("normalizes the deprecated docker project alias for raw Playwright runs", () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-e2e-raw-")); + tempDirs.push(binDir); + const playwrightPath = path.join(binDir, "playwright"); + fs.writeFileSync(playwrightPath, "#!/usr/bin/env sh\nprintf '%s' \"$*\"\n"); + fs.chmodSync(playwrightPath, 0o755); + + const result = spawnSync("bash", [rawScriptPath, "--project=docker", "--help"], { + encoding: "utf8", + env: { + ...process.env, + PATH: `${binDir}:${process.env.PATH ?? ""}`, + }, + }); + + expect(result.status).toBe(0); + expect(result.stdout).toBe( + "test --config e2e/playwright.config.ts --project=containers --help", + ); + }); + + it("clears inherited container flags before an ordinary managed run", () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-e2e-runner-")); + tempDirs.push(binDir); + const pnpmPath = path.join(binDir, "pnpm"); + fs.writeFileSync( + pnpmPath, + '#!/usr/bin/env sh\nprintf \'CONTAINERS=%s DOCKER=%s\' "${KANDEV_E2E_CONTAINERS:-unset}" "${KANDEV_E2E_DOCKER:-unset}"\n', + ); + fs.chmodSync(pnpmPath, 0o755); + + const result = spawnSync( + "bash", + [scriptPath, "--host", "--no-build", "--project", "chromium", "--", "--help"], + { + encoding: "utf8", + env: { + ...process.env, + PATH: `${binDir}:${process.env.PATH ?? ""}`, + KANDEV_E2E_CONTAINERS: "1", + KANDEV_E2E_DOCKER: "1", + }, + }, + ); + + expect(result.status).toBe(0); + expect(result.stdout).toBe("CONTAINERS=unset DOCKER=unset"); + }); + + it("builds the Linux helper targets for the deprecated docker project", () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-e2e-runner-")); + const makeLogFile = path.join(binDir, "make.log"); + const pnpmLogFile = path.join(binDir, "pnpm.log"); + tempDirs.push(binDir); + tempFiles.push("/tmp/e2e-host-shard-1.log"); + + const makePath = path.join(binDir, "make"); + fs.writeFileSync( + makePath, + '#!/usr/bin/env sh\nprintf \'%s\\n\' "$*" >> "$KANDEV_RUNNER_MAKE_LOG"\nexit 0\n', + ); + fs.chmodSync(makePath, 0o755); + + const pnpmPath = path.join(binDir, "pnpm"); + fs.writeFileSync( + pnpmPath, + '#!/usr/bin/env sh\nprintf \'%s\\n\' "$*" >> "$KANDEV_RUNNER_PNPM_LOG"\nexit 0\n', + ); + fs.chmodSync(pnpmPath, 0o755); + + const result = spawnSync( + "bash", + [scriptPath, "--host", "--project", "docker", "--", "--help"], + { + encoding: "utf8", + env: runnerEnv(binDir, { + KANDEV_RUNNER_MAKE_LOG: makeLogFile, + KANDEV_RUNNER_PNPM_LOG: pnpmLogFile, + }), + }, + ); + + expect(result.status).toBe(0); + const makeInvocations = fs.readFileSync(makeLogFile, "utf8").trim().split("\n"); + expect( + makeInvocations.some( + (args) => args.includes("build-agentctl-linux") && args.includes("build-mock-agent-linux"), + ), + ).toBe(true); + }); }); diff --git a/apps/web/e2e/scripts/run-raw-e2e.sh b/apps/web/e2e/scripts/run-raw-e2e.sh new file mode 100755 index 0000000000..47ecf45320 --- /dev/null +++ b/apps/web/e2e/scripts/run-raw-e2e.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Run Playwright directly, while keeping the deprecated project alias usable. +set -euo pipefail + +PW_ARGS=() +while [[ $# -gt 0 ]]; do + case "$1" in + --project) + if [[ $# -lt 2 ]]; then + printf '%s\n' "--project requires a project name" >&2 + exit 2 + fi + if [[ "$2" == docker ]]; then + PW_ARGS+=(--project=containers) + else + PW_ARGS+=("$1" "$2") + fi + shift 2 + ;; + --project=docker) + PW_ARGS+=(--project=containers) + shift + ;; + *) + PW_ARGS+=("$1") + shift + ;; + esac +done + +exec playwright test --config e2e/playwright.config.ts "${PW_ARGS[@]}" diff --git a/apps/web/package.json b/apps/web/package.json index fbf34fc256..0b63691d17 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -17,7 +17,7 @@ "typecheck": "tsc --noEmit", "pretest": "node scripts/generate-release-notes.mjs && node scripts/generate-changelog.mjs", "test": "vitest run", - "e2e:raw": "playwright test --config e2e/playwright.config.ts", + "e2e:raw": "e2e/scripts/run-raw-e2e.sh", "e2e:run": "e2e/scripts/run-e2e.sh", "e2e:docker": "e2e/scripts/run-e2e.sh --docker", "e2e:clean": "e2e/scripts/run-e2e.sh clean",