diff --git a/apps/electron-backend-e2e/playwright.config.ts b/apps/electron-backend-e2e/playwright.config.ts index 9446117e2..5dd6cccd9 100644 --- a/apps/electron-backend-e2e/playwright.config.ts +++ b/apps/electron-backend-e2e/playwright.config.ts @@ -46,15 +46,27 @@ export default defineConfig({ /* Video on failure */ video: 'on-first-retry', }, + /* The mocks run as one node process, not through `nx run …:serve`: Nx + * starts its command in a detached process group, so Playwright's + * process-group kill missed it and the server kept its port after the run. + * See docs/architecture/xtream-mock-server.md#playwright-integration. */ webServer: [ { - command: 'pnpm nx run stalker-mock-server:serve', + command: 'node --import tsx apps/stalker-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, url: `http://localhost:${process.env['MOCK_PORT'] ?? '3210'}/health`, reuseExistingServer: !process.env['CI'], cwd: workspaceRoot, }, { - command: 'pnpm nx run xtream-mock-server:serve', + command: 'node --import tsx apps/xtream-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, url: `http://localhost:${process.env['XTREAM_MOCK_PORT'] ?? '3211'}/health`, reuseExistingServer: !process.env['CI'], cwd: workspaceRoot, diff --git a/apps/electron-backend-e2e/playwright.journeys.config.ts b/apps/electron-backend-e2e/playwright.journeys.config.ts index 02f158d43..0d04e996a 100644 --- a/apps/electron-backend-e2e/playwright.journeys.config.ts +++ b/apps/electron-backend-e2e/playwright.journeys.config.ts @@ -6,9 +6,11 @@ import { defineConfig } from '@playwright/test'; * worker, no retries: every journey spawns its own Electron processes and * writes one summary per run. The Xtream mock serves both the M3U playlist * and the portal on a dedicated loopback port so a normal E2E server on - * 3211 cannot be reused by accident. Locally a server left behind by an - * earlier run on that port is reused (its fixtures are deterministic); CI - * always starts its own. + * 3211 cannot be reused by accident. The mock runs as one node process + * rather than through `nx run …:serve`, whose detached process group outlived + * the run and kept the port. Locally a server already on the port (e.g. one + * started by hand) is reused, since its fixtures are deterministic; CI always + * starts its own. */ const xtreamMockPort = process.env['IPTVNATOR_JOURNEY_XTREAM_MOCK_PORT'] ?? '3231'; @@ -24,11 +26,13 @@ export default defineConfig({ testIdAttribute: 'data-test-id', }, webServer: { - command: 'pnpm nx run xtream-mock-server:serve', + command: 'node --import tsx apps/xtream-mock-server/src/main.ts', cwd: workspaceRoot, env: { HOST: '127.0.0.1', + NODE_ENV: 'development', PORT: xtreamMockPort, + TSX_TSCONFIG_PATH: 'tsconfig.base.json', }, reuseExistingServer: !process.env['CI'], url: `http://127.0.0.1:${xtreamMockPort}/health`, diff --git a/apps/electron-backend-e2e/playwright.xtream-performance.config.ts b/apps/electron-backend-e2e/playwright.xtream-performance.config.ts index fcf8086e9..57efa40a5 100644 --- a/apps/electron-backend-e2e/playwright.xtream-performance.config.ts +++ b/apps/electron-backend-e2e/playwright.xtream-performance.config.ts @@ -19,13 +19,16 @@ export default defineConfig({ testIdAttribute: 'data-test-id', }, webServer: { - command: 'pnpm nx run xtream-mock-server:serve', + // One node process (not `nx run`) so the run's kill reaches it. + command: 'node --import tsx apps/xtream-mock-server/src/main.ts', cwd: workspaceRoot, env: { HOST: '127.0.0.1', IPTVNATOR_XTREAM_MOCK_CONTROL: '1', IPTVNATOR_XTREAM_MOCK_CONTROL_TOKEN: controlToken, + NODE_ENV: 'development', PORT: '3221', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', }, reuseExistingServer: false, url: 'http://127.0.0.1:3221/health', diff --git a/apps/web-e2e/playwright.config.ts b/apps/web-e2e/playwright.config.ts index 0aa4c6a5d..2531cd588 100644 --- a/apps/web-e2e/playwright.config.ts +++ b/apps/web-e2e/playwright.config.ts @@ -66,14 +66,25 @@ export default defineConfig({ reuseExistingServer: reuseExistingWebServer, cwd: workspaceRoot, }, + /* The mocks run as one node process, not through `nx run …:serve`: + * Nx starts its command in a detached process group, so Playwright's + * process-group kill missed it and the server kept its port. */ { - command: 'pnpm nx run stalker-mock-server:serve', + command: 'node --import tsx apps/stalker-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, url: `http://localhost:${process.env['MOCK_PORT'] ?? '3210'}/health`, reuseExistingServer: !process.env['CI'], cwd: workspaceRoot, }, { - command: 'pnpm nx run xtream-mock-server:serve', + command: 'node --import tsx apps/xtream-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, url: `http://localhost:${process.env['XTREAM_MOCK_PORT'] ?? '3211'}/health`, reuseExistingServer: !process.env['CI'], cwd: workspaceRoot, diff --git a/apps/xtream-mock-server/src/app/project-config.spec.ts b/apps/xtream-mock-server/src/app/project-config.spec.ts index ace0639fb..c60fbd511 100644 --- a/apps/xtream-mock-server/src/app/project-config.spec.ts +++ b/apps/xtream-mock-server/src/app/project-config.spec.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs'; +import { readdirSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; describe('Xtream mock Nx serve environment', () => { @@ -26,3 +26,49 @@ describe('Xtream mock Nx serve environment', () => { } ); }); + +describe('Playwright mock-server launch', () => { + const appsDirectory = join(process.cwd(), 'apps'); + const playwrightConfigs = readdirSync(appsDirectory, { + withFileTypes: true, + }) + .filter((entry) => entry.isDirectory()) + .flatMap((entry) => + readdirSync(join(appsDirectory, entry.name)) + .filter((name) => /^playwright(\..+)?\.config\.ts$/.test(name)) + .map((name) => `apps/${entry.name}/${name}`) + ); + + it('finds every config that starts a mock server', () => { + expect(playwrightConfigs).toEqual( + expect.arrayContaining([ + 'apps/electron-backend-e2e/playwright.config.ts', + 'apps/electron-backend-e2e/playwright.journeys.config.ts', + 'apps/electron-backend-e2e/playwright.xtream-performance.config.ts', + 'apps/web-e2e/playwright.config.ts', + ]) + ); + }); + + // Nx starts a run-commands child in its own detached process group, and + // Playwright stops a webServer with a process-group SIGKILL, so a mock + // launched through `nx run …:serve` outlived the run and kept its port. + it.each(playwrightConfigs)( + 'starts mock servers as a single node process in %s', + (configPath) => { + const source = readFileSync( + join(process.cwd(), configPath), + 'utf8' + ); + + expect(source).not.toMatch(/nx run [\w-]+-mock-server:serve/); + const launches = + source.match(/node --import tsx apps\/[\w-]+-mock-server\//g) ?? + []; + const tsconfigs = + source.match(/TSX_TSCONFIG_PATH: 'tsconfig\.base\.json'/g) ?? + []; + expect(tsconfigs).toHaveLength(launches.length); + } + ); +}); diff --git a/docs/architecture/stalker-mock-server.md b/docs/architecture/stalker-mock-server.md index e1d6f2311..93cd022ef 100644 --- a/docs/architecture/stalker-mock-server.md +++ b/docs/architecture/stalker-mock-server.md @@ -364,7 +364,11 @@ form is the one specs should use. webServer: [ { command: webServerCommand /* web:serve */, url: baseURL }, { - command: 'pnpm nx run stalker-mock-server:serve', + command: 'node --import tsx apps/stalker-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, url: `http://localhost:${process.env['MOCK_PORT'] ?? '3210'}/health`, reuseExistingServer: !process.env['CI'], }, @@ -374,12 +378,20 @@ webServer: [ Playwright waits for every server to be healthy before starting tests. If one is already running (e.g. in local dev), it reuses the existing instance. +The mock runs as a single `node` process instead of `pnpm nx run +stalker-mock-server:serve`, because Nx starts its command in a detached +process group that Playwright's process-group kill never reached, so the server +outlived the run. The +[Xtream mock's Playwright section](xtream-mock-server.md#playwright-integration) +has the details. + **`MOCK_PORT` relocates the whole run.** Playwright's health-check URL and the `MOCK_SERVER` constants in the specs read it, and `main.ts` resolves the server's port as `PORT`, then `MOCK_PORT`, then `3210` — so -`MOCK_PORT=3310 pnpm exec playwright test …` starts the Nx-managed mock on +`MOCK_PORT=3310 pnpm exec playwright test …` starts the mock on 3310 and points every spec at it, which is how two worktrees run E2E side by -side when one already holds 3210. This only works because the `serve` and +side when one already holds 3210. A hand-started Nx mock honours the same +knobs because the `serve` and `serve-with-watch` targets no longer pin `PORT` in `project.json`: an `env` entry in `nx:run-commands` overrides the shell (`{...process.env, ...env}`), so a pinned value silently discarded every override. `PORT=3310 pnpm nx run diff --git a/docs/architecture/xtream-mock-server.md b/docs/architecture/xtream-mock-server.md index ffcbd26af..7bdaa6c05 100644 --- a/docs/architecture/xtream-mock-server.md +++ b/docs/architecture/xtream-mock-server.md @@ -550,13 +550,31 @@ The mock server is listed as a third `webServer` entry: ```typescript { - command: 'pnpm nx run xtream-mock-server:serve', - url: 'http://localhost:3211/health', + command: 'node --import tsx apps/xtream-mock-server/src/main.ts', + env: { + NODE_ENV: 'development', + TSX_TSCONFIG_PATH: 'tsconfig.base.json', + }, + url: `http://localhost:${process.env['XTREAM_MOCK_PORT'] ?? '3211'}/health`, reuseExistingServer: !process.env['CI'], cwd: workspaceRoot, } ``` +Every Playwright config (web, Electron, Xtream benchmark, journeys) starts the +Xtream and Stalker mocks this way, never through `pnpm nx run …:serve`. +Playwright stops a `webServer` by sending `SIGKILL` to the process group it +spawned (`taskkill /T /F` on Windows), and Nx `run-commands` starts its command +in a detached process group of its own. The kill therefore reached only +`pnpm`/`nx`, and the `tsx` server was reparented and kept the port, which made +the next run fail with "…/health is already used" or, with +`reuseExistingServer`, silently reuse a stale server. `node --import tsx` keeps +the server a single process in Playwright's group. `TSX_TSCONFIG_PATH` stands in +for the serve target's `--tsconfig` flag and is required for the `@iptvnator/*` +path aliases. `project-config.spec.ts` guards every `playwright*.config.ts` +against regressing to the Nx launch. The `serve` targets remain the entry point +for starting a mock by hand. + ### Request Interception The Angular PWA calls `localhost:3000/xtream?...`. Playwright intercepts these: