diff --git a/client/src/api/client.ts b/client/src/api/client.ts index d93ef70..a5459a7 100644 --- a/client/src/api/client.ts +++ b/client/src/api/client.ts @@ -275,8 +275,9 @@ export const scenarios = { } return request(`/scenarios/${id}/export?${params.toString()}`); }, - exportE2eTestUrl(id: string): string { - return `${BASE_URL}/scenarios/${id}/export-e2e`; + exportE2eTestUrl(id: string, version?: number): string { + const query = version === undefined ? '' : `?version=${version}`; + return `${BASE_URL}/scenarios/${id}/export-e2e${query}`; }, usedSnippets(id: string): Promise { return request(`/scenarios/${id}/used-snippets`); diff --git a/client/src/pages/scenario/ScenarioDetailPage.tsx b/client/src/pages/scenario/ScenarioDetailPage.tsx index a476ad6..317449e 100644 --- a/client/src/pages/scenario/ScenarioDetailPage.tsx +++ b/client/src/pages/scenario/ScenarioDetailPage.tsx @@ -504,7 +504,10 @@ export function ScenarioDetailPage() { variant="secondary" size="sm" onClick={() => - handleFileDownload(scenarios.exportE2eTestUrl(id!), `${scenario.name}-e2e-test.zip`) + handleFileDownload( + scenarios.exportE2eTestUrl(id!, scenario.version), + `${scenario.name}-v${scenario.version}-e2e-test.zip`, + ) } > diff --git a/docs/mcp.md b/docs/mcp.md index 4842ee3..ee4fa69 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -61,7 +61,7 @@ The server registers the following tools. All entity IDs are UUIDs; list tools a | `export_scenario` | Export one scenario version (optional `version`, defaults to the latest) | | `import_scenario` | Import one scenario version; `version` is required (see "Versioning" in [scenario.md](scenario.md)) | | `import_yaml` | Import any mix of environments, credentials, snippets and scenarios from YAML (see "Import" in [scenario.md](scenario.md)) | -| `export_e2e_test` | Export a scenario as a standalone Playwright test project (zip, base64) | +| `export_e2e_test` | Export one scenario version as a standalone Playwright test project (zip, base64; optional `version`, defaults to the latest) | Changes to scenario content (fields and steps) create a new scenario version; credential assignments and files are not versioned. diff --git a/docs/scenario.md b/docs/scenario.md index de9895a..14cb5bc 100644 --- a/docs/scenario.md +++ b/docs/scenario.md @@ -114,6 +114,10 @@ steps: execCode: await context.page.goto(context.getEnv("base_url")); ``` +### E2e test export + +`GET /scenarios/:id/export-e2e` (the "Export e2e test" button, the `export_e2e_test` MCP tool) exports one scenario version as a standalone Playwright test project, zipped as `-v-e2e-test.zip`. It takes an optional `version` (default: the latest). Snippets are bundled the same way as in the scenario export: the latest version for plain `runSnippet("alias")` calls, plus each version pinned with `alias@N`. The environment, credentials and files are the scenario's current ones; they are not versioned. + ## Playwright traces A run can record a [Playwright trace](https://playwright.dev/docs/trace-viewer) of its browser context (screenshots and DOM snapshots). When the run finishes, the trace is uploaded to the run's files as `trace.zip`. Download it from the run detail page, then open it with `npx playwright show-trace trace.zip` or at https://trace.playwright.dev. diff --git a/server/src/mcp/mcp.service.ts b/server/src/mcp/mcp.service.ts index d97626f..cdb7ff0 100644 --- a/server/src/mcp/mcp.service.ts +++ b/server/src/mcp/mcp.service.ts @@ -1343,15 +1343,21 @@ export class McpService { "export_e2e_test", { description: - "Export a scenario as a standalone, plug-and-play Playwright e2e test project (package.json, playwright.config.ts, test.spec.ts, .env), zipped and returned as base64", + "Export one version of a scenario as a standalone, plug-and-play Playwright e2e test project (package.json, playwright.config.ts, test.spec.ts, .env), zipped and returned as base64", inputSchema: { id: z.uuid().describe("Scenario ID to export"), + version: z + .number() + .int() + .min(1) + .optional() + .describe("Scenario version to export. Defaults to the latest"), }, }, - async ({ id }) => { + async ({ id, version }) => { try { const { filename, buffer } = - await this.e2eExportService.buildE2eTestPackage(id); + await this.e2eExportService.buildE2eTestPackage(id, version); return { content: [ { diff --git a/server/src/scenario/e2e-export.service.ts b/server/src/scenario/e2e-export.service.ts index b4cbd7f..b2ae4fb 100644 --- a/server/src/scenario/e2e-export.service.ts +++ b/server/src/scenario/e2e-export.service.ts @@ -42,8 +42,16 @@ export class E2eExportService { private readonly snippetService: SnippetService, ) {} - async buildE2eTestPackage(scenarioId: string): Promise<{ filename: string; buffer: Buffer }> { + /** Builds the package for the latest version, or `version` when given. */ + async buildE2eTestPackage( + scenarioId: string, + version?: number, + ): Promise<{ filename: string; buffer: Buffer }> { const scenario = await this.scenarioService.findOne(scenarioId); + const content = await this.scenarioService.findVersion( + scenarioId, + version ?? scenario.version, + ); const [credentialMap, snippetMap, attachedFiles] = await Promise.all([ this.scenarioService.buildCredentialMap(scenarioId), this.snippetService.buildSnippetMap(), @@ -51,10 +59,10 @@ export class E2eExportService { ]); const envData = scenario.environment?.data ?? {}; - const slug = slugify(scenario.name); + const slug = `${slugify(content.name)}-v${content.version}`; const files: Record = { "package.json": this.buildPackageJson(slug), - "playwright.config.ts": this.buildPlaywrightConfig(scenario.timeoutSeconds), + "playwright.config.ts": this.buildPlaywrightConfig(content.timeoutSeconds), ".env": this.buildEnvFile(envData), "credentials.json": JSON.stringify(credentialMap, null, 2) + "\n", ".gitignore": [ @@ -65,7 +73,7 @@ export class E2eExportService { ".env", "credentials.json", ].join("\n") + "\n", - "README.md": this.buildReadme(scenario.name, scenario.description), + "README.md": this.buildReadme(content.name, content.version, content.description), }; // Bundle any files attached to the scenario so getScenarioFiles works offline. @@ -97,7 +105,7 @@ export class E2eExportService { // Bundle only the snippets actually referenced (directly or transitively via // other snippets) by this scenario's steps, as individual .js files under snippets/. const usedAliases = resolveUsedSnippetAliases( - scenario.steps.map((s) => s.execCode ?? ""), + content.steps.map((s) => s.execCode ?? ""), snippetMap, ); const snippetManifest: { alias: string; filename: string }[] = []; @@ -116,7 +124,7 @@ export class E2eExportService { snippetManifest.push({ alias, filename }); } - files["test.spec.ts"] = this.buildTestSpec(scenario, snippetManifest, fileManifest); + files["test.spec.ts"] = this.buildTestSpec(content, snippetManifest, fileManifest); const buffer = await this.zip(files); return { filename: `${slug}-e2e-test.zip`, buffer }; @@ -173,9 +181,9 @@ export default defineConfig({ ); } - private buildReadme(scenarioName: string, description: string | null): string { + private buildReadme(scenarioName: string, version: number, description: string | null): string { const descriptionSection = description?.trim() ? `${description.trim()}\n\n` : ""; - return `# ${scenarioName} - standalone e2e test + return `# ${scenarioName} (v${version}) - standalone e2e test ${descriptionSection}## Setup diff --git a/server/src/scenario/scenario.controller.ts b/server/src/scenario/scenario.controller.ts index 7117e7b..d2fd47a 100644 --- a/server/src/scenario/scenario.controller.ts +++ b/server/src/scenario/scenario.controller.ts @@ -203,12 +203,17 @@ export class ScenarioController { summary: "Export a scenario as a standalone, plug-and-play Playwright e2e test (zip)", }) @ApiResponse({ status: 200, description: "Zip archive" }) - @ApiResponse({ status: 404, description: "Scenario not found" }) + @ApiResponse({ status: 404, description: "Scenario or version not found" }) async exportE2eTest( @Param("id", ParseUUIDPipe) id: string, + @Query("version", new ParseIntPipe({ optional: true })) + version: number | undefined, @Res() res: Response, ) { - const { filename, buffer } = await this.e2eExportService.buildE2eTestPackage(id); + const { filename, buffer } = await this.e2eExportService.buildE2eTestPackage( + id, + version, + ); res.setHeader("Content-Type", "application/zip"); res.setHeader("Content-Disposition", `attachment; filename="${filename}"`); res.setHeader("Content-Length", buffer.length); diff --git a/server/test/versioning.spec.ts b/server/test/versioning.spec.ts index ff70cc7..759c1a1 100644 --- a/server/test/versioning.spec.ts +++ b/server/test/versioning.spec.ts @@ -4,6 +4,7 @@ * - runs record and execute the scenario version they were created with * - runSnippet("alias@N") pins a snippet version * - export/import carry and restore exact versions + * - e2e test exports build the requested version */ import { INestApplication } from "@nestjs/common"; @@ -12,6 +13,7 @@ import { DataSource } from "typeorm"; import { buildTestApp } from "./app.harness"; import { CredentialEntity } from "../src/credential/credential.entity"; import { EnvironmentEntity } from "../src/environment/environment.entity"; +import { E2eExportService } from "../src/scenario/e2e-export.service"; import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service"; import { ScenarioService } from "../src/scenario/scenario.service"; import { SnippetService } from "../src/snippet/snippet.service"; @@ -367,6 +369,62 @@ describe("Versioning", () => { }); }); + describe("e2e test export", () => { + /** Captures the files the next e2e export zips. */ + function captureZippedFiles() { + const service = app.get(E2eExportService) as unknown as { + zip: (files: Record) => Promise; + }; + const spy = jest.spyOn(service, "zip"); + return () => { + const files = spy.mock.calls[0][0]; + spy.mockRestore(); + return files; + }; + } + + it("exports the latest version, or the one asked for", async () => { + const name = `E2e ${uniq()}`; + const slug = name.toLowerCase().replace(" ", "-"); + const sc = await scenarioService.create({ name }); + await scenarioService.createStep(sc.id, { execCode: "return 'v2';" }); + + let files = captureZippedFiles(); + const latest = await request(app.getHttpServer()) + .get(`/scenarios/${sc.id}/export-e2e`) + .expect(200); + expect(latest.headers["content-disposition"]).toBe( + `attachment; filename="${slug}-v2-e2e-test.zip"`, + ); + let zipped = files(); + expect(zipped["test.spec.ts"]).toContain("return 'v2';"); + expect(JSON.parse(zipped["package.json"] as string).name).toBe( + `${slug}-v2-e2e-test`, + ); + expect(zipped["README.md"]).toContain(`# ${name} (v2)`); + + files = captureZippedFiles(); + const first = await request(app.getHttpServer()) + .get(`/scenarios/${sc.id}/export-e2e?version=1`) + .expect(200); + expect(first.headers["content-disposition"]).toBe( + `attachment; filename="${slug}-v1-e2e-test.zip"`, + ); + zipped = files(); + expect(zipped["test.spec.ts"]).not.toContain("return 'v2';"); + }); + + it("rejects a missing or invalid version", async () => { + const sc = await scenarioService.create({ name: `e2e-${uniq()}` }); + await request(app.getHttpServer()) + .get(`/scenarios/${sc.id}/export-e2e?version=9`) + .expect(404); + await request(app.getHttpServer()) + .get(`/scenarios/${sc.id}/export-e2e?version=abc`) + .expect(400); + }); + }); + describe("snippet export/import", () => { it("export includes the version and import requires it", async () => { const alias = `sexp-${uniq()}`;