feat: export e2e tests for a chosen scenario version
CI / changes (pull_request) Successful in 14s
CI / server (pull_request) Successful in 2m25s
CI / client (pull_request) Successful in 1m35s
CI / image (pull_request) Skipped

- GET /scenarios/:id/export-e2e and the export_e2e_test MCP tool take
  an optional version, defaulting to the latest
- build the test project from that version's steps, name and timeout
- put the version in the zip name (<scenario>-vN-e2e-test.zip), the
  generated package name and the README heading
- the Export e2e test button exports the version being viewed
- document the e2e export in scenario.md and mcp.md
This commit is contained in:
ars9 committed 2026-10-08 18:37:33 +03:00
1 parent 14763103cf
commit 81164f8494
8 files changed
+102 -17

No files matched your search

+9 -3
View File
@@ -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: [
{
+16 -8
View File
@@ -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<string, string | Buffer> = {
"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
+7 -2
View File
@@ -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);
+58
View File
@@ -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<string, string | Buffer>) => Promise<Buffer>;
};
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()}`;