) => {
- const file = e.target.files?.[0];
- if (!file) return;
- e.target.value = '';
- try {
- const text = await file.text();
- const payload = yamlParse(text) as unknown;
- const imported = await snippets.importSnippet(payload);
- navigate(`/snippets/${imported.id}`);
- } catch {
- // Import errors are silently ignored
- }
- };
+ const handleImport = useImportFile('snippet');
return (
diff --git a/docs/mcp.md b/docs/mcp.md
index 04e8da3..569195f 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -51,6 +51,7 @@ The server currently registers the following tools.
| `wait_for_scenario_run` | Wait until run is `pass` or `fail` |
| `export_scenario` | Export one scenario version (optional `version`, defaults to latest) |
| `import_scenario` | Import one scenario version; `version` is required (see "Versioning" in `scenario.md`) |
+| `import_yaml` | Import any mix of environments, credentials, snippets and scenarios from YAML (see "Import" in `scenario.md`) |
## Snippets
diff --git a/docs/scenario.md b/docs/scenario.md
index 4e4b7de..42fcac6 100644
--- a/docs/scenario.md
+++ b/docs/scenario.md
@@ -61,6 +61,38 @@ Exports hold exactly one version and always include its `version` number (the la
- Otherwise it is stored under that number, and becomes the latest if it is higher than the current latest version.
- Without an `id`, a new scenario or snippet is created at the given version.
+## Import
+
+`POST /import` (the Import page in the UI, and the Import buttons on the list pages; the `import_yaml` MCP tool) imports any mix of environments, credentials, snippets and scenarios.
+
+- Send YAML with `Content-Type: application/yaml`. A file is a stream of `---`-separated documents; each document holds one exported item or a list of items. JSON is valid YAML, and a JSON body (one item or an array) is accepted too.
+- Every item needs a `kind`: `environment`, `credential`, `snippet` or `scenario`. Snippets and scenarios also need `version` (see above).
+- All items are validated before anything is imported, so one invalid item means nothing from that request is imported. The error lists every invalid item by its position.
+- Items are imported in dependency order - environments, credentials, snippets, scenarios - regardless of their order in the file, so a scenario can reference an environment or credential from the same file.
+- The response lists what was imported: `{ imported: [{ kind, id, name, version? }] }`.
+
+A scenario export (`GET /scenarios/:id/export`, optionally with its environment and credentials) is in this format, one document per entity, and can be imported back as is.
+
+```yaml
+---
+# Environment: staging
+kind: environment
+id: 5b0c…
+name: staging
+data:
+ base_url: https://staging.example
+---
+# Scenario: Login flow
+kind: scenario
+id: 9e1f…
+version: 3
+name: Login flow
+environmentId: 5b0c…
+steps:
+ - title: Open login page
+ execCode: await context.page.goto(context.getEnv("base_url"));
+```
+
## 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/package-lock.json b/package-lock.json
index 2efa8c7..c7c0a06 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "liqa",
- "version": "1.17.0",
+ "version": "1.18.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "liqa",
- "version": "1.17.0",
+ "version": "1.18.0",
"workspaces": [
"server",
"client"
diff --git a/package.json b/package.json
index 35b849b..4cb1bac 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "liqa",
- "version": "1.17.0",
+ "version": "1.18.0",
"private": true,
"workspaces": [
"server",
diff --git a/server/src/app.module.ts b/server/src/app.module.ts
index c921cd5..4e14182 100644
--- a/server/src/app.module.ts
+++ b/server/src/app.module.ts
@@ -13,6 +13,7 @@ import { FileModule } from "./file/file.module";
import { ScenarioFileEntity } from "./file/scenario-file.entity";
import { ScenarioRunFileEntity } from "./file/scenario-run-file.entity";
import { HealthModule } from "./health/health.module";
+import { ImportModule } from "./import/import.module";
import { McpModule } from "./mcp/mcp.module";
import { ScenarioCredentialEntity } from "./scenario/scenario-credential.entity";
import { ScenarioRunLogEntity } from "./scenario/scenario-run-log.entity";
@@ -66,6 +67,7 @@ import { SnippetModule } from "./snippet/snippet.module";
ScenarioModule,
SnippetModule,
FileModule,
+ ImportModule,
McpModule,
HealthModule,
],
diff --git a/server/src/code-executor/code-executor.service.ts b/server/src/code-executor/code-executor.service.ts
index 7146805..a58cbd0 100644
--- a/server/src/code-executor/code-executor.service.ts
+++ b/server/src/code-executor/code-executor.service.ts
@@ -6,7 +6,7 @@ import {
import { expect as playwrightExpect } from "@playwright/test";
import { parse } from "acorn";
import type { BrowserContext, Page } from "playwright";
-import { URL } from "url";
+import { URL } from "node:url";
import { downloadFile as downloadFileImpl } from "../common/file-downloader";
import { TraceLogger } from "../common/trace-logger";
import type { EnvironmentData } from "../environment/environment.entity";
diff --git a/server/src/common/body-parsers.ts b/server/src/common/body-parsers.ts
new file mode 100644
index 0000000..bf41cc6
--- /dev/null
+++ b/server/src/common/body-parsers.ts
@@ -0,0 +1,14 @@
+import type { NestExpressApplication } from "@nestjs/platform-express";
+
+/** Content types accepted as raw YAML text (e.g. by `POST /import`). */
+export const YAML_CONTENT_TYPES = [
+ "application/yaml",
+ "application/x-yaml",
+ "text/yaml",
+ "text/x-yaml",
+];
+
+/** Registers body parsers beyond Nest's default JSON/urlencoded ones. */
+export function configureBodyParsers(app: NestExpressApplication): void {
+ app.useBodyParser("text", { type: YAML_CONTENT_TYPES, limit: "10mb" });
+}
diff --git a/server/src/common/file-downloader.ts b/server/src/common/file-downloader.ts
index 0ab9dc2..fb876cc 100644
--- a/server/src/common/file-downloader.ts
+++ b/server/src/common/file-downloader.ts
@@ -1,6 +1,6 @@
-import * as https from "https";
-import * as http from "http";
-import { URL } from "url";
+import * as https from "node:https";
+import * as http from "node:http";
+import { URL } from "node:url";
const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100 MB
const TIMEOUT_MS = 30000; // 30 seconds
diff --git a/server/src/common/trace-context.ts b/server/src/common/trace-context.ts
index b794db8..f3c1dc3 100644
--- a/server/src/common/trace-context.ts
+++ b/server/src/common/trace-context.ts
@@ -1,4 +1,4 @@
-import { AsyncLocalStorage } from "async_hooks";
+import { AsyncLocalStorage } from "node:async_hooks";
export interface TraceStore {
traceId: string;
diff --git a/server/src/file/file-storage.service.ts b/server/src/file/file-storage.service.ts
index 3453ebb..ad3fe5e 100644
--- a/server/src/file/file-storage.service.ts
+++ b/server/src/file/file-storage.service.ts
@@ -2,9 +2,9 @@ import { Injectable, OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { SchedulerRegistry } from "@nestjs/schedule";
import { InjectRepository } from "@nestjs/typeorm";
-import * as crypto from "crypto";
-import * as fs from "fs/promises";
-import * as path from "path";
+import * as crypto from "node:crypto";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import { CronJob } from "cron";
import { Repository } from "typeorm";
import { TraceLogger } from "../common/trace-logger";
diff --git a/server/src/import/import.controller.ts b/server/src/import/import.controller.ts
new file mode 100644
index 0000000..657ce1f
--- /dev/null
+++ b/server/src/import/import.controller.ts
@@ -0,0 +1,45 @@
+import { BadRequestException, Body, Controller, Post } from "@nestjs/common";
+import {
+ ApiBody,
+ ApiConsumes,
+ ApiOperation,
+ ApiResponse,
+ ApiTags,
+} from "@nestjs/swagger";
+import { YAML_CONTENT_TYPES } from "../common/body-parsers";
+import { ImportService } from "./import.service";
+
+@ApiTags("import")
+@Controller("import")
+export class ImportController {
+ constructor(private readonly importService: ImportService) {}
+
+ @Post()
+ @ApiOperation({
+ summary:
+ "Import any mix of environments, credentials, snippets and scenarios",
+ description:
+ "Send YAML (Content-Type application/yaml): `---`-separated documents, each holding one exported item or a list of them. " +
+ "A JSON array of items (or a single item) is accepted too. Every item needs a `kind`. " +
+ "All items are validated before anything is imported, then imported in dependency order: environments, credentials, snippets, scenarios.",
+ })
+ @ApiConsumes(...YAML_CONTENT_TYPES, "application/json")
+ @ApiBody({ schema: { type: "string" } })
+ @ApiResponse({ status: 201, description: "Imported items" })
+ @ApiResponse({
+ status: 400,
+ description: "Invalid YAML or items; nothing was imported",
+ })
+ import(@Body() body: unknown) {
+ if (typeof body === "string") {
+ return this.importService.importYaml(body);
+ }
+ if (Array.isArray(body)) return this.importService.importItems(body);
+ if (body && typeof body === "object" && Object.keys(body).length > 0) {
+ return this.importService.importItems([body]);
+ }
+ throw new BadRequestException(
+ "Send YAML with Content-Type application/yaml, or a JSON item or array of items",
+ );
+ }
+}
diff --git a/server/src/import/import.module.ts b/server/src/import/import.module.ts
new file mode 100644
index 0000000..effd3c5
--- /dev/null
+++ b/server/src/import/import.module.ts
@@ -0,0 +1,15 @@
+import { Module } from "@nestjs/common";
+import { CredentialModule } from "../credential/credential.module";
+import { EnvironmentModule } from "../environment/environment.module";
+import { ScenarioModule } from "../scenario/scenario.module";
+import { SnippetModule } from "../snippet/snippet.module";
+import { ImportController } from "./import.controller";
+import { ImportService } from "./import.service";
+
+@Module({
+ imports: [EnvironmentModule, CredentialModule, SnippetModule, ScenarioModule],
+ controllers: [ImportController],
+ providers: [ImportService],
+ exports: [ImportService],
+})
+export class ImportModule {}
diff --git a/server/src/import/import.service.ts b/server/src/import/import.service.ts
new file mode 100644
index 0000000..dd9a567
--- /dev/null
+++ b/server/src/import/import.service.ts
@@ -0,0 +1,176 @@
+import { BadRequestException, Injectable } from "@nestjs/common";
+import { ClassConstructor, plainToInstance } from "class-transformer";
+import { validateSync, ValidationError } from "class-validator";
+import { parseAllDocuments } from "yaml";
+import { CredentialExportDto } from "../credential/dto/credential-export.dto";
+import { CredentialService } from "../credential/credential.service";
+import { EnvironmentExportDto } from "../environment/dto/environment-export.dto";
+import { EnvironmentService } from "../environment/environment.service";
+import { ScenarioExportDto } from "../scenario/dto/scenario-export.dto";
+import { ScenarioService } from "../scenario/scenario.service";
+import { SnippetExportDto } from "../snippet/dto/snippet-export.dto";
+import { SnippetService } from "../snippet/snippet.service";
+
+export type ImportKind = "environment" | "credential" | "snippet" | "scenario";
+
+/** Import order: later kinds may reference earlier ones (scenarios → environments/credentials). */
+const KIND_ORDER: ImportKind[] = [
+ "environment",
+ "credential",
+ "snippet",
+ "scenario",
+];
+
+const DTO_BY_KIND: Record> = {
+ environment: EnvironmentExportDto,
+ credential: CredentialExportDto,
+ snippet: SnippetExportDto,
+ scenario: ScenarioExportDto,
+};
+
+export interface ImportedItem {
+ kind: ImportKind;
+ id: string;
+ name: string;
+ /** Version stored for versioned kinds (snippets, scenarios). */
+ version?: number;
+}
+
+export interface ImportResult {
+ imported: ImportedItem[];
+}
+
+function formatErrors(errors: ValidationError[], prefix = ""): string[] {
+ return errors.flatMap((e) => {
+ const field = prefix ? `${prefix}.${e.property}` : e.property;
+ return [
+ ...Object.values(e.constraints ?? {}).map((msg) => `${field}: ${msg}`),
+ ...formatErrors(e.children ?? [], field),
+ ];
+ });
+}
+
+/**
+ * Imports any mix of exported environments, credentials, snippets and
+ * scenarios in one go. Each item must carry its `kind`.
+ */
+@Injectable()
+export class ImportService {
+ constructor(
+ private readonly environmentService: EnvironmentService,
+ private readonly credentialService: CredentialService,
+ private readonly snippetService: SnippetService,
+ private readonly scenarioService: ScenarioService,
+ ) {}
+
+ /**
+ * Parses a YAML stream: `---`-separated documents, each holding a single
+ * item or a list of items. JSON is valid YAML, so JSON input works too.
+ */
+ parseYaml(text: string): unknown[] {
+ const docs = parseAllDocuments(text);
+ const items: unknown[] = [];
+ docs.forEach((doc, index) => {
+ if (doc.errors.length > 0) {
+ throw new BadRequestException(
+ `YAML document ${index + 1}: ${doc.errors[0].message}`,
+ );
+ }
+ const value: unknown = doc.toJS();
+ if (value == null) return; // empty document, e.g. a trailing `---`
+ items.push(...(Array.isArray(value) ? value : [value]));
+ });
+ return items;
+ }
+
+ async importYaml(text: string): Promise {
+ return this.importItems(this.parseYaml(text));
+ }
+
+ /**
+ * Validates every item before importing any, then imports them in
+ * dependency order (environments, credentials, snippets, scenarios).
+ */
+ async importItems(items: unknown[]): Promise {
+ if (items.length === 0) {
+ throw new BadRequestException("Nothing to import");
+ }
+
+ const errors: string[] = [];
+ const validated: { kind: ImportKind; dto: object }[] = [];
+ items.forEach((item, index) => {
+ const label = `Item ${index + 1}`;
+ if (item == null || typeof item !== "object" || Array.isArray(item)) {
+ errors.push(`${label}: must be an object`);
+ return;
+ }
+ const kind = (item as { kind?: unknown }).kind;
+ if (typeof kind !== "string" || !(kind in DTO_BY_KIND)) {
+ errors.push(
+ `${label}: kind must be one of ${KIND_ORDER.join(", ")} (got ${JSON.stringify(kind)})`,
+ );
+ return;
+ }
+ const dto = plainToInstance(DTO_BY_KIND[kind as ImportKind], item);
+ const itemErrors = formatErrors(validateSync(dto, { whitelist: true }));
+ if (itemErrors.length > 0) {
+ errors.push(`${label} (${kind}): ${itemErrors.join("; ")}`);
+ return;
+ }
+ validated.push({ kind: kind as ImportKind, dto });
+ });
+ if (errors.length > 0) {
+ throw new BadRequestException(errors);
+ }
+
+ const imported: ImportedItem[] = [];
+ for (const kind of KIND_ORDER) {
+ for (const item of validated.filter((v) => v.kind === kind)) {
+ imported.push(await this.importOne(kind, item.dto));
+ }
+ }
+ return { imported };
+ }
+
+ private async importOne(
+ kind: ImportKind,
+ dto: object,
+ ): Promise {
+ switch (kind) {
+ case "environment": {
+ const env = await this.environmentService.importEnvironment(
+ dto as EnvironmentExportDto,
+ );
+ return { kind, id: env.id, name: env.name };
+ }
+ case "credential": {
+ const cred = await this.credentialService.importCredential(
+ dto as CredentialExportDto,
+ );
+ return { kind, id: cred.id, name: cred.name };
+ }
+ case "snippet": {
+ const snippet = await this.snippetService.importSnippet(
+ dto as SnippetExportDto,
+ );
+ return {
+ kind,
+ id: snippet.id,
+ name: snippet.alias,
+ version: snippet.version,
+ };
+ }
+ case "scenario": {
+ const scenario = await this.scenarioService.importScenario(
+ dto as ScenarioExportDto,
+ );
+ return {
+ kind,
+ id: scenario.id,
+ name: scenario.name,
+ version: scenario.version,
+ };
+ }
+ }
+ }
+}
diff --git a/server/src/interceptors/trace.interceptor.ts b/server/src/interceptors/trace.interceptor.ts
index 225d66b..da29efb 100644
--- a/server/src/interceptors/trace.interceptor.ts
+++ b/server/src/interceptors/trace.interceptor.ts
@@ -4,7 +4,7 @@ import {
Injectable,
NestInterceptor,
} from "@nestjs/common";
-import * as crypto from "crypto";
+import * as crypto from "node:crypto";
import type { Request } from "express";
import { Observable } from "rxjs";
import { traceStorage } from "../common/trace-context";
diff --git a/server/src/main.ts b/server/src/main.ts
index 3fea5f9..71b234d 100644
--- a/server/src/main.ts
+++ b/server/src/main.ts
@@ -1,9 +1,11 @@
import { RequestMethod, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
+import type { NestExpressApplication } from "@nestjs/platform-express";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { name as pkgName, version as pkgVersion } from "../package.json";
import { AppModule } from "./app.module";
+import { configureBodyParsers } from "./common/body-parsers";
import { TraceLogger } from "./common/trace-logger";
import { AppConfig } from "./config/app.config";
import { HttpExceptionFilter } from "./filters/http-exception.filter";
@@ -28,7 +30,7 @@ process.on("unhandledRejection", (reason) => {
async function bootstrap() {
const API_PREFIX = "api/v1";
const logger = new TraceLogger("Bootstrap");
- const app = await NestFactory.create(AppModule, {
+ const app = await NestFactory.create(AppModule, {
logger: new TraceLogger("Bootstrap", { timestamp: true }),
});
@@ -40,6 +42,7 @@ async function bootstrap() {
],
});
+ configureBodyParsers(app);
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TraceInterceptor(), new LoggingInterceptor());
diff --git a/server/src/mcp/mcp.module.ts b/server/src/mcp/mcp.module.ts
index 2e14d8c..de331e3 100644
--- a/server/src/mcp/mcp.module.ts
+++ b/server/src/mcp/mcp.module.ts
@@ -6,6 +6,7 @@ import { EnvironmentModule } from "../environment/environment.module";
import { ScenarioModule } from "../scenario/scenario.module";
import { SessionModule } from "../session/session.module";
import { SnippetModule } from "../snippet/snippet.module";
+import { ImportModule } from "../import/import.module";
import { McpController } from "./mcp.controller";
import { McpService } from "./mcp.service";
@@ -18,6 +19,7 @@ import { McpService } from "./mcp.service";
CodeExecutorModule,
ScenarioModule,
SnippetModule,
+ ImportModule,
],
controllers: [McpController],
providers: [McpService],
diff --git a/server/src/mcp/mcp.service.ts b/server/src/mcp/mcp.service.ts
index 1e0a25a..19b49bf 100644
--- a/server/src/mcp/mcp.service.ts
+++ b/server/src/mcp/mcp.service.ts
@@ -14,6 +14,7 @@ import { CodeExecutorService } from "../code-executor/code-executor.service";
import { ScenarioService } from "../scenario/scenario.service";
import { E2eExportService } from "../scenario/e2e-export.service";
import { SnippetService } from "../snippet/snippet.service";
+import { ImportService } from "../import/import.service";
import pkg from "../../package.json";
@@ -29,6 +30,7 @@ export class McpService {
private readonly scenarioService: ScenarioService,
private readonly e2eExportService: E2eExportService,
private readonly snippetService: SnippetService,
+ private readonly importService: ImportService,
) {}
private createServer(): McpServer {
@@ -1307,6 +1309,35 @@ export class McpService {
},
);
+ server.registerTool(
+ "import_yaml",
+ {
+ description:
+ "Import any mix of exported environments, credentials, snippets and scenarios from YAML: `---`-separated documents, each holding one item or a list of items, each with a `kind`. All items are validated before anything is imported; versioned items (snippets, scenarios) must include `version`",
+ inputSchema: {
+ yaml: z.string().describe("YAML text, e.g. the content of an export file"),
+ },
+ },
+ async ({ yaml }) => {
+ try {
+ const result = await this.importService.importYaml(yaml);
+ return {
+ content: [{ type: "text" as const, text: JSON.stringify(result) }],
+ };
+ } catch (err) {
+ const response = (err as { getResponse?: () => unknown }).getResponse?.();
+ const message =
+ response && typeof response === "object" && "message" in response
+ ? JSON.stringify((response as { message: unknown }).message)
+ : (err as Error).message;
+ return {
+ isError: true,
+ content: [{ type: "text" as const, text: message }],
+ };
+ }
+ },
+ );
+
server.registerTool(
"export_e2e_test",
{
diff --git a/server/src/scenario/e2e-export.service.ts b/server/src/scenario/e2e-export.service.ts
index 44c65d8..f6fa33f 100644
--- a/server/src/scenario/e2e-export.service.ts
+++ b/server/src/scenario/e2e-export.service.ts
@@ -148,7 +148,7 @@ export class E2eExportService {
const timeoutMs = (scenarioTimeoutSeconds ?? DEFAULT_SCENARIO_TIMEOUT_SEC) * 1000;
return `import { defineConfig } from "@playwright/test";
import * as dotenv from "dotenv";
-import * as path from "path";
+import * as path from "node:path";
dotenv.config({ path: path.resolve(__dirname, ".env") });
@@ -235,13 +235,13 @@ the scenario are bundled under \`files/\` and served locally by
return `import { test, expect } from "@playwright/test";
import type { BrowserContext, Page } from "@playwright/test";
-import * as fs from "fs";
-import * as path from "path";
-import * as crypto from "crypto";
-import * as http from "http";
-import * as https from "https";
-import { URL } from "url";
-import { randomUUID } from "crypto";
+import * as fs from "node:fs";
+import * as path from "node:path";
+import * as crypto from "node:crypto";
+import * as http from "node:http";
+import * as https from "node:https";
+import { URL } from "node:url";
+import { randomUUID } from "node:crypto";
// Credential data resolved from this scenario's aliases at export time.
// Stored in credentials.json (gitignored) - contains sensitive values.
diff --git a/server/src/scenario/scenario-scheduler.service.ts b/server/src/scenario/scenario-scheduler.service.ts
index 79d468d..0994ca7 100644
--- a/server/src/scenario/scenario-scheduler.service.ts
+++ b/server/src/scenario/scenario-scheduler.service.ts
@@ -2,10 +2,10 @@ import { Injectable, OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Interval } from "@nestjs/schedule";
import { InjectRepository } from "@nestjs/typeorm";
-import * as crypto from "crypto";
-import * as fs from "fs/promises";
-import * as os from "os";
-import * as path from "path";
+import * as crypto from "node:crypto";
+import * as fs from "node:fs/promises";
+import * as os from "node:os";
+import * as path from "node:path";
import { Effect } from "effect";
import type { Browser, BrowserContext, Page } from "playwright";
import { chromium } from "playwright";
diff --git a/server/src/scenario/scenario.controller.ts b/server/src/scenario/scenario.controller.ts
index a3b61ee..6676a25 100644
--- a/server/src/scenario/scenario.controller.ts
+++ b/server/src/scenario/scenario.controller.ts
@@ -150,20 +150,20 @@ export class ScenarioController {
version,
});
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
- // Serialize as YAML stream: each entity separated by a comment header
+ // Serialize as a YAML stream: one `---` document per entity, each with a
+ // comment naming it. POST /import reads this format back.
const kindLabels: Record = {
environment: "Environment",
credential: "Credential",
scenario: "Scenario",
};
- const parts = (entities as ExportEntity[]).map((entity) => {
- const label = kindLabels[entity.kind ?? ""] ?? entity.kind ?? "Entity";
- const comment = `# --- ${label}: ${
- entity.name ?? entity.id ?? ""
- } ---`;
- return `${comment}\n${yamlStringify(entity)}`;
- });
- return parts.join("\n");
+ return (entities as ExportEntity[])
+ .map((entity) => {
+ const label = kindLabels[entity.kind ?? ""] ?? entity.kind ?? "Entity";
+ const comment = `# ${label}: ${entity.name ?? entity.id ?? ""}`;
+ return `---\n${comment}\n${yamlStringify(entity)}`;
+ })
+ .join("");
}
@Get(":id/export-e2e")
diff --git a/server/src/scenario/scenario.service.ts b/server/src/scenario/scenario.service.ts
index 5092e30..68c2c55 100644
--- a/server/src/scenario/scenario.service.ts
+++ b/server/src/scenario/scenario.service.ts
@@ -7,8 +7,8 @@ import {
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { InjectRepository } from "@nestjs/typeorm";
-import * as fs from "fs/promises";
-import * as path from "path";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import { Like, Repository } from "typeorm";
import {
PaginatedResult,
diff --git a/server/test/__mocks__/playwright.ts b/server/test/__mocks__/playwright.ts
index 06bad94..b5e29bb 100644
--- a/server/test/__mocks__/playwright.ts
+++ b/server/test/__mocks__/playwright.ts
@@ -18,7 +18,7 @@ const makeContext = () => ({
stop: jest.fn().mockImplementation(async (opts?: { path?: string }) => {
if (opts?.path) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
- await require("fs/promises").writeFile(opts.path, "fake trace zip");
+ await require("node:fs/promises").writeFile(opts.path, "fake trace zip");
}
}),
},
diff --git a/server/test/app.harness.ts b/server/test/app.harness.ts
index 1f1d925..741b758 100644
--- a/server/test/app.harness.ts
+++ b/server/test/app.harness.ts
@@ -1,8 +1,16 @@
import { INestApplication, ValidationPipe } from "@nestjs/common";
+import * as fs from "node:fs";
+import * as os from "node:os";
+import * as path from "node:path";
import { Test, TestingModule } from "@nestjs/testing";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule } from "@nestjs/config";
+import type { NestExpressApplication } from "@nestjs/platform-express";
import { ScheduleModule } from "@nestjs/schedule";
+
+import { configureBodyParsers } from "../src/common/body-parsers";
+import { ImportModule } from "../src/import/import.module";
+
jest.mock("@nestjs/common", () => {
const actual = jest.requireActual("@nestjs/common");
// eslint-disable-next-line @typescript-eslint/no-require-imports
@@ -55,6 +63,11 @@ import { ScenarioFileEntity } from "../src/file/scenario-file.entity";
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
export async function buildTestApp(): Promise {
+ // Each test app stores files in its own temp dir: suites run in parallel and
+ // some clean up their files dir, which must not affect other suites.
+ const filesDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-test-files-"));
+ process.env.FILES_DIR = filesDir;
+
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
@@ -89,15 +102,23 @@ export async function buildTestApp(): Promise {
SessionModule,
EnvironmentModule,
ScenarioModule,
+ ImportModule,
McpModule,
],
controllers: [HealthController],
}).compile();
- const app = module.createNestApplication();
+ const app = module.createNestApplication();
+ configureBodyParsers(app);
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new LoggingInterceptor());
await app.init();
+
+ const close = app.close.bind(app);
+ app.close = async () => {
+ await close();
+ fs.rmSync(filesDir, { recursive: true, force: true });
+ };
return app;
}
diff --git a/server/test/exec-context-files.spec.ts b/server/test/exec-context-files.spec.ts
index 237cc71..c25dd65 100644
--- a/server/test/exec-context-files.spec.ts
+++ b/server/test/exec-context-files.spec.ts
@@ -1,7 +1,7 @@
import { INestApplication } from "@nestjs/common";
import { getRepositoryToken } from "@nestjs/typeorm";
-import * as http from "http";
-import * as fs from "fs/promises";
+import * as http from "node:http";
+import * as fs from "node:fs/promises";
import { Repository } from "typeorm";
import { buildTestApp } from "./app.harness";
import { FileStorageService } from "../src/file/file-storage.service";
@@ -13,7 +13,7 @@ import { ScenarioStepEntity } from "../src/scenario/scenario-step.entity";
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
import { ScenarioRunStepEntity } from "../src/scenario/scenario-run-step.entity";
import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service";
-import type { AddressInfo } from "net";
+import type { AddressInfo } from "node:net";
describe("Exec Context File Methods Integration Tests", () => {
let app: INestApplication;
diff --git a/server/test/file.controller.spec.ts b/server/test/file.controller.spec.ts
index e6a6f0a..3634933 100644
--- a/server/test/file.controller.spec.ts
+++ b/server/test/file.controller.spec.ts
@@ -1,7 +1,7 @@
import { INestApplication } from "@nestjs/common";
import { getRepositoryToken } from "@nestjs/typeorm";
import request from "supertest";
-import * as fs from "fs/promises";
+import * as fs from "node:fs/promises";
import { Repository } from "typeorm";
import { buildTestApp } from "./app.harness";
import { FileStorageService } from "../src/file/file-storage.service";
diff --git a/server/test/import.spec.ts b/server/test/import.spec.ts
new file mode 100644
index 0000000..76f724a
--- /dev/null
+++ b/server/test/import.spec.ts
@@ -0,0 +1,219 @@
+/**
+ * Integration tests for the central import (`POST /import`) and the
+ * multi-document YAML scenario export it reads back.
+ */
+
+import { INestApplication } from "@nestjs/common";
+import request from "supertest";
+import { DataSource } from "typeorm";
+import { parseAllDocuments } from "yaml";
+import { buildTestApp } from "./app.harness";
+import { CredentialEntity } from "../src/credential/credential.entity";
+import { EnvironmentEntity } from "../src/environment/environment.entity";
+import { ScenarioService } from "../src/scenario/scenario.service";
+
+describe("Central import", () => {
+ let app: INestApplication;
+ let dataSource: DataSource;
+ let scenarioService: ScenarioService;
+
+ beforeAll(async () => {
+ app = await buildTestApp();
+ dataSource = app.get(DataSource);
+ scenarioService = app.get(ScenarioService);
+ });
+
+ afterAll(async () => {
+ await app.close();
+ });
+
+ const uniq = () => Math.random().toString(36).slice(2, 8);
+
+ function postYaml(yaml: string) {
+ return request(app.getHttpServer())
+ .post("/import")
+ .set("Content-Type", "application/yaml")
+ .send(yaml);
+ }
+
+ it("imports every kind from a multi-document YAML in dependency order", async () => {
+ const envId = crypto.randomUUID();
+ const credId = crypto.randomUUID();
+ const snippetAlias = `imp-${uniq()}`;
+ // Scenario first in the file: it references the environment and credential
+ const yaml = `---
+kind: scenario
+name: imported-${uniq()}
+version: 1
+environmentId: ${envId}
+credentials:
+ - credentialId: ${credId}
+ alias: main
+steps:
+ - title: use snippet
+ execCode: return context.runSnippet("${snippetAlias}");
+---
+kind: snippet
+alias: ${snippetAlias}
+title: Imported snippet
+version: 2
+code: return 1;
+---
+kind: credential
+id: ${credId}
+name: imported-cred
+data: '{"token":"t"}'
+---
+kind: environment
+id: ${envId}
+name: imported-env-${uniq()}
+data:
+ base_url: https://example.test
+`;
+ const res = await postYaml(yaml).expect(201);
+ const kinds = res.body.imported.map((i: { kind: string }) => i.kind);
+ expect(kinds).toEqual(["environment", "credential", "snippet", "scenario"]);
+
+ const scenario = res.body.imported[3];
+ expect(scenario.version).toBe(1);
+ const sc = await scenarioService.findOne(scenario.id);
+ expect(sc.environmentId).toBe(envId);
+ expect(sc.scenarioCredentials.map((c) => c.credentialId)).toEqual([credId]);
+ expect(res.body.imported[2]).toMatchObject({
+ name: snippetAlias,
+ version: 2,
+ });
+ });
+
+ it("accepts a document holding a list of items", async () => {
+ const yaml = `- kind: credential
+ name: list-a-${uniq()}
+- kind: credential
+ name: list-b-${uniq()}
+`;
+ const res = await postYaml(yaml).expect(201);
+ expect(res.body.imported).toHaveLength(2);
+ });
+
+ it("accepts a JSON array", async () => {
+ const res = await request(app.getHttpServer())
+ .post("/import")
+ .send([{ kind: "credential", name: `json-${uniq()}` }])
+ .expect(201);
+ expect(res.body.imported[0].kind).toBe("credential");
+ });
+
+ it("imports nothing when any item is invalid", async () => {
+ const envName = `not-imported-${uniq()}`;
+ const yaml = `---
+kind: environment
+name: ${envName}
+data: {}
+---
+kind: snippet
+alias: no-version-${uniq()}
+code: return 1;
+---
+kind: gadget
+name: what
+`;
+ const res = await postYaml(yaml).expect(400);
+ const message = JSON.stringify(res.body);
+ expect(message).toContain("Item 2 (snippet)");
+ expect(message).toContain("version");
+ expect(message).toContain("Item 3");
+ const envRepo = dataSource.getRepository(EnvironmentEntity);
+ expect(await envRepo.findOneBy({ name: envName })).toBeNull();
+ });
+
+ it("rejects invalid YAML", async () => {
+ const res = await postYaml("kind: scenario\n bad: [indent").expect(400);
+ expect(JSON.stringify(res.body)).toContain("YAML document 1");
+ });
+
+ it("rejects an empty body", async () => {
+ await postYaml("").expect(400);
+ });
+
+ describe("scenario export round trip", () => {
+ it("exports one YAML document per entity and imports it back", async () => {
+ const envRepo = dataSource.getRepository(EnvironmentEntity);
+ const env = await envRepo.save(
+ envRepo.create({ name: `rt-env-${uniq()}`, data: { a: "1" } }),
+ );
+ const credRepo = dataSource.getRepository(CredentialEntity);
+ const cred = await credRepo.save(
+ credRepo.create({ name: "rt-cred", data: null }),
+ );
+ const sc = await scenarioService.create({
+ name: `rt-${uniq()}`,
+ environmentId: env.id,
+ });
+ await scenarioService.createStep(sc.id, { execCode: "return 1;" });
+ await scenarioService.addScenarioCredential(sc.id, {
+ credentialId: cred.id,
+ alias: "c",
+ });
+
+ const exported = await request(app.getHttpServer())
+ .get(
+ `/scenarios/${sc.id}/export?includeEnvironment=true&credentialIds=${cred.id}`,
+ )
+ .expect(200);
+ const docs = parseAllDocuments(exported.text);
+ expect(docs.map((d) => d.errors)).toEqual([[], [], []]);
+ expect(docs.map((d) => (d.toJS() as { kind: string }).kind)).toEqual([
+ "environment",
+ "credential",
+ "scenario",
+ ]);
+
+ const res = await postYaml(exported.text).expect(201);
+ expect(res.body.imported.map((i: { id: string }) => i.id)).toEqual([
+ env.id,
+ cred.id,
+ sc.id,
+ ]);
+ });
+ });
+
+ describe("import_yaml MCP tool", () => {
+ async function mcpCall(name: string, args: Record) {
+ const res = await request(app.getHttpServer())
+ .post("/mcp")
+ .set("Accept", "application/json, text/event-stream")
+ .send({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "tools/call",
+ params: { name, arguments: args },
+ });
+ const text = res.text.startsWith("event:")
+ ? res.text
+ .split("\n")
+ .find((l) => l.startsWith("data:"))!
+ .slice(5)
+ : res.text;
+ return JSON.parse(text).result as {
+ isError?: boolean;
+ content: { text: string }[];
+ };
+ }
+
+ it("imports items from YAML text", async () => {
+ const result = await mcpCall("import_yaml", {
+ yaml: `kind: credential\nname: mcp-${uniq()}\n`,
+ });
+ expect(result.isError).toBeFalsy();
+ expect(JSON.parse(result.content[0].text).imported).toHaveLength(1);
+ });
+
+ it("returns the validation errors as an MCP error", async () => {
+ const result = await mcpCall("import_yaml", {
+ yaml: `kind: snippet\nalias: x\ncode: return 1;\n`,
+ });
+ expect(result.isError).toBe(true);
+ expect(result.content[0].text).toContain("version");
+ });
+ });
+});
diff --git a/server/test/mcp.controller.spec.ts b/server/test/mcp.controller.spec.ts
index fe56b8b..921ec53 100644
--- a/server/test/mcp.controller.spec.ts
+++ b/server/test/mcp.controller.spec.ts
@@ -1,7 +1,7 @@
import { INestApplication } from "@nestjs/common";
import { getRepositoryToken } from "@nestjs/typeorm";
import request from "supertest";
-import * as http from "http";
+import * as http from "node:http";
import { Repository } from "typeorm";
import { buildTestApp } from "./app.harness";
import { ScenarioEntity } from "../src/scenario/scenario.entity";
@@ -9,7 +9,7 @@ import { ScenarioStepEntity } from "../src/scenario/scenario-step.entity";
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
import { ScenarioRunStepEntity } from "../src/scenario/scenario-run-step.entity";
import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service";
-import type { AddressInfo } from "net";
+import type { AddressInfo } from "node:net";
/**
* MCP controller integration tests.