/** * Integration tests for Playwright trace recording on scenario runs. * * The playwright mock writes a fake archive when tracing.stop() is given a * path, so we can assert that a `trace.zip` run artifact is (or is not) * uploaded depending on the run's trace mode and outcome. */ import { INestApplication } from "@nestjs/common"; import request from "supertest"; import { DataSource } from "typeorm"; import { buildTestApp } from "./app.harness"; import { TraceMode } from "../src/config/app.config"; import { EnvironmentEntity } from "../src/environment/environment.entity"; import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service"; import { ScenarioService } from "../src/scenario/scenario.service"; describe("ScenarioSchedulerService Playwright traces", () => { let app: INestApplication; let dataSource: DataSource; let scenarioService: ScenarioService; let scheduler: ScenarioSchedulerService; beforeAll(async () => { app = await buildTestApp(); dataSource = app.get(DataSource); scenarioService = app.get(ScenarioService); scheduler = app.get(ScenarioSchedulerService); }); afterAll(async () => { await app.close(); }); // ── helpers ──────────────────────────────────────────────────────────────── async function runScenario( name: string, execCode: string, traceMode?: TraceMode, ): Promise { const sc = await scenarioService.create({ name }); await scenarioService.createStep(sc.id, { execCode }); const envRepo = dataSource.getRepository(EnvironmentEntity); const env = await envRepo.save( envRepo.create({ name: `${name}-env`, data: {} }), ); const run = await scenarioService.createRun( sc.id, env.id, false, traceMode, ); await scheduler.pickUpPendingRuns(); await scenarioService.waitForRun(sc.id, run.id, 10_000); // The trace is saved after the run reaches its final status; wait for // the scheduler to finish its cleanup for this run. const activeRuns = (scheduler as unknown as { activeRuns: Set }) .activeRuns; const deadline = Date.now() + 5_000; while (activeRuns.has(run.id) && Date.now() < deadline) { await new Promise((r) => setTimeout(r, 20)); } return run.id; } async function traceArtifacts( runId: string, ): Promise<{ mimeType: string }[]> { return dataSource.query( `SELECT f.mimeType FROM scenario_run_files rf JOIN files f ON f.id = rf.fileId WHERE rf.runId = ? AND f.originalName = 'trace.zip'`, [runId], ); } // ── tests ────────────────────────────────────────────────────────────────── it("uploads trace.zip for a passing run in 'on' mode", async () => { const runId = await runScenario("trace-on-pass", "return 1;", TraceMode.On); const files = await traceArtifacts(runId); expect(files).toHaveLength(1); expect(files[0].mimeType).toBe("application/zip"); }); it("uploads trace.zip for a failing run in 'retain-on-failure' mode", async () => { const runId = await runScenario( "trace-rof-fail", "throw new Error('boom');", TraceMode.RetainOnFailure, ); expect(await traceArtifacts(runId)).toHaveLength(1); }); it("discards the trace of a passing run in 'retain-on-failure' mode", async () => { const runId = await runScenario( "trace-rof-pass", "return 1;", TraceMode.RetainOnFailure, ); expect(await traceArtifacts(runId)).toHaveLength(0); }); it("does not record a trace in 'off' mode", async () => { const runId = await runScenario( "trace-off-fail", "throw new Error('boom');", TraceMode.Off, ); expect(await traceArtifacts(runId)).toHaveLength(0); }); it("stores the configured default trace mode when none is given", async () => { const runId = await runScenario("trace-default", "return 1;"); const [row] = await dataSource.query( `SELECT traceMode FROM scenario_runs WHERE id = ?`, [runId], ); expect(row.traceMode).toBe("on"); expect(await traceArtifacts(runId)).toHaveLength(1); }); it("falls back to the configured mode for runs without a stored trace mode", async () => { const sc = await scenarioService.create({ name: "trace-null-mode" }); await scenarioService.createStep(sc.id, { execCode: "return 1;" }); const envRepo = dataSource.getRepository(EnvironmentEntity); const env = await envRepo.save( envRepo.create({ name: "trace-null-mode-env", data: {} }), ); const run = await scenarioService.createRun(sc.id, env.id); await dataSource.query( `UPDATE scenario_runs SET traceMode = NULL WHERE id = ?`, [run.id], ); await scheduler.pickUpPendingRuns(); await scenarioService.waitForRun(sc.id, run.id, 10_000); const activeRuns = (scheduler as unknown as { activeRuns: Set }) .activeRuns; const deadline = Date.now() + 5_000; while (activeRuns.has(run.id) && Date.now() < deadline) { await new Promise((r) => setTimeout(r, 20)); } expect(await traceArtifacts(run.id)).toHaveLength(1); }); describe("HTTP", () => { it("GET /scenarios/run-defaults returns the configured trace mode", async () => { const res = await request(app.getHttpServer()).get( "/scenarios/run-defaults", ); expect(res.status).toBe(200); expect(res.body).toEqual({ traceMode: "on" }); }); it("POST /scenarios/:id/run stores the requested trace mode", async () => { const sc = await scenarioService.create({ name: "trace-http" }); const envRepo = dataSource.getRepository(EnvironmentEntity); const env = await envRepo.save( envRepo.create({ name: "trace-http-env", data: {} }), ); const res = await request(app.getHttpServer()) .post(`/scenarios/${sc.id}/run`) .send({ environmentId: env.id, traceMode: "retain-on-failure" }); expect(res.status).toBe(201); expect(res.body.traceMode).toBe("retain-on-failure"); }); it("POST /scenarios/:id/run rejects an unknown trace mode", async () => { const sc = await scenarioService.create({ name: "trace-http-bad" }); const envRepo = dataSource.getRepository(EnvironmentEntity); const env = await envRepo.save( envRepo.create({ name: "trace-http-bad-env", data: {} }), ); const res = await request(app.getHttpServer()) .post(`/scenarios/${sc.id}/run`) .send({ environmentId: env.id, traceMode: "sometimes" }); expect(res.status).toBe(400); }); }); });