Compare commits

..
12 Commits
Author SHA1 Message Date
ars9 a4bfa3117f feat(docker): serve the whole app from a single container
- serve the built client from the server via serve-static (CLIENT_DIR),
  falling back to index.html outside /api, /mcp, /docs and /health
- replace server and client Dockerfiles and nginx with one root
  Dockerfile and a single liqa compose service on port 13000
- use Playwright's chromium-headless-shell with only its required libs
  and DejaVu/Liberation fonts instead of Debian chromium and Noto
- clear the npm cache in the runtime image
- bump version to 1.19.0
2026-09-30 20:28:10 +03:00
ars9 8935783371 feat(import): add central yaml import for all exportable kinds
- add POST /import, an import page and the import_yaml mcp tool; items
  are validated up front and imported in dependency order
- switch list page import buttons to the central import
- write scenario exports as separate --- yaml documents per entity
- give each test app its own files dir to avoid cross-suite races
- use node: prefix for node built-in imports
- bump version to 1.18.0
2026-09-30 19:25:03 +03:00
ars9 cf56840df9 refactor: fix lint errors across server and client
- replace any types with explicit types in scenario file listing,
  import/export and mcp upload code
- type test server addresses and list callbacks, drop unused vars
- document why e2e export keeps the archiver import-require form
- remove unused scenario files total state in the client
- require clean lint in both packages in CLAUDE.md
2026-09-30 18:55:17 +03:00
ars9 1c5c906cbb feat(runs): add deleting files from a run
- add DELETE /scenarios/:id/runs/:runId/files/:fileId and the
  delete_run_file MCP tool; the stored file is removed once no scenario
  or run references it
- add a delete button with confirmation to the run detail files table
- bump version to 1.17.0
2026-09-30 18:51:25 +03:00
ars9 ac12301818 docs: add CLAUDE.md and rename compose file to compose.yaml
- add CLAUDE.md with app overview, compose deployment details, doc
  references, code quality checks and the release/commit workflow
- rename docker-compose.yaml to compose.yaml, the preferred compose
  spec file name
2026-09-30 18:45:59 +03:00
ars9 84dba6d670 fix(runs): disable playwright traces by default, bump to 1.16.1
- default PLAYWRIGHT_TRACE_MODE to off since traces often reach ~10 MB
  per run; traces can still be enabled per run or via the setting
2026-09-30 18:35:26 +03:00
ars9 ff83b6c7da feat(versioning): version scenarios and snippets, bump to 1.16.0
- snapshot scenario and snippet content into version tables on every
  content change; live tables keep holding the latest version
- record the scenario version on each run and copy its steps into the
  run steps, so runs execute and show the version they were created with
- keep run step history when a step is deleted (FK set null)
- pin a snippet version with runSnippet('alias@N')
- add version picker to the run modal and version info across the UI
- add version list/get endpoints and MCP tools
- include version in exports and require it on import, restoring the
  exact version by id + version
2026-09-30 15:24:44 +03:00
ars9 a328ef5ea7 feat(runs): record playwright traces with per-run trace mode, bump to 1.15.0
- record a trace for each scenario run and upload it as trace.zip
- add PLAYWRIGHT_TRACE_MODE (on, retain-on-failure, off) as the default
- add a trace mode option to the run modal, stored per run and reused
  by repeat run; expose the default via GET /scenarios/run-defaults
- accept traceMode in POST /scenarios/:id/run and run_scenario MCP tool
- share one run modal component between scenario list and detail pages
- git-ignore server/data
2026-09-30 14:58:07 +03:00
ars9 7d366e6368 feat(runs): add repeat run action to run detail page, bump to 1.14.0
- start a new run with the same environment and saveSession settings
  and navigate to it; disabled when the run's environment was deleted
- remount the run detail page per run id so per-run state resets
- type ScenarioRun.environmentId as nullable to match the backend
2026-09-30 14:35:31 +03:00
ars9 5ea1e38f25 feat(mcp): add credential CRUD tools, bump to 1.13.0
- add list/get/create/update/delete_credential MCP tools so credentials
  can be created and attached to scenarios entirely via MCP
- update_credential only changes the fields passed, keeping data intact
- add changelog entries for 1.13.0 and the missing 1.12.0 release
2026-09-30 14:26:15 +03:00
ars9andClaude Sonnet 5 d99bb75fc2 chore: sync package-lock.json version to 1.12.0
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-16 15:53:21 +03:00
ars9andClaude Sonnet 5 d79415cbb3 feat(scenario): dump page screenshot and HTML to run files on step failure
Captures a full-page screenshot and the current page HTML whenever a
scenario or snippet step fails, and uploads both as run artifacts so
failures can be diagnosed from the run's file list.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-16 15:52:32 +03:00
77 changed files with 3928 additions and 473 deletions

No files matched your search

+1
View File
@@ -7,3 +7,4 @@ node_modules
**/*.log
data
keys
**/*.tsbuildinfo
+11 -2
View File
@@ -7,11 +7,20 @@ KEYS_DIR=keys
# SQLite database path
DB_PATH=data/sessions.db
# Playwright executable (set in Docker; leave empty to use system default)
# Chromium binary to use instead of Playwright's own (not needed in Docker,
# which ships Playwright's chromium-headless-shell)
# PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium
# Session lifecycle
# Close open sessions that have been idle for longer than this many minutes
SESSION_IDLE_TIMEOUT_MINUTES=30
# Delete closed sessions whose updatedAt is older than this many days
SESSION_DELETE_CLOSED_DAYS=7
SESSION_DELETE_CLOSED_DAYS=7
# Playwright trace recording for scenario runs: on | retain-on-failure | off
# (traces often reach ~10 MB per run; can be overridden per run in the run modal)
PLAYWRIGHT_TRACE_MODE=off
# Directory with the built client to serve at / (set to "client" in the Docker image;
# leave empty to not serve the UI, e.g. when using the Vite dev server)
# CLIENT_DIR=
+1
View File
@@ -1,5 +1,6 @@
# runtime
/data
server/data/
.env
*.log
+100
View File
@@ -2,6 +2,106 @@
All notable changes to this project will be documented in this file.
## 1.19.0 - 2026-09-30
Changes since 1.18.0:
### Changed
- **Breaking (deployment):** the whole application now runs from a single image and container. The server serves the built client itself (new `CLIENT_DIR` setting, set by the image) next to the API, MCP and Swagger, so the UI moved from http://localhost:13001 to **http://localhost:13000**. The root `Dockerfile` replaces `server/Dockerfile`, `client/Dockerfile` and `client/nginx.conf`, and `compose.yaml` has a single `liqa` service. When upgrading, run `docker compose up -d --build --remove-orphans` to remove the old `server` and `client` containers.
- The image uses Playwright's headless-only Chromium build (`chromium-headless-shell`, always matching the installed Playwright version) with only the libraries it needs, instead of Debian's desktop `chromium` package. Fonts are now DejaVu and Liberation (metric-compatible with Arial/Times) instead of Noto. Together with clearing the npm cache, this shrinks the image from 1.76 GB to 1.07 GB.
## 1.18.0 - 2026-09-30
Changes since 1.17.0:
### Added
- Added a central import: `POST /import`, an Import page in the UI, and the `import_yaml` MCP tool. It takes YAML made of `---`-separated documents, each holding one exported item or a list of items (JSON is accepted too), with any mix of environments, credentials, snippets and scenarios. All items are validated before anything is imported, and they are imported in dependency order (environments, credentials, snippets, scenarios), so a scenario can reference an environment or credential from the same file.
### Changed
- The Import buttons on the scenario, snippet, credential and environment list pages use the central import. When the file holds exactly one item of the page's kind, that item is opened; otherwise the Import page shows everything that was imported.
### Fixed
- Scenario exports that included the environment or credentials were not valid YAML (all entities were written into one document). Each entity is now a separate `---` document.
## 1.17.0 - 2026-09-30
Changes since 1.16.1:
### Added
- Files can be deleted from a run: a delete button (with confirmation) in the Files section of the run detail page, `DELETE /scenarios/:id/runs/:runId/files/:fileId`, and the `delete_run_file` MCP tool. The stored file is removed once no scenario or run references it.
## 1.16.1 - 2026-09-30
Changes since 1.16.0:
### Changed
- Playwright traces are now off by default (`PLAYWRIGHT_TRACE_MODE` defaults to `off`), since a trace often reaches ~10 MB per run. Traces can still be enabled per run in the run modal, via `traceMode` in `POST /scenarios/:id/run`, or for all runs by setting `PLAYWRIGHT_TRACE_MODE`.
## 1.16.0 - 2026-09-30
Changes since 1.15.0:
### Added
- Scenarios and snippets are now versioned, starting at version 1. A scenario's version goes up when its name, description, default environment, timeout or steps change; a snippet's when its alias, title, description or code changes. Saving without changes does not create a new version. Credential assignments and scenario files are not versioned.
- Each run records the scenario version it runs. The run modal has a version picker (latest by default), `POST /scenarios/:id/run` and the `run_scenario` MCP tool accept an optional `version`, and "Repeat run" reuses the run's version.
- `context.runSnippet("alias@N")` pins version N of a snippet; `runSnippet("alias")` keeps using the latest. The scenario page's "Used Snippets" section shows which snippets are pinned.
- Version numbers are shown on the scenario, snippet and run pages and in the scenario and run lists.
- Added `GET /scenarios/:id/versions[/:version]` and `GET /snippets/:id/versions[/:version]`, and the `list_scenario_versions`, `get_scenario_version`, `list_snippet_versions` and `get_snippet_version` MCP tools.
- Scenario and snippet exports accept `?version=N` (the `export_scenario` MCP tool takes `version`).
### Changed
- **Breaking:** scenario and snippet exports now include `version`, and imports require it. An import restores that exact version by id + version: an existing version is left untouched, and a new one is stored under its number and becomes the latest when it is higher than the current latest. Export files from earlier releases need a `version` field added before they can be imported.
- Scenario exports now include scenario and step timeouts.
- Runs keep their own copy of the steps they were created with. Editing or deleting a step no longer changes past runs, and deleting a step no longer deletes its results from past runs. Existing run steps are backfilled from the current steps on startup.
## 1.15.0 - 2026-09-30
Changes since 1.14.0:
### Added
- Scenario runs now record a Playwright trace (screenshots and DOM snapshots) and upload it to the run's files as `trace.zip` when the run finishes. Open it with `npx playwright show-trace trace.zip` or at https://trace.playwright.dev.
- Added the `PLAYWRIGHT_TRACE_MODE` setting (`on` (default), `retain-on-failure`, or `off`) that controls which traces are kept.
- Added a "Playwright trace" option to the run modal to override the trace mode for a single run. It defaults to the server's `PLAYWRIGHT_TRACE_MODE`, served by the new `GET /scenarios/run-defaults` endpoint.
- `POST /scenarios/:id/run` and the `run_scenario` MCP tool accept an optional `traceMode`. The trace mode is stored on the run, shown on the run detail page, and reused by "Repeat run".
### Changed
- The run modals on the scenario list and scenario detail pages now share one component.
- `server/data/` is now git-ignored.
## 1.14.0 - 2026-09-30
Changes since 1.13.0:
### Added
- Added a "Repeat run" button on the scenario run detail page that starts a new run of the same scenario with the same environment and "save session" setting, then navigates to it. The button is disabled when the run's environment has since been deleted.
## 1.13.0 - 2026-09-30
Changes since 1.12.0:
### Added
- Added credential CRUD tools to the MCP server: `list_credentials`, `get_credential`, `create_credential`, `update_credential`, and `delete_credential`. Combined with the existing `add_scenario_credential` tool, credentials can now be created and attached to scenarios entirely via MCP.
## 1.12.0 - 2026-09-16
Changes since 1.11.0:
### Added
- When a scenario or snippet step fails, a full-page screenshot and the current page HTML are captured and uploaded as run artifacts, so failures can be diagnosed from the run's file list.
## 1.11.0 - 2026-09-15
Changes since 1.10.1:
+55
View File
@@ -0,0 +1,55 @@
# liqa
liqa (liquio-qa-bot) is a QA automation service for Liquio environments. It stores **scenarios** (ordered Playwright steps), **snippets** (reusable code run via `context.runSnippet`), **environments**, **credentials** and **sessions**, and runs scenarios in headless Chromium. Scenarios and snippets are versioned; each run records the scenario version it executed.
It is an npm workspace with two packages:
- `server/` - NestJS API + scheduler + MCP server, SQLite via TypeORM (`synchronize: true`, so entity changes alter the schema on startup - there are no migrations). REST API under `/api/v1`, Swagger UI at `/docs`, MCP at `/mcp`, health at `/health`.
- `client/` - React 19 + Vite SPA (i18n strings in `client/src/i18n/locales/en.json`).
## Docs
- [README.md](README.md) - overview
- [docs/architecture.md](docs/architecture.md) - services and modules
- [docs/development.md](docs/development.md) - local setup, running, testing, Docker
- [docs/scenario.md](docs/scenario.md) - scenario execution, script `context` API, versioning, export/import, Playwright traces
- [docs/mcp.md](docs/mcp.md) - MCP tool reference
- [CHANGELOG.md](CHANGELOG.md) - release notes per version
- [Dockerfile](Dockerfile) - single image: builds client and server, runtime serves both
- [.env.example](.env.example) - all configuration variables (validated in `server/src/config/app.config.ts`)
## Default deployment (docker compose)
`compose.yaml` at the repo root runs a single `liqa` container, built from the root `Dockerfile`, at http://localhost:13000. The server serves everything: the UI at `/` (the built client, via `CLIENT_DIR`), the REST API at `/api/v1`, MCP at `/mcp`, Swagger at `/docs`. It reads `.env` and mounts `./data` (SQLite DB `data/liqa.db` + stored files) and `./keys`.
Rebuild and restart: `docker compose up -d --build`. Check with `docker compose ps`, `curl localhost:13000/health` and `docker compose logs liqa`.
The live database is `data/liqa.db`. Because the schema is synced on startup, back it up before deploying entity changes.
## Code quality
Before committing, run and pass all of these (and report the results):
```bash
# server
cd server && npm run lint && npx tsc --noEmit -p tsconfig.json && npm test && npm run build
# client
cd client && npm run lint && npm run build
```
- Lint must stay clean in both packages (no errors, no warnings).
- Don't run prettier over whole existing files; several aren't prettier-clean and it produces unrelated diffs. Format only new files or the lines you changed.
- Server tests use an in-memory DB, a Playwright mock (`server/test/__mocks__/playwright.ts`), and a temporary files directory per test app (see `server/test/app.harness.ts`).
- Import Node.js built-in modules with the `node:` prefix (`node:fs`, `node:path`, `node:crypto`, ...).
## Committing a new version
When asked to commit:
1. **Ask which version to bump** - major, minor or patch - and propose a sensible default based on the changes: patch for fixes and small behavior/default tweaks, minor for new features, major for breaking changes (e.g. changed API or export/import formats that old clients or files can't use).
2. Run the code quality checks above.
3. Bump `version` in the root `package.json` and in both places at the top of `package-lock.json` (the root entry and `packages[""]`). `server/package.json` and `client/package.json` keep their own versions; don't bump them.
4. Add a `## <version> - <YYYY-MM-DD>` entry at the top of `CHANGELOG.md` ("Changes since <previous version>:", then `### Added` / `### Changed` / `### Fixed`). Mark breaking changes with **Breaking:**.
5. Update the docs affected by the change (`docs/*.md`, `README.md`, `.env.example`, MCP tool descriptions).
6. Build the commit message with the `/git-commit` skill if it is available, following its format and rules. Otherwise use a conventional commit message (`type(scope): subject`, then a bullet list body). Either way, put the version bump in the body as a bullet (e.g. `- bump version to <version>`), not in the subject line. This overrides the skill's rule of putting versions in the subject. Then make the commit with that message.
7. **After committing, ask the user whether to rebuild and restart the docker compose containers** (`docker compose up -d --build`); don't do it without confirmation.
+87
View File
@@ -0,0 +1,87 @@
# Single image serving the whole application: the NestJS server (API, MCP,
# scheduler) also serves the built client, so one container is enough.
# ── Client build ──────────────────────────────────────────────────────────────
FROM node:22-slim AS client-build
WORKDIR /app
# Root lockfile + workspace manifest so npm ci can resolve the full graph
COPY package*.json ./
COPY client/package.json ./client/
RUN npm ci -w client
COPY client ./client
RUN npm run -w client build
# ── Server build ──────────────────────────────────────────────────────────────
FROM node:22-slim AS server-build
WORKDIR /app
COPY package*.json ./
COPY server/package.json ./server/
RUN npm ci -w server
COPY server/nest-cli.json ./server/
COPY server/tsconfig*.json ./server/
COPY server/src ./server/src
RUN npm run -w server build
# ── Runtime ───────────────────────────────────────────────────────────────────
FROM node:22-slim AS runtime
# Shared libraries chrome-headless-shell links against (from `ldd`), plus
# fontconfig and basic fonts for text rendering. Playwright's `--with-deps`
# would also pull in xvfb (with Mesa/LLVM) and extra font packages, which
# headless-only use doesn't need.
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
fontconfig \
fonts-dejavu-core \
fonts-liberation \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libatspi2.0-0 \
libdbus-1-3 \
libexpat1 \
libgbm1 \
libglib2.0-0 \
libnspr4 \
libnss3 \
libx11-6 \
libxcb1 \
libxcomposite1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxkbcommon0 \
libxrandr2 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
COPY server/package.json ./server/
# Playwright's headless-only Chromium build, matching the installed Playwright
# version. All browser launches are headless, so the full desktop Chromium
# (GTK, Mesa/LLVM, icon themes) isn't needed.
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN npm ci --omit=dev -w server \
&& npx playwright install --only-shell chromium \
&& npm cache clean --force
COPY --from=server-build /app/server/dist ./dist
COPY --from=client-build /app/client/dist ./client
# Serve the built client at / (see server/src/common/client-static.ts)
ENV CLIENT_DIR=client
# Runtime directories (keys and SQLite DB mounted via volumes)
RUN mkdir -p data
EXPOSE 3000
CMD ["node", "dist/main"]
-20
View File
@@ -1,20 +0,0 @@
# ── Build stage ───────────────────────────────────────────────────────────────
FROM node:22-slim AS builder
WORKDIR /app
# Copy root lockfile + workspace manifests so npm ci can resolve the full graph
COPY package*.json ./
COPY client/package.json ./client/
RUN npm ci -w client
COPY client ./client
RUN npm run -w client build
# ── Runtime stage ─────────────────────────────────────────────────────────────
FROM nginx:alpine AS runtime
COPY --from=builder /app/client/dist /usr/share/nginx/html
COPY client/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
-17
View File
@@ -1,17 +0,0 @@
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
location /api/ {
proxy_pass http://server:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
try_files $uri $uri/ /index.html;
}
}
+7 -1
View File
@@ -1,7 +1,7 @@
import { lazy, Suspense } from 'react';
import { NavLink, Navigate, Route, Routes } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Globe, Monitor, ClipboardList, Activity, KeyRound, Braces } from 'lucide-react';
import { Globe, Monitor, ClipboardList, Activity, KeyRound, Braces, Upload } from 'lucide-react';
import type { LucideIcon } from 'lucide-react';
import styles from './App.module.css';
import { SidePanel, ThemeSwitcher } from './ui';
@@ -89,6 +89,9 @@ const EditSnippetPage = lazy(() =>
const SnippetDetailPage = lazy(() =>
import('./pages/snippet/SnippetDetailPage').then((m) => ({ default: m.SnippetDetailPage })),
);
const ImportPage = lazy(() =>
import('./pages/import/ImportPage').then((m) => ({ default: m.ImportPage })),
);
const NAV: ({ path: string; labelKey: string; Icon: LucideIcon } | { separator: true })[] = [
{ path: '/runs', labelKey: 'nav.runs', Icon: Activity },
@@ -98,6 +101,8 @@ const NAV: ({ path: string; labelKey: string; Icon: LucideIcon } | { separator:
{ path: '/snippets', labelKey: 'nav.snippets', Icon: Braces },
{ path: '/credentials', labelKey: 'nav.credentials', Icon: KeyRound },
{ path: '/environments', labelKey: 'nav.environments', Icon: Globe },
{ separator: true },
{ path: '/import', labelKey: 'nav.import', Icon: Upload },
];
export default function App() {
@@ -163,6 +168,7 @@ export default function App() {
<Route path="/credentials/:id/edit" element={<EditCredentialPage />} />
<Route path="/credentials/:id" element={<CredentialDetailPage />} />
<Route path="/snippets" element={<SnippetsPage />} />
<Route path="/import" element={<ImportPage />} />
<Route path="/snippets/new" element={<CreateSnippetPage />} />
<Route path="/snippets/:id/edit" element={<EditSnippetPage />} />
<Route path="/snippets/:id" element={<SnippetDetailPage />} />
+46 -27
View File
@@ -4,6 +4,8 @@ import type {
Environment,
FileMetadata,
FileListResponse,
ImportResult,
RunDefaults,
ScenarioCredential,
Session,
Scenario,
@@ -11,7 +13,11 @@ import type {
ScenarioRunDetail,
ScenarioRunStep,
ScenarioStep,
ScenarioVersion,
Snippet,
SnippetVersion,
TraceMode,
UsedSnippet,
} from './types';
import { emitApiErrorToast } from '../lib/toast-events';
@@ -83,12 +89,6 @@ export const credentials = {
exportCredential(id: string): Promise<unknown> {
return request(`/credentials/${id}/export`);
},
importCredential(payload: unknown): Promise<Credential> {
return request('/credentials/import', {
method: 'POST',
body: JSON.stringify(payload),
});
},
};
// ── Snippets ──────────────────────────────────────────────────────────────────
@@ -120,15 +120,12 @@ export const snippets = {
remove(id: string): Promise<void> {
return request(`/snippets/${id}`, { method: 'DELETE' });
},
versions(id: string): Promise<SnippetVersion[]> {
return request(`/snippets/${id}/versions`);
},
exportSnippet(id: string): Promise<unknown> {
return request(`/snippets/${id}/export`);
},
importSnippet(payload: unknown): Promise<Snippet> {
return request('/snippets/import', {
method: 'POST',
body: JSON.stringify(payload),
});
},
};
// ── Environments ──────────────────────────────────────────────────────────────
@@ -161,12 +158,6 @@ export const environments = {
exportEnvironment(id: string): Promise<unknown> {
return request(`/environments/${id}/export`);
},
importEnvironment(payload: unknown): Promise<Environment> {
return request('/environments/import', {
method: 'POST',
body: JSON.stringify(payload),
});
},
};
// ── Sessions ──────────────────────────────────────────────────────────────────
@@ -222,12 +213,25 @@ export const scenarios = {
remove(id: string): Promise<void> {
return request(`/scenarios/${id}`, { method: 'DELETE' });
},
run(id: string, environmentId: string, saveSession?: boolean): Promise<ScenarioRun> {
run(
id: string,
environmentId: string,
saveSession?: boolean,
traceMode?: TraceMode,
/** Scenario version to run; omit for the latest. */
version?: number,
): Promise<ScenarioRun> {
return request(`/scenarios/${id}/run`, {
method: 'POST',
body: JSON.stringify({ environmentId, saveSession }),
body: JSON.stringify({ environmentId, saveSession, traceMode, version }),
});
},
versions(id: string): Promise<ScenarioVersion[]> {
return request(`/scenarios/${id}/versions`);
},
runDefaults(): Promise<RunDefaults> {
return request('/scenarios/run-defaults');
},
getRun(scenarioId: string, runId: string): Promise<ScenarioRunDetail> {
return request(`/scenarios/${scenarioId}/run/${runId}`);
},
@@ -255,16 +259,10 @@ export const scenarios = {
}
return request(`/scenarios/${id}/export?${params.toString()}`);
},
importScenario(payload: unknown): Promise<Scenario> {
return request('/scenarios/import', {
method: 'POST',
body: JSON.stringify(payload),
});
},
exportE2eTestUrl(id: string): string {
return `${BASE_URL}/scenarios/${id}/export-e2e`;
},
usedSnippets(id: string): Promise<Snippet[]> {
usedSnippets(id: string): Promise<UsedSnippet[]> {
return request(`/scenarios/${id}/used-snippets`);
},
};
@@ -388,4 +386,25 @@ export const runFiles = {
contentUrl(scenarioId: string, runId: string, fileId: string): string {
return `${BASE_URL}/scenarios/${scenarioId}/runs/${runId}/files/${fileId}/content`;
},
remove(scenarioId: string, runId: string, fileId: string): Promise<void> {
return request(`/scenarios/${scenarioId}/runs/${runId}/files/${fileId}`, {
method: 'DELETE',
});
},
};
// ── Import ────────────────────────────────────────────────────────────────────
export const imports = {
/**
* Imports any mix of environments, credentials, snippets and scenarios from
* YAML (`---`-separated documents, each one item or a list of items).
*/
yaml(text: string): Promise<ImportResult> {
return request('/import', {
method: 'POST',
headers: { 'Content-Type': 'application/yaml' },
body: text,
});
},
};
+73 -3
View File
@@ -41,10 +41,29 @@ export interface Snippet {
title: string;
description: string | null;
code: string;
/** Latest version number. */
version: number;
createdAt: string;
updatedAt: string;
}
export interface SnippetVersion {
id: string;
snippetId: string;
version: number;
alias: string;
title: string;
description: string | null;
code: string;
createdAt: string;
}
/** A snippet referenced by a scenario's steps. */
export interface UsedSnippet extends Snippet {
/** Version pinned via runSnippet('alias@N'), or null for the latest. */
pinnedVersion: number | null;
}
// ── Sessions ──────────────────────────────────────────────────────────────────
export type SessionStatus = 'open' | 'closed';
@@ -87,6 +106,8 @@ export interface Scenario {
description?: string;
environmentId?: string | null;
timeoutSeconds?: number | null;
/** Latest version number. */
version: number;
environment?: Pick<Environment, 'id' | 'name'>;
steps?: ScenarioStep[];
scenarioCredentials?: ScenarioCredential[];
@@ -96,8 +117,34 @@ export interface Scenario {
updatedAt: string;
}
export interface ScenarioStepSnapshot {
id: string | null;
order: number;
title: string | null;
execCode: string | null;
timeoutSeconds: number | null;
}
export interface ScenarioVersion {
id: string;
scenarioId: string;
version: number;
name: string;
description: string | null;
environmentId: string | null;
timeoutSeconds: number | null;
steps: ScenarioStepSnapshot[];
createdAt: string;
}
export type ScenarioRunStatus = 'pending' | 'in_progress' | 'pass' | 'fail';
export type TraceMode = 'on' | 'retain-on-failure' | 'off';
export interface RunDefaults {
traceMode: TraceMode;
}
export type RunStepStatus = 'waiting' | 'pending' | 'in_progress' | 'pass' | 'fail' | 'cancelled';
export type LogLevel = 'log' | 'warn' | 'error';
@@ -105,9 +152,12 @@ export type LogLevel = 'log' | 'warn' | 'error';
export interface ScenarioRun {
id: string;
scenarioId: string;
environmentId: string;
environmentId: string | null;
sessionId?: string | null;
saveSession: boolean;
traceMode: TraceMode | null;
/** Scenario version the run executes; null for runs created before versioning. */
scenarioVersion: number | null;
scenario?: Pick<Scenario, 'id' | 'name'>;
environment?: Pick<Environment, 'id' | 'name'>;
session?: { id: string; sessionName: string };
@@ -120,8 +170,12 @@ export interface ScenarioRun {
export interface ScenarioRunStep {
id: string;
runId: string;
scenarioStepId: string;
scenarioStep: ScenarioStep;
/** Live step this run step came from; null once that step is deleted. */
scenarioStepId: string | null;
// Step content as of the run's scenario version
title: string | null;
execCode: string | null;
timeoutSeconds: number | null;
status: RunStepStatus;
order: number;
description: string | null;
@@ -160,3 +214,19 @@ export interface FileListResponse {
items: FileMetadata[];
total: number;
}
// ── Import ────────────────────────────────────────────────────────────────────
export type ImportKind = 'environment' | 'credential' | 'snippet' | 'scenario';
export interface ImportedItem {
kind: ImportKind;
id: string;
name: string;
/** Version stored for versioned kinds (snippets, scenarios). */
version?: number;
}
export interface ImportResult {
imported: ImportedItem[];
}
@@ -1,7 +1,9 @@
import { useTranslation } from 'react-i18next';
import { X, Play } from 'lucide-react';
import { Modal, Button, Select } from '../../ui';
import type { Environment } from '../../api';
import type { Environment, ScenarioVersion, TraceMode } from '../../api';
const TRACE_MODES: TraceMode[] = ['on', 'retain-on-failure', 'off'];
export interface RunScenarioModalProps {
open: boolean;
@@ -10,6 +12,14 @@ export interface RunScenarioModalProps {
onEnvSelect: (envId: string) => void;
saveSessionFlag: boolean;
onSaveSessionChange: (flag: boolean) => void;
/** Scenario versions, newest first; empty while loading. */
versions: Pick<ScenarioVersion, 'version' | 'createdAt'>[];
/** Selected version, or null for the latest. */
selectedVersion: number | null;
onVersionChange: (version: number | null) => void;
traceMode: TraceMode;
defaultTraceMode: TraceMode | null;
onTraceModeChange: (mode: TraceMode) => void;
onClose: () => void;
onRun: () => void;
isRunning: boolean;
@@ -23,6 +33,12 @@ export function RunScenarioModal({
onEnvSelect,
saveSessionFlag,
onSaveSessionChange,
versions,
selectedVersion,
onVersionChange,
traceMode,
defaultTraceMode,
onTraceModeChange,
onClose,
onRun,
isRunning,
@@ -58,6 +74,25 @@ export function RunScenarioModal({
onChange={(e) => onEnvSelect(e.target.value)}
options={environments.map((env) => ({ value: env.id, label: env.name }))}
/>
<div style={{ marginTop: '1rem' }}>
<Select
label={t('scenarios.run_modal_version_label')}
value={selectedVersion == null ? '' : String(selectedVersion)}
onChange={(e) => onVersionChange(e.target.value ? Number(e.target.value) : null)}
options={[
{
value: '',
label: versions[0]
? t('scenarios.run_modal_version_latest_n', { version: versions[0].version })
: t('scenarios.run_modal_version_latest'),
},
...versions.map((v) => ({
value: String(v.version),
label: `v${v.version} — ${new Date(v.createdAt).toLocaleString()}`,
})),
]}
/>
</div>
<label
style={{
display: 'flex',
@@ -73,6 +108,19 @@ export function RunScenarioModal({
/>
<span>{t('common.save_session')}</span>
</label>
<div style={{ marginTop: '1rem' }}>
<Select
label={t('scenarios.run_modal_trace_label')}
value={traceMode}
onChange={(e) => onTraceModeChange(e.target.value as TraceMode)}
options={TRACE_MODES.map((mode) => ({
value: mode,
label:
t(`scenarios.trace_mode_${mode.replace(/-/g, '_')}`) +
(mode === defaultTraceMode ? ` ${t('scenarios.trace_mode_default_suffix')}` : ''),
}))}
/>
</div>
</>
)}
</Modal>
+43
View File
@@ -0,0 +1,43 @@
import type { ChangeEvent } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { imports } from '../api';
import type { ImportedItem, ImportKind } from '../api';
import { useToast } from '../ui';
/** Detail page path of an imported item. */
export function importedItemPath(item: ImportedItem): string {
return `/${item.kind}s/${item.id}`;
}
/**
* Returns a file input `onChange` handler that imports the chosen YAML file
* through the central import. When the file holds exactly one item of `kind`
* (e.g. a scenario export with its environment), opens that item; otherwise
* opens the import page with the full result.
*/
export function useImportFile(kind: ImportKind) {
const navigate = useNavigate();
const toast = useToast();
const { t } = useTranslation();
return async (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
e.target.value = '';
try {
const result = await imports.yaml(await file.text());
const own = result.imported.filter((item) => item.kind === kind);
if (own.length === 1) {
if (result.imported.length > 1) {
toast.success(t('import.imported_count', { count: result.imported.length }));
}
navigate(importedItemPath(own[0]));
} else {
navigate('/import', { state: { results: [{ fileName: file.name, result }] } });
}
} catch {
// error toast is already emitted by the API client
}
};
}
+26
View File
@@ -0,0 +1,26 @@
import { useEffect, useState } from 'react';
import { scenarios } from '../api';
import type { RunDefaults } from '../api';
// Server run defaults don't change while the app is open; fetch them once
let cached: Promise<RunDefaults> | null = null;
/**
* Returns the server-configured defaults for new scenario runs, or null
* while they are loading (or if the request failed).
*/
export function useRunDefaults(): RunDefaults | null {
const [defaults, setDefaults] = useState<RunDefaults | null>(null);
useEffect(() => {
let cancelled = false;
cached ??= scenarios.runDefaults().catch((err: unknown) => {
cached = null;
throw err;
});
cached.then((d) => !cancelled && setDefaults(d)).catch(() => undefined);
return () => {
cancelled = true;
};
}, []);
return defaults;
}
+40 -1
View File
@@ -28,7 +28,24 @@
"keys": "Keys",
"sessions": "Sessions",
"scenarios": "Scenarios",
"runs": "Runs"
"runs": "Runs",
"import": "Import"
},
"import": {
"title": "Import",
"action_choose": "Choose files",
"description": "Import exported environments, credentials, snippets and scenarios. Each YAML file may hold several items as ---separated documents, and each document may hold one item or a list of items. JSON files work too.",
"description_validation": "Every item in a file is checked before anything is imported: if one item is invalid, nothing from that file is imported. Items are imported in dependency order (environments, credentials, snippets, scenarios), and snippets and scenarios keep the version they were exported with.",
"imported_count_one": "Imported {{count}} item",
"imported_count_other": "Imported {{count}} items",
"col_file": "File",
"col_kind": "Kind",
"col_name": "Name",
"col_version": "Version",
"kind_environment": "Environment",
"kind_credential": "Credential",
"kind_snippet": "Snippet",
"kind_scenario": "Scenario"
},
"environments": {
"title": "Environments",
@@ -126,6 +143,7 @@
"run_started": "Scenario run started",
"col_id": "ID",
"col_name": "Name",
"col_version": "Version",
"col_description": "Description",
"col_last_run": "Last Run",
"col_updated": "Updated",
@@ -135,6 +153,7 @@
"action_delete": "Delete",
"field_id": "ID",
"field_name": "Name",
"field_version": "Version",
"field_description": "Description",
"field_steps": "Steps",
"field_created": "Created",
@@ -167,6 +186,8 @@
"form_name_required": "Name is required",
"credentials_heading": "Credentials",
"used_snippets_heading": "Used Snippets",
"used_snippet_pinned": "v{{version}} (pinned)",
"used_snippet_latest": "Latest (v{{version}})",
"cred_empty": "No credentials assigned.",
"cred_col_alias": "Alias",
"cred_col_name": "Credential",
@@ -185,6 +206,14 @@
"run_modal_env_label": "Environment",
"run_modal_no_env": "No environments found. Create an environment before running a scenario.",
"run_modal_env_required": "Please select an environment",
"run_modal_version_label": "Version",
"run_modal_version_latest": "Latest",
"run_modal_version_latest_n": "Latest (v{{version}})",
"run_modal_trace_label": "Playwright trace",
"trace_mode_on": "Always keep",
"trace_mode_retain_on_failure": "Keep on failure",
"trace_mode_off": "Off",
"trace_mode_default_suffix": "(default)",
"export_modal_title": "Export Scenario",
"export_include_environment": "Include Environment",
"export_include_credentials": "Include Credentials",
@@ -242,12 +271,15 @@
"col_scenario": "Scenario",
"col_status": "Status",
"col_steps": "Steps",
"col_version": "Version",
"col_created": "Started",
"field_id": "ID",
"field_status": "Status",
"field_steps": "Steps",
"field_created": "Started",
"field_updated": "Updated",
"field_trace": "Playwright trace",
"field_scenario_version": "Scenario version",
"steps_heading": "Step runs",
"step_title": "Title",
"logs_heading": "Logs",
@@ -261,6 +293,8 @@
"log_message": "Message",
"log_time": "Time",
"action_download_logs": "Download logs",
"action_repeat": "Repeat run",
"repeat_no_environment": "The environment used by this run no longer exists",
"export_logs_modal_title": "Export logs",
"export_logs_format": "Format",
"export_logs_format_md": "Markdown (.md)",
@@ -295,6 +329,7 @@
"form_code_required": "Code is required",
"field_id": "ID",
"field_alias": "Alias",
"field_version": "Version",
"field_title": "Title",
"field_description": "Description",
"field_created": "Created",
@@ -316,6 +351,10 @@
"expiresAtLabel": "Expires At (optional)",
"createdAt": "Created",
"download": "Download",
"delete": "Delete",
"deleteTitle": "Delete file",
"confirmDelete": "Are you sure you want to delete \"{{name}}\" from this run?",
"deleteSuccess": "File deleted",
"expiredTooltip": "File is expired",
"empty": "No files uploaded.",
"totalCount": "Total: {{count}} file(s)",
@@ -2,8 +2,8 @@ import { useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { usePageTitle } from '../../hooks/usePageTitle';
import { useImportFile } from '../../hooks/useImportFile';
import { Settings, Pencil, Trash2, Plus, Download, KeyRound } from 'lucide-react';
import { parse as yamlParse } from 'yaml';
import { credentials } from '../../api';
import type { Credential } from '../../api';
import {
@@ -102,7 +102,6 @@ function AddCredentialCard() {
export function CredentialsPage() {
const { t } = useTranslation();
usePageTitle(t('credentials.title'));
const navigate = useNavigate();
const [items, setItems] = useState<Credential[]>([]);
const [loading, setLoading] = useState(true);
const [deleteId, setDeleteId] = useState<string | null>(null);
@@ -136,19 +135,7 @@ export function CredentialsPage() {
}
};
const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
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 credentials.importCredential(payload);
navigate(`/credentials/${imported.id}`);
} catch {
// Import errors are silently ignored
}
};
const handleImport = useImportFile('credential');
return (
<div>
@@ -2,8 +2,8 @@ import { useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { usePageTitle } from '../../hooks/usePageTitle';
import { useImportFile } from '../../hooks/useImportFile';
import { Settings, ExternalLink, Pencil, Trash2, Plus, Globe, Download } from 'lucide-react';
import { parse as yamlParse } from 'yaml';
import { stringify as yamlStringify } from 'yaml';
import { environments } from '../../api';
import type { Environment } from '../../api';
@@ -119,7 +119,6 @@ function AddEnvironmentCard() {
export function EnvironmentsPage() {
const { t } = useTranslation();
usePageTitle(t('environments.title'));
const navigate = useNavigate();
const [items, setItems] = useState<Environment[]>([]);
const [loading, setLoading] = useState(true);
const [deleteId, setDeleteId] = useState<string | null>(null);
@@ -153,19 +152,7 @@ export function EnvironmentsPage() {
}
};
const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
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 environments.importEnvironment(payload);
navigate(`/environments/${imported.id}`);
} catch {
// Import errors are silently ignored
}
};
const handleImport = useImportFile('environment');
return (
<div>
+116
View File
@@ -0,0 +1,116 @@
import { useRef, useState, type ChangeEvent } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Upload } from 'lucide-react';
import { usePageTitle } from '../../hooks/usePageTitle';
import { importedItemPath } from '../../hooks/useImportFile';
import { imports } from '../../api';
import type { ImportedItem, ImportResult } from '../../api';
import { Badge, Breadcrumbs, Button, Card, Table, type TableColumn } from '../../ui';
import styles from '../Page.module.css';
interface FileResult {
fileName: string;
result: ImportResult;
}
type Row = ImportedItem & { fileName: string; rowKey: string };
export function ImportPage() {
const { t } = useTranslation();
usePageTitle(t('import.title'));
const navigate = useNavigate();
const location = useLocation();
const fileInputRef = useRef<HTMLInputElement>(null);
// List pages hand over the result of an import they ran
const [results, setResults] = useState<FileResult[]>(
() => (location.state as { results?: FileResult[] } | null)?.results ?? [],
);
const [importing, setImporting] = useState(false);
const handleFiles = async (e: ChangeEvent<HTMLInputElement>) => {
const files = Array.from(e.target.files ?? []);
e.target.value = '';
if (files.length === 0) return;
setImporting(true);
const next: FileResult[] = [];
try {
for (const file of files) {
try {
next.push({ fileName: file.name, result: await imports.yaml(await file.text()) });
} catch {
// error toast is already emitted by the API client; nothing from
// this file was imported, continue with the next one
}
}
} finally {
setResults(next);
setImporting(false);
}
};
const rows: Row[] = results.flatMap(({ fileName, result }) =>
result.imported.map((item, i) => ({ ...item, fileName, rowKey: `${fileName}-${i}` })),
);
const columns: TableColumn<Row>[] = [
{ key: 'fileName', header: t('import.col_file'), render: (r) => r.fileName },
{
key: 'kind',
header: t('import.col_kind'),
width: 120,
render: (r) => <Badge variant="neutral">{t(`import.kind_${r.kind}`)}</Badge>,
},
{ key: 'name', header: t('import.col_name'), render: (r) => r.name },
{
key: 'version',
header: t('import.col_version'),
width: 90,
render: (r) =>
r.version != null ? `v${r.version}` : <span className={styles.muted}>–</span>,
},
];
return (
<div>
<div className={styles.pageToolbar}>
<Breadcrumbs items={[{ label: t('import.title'), icon: <Upload size={14} /> }]} />
<div className={styles.toolbarActions}>
<input
ref={fileInputRef}
type="file"
accept=".yaml,.yml,.json"
multiple
style={{ display: 'none' }}
onChange={handleFiles}
/>
<Button size="sm" onClick={() => fileInputRef.current?.click()} disabled={importing}>
<Upload size={14} />
{t('import.action_choose')}
</Button>
</div>
</div>
<Card>
<p>{t('import.description')}</p>
<p className={styles.muted}>{t('import.description_validation')}</p>
</Card>
{rows.length > 0 && (
<div className={styles.stepsSection}>
<h2 className={styles.sectionHeading}>
{t('import.imported_count', { count: rows.length })}
</h2>
<Table<Row>
columns={columns}
data={rows}
rowKey={(r) => r.rowKey}
loading={false}
emptyMessage=""
onRowClick={(r) => navigate(importedItemPath(r))}
/>
</div>
)}
</div>
);
}
+11
View File
@@ -96,6 +96,17 @@ export function AllRunsPage() {
sortable: true,
render: (r) => <Badge variant={STATUS_VARIANT[r.status]}>{STATUS_LABEL[r.status]}</Badge>,
},
{
key: 'scenarioVersion',
header: t('runs.col_version'),
width: 80,
render: (r) =>
r.scenarioVersion != null ? (
`v${r.scenarioVersion}`
) : (
<span className={styles.muted}>–</span>
),
},
{
key: 'createdAt',
header: t('runs.col_created'),
+120 -9
View File
@@ -14,7 +14,11 @@ import type {
FileMetadata,
} from '../../api';
import { scenarios } from '../../api';
import { ExportLogsModal, type LogExportFormat } from '../../components/modals';
import {
DeleteConfirmationModal,
ExportLogsModal,
type LogExportFormat,
} from '../../components/modals';
import {
AutoRefreshIndicator,
Badge,
@@ -28,10 +32,19 @@ import {
Table,
Timestamp,
UuidBadge,
useToast,
type TableColumn,
} from '../../ui';
import type { BadgeVariant } from '../../ui';
import { ChevronDown, ChevronRight, ClipboardList, Activity, Download } from 'lucide-react';
import {
ChevronDown,
ChevronRight,
ClipboardList,
Activity,
Download,
Trash2,
RotateCcw,
} from 'lucide-react';
import styles from '../Page.module.css';
const RUN_STATUS_VARIANT: Record<ScenarioRunStatus, BadgeVariant> = {
@@ -66,9 +79,16 @@ const LOG_LEVEL_VARIANT: Record<LogLevel, BadgeVariant> = {
const FINAL: ScenarioRunStatus[] = ['pass', 'fail'];
export function RunDetailPage() {
const { runId } = useParams<{ runId: string }>();
// Remount per run so "Repeat run" navigation starts from a clean state
return <RunDetail key={runId} />;
}
function RunDetail() {
const { t } = useTranslation();
const { id, runId } = useParams<{ id: string; runId: string }>();
const navigate = useNavigate();
const toast = useToast();
const [scenario, setScenario] = useState<Scenario | null>(null);
const [run, setRun] = useState<ScenarioRunDetail | null>(null);
@@ -88,6 +108,9 @@ export function RunDetailPage() {
const [exportLogsOpen, setExportLogsOpen] = useState(false);
const [exportLogsFormat, setExportLogsFormat] = useState<LogExportFormat>('md');
const [exportingLogs, setExportingLogs] = useState(false);
const [pendingDeleteFile, setPendingDeleteFile] = useState<FileMetadata | null>(null);
const [deletingFile, setDeletingFile] = useState(false);
const [repeating, setRepeating] = useState(false);
const logSearchRef = useRef('');
const LOG_PAGE_SIZE = 25;
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
@@ -131,6 +154,41 @@ export function RunDetailPage() {
}
};
const handleRepeatRun = async () => {
if (!id || !run?.environmentId) return;
setRepeating(true);
try {
const newRun = await scenarios.run(
id,
run.environmentId,
run.saveSession,
run.traceMode ?? undefined,
run.scenarioVersion ?? undefined,
);
toast.success(t('scenarios.run_started'));
navigate(`/scenarios/${id}/runs/${newRun.id}`);
} catch (err) {
toast.error((err as Error).message);
} finally {
setRepeating(false);
}
};
const handleDeleteFile = async () => {
if (!id || !runId || !pendingDeleteFile) return;
setDeletingFile(true);
try {
await runFiles.remove(id, runId, pendingDeleteFile.id);
toast.success(t('files.deleteSuccess'));
setPendingDeleteFile(null);
await loadArtifacts();
} catch {
// error toast is already emitted by the API client
} finally {
setDeletingFile(false);
}
};
const manualRefresh = () => {
if (!id || !runId) return;
runs
@@ -223,7 +281,7 @@ export function RunDetailPage() {
key: 'title',
header: t('runs.step_title'),
width: 220,
render: (s) => s.scenarioStep?.title ?? <span className={styles.muted}>–</span>,
render: (s) => s.title ?? <span className={styles.muted}>–</span>,
},
{
key: 'status',
@@ -384,12 +442,27 @@ export function RunDetailPage() {
{ label: `${runId}` },
]}
/>
<AutoRefreshIndicator
active={polling}
pulseKey={pulseKey}
error={!!error}
onClick={manualRefresh}
/>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
{run && (
<span title={run.environmentId ? undefined : t('runs.repeat_no_environment')}>
<Button
variant="secondary"
size="sm"
onClick={handleRepeatRun}
disabled={!run.environmentId || repeating}
>
<RotateCcw size={14} />
{t('runs.action_repeat')}
</Button>
</span>
)}
<AutoRefreshIndicator
active={polling}
pulseKey={pulseKey}
error={!!error}
onClick={manualRefresh}
/>
</div>
</div>
{loading && <p className={styles.muted}>{t('runs.loading')}</p>}
@@ -413,6 +486,20 @@ export function RunDetailPage() {
{ term: t('runs.field_created'), detail: <Timestamp value={run.createdAt} /> },
{ term: t('runs.field_updated'), detail: <Timestamp value={run.updatedAt} /> },
];
if (run.scenarioVersion != null) {
items.push({
term: t('runs.field_scenario_version'),
detail: <span>v{run.scenarioVersion}</span>,
});
}
if (run.traceMode) {
items.push({
term: t('runs.field_trace'),
detail: (
<span>{t(`scenarios.trace_mode_${run.traceMode.replace(/-/g, '_')}`)}</span>
),
});
}
if (run.environment) {
items.push({
term: 'Environment',
@@ -546,6 +633,21 @@ export function RunDetailPage() {
);
},
},
{
key: 'delete',
header: '',
render: (f) => (
<Button
variant="secondary"
size="sm"
title={t('files.delete')}
aria-label={t('files.delete')}
onClick={() => setPendingDeleteFile(f)}
>
<Trash2 size={14} />
</Button>
),
},
] satisfies TableColumn<FileMetadata>[]
}
emptyMessage={t('files.empty')}
@@ -563,6 +665,15 @@ export function RunDetailPage() {
onExport={handleExportLogs}
isExporting={exportingLogs}
/>
<DeleteConfirmationModal
open={pendingDeleteFile != null}
title={t('files.deleteTitle')}
message={t('files.confirmDelete', { name: pendingDeleteFile?.name ?? '' })}
onClose={() => setPendingDeleteFile(null)}
onConfirm={handleDeleteFile}
isDeleting={deletingFile}
/>
</div>
);
}
+11
View File
@@ -161,6 +161,17 @@ export function RunsPage() {
sortable: true,
render: (r) => <Badge variant={STATUS_VARIANT[r.status]}>{STATUS_LABEL[r.status]}</Badge>,
},
{
key: 'scenarioVersion',
header: t('runs.col_version'),
width: 80,
render: (r) =>
r.scenarioVersion != null ? (
`v${r.scenarioVersion}`
) : (
<span className={styles.muted}>–</span>
),
},
{
key: 'createdAt',
header: t('runs.col_created'),
@@ -29,7 +29,9 @@ import type {
Credential,
Environment,
FileMetadata,
Snippet,
ScenarioVersion,
TraceMode,
UsedSnippet,
} from '../../api';
import {
Breadcrumbs,
@@ -46,7 +48,8 @@ import {
useToast,
type TableColumn,
} from '../../ui';
import { ExportScenarioModal } from '../../components/modals';
import { ExportScenarioModal, RunScenarioModal } from '../../components/modals';
import { useRunDefaults } from '../../hooks/useRunDefaults';
import styles from '../Page.module.css';
export function ScenarioDetailPage() {
@@ -73,6 +76,13 @@ export function ScenarioDetailPage() {
const [runModalOpen, setRunModalOpen] = useState(false);
const [running, setRunning] = useState(false);
const [saveSessionFlag, setSaveSessionFlag] = useState(false);
// null = use the server's configured default
const [traceModeOverride, setTraceModeOverride] = useState<TraceMode | null>(null);
const runDefaults = useRunDefaults();
const [runVersions, setRunVersions] = useState<ScenarioVersion[]>([]);
// null = latest
const [runVersion, setRunVersion] = useState<number | null>(null);
const traceMode = traceModeOverride ?? runDefaults?.traceMode ?? 'off';
const [envs, setEnvs] = useState<Environment[]>([]);
const [selectedEnvId, setSelectedEnvId] = useState('');
const envInitRef = useRef(false);
@@ -89,14 +99,13 @@ export function ScenarioDetailPage() {
// Files state
const [files, setFiles] = useState<FileMetadata[]>([]);
const [filesTotal, setFilesTotal] = useState(0);
const [filesLoading, setFilesLoading] = useState(false);
const [uploadOpen, setUploadOpen] = useState(false);
const [uploadFile, setUploadFile] = useState<File | null>(null);
const [uploadExpiresAt, setUploadExpiresAt] = useState('');
const [uploading, setUploading] = useState(false);
const [usedSnippets, setUsedSnippets] = useState<Snippet[]>([]);
const [usedSnippets, setUsedSnippets] = useState<UsedSnippet[]>([]);
const loadFiles = useCallback(async () => {
if (!id) return;
@@ -104,7 +113,6 @@ export function ScenarioDetailPage() {
try {
const result = await scenarioFiles.list(id);
setFiles(result.items);
setFilesTotal(result.total);
} finally {
setFilesLoading(false);
}
@@ -164,7 +172,13 @@ export function ScenarioDetailPage() {
}
setRunning(true);
try {
const run = await scenarios.run(scenario.id, selectedEnvId, saveSessionFlag);
const run = await scenarios.run(
scenario.id,
selectedEnvId,
saveSessionFlag,
traceModeOverride ?? undefined,
runVersion ?? undefined,
);
toast.success(t('scenarios.run_started'));
navigate(`/scenarios/${id}/runs/${run.id}`);
setRunModalOpen(false);
@@ -456,7 +470,21 @@ export function ScenarioDetailPage() {
/>
{scenario && (
<div className={styles.toolbarActions}>
<Button size="sm" onClick={() => setRunModalOpen(true)}>
<Button
size="sm"
onClick={() => {
setTraceModeOverride(null);
setRunVersion(null);
setRunVersions([]);
if (id) {
scenarios
.versions(id)
.then(setRunVersions)
.catch(() => undefined);
}
setRunModalOpen(true);
}}
>
<Play size={14} />
{t('scenarios.action_run')}
</Button>
@@ -504,6 +532,7 @@ export function ScenarioDetailPage() {
items={[
{ term: t('scenarios.field_id'), detail: <UuidBadge id={scenario.id} /> },
{ term: t('scenarios.field_name'), detail: scenario.name },
{ term: t('scenarios.field_version'), detail: `v${scenario.version}` },
...(scenario.environment
? [
{
@@ -679,9 +708,9 @@ export function ScenarioDetailPage() {
<div className={styles.sectionToolbar}>
<h2 className={styles.sectionHeading}>{t('scenarios.used_snippets_heading')}</h2>
</div>
<Table<Snippet>
<Table<UsedSnippet>
data={usedSnippets}
rowKey={(s) => s.id}
rowKey={(s) => `${s.id}@${s.pinnedVersion ?? 'latest'}`}
loading={false}
emptyMessage=""
onRowClick={(s) => navigate(`/snippets/${s.id}`)}
@@ -697,7 +726,15 @@ export function ScenarioDetailPage() {
header: t('snippets.field_title'),
render: (s) => s.title,
},
] satisfies TableColumn<Snippet>[]
{
key: 'version',
header: t('snippets.field_version'),
render: (s) =>
s.pinnedVersion != null
? t('scenarios.used_snippet_pinned', { version: s.pinnedVersion })
: t('scenarios.used_snippet_latest', { version: s.version }),
},
] satisfies TableColumn<UsedSnippet>[]
}
/>
</div>
@@ -826,44 +863,23 @@ export function ScenarioDetailPage() {
{pendingDelete?.type === 'scenario' && t('common.confirm_delete_scenario')}
</Modal>
<Modal
<RunScenarioModal
open={runModalOpen}
title={t('scenarios.run_modal_title')}
onClose={() => !running && setRunModalOpen(false)}
footer={
<>
<Button variant="secondary" onClick={() => setRunModalOpen(false)} disabled={running}>
{t('common.button_cancel')}
</Button>
<Button variant="primary" onClick={handleRun} disabled={running || !selectedEnvId}>
{t('scenarios.action_run')}
</Button>
</>
}
>
{envs.length === 0 ? (
<p>{t('scenarios.run_modal_no_env')}</p>
) : (
<>
<Select
label={t('scenarios.run_modal_env_label')}
value={selectedEnvId}
onChange={(e) => setSelectedEnvId(e.target.value)}
options={envs.map((env) => ({ value: env.id, label: env.name }))}
/>
<label
style={{ display: 'flex', alignItems: 'center', marginTop: '1rem', gap: '0.5rem' }}
>
<input
type="checkbox"
checked={saveSessionFlag}
onChange={(e) => setSaveSessionFlag(e.target.checked)}
/>
<span>{t('common.save_session')}</span>
</label>
</>
)}
</Modal>
environments={envs}
selectedEnvId={selectedEnvId}
onEnvSelect={setSelectedEnvId}
saveSessionFlag={saveSessionFlag}
onSaveSessionChange={setSaveSessionFlag}
versions={runVersions}
selectedVersion={runVersion}
onVersionChange={setRunVersion}
traceMode={traceMode}
defaultTraceMode={runDefaults?.traceMode ?? null}
onTraceModeChange={setTraceModeOverride}
onClose={() => setRunModalOpen(false)}
onRun={handleRun}
isRunning={running}
/>
<ExportScenarioModal
open={exportModalOpen}
+48 -54
View File
@@ -2,16 +2,17 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { usePageTitle } from '../../hooks/usePageTitle';
import { useImportFile } from '../../hooks/useImportFile';
import { Play, Plus, Trash2, Download, ClipboardList } from 'lucide-react';
import { parse as yamlParse } from 'yaml';
import { environments, scenarios } from '../../api';
import type { Environment, Scenario } from '../../api';
import type { Environment, Scenario, ScenarioVersion, TraceMode } from '../../api';
import { RunScenarioModal } from '../../components/modals';
import { useRunDefaults } from '../../hooks/useRunDefaults';
import {
Badge,
Breadcrumbs,
Button,
Modal,
Select,
Table,
type TableColumn,
type SortDirection,
@@ -49,6 +50,13 @@ export function ScenariosPage() {
const [runScenarioId, setRunScenarioId] = useState<string | null>(null);
const [running, setRunning] = useState(false);
const [saveSessionFlag, setSaveSessionFlag] = useState(false);
// null = use the server's configured default
const [traceModeOverride, setTraceModeOverride] = useState<TraceMode | null>(null);
const runDefaults = useRunDefaults();
const [runVersions, setRunVersions] = useState<ScenarioVersion[]>([]);
// null = latest
const [runVersion, setRunVersion] = useState<number | null>(null);
const traceMode = traceModeOverride ?? runDefaults?.traceMode ?? 'off';
const [envs, setEnvs] = useState<Environment[]>([]);
const [selectedEnvId, setSelectedEnvId] = useState('');
@@ -104,6 +112,13 @@ export function ScenariosPage() {
const openRunModal = (id: string) => {
setRunScenarioId(id);
setTraceModeOverride(null);
setRunVersion(null);
setRunVersions([]);
scenarios
.versions(id)
.then(setRunVersions)
.catch(() => undefined);
const scenario = items.find((s) => s.id === id);
const preferred = scenario?.environmentId ?? null;
const exists = preferred ? envs.some((e) => e.id === preferred) : false;
@@ -114,7 +129,13 @@ export function ScenariosPage() {
if (!runScenarioId || !selectedEnvId) return;
setRunning(true);
try {
const run = await scenarios.run(runScenarioId, selectedEnvId, saveSessionFlag);
const run = await scenarios.run(
runScenarioId,
selectedEnvId,
saveSessionFlag,
traceModeOverride ?? undefined,
runVersion ?? undefined,
);
toast.success(t('scenarios.run_started'));
navigate(`/scenarios/${runScenarioId}/runs/${run.id}`);
setRunScenarioId(null);
@@ -138,23 +159,17 @@ export function ScenariosPage() {
}
};
const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
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 scenarios.importScenario(payload);
navigate(`/scenarios/${imported.id}`);
} catch {
// Import errors are silently ignored
}
};
const handleImport = useImportFile('scenario');
const columns: TableColumn<Scenario>[] = [
{ key: 'id', header: t('scenarios.col_id'), render: (s) => <UuidBadge id={s.id} />, width: 60 },
{ key: 'name', header: t('scenarios.col_name'), render: (s) => s.name, sortable: true },
{
key: 'version',
header: t('scenarios.col_version'),
width: 80,
render: (s) => `v${s.version}`,
},
{
key: 'lastRunStatus',
header: t('scenarios.col_last_run'),
@@ -271,44 +286,23 @@ export function ScenariosPage() {
{t('common.confirm_delete_scenario')}
</Modal>
<Modal
<RunScenarioModal
open={runScenarioId != null}
title={t('scenarios.run_modal_title')}
onClose={() => !running && setRunScenarioId(null)}
footer={
<>
<Button variant="secondary" onClick={() => setRunScenarioId(null)} disabled={running}>
Cancel
</Button>
<Button variant="primary" onClick={handleRun} disabled={running || !selectedEnvId}>
{t('scenarios.action_run')}
</Button>
</>
}
>
{envs.length === 0 ? (
<p>{t('scenarios.run_modal_no_env')}</p>
) : (
<>
<Select
label={t('scenarios.run_modal_env_label')}
value={selectedEnvId}
onChange={(e) => setSelectedEnvId(e.target.value)}
options={envs.map((env) => ({ value: env.id, label: env.name }))}
/>
<label
style={{ display: 'flex', alignItems: 'center', marginTop: '1rem', gap: '0.5rem' }}
>
<input
type="checkbox"
checked={saveSessionFlag}
onChange={(e) => setSaveSessionFlag(e.target.checked)}
/>
<span>{t('common.save_session')}</span>
</label>
</>
)}
</Modal>
environments={envs}
selectedEnvId={selectedEnvId}
onEnvSelect={setSelectedEnvId}
saveSessionFlag={saveSessionFlag}
onSaveSessionChange={setSaveSessionFlag}
versions={runVersions}
selectedVersion={runVersion}
onVersionChange={setRunVersion}
traceMode={traceMode}
defaultTraceMode={runDefaults?.traceMode ?? null}
onTraceModeChange={setTraceModeOverride}
onClose={() => setRunScenarioId(null)}
onRun={handleRun}
isRunning={running}
/>
</div>
);
}
@@ -107,6 +107,7 @@ export function SnippetDetailPage() {
{ term: t('snippets.field_id'), detail: <UuidBadge id={snippet.id} /> },
{ term: t('snippets.field_alias'), detail: snippet.alias },
{ term: t('snippets.field_title'), detail: snippet.title },
{ term: t('snippets.field_version'), detail: `v${snippet.version}` },
{
term: t('snippets.field_created'),
detail: <Timestamp value={snippet.createdAt} />,
+3 -15
View File
@@ -2,8 +2,8 @@ import { useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { usePageTitle } from '../../hooks/usePageTitle';
import { useImportFile } from '../../hooks/useImportFile';
import { Settings, Pencil, Trash2, Plus, Download, Braces } from 'lucide-react';
import { parse as yamlParse } from 'yaml';
import { snippets } from '../../api';
import type { Snippet } from '../../api';
import {
@@ -84,6 +84,7 @@ function SnippetCard({ snippet, onDelete }: { snippet: Snippet; onDelete: (id: s
>
<p className={styles.muted}>
{t('snippets.field_alias')}: <code className={styles.codeInline}>{snippet.alias}</code>
{' · '}v{snippet.version}
</p>
{snippet.description &&
(() => {
@@ -116,7 +117,6 @@ function AddSnippetCard() {
export function SnippetsPage() {
const { t } = useTranslation();
usePageTitle(t('snippets.title'));
const navigate = useNavigate();
const [items, setItems] = useState<Snippet[]>([]);
const [loading, setLoading] = useState(true);
const [deleteId, setDeleteId] = useState<string | null>(null);
@@ -150,19 +150,7 @@ export function SnippetsPage() {
}
};
const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
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 (
<div>
+2 -10
View File
@@ -1,9 +1,9 @@
services:
server:
liqa:
build:
context: .
dockerfile: server/Dockerfile
ports:
# UI, REST API (/api/v1), MCP (/mcp) and Swagger (/docs)
- "13000:3000"
env_file:
- .env
@@ -13,11 +13,3 @@ services:
# mount key files into the container
- ./keys:/app/keys
restart: unless-stopped
client:
build:
context: .
dockerfile: client/Dockerfile
ports:
- "13001:80"
restart: unless-stopped
+2 -2
View File
@@ -41,9 +41,9 @@ MCP client (AI agent / VS Code Copilot)
## Client (`client/`)
A React 19 + TypeScript SPA served separately by Vite during development. In Docker Compose the same build artefacts are served statically alongside the API.
A React 19 + TypeScript SPA served by the Vite dev server during development. In the Docker image the server serves the built client itself (`ServeStaticModule`, see `server/src/common/client-static.ts`), from the same origin as the API.
The client calls the server REST endpoints directly — there is no separate BFF layer. Routing uses HashRouter so the Vite proxy and server-side routes are never ambiguous.
The client calls the server REST endpoints directly — there is no separate BFF layer. Routing uses `BrowserRouter`; the server answers any GET outside `/api`, `/mcp`, `/docs` and `/health` with the client's `index.html`, so client-side routes survive a page refresh.
The sidebar footer contains the theme switcher and the app version (injected from the workspace root `package.json` at build time).
+9 -2
View File
@@ -30,7 +30,8 @@ Copy `.env.example` to `.env` and fill in the required values before running.
| `DB_PATH` | `data/sessions.db` | SQLite database file |
| `SESSION_IDLE_TIMEOUT_MINUTES` | `30` | Idle timeout before open sessions are closed |
| `SESSION_DELETE_CLOSED_DAYS` | `7` | Retention period for closed sessions |
| `PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH` | _(Playwright default)_ | Path to Chromium binary. Set automatically in Docker (`/usr/bin/chromium`). |
| `CLIENT_DIR` | *(empty)* | Directory with the built client to serve at `/` (the Docker image sets it to `client`); empty means the server doesn't serve the UI |
| `PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH` | _(Playwright default)_ | Path to a Chromium binary to use instead of Playwright's own. Not set in Docker: the image ships Playwright's `chromium-headless-shell` (matching the Playwright version) under `PLAYWRIGHT_BROWSERS_PATH=/ms-playwright`. |
---
@@ -163,6 +164,10 @@ Both workspaces target zero errors. Run lint before committing.
## Building the container
The root `Dockerfile` builds a single image with the whole application: it builds the client and the server in separate stages, and the runtime image runs the server, which also serves the built client (`CLIENT_DIR`).
The runtime image uses Playwright's headless-only Chromium build (`chromium-headless-shell`) with just the shared libraries it links against, plus DejaVu and Liberation fonts (Liberation is metric-compatible with Arial/Times, so pages lay out as they would with those fonts). All browser launches are headless, so the full desktop Chromium isn't installed. If a new Playwright version needs another library, `ldd` on the `chrome-headless-shell` binary in the image lists what's missing.
```bash
docker compose build
```
@@ -189,7 +194,9 @@ Build and start in one step:
docker compose up -d --build
```
The server API is exposed at **http://localhost:13000**.
Everything is served by the single `liqa` container at **http://localhost:13000**: the UI at `/`, the REST API at `/api/v1`, MCP at `/mcp`, Swagger at `/docs` and health at `/health`. Unknown paths outside those routes get the client's `index.html`, so client-side routes survive a page refresh.
When switching from the old two-container setup (`server` + `client`), remove the old containers with `docker compose up -d --build --remove-orphans`.
SQLite data is persisted in `./data/` and key files are mounted from `./keys/` — both directories are volume-mounted into the container.
+20 -4
View File
@@ -44,11 +44,26 @@ The server currently registers the following tools.
| `update_scenario_step` | Update step fields |
| `delete_scenario_step` | Delete step |
| `list_scenario_runs` | List runs for a scenario |
| `run_scenario` | Trigger run for scenario |
| `list_scenario_versions` | List a scenario's versions, newest first |
| `get_scenario_version` | Get one scenario version with its steps |
| `run_scenario` | Trigger run for scenario; optional `version` (defaults to latest) and `traceMode` |
| `get_scenario_run` | Get run with step runs and logs |
| `wait_for_scenario_run` | Wait until run is `pass` or `fail` |
| `export_scenario` | Export scenario payload |
| `import_scenario` | Import scenario payload |
| `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
| Tool | Description |
|---|---|
| `list_snippets` | List snippets (paginated) |
| `get_snippet` | Get one snippet (latest version) |
| `create_snippet` | Create snippet (`alias`, `title`, `code`) |
| `update_snippet` | Update snippet fields; bumps its version when content changes |
| `delete_snippet` | Delete snippet and all its versions |
| `list_snippet_versions` | List a snippet's versions, newest first |
| `get_snippet_version` | Get one snippet version |
## Scenario Files
@@ -63,7 +78,8 @@ The server currently registers the following tools.
| Tool | Description |
|---|---|
| `list_run_files` | List files (artifacts) created during a scenario run (paginated) |
| `get_run_file_content` | Retrieve run artifact content as base64 with metadata; run artifacts are read-only and inherit expiry from the run artifact subsystem |
| `get_run_file_content` | Retrieve run artifact content as base64 with metadata; run artifact content can't be modified and inherits expiry from the run artifact subsystem |
| `delete_run_file` | Delete a file from a run; the stored file is removed once no scenario or run references it (also `DELETE /scenarios/:id/runs/:runId/files/:fileId` and the delete button on the run detail page) |
## Binary Content Handling
+65 -1
View File
@@ -36,13 +36,77 @@ Available in scope:
- `context.dumpDom(selector?)`: simplified DOM snapshot
- `context.log(...args)`, `context.warn(...args)`, `context.error(...args)`: structured step logs
- `context.assert(() => boolean, description)`: runs the arrow function and requires it to return `true`. Logs `description` (as a `log` entry on success, as an `error` entry on failure or exception), then throws to stop the step if the function returned anything other than `true` or threw
- `context.runSnippet(name, ...args)`: execute stored snippet code with the same context
- `context.runSnippet(name, ...args)`: execute stored snippet code with the same context. `name` is the snippet alias for its latest version, or `alias@<version>` (e.g. `login@3`) to pin a specific version
- `context.getScenarioFiles(opts?)`: list files uploaded to the scenario (`limit`, `offset`)
- `context.downloadFile(url, opts?)`: fetch `url` and save the result as a run artifact (requires a real scenario run — throws when invoked ad hoc, e.g. via the `exec_code` MCP tool). `opts` may include `method`, `headers`, `body`, `filename`. `url` also accepts a `data:` URI (`data:<mediaType>;base64,<data>` or `data:<mediaType>,<percent-encoded data>`) to save content generated in-script (e.g. a credential JWT) directly, without an actual network fetch.
- `console`: proxied to run logs (`log`, `warn`, `error`, etc.)
`validateCode` additionally receives `result` in scope, which is the value returned by `execCode`.
## Versioning
Scenarios and snippets are versioned, starting at version 1.
- A scenario's version goes up when its content changes: name, description, default environment, timeout, or any step change (add, edit, delete, reorder). Credential assignments and uploaded files are not versioned. Saving without changes does not create a new version.
- A snippet's version goes up when its alias, title, description or code changes.
- The scenario and snippet pages always show the latest version. Older versions are available via `GET /scenarios/:id/versions[/:version]` and `GET /snippets/:id/versions[/:version]`.
- A run executes one scenario version, chosen in the run modal (latest by default) or via `version` in `POST /scenarios/:id/run`. The run keeps its own copy of that version's steps, so deleting or editing steps later doesn't change what the run shows. "Repeat run" reuses the run's version.
- Step code uses the latest version of a snippet by default. `context.runSnippet("alias@3")` pins version 3 instead. The version is looked up through the snippet's current alias. Pinned versions are listed under "Used Snippets" on the scenario page.
### Export / import
Exports hold exactly one version and always include its `version` number (the latest by default, or `?version=N`). Imports require `version`, and restore that exact version by id + version:
- If that version already exists, it is left untouched.
- 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.
The trace mode is chosen per run: the run modal in the UI, the `traceMode` field of `POST /scenarios/:id/run`, and the `traceMode` argument of the `run_scenario` MCP tool all accept one of the values below. When it is omitted, the server's `PLAYWRIGHT_TRACE_MODE` setting is used (exposed via `GET /scenarios/run-defaults`). "Repeat run" reuses the original run's mode.
- `on`: keep the trace of every run
- `retain-on-failure`: keep traces only for runs that failed
- `off` (default): do not record traces
Traces are off by default because they often reach ~10 MB per run.
Traces are run artifacts, so they expire after `FILE_RUN_EXPIRATION_DAYS` like other run files.
## Validation contract
`validateCode` may return:
+30 -2
View File
@@ -1,12 +1,12 @@
{
"name": "liqa",
"version": "1.10.1",
"version": "1.19.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "liqa",
"version": "1.10.1",
"version": "1.19.0",
"workspaces": [
"server",
"client"
@@ -3387,6 +3387,33 @@
"tslib": "^2.1.0"
}
},
"node_modules/@nestjs/serve-static": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/@nestjs/serve-static/-/serve-static-5.0.5.tgz",
"integrity": "sha512-AhYx3N9aMwR2cb0w5Nlb5nHNYiAcF74ea/D/xna+PxlXwjmwGN/PpC/5fuMtOwmPBMgOTxNPOnB8C9LDZBSgyw==",
"license": "MIT",
"dependencies": {
"path-to-regexp": "8.4.2"
},
"peerDependencies": {
"@fastify/static": "^8.0.4 || ^9.0.0",
"@nestjs/common": "^11.0.2",
"@nestjs/core": "^11.0.2",
"express": "^5.0.1",
"fastify": "^5.2.1"
},
"peerDependenciesMeta": {
"@fastify/static": {
"optional": true
},
"express": {
"optional": true
},
"fastify": {
"optional": true
}
}
},
"node_modules/@nestjs/swagger": {
"version": "11.2.6",
"resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-11.2.6.tgz",
@@ -16676,6 +16703,7 @@
"@nestjs/core": "^11.1.18",
"@nestjs/platform-express": "^11.1.18",
"@nestjs/schedule": "^6.1.1",
"@nestjs/serve-static": "^5.0.5",
"@nestjs/swagger": "^11.2.6",
"@nestjs/typeorm": "^11.0.1",
"@playwright/test": "^1.59.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "liqa",
"version": "1.11.0",
"version": "1.19.0",
"private": true,
"workspaces": [
"server",
-42
View File
@@ -1,42 +0,0 @@
# ── Build stage ──────────────────────────────────────────────────────────────
FROM node:22-slim AS builder
WORKDIR /app
COPY package*.json ./
COPY server/package.json ./server/
RUN npm ci -w server
COPY server/nest-cli.json ./server/
COPY server/tsconfig*.json ./server/
COPY server/src ./server/src
RUN npm run -w server build
# ── Runtime stage ─────────────────────────────────────────────────────────────
FROM node:22-slim AS runtime
# Playwright / Chromium system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
chromium \
fonts-noto \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Tell Playwright to use the system Chromium instead of downloading its own
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
ENV PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium
WORKDIR /app
COPY package*.json ./
COPY server/package.json ./server/
RUN npm ci --omit=dev -w server
COPY --from=builder /app/server/dist ./dist
# Runtime directories (keys and SQLite DB mounted via volumes)
RUN mkdir -p data
EXPOSE 3000
CMD ["node", "dist/main"]
+1
View File
@@ -25,6 +25,7 @@
"@nestjs/core": "^11.1.18",
"@nestjs/platform-express": "^11.1.18",
"@nestjs/schedule": "^6.1.1",
"@nestjs/serve-static": "^5.0.5",
"@nestjs/swagger": "^11.2.6",
"@nestjs/typeorm": "^11.0.1",
"@playwright/test": "^1.59.1",
+13
View File
@@ -1,8 +1,10 @@
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { ScheduleModule } from "@nestjs/schedule";
import { ServeStaticModule } from "@nestjs/serve-static";
import { TypeOrmModule } from "@nestjs/typeorm";
import { BrowserModule } from "./browser/browser.module";
import { clientStaticOptions } from "./common/client-static";
import { AppConfig, validateAppConfig } from "./config/app.config";
import { CredentialEntity } from "./credential/credential.entity";
import { CredentialModule } from "./credential/credential.module";
@@ -13,12 +15,15 @@ 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";
import { ScenarioRunStepEntity } from "./scenario/scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario/scenario-run.entity";
import { ScenarioStepEntity } from "./scenario/scenario-step.entity";
import { ScenarioVersionEntity } from "./scenario/scenario-version.entity";
import { SnippetVersionEntity } from "./snippet/snippet-version.entity";
import { ScenarioEntity } from "./scenario/scenario.entity";
import { ScenarioModule } from "./scenario/scenario.module";
import { SessionEntity } from "./session/session.entity";
@@ -33,6 +38,11 @@ import { SnippetModule } from "./snippet/snippet.module";
validate: validateAppConfig,
}),
ScheduleModule.forRoot(),
ServeStaticModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<AppConfig, true>) =>
clientStaticOptions(config.get("CLIENT_DIR")),
}),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<AppConfig, true>) => ({
@@ -44,11 +54,13 @@ import { SnippetModule } from "./snippet/snippet.module";
CredentialEntity,
ScenarioEntity,
ScenarioStepEntity,
ScenarioVersionEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
ScenarioCredentialEntity,
SnippetEntity,
SnippetVersionEntity,
FileEntity,
ScenarioFileEntity,
ScenarioRunFileEntity,
@@ -62,6 +74,7 @@ import { SnippetModule } from "./snippet/snippet.module";
ScenarioModule,
SnippetModule,
FileModule,
ImportModule,
McpModule,
HealthModule,
],
@@ -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";
+14
View File
@@ -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" });
}
+40
View File
@@ -0,0 +1,40 @@
import type { ServeStaticModuleOptions } from "@nestjs/serve-static";
import type { ServerResponse } from "node:http";
import * as path from "node:path";
/** Server routes that must never fall back to the client's index.html. */
const SERVER_ROUTES = [
"/api/{*path}",
"/mcp{*path}",
"/docs{*path}",
"/health",
"/healthz",
];
/**
* Serves the built client (SPA) from `clientDir`: static files as they are,
* any other GET outside the server routes gets index.html so client-side
* routes survive a page refresh. Returns no options (nothing served) when
* `clientDir` is empty, e.g. in tests and local development.
*/
export function clientStaticOptions(
clientDir: string,
): ServeStaticModuleOptions[] {
if (!clientDir) return [];
return [
{
rootPath: path.resolve(clientDir),
exclude: SERVER_ROUTES,
serveStaticOptions: {
setHeaders: (res: ServerResponse, filePath: string) => {
// Vite emits content-hashed files under assets/; index.html must
// always be revalidated so a new deploy is picked up immediately
const cacheControl = filePath.includes(`${path.sep}assets${path.sep}`)
? "public, max-age=31536000, immutable"
: "no-cache";
res.setHeader("Cache-Control", cacheControl);
},
},
},
];
}
+3 -3
View File
@@ -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
+14
View File
@@ -32,3 +32,17 @@ export function resolveUsedSnippetAliases(
}
return used;
}
/**
* Splits a `runSnippet` reference into its alias and pinned version:
* `"login@3"` → `{ alias: "login", version: 3 }`, `"login"` → `{ alias: "login", version: null }`.
*/
export function parseSnippetRef(ref: string): {
alias: string;
version: number | null;
} {
const match = /^(.+)@(\d+)$/.exec(ref);
return match
? { alias: match[1], version: Number(match[2]) }
: { alias: ref, version: null };
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { AsyncLocalStorage } from "async_hooks";
import { AsyncLocalStorage } from "node:async_hooks";
export interface TraceStore {
traceId: string;
+22 -1
View File
@@ -1,8 +1,21 @@
import { plainToInstance } from "class-transformer";
import { IsInt, IsString, Max, Min, validateSync } from "class-validator";
import {
IsEnum,
IsInt,
IsString,
Max,
Min,
validateSync,
} from "class-validator";
import pkg from "../../package.json";
export enum TraceMode {
On = "on",
RetainOnFailure = "retain-on-failure",
Off = "off",
}
export class AppConfig {
@IsString()
NODE_ENV: string = "development";
@@ -41,6 +54,14 @@ export class AppConfig {
@IsString()
FILE_CLEANUP_CRON: string = "0 3 * * *";
/** Directory with the built client to serve at `/`; empty to not serve it. */
@IsString()
CLIENT_DIR: string = "";
@IsEnum(TraceMode)
// Off by default: traces with screenshots and snapshots easily reach ~10 MB per run
PLAYWRIGHT_TRACE_MODE: TraceMode = TraceMode.Off;
}
export function validateAppConfig(config: Record<string, unknown>): AppConfig {
+3 -3
View File
@@ -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";
+45
View File
@@ -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",
);
}
}
+15
View File
@@ -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 {}
+176
View File
@@ -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<ImportKind, ClassConstructor<object>> = {
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<ImportResult> {
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<ImportResult> {
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<ImportedItem> {
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,
};
}
}
}
}
+1 -1
View File
@@ -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";
+4 -1
View File
@@ -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<NestExpressApplication>(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());
+2
View File
@@ -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],
+362 -9
View File
@@ -7,12 +7,14 @@ import { SessionService } from "../session/session.service";
import { SessionContextService } from "../session/session-context.service";
import { EnvironmentService } from "../environment/environment.service";
import type { EnvironmentData } from "../environment/environment.entity";
import { TraceMode } from "../config/app.config";
import { CredentialService } from "../credential/credential.service";
import { BrowserService } from "../browser/browser.service";
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";
@@ -28,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 {
@@ -275,6 +278,166 @@ export class McpService {
},
);
// ── Credentials ───────────────────────────────────────────────────────────
server.registerTool(
"list_credentials",
{
description: "List all credentials (paginated)",
inputSchema: {
page: z
.number()
.int()
.min(1)
.optional()
.describe("Page number (default 1)"),
limit: z
.number()
.int()
.min(1)
.optional()
.describe("Items per page (default 20)"),
orderBy: z
.enum(["id", "name", "lastUsedAt", "createdAt", "updatedAt"])
.optional()
.describe("Field to order by (default id)"),
orderDir: z
.enum(["ASC", "DESC"])
.optional()
.describe("Sort direction (default ASC)"),
},
},
async ({ page, limit, orderBy, orderDir }) => {
const result = await this.credentialService.findAll({
page,
limit,
orderBy,
orderDir,
});
return {
content: [{ type: "text" as const, text: JSON.stringify(result) }],
};
},
);
server.registerTool(
"get_credential",
{
description: "Get a credential by ID",
inputSchema: {
id: z.uuid().describe("Credential ID"),
},
},
async ({ id }) => {
try {
const credential = await this.credentialService.findOne(id);
return {
content: [
{ type: "text" as const, text: JSON.stringify(credential) },
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"create_credential",
{
description:
"Create a new credential. Assign it to a scenario with add_scenario_credential to use it via context.getCredential(alias).",
inputSchema: {
name: z.string().min(1).describe("Human-readable credential name"),
data: z
.string()
.optional()
.describe(
'JSON-encoded credential payload, e.g. {"token":"abc123"}',
),
},
},
async ({ name, data }) => {
try {
const credential = await this.credentialService.create({
name,
data,
});
return {
content: [
{ type: "text" as const, text: JSON.stringify(credential) },
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"update_credential",
{
description: "Update an existing credential (name and/or data)",
inputSchema: {
id: z.uuid().describe("Credential ID to update"),
name: z.string().min(1).optional().describe("New name"),
data: z
.string()
.optional()
.describe("New JSON-encoded credential payload"),
},
},
async ({ id, name, data }) => {
try {
const credential = await this.credentialService.update(id, {
...(name !== undefined && { name }),
...(data !== undefined && { data }),
});
return {
content: [
{ type: "text" as const, text: JSON.stringify(credential) },
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"delete_credential",
{
description: "Delete a credential by ID",
inputSchema: {
id: z.uuid().describe("Credential ID to delete"),
},
},
async ({ id }) => {
try {
await this.credentialService.remove(id);
return {
content: [
{ type: "text" as const, text: `Credential ${id} deleted` },
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
// ── Browser ───────────────────────────────────────────────────────────────
server.registerTool(
@@ -779,14 +942,30 @@ export class McpService {
.boolean()
.optional()
.describe("Save session after run completes"),
traceMode: z
.enum(TraceMode)
.optional()
.describe(
"Playwright trace recording: on (keep every trace), retain-on-failure, or off. Defaults to the server's PLAYWRIGHT_TRACE_MODE",
),
version: z
.number()
.int()
.min(1)
.optional()
.describe(
"Scenario version to run (see list_scenario_versions). Defaults to the latest",
),
},
},
async ({ id, environmentId, saveSession }) => {
async ({ id, environmentId, saveSession, traceMode, version }) => {
try {
const run = await this.scenarioService.createRun(
id,
environmentId,
saveSession,
traceMode,
version,
);
return {
content: [{ type: "text" as const, text: JSON.stringify(run) }],
@@ -987,19 +1166,74 @@ export class McpService {
);
server.registerTool(
"export_scenario",
"list_scenario_versions",
{
description:
"Export a scenario as a portable JSON payload (name + steps)",
"List a scenario's versions, newest first. Each version holds the scenario content and steps as of that version",
inputSchema: {
id: z.uuid().describe("Scenario ID to export"),
id: z.uuid().describe("Scenario ID"),
},
},
async ({ id }) => {
try {
const versions = await this.scenarioService.listVersions(id);
return {
content: [{ type: "text" as const, text: JSON.stringify(versions) }],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"get_scenario_version",
{
description: "Get one version of a scenario, with its steps",
inputSchema: {
id: z.uuid().describe("Scenario ID"),
version: z.number().int().min(1).describe("Scenario version"),
},
},
async ({ id, version }) => {
try {
const row = await this.scenarioService.findVersion(id, version);
return {
content: [{ type: "text" as const, text: JSON.stringify(row) }],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"export_scenario",
{
description:
"Export one version of a scenario as a portable JSON payload (version, name + steps)",
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, version }) => {
try {
const exported = await this.scenarioService.exportScenario(id, {
includeEnvironment: false,
credentialIds: [],
version,
});
return {
content: [
@@ -1019,8 +1253,17 @@ export class McpService {
"import_scenario",
{
description:
"Import a scenario from an export payload, creating a new scenario with all its steps",
"Import one scenario version from an export payload. Upserts by id + version: an existing version is left untouched; a new one is stored under its number and becomes the latest when higher than the current latest. Without an id a new scenario is created",
inputSchema: {
id: z
.uuid()
.optional()
.describe("Scenario ID to import into (from the export)"),
version: z
.number()
.int()
.min(1)
.describe("Scenario version the payload holds (from the export)"),
name: z.string().describe("Scenario name"),
description: z
.string()
@@ -1042,10 +1285,12 @@ export class McpService {
.describe("Ordered list of steps"),
},
},
async ({ name, description, steps }) => {
async ({ id, version, name, description, steps }) => {
try {
const scenario = await this.scenarioService.importScenario({
kind: "scenario",
id,
version,
name,
description,
steps: steps as { title: string | null; execCode: string | null }[],
@@ -1064,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",
{
@@ -1164,12 +1438,12 @@ export class McpService {
};
}
// Create a mock Multer file object for the service
// Build the subset of a Multer file object the service uses
const file = {
buffer,
originalname: name,
mimetype: mimeType || "application/octet-stream",
} as any;
};
// Call the service method
const result = await this.scenarioService.uploadFile(
@@ -1313,6 +1587,37 @@ export class McpService {
},
);
server.registerTool(
"delete_run_file",
{
description:
"Delete a file from a run. The stored file is removed once nothing else references it",
inputSchema: {
scenarioId: z.uuid().describe("Scenario ID"),
runId: z.uuid().describe("Run ID"),
fileId: z.uuid().describe("File ID (from list_run_files)"),
},
},
async ({ scenarioId, runId, fileId }) => {
try {
await this.scenarioService.deleteRunFile(scenarioId, runId, fileId);
return {
content: [
{
type: "text" as const,
text: `File ${fileId} deleted from run ${runId}`,
},
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"get_run_file_content",
{
@@ -1427,7 +1732,7 @@ export class McpService {
alias: z
.string()
.describe(
"Unique identifier used to invoke the snippet via context.runSnippet(alias, ...args)",
"Unique identifier used to invoke the snippet via context.runSnippet(alias, ...args). Use 'alias@<version>' to pin a specific version instead of the latest",
),
title: z.string().describe("Human-readable title"),
description: z
@@ -1496,6 +1801,54 @@ export class McpService {
},
);
server.registerTool(
"list_snippet_versions",
{
description:
"List a snippet's versions, newest first. Step code pins a version with context.runSnippet('alias@<version>')",
inputSchema: {
id: z.uuid().describe("Snippet ID"),
},
},
async ({ id }) => {
try {
const versions = await this.snippetService.listVersions(id);
return {
content: [{ type: "text" as const, text: JSON.stringify(versions) }],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"get_snippet_version",
{
description: "Get one version of a snippet",
inputSchema: {
id: z.uuid().describe("Snippet ID"),
version: z.number().int().min(1).describe("Snippet version"),
},
},
async ({ id, version }) => {
try {
const row = await this.snippetService.findVersion(id, version);
return {
content: [{ type: "text" as const, text: JSON.stringify(row) }],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
server.registerTool(
"delete_snippet",
{
@@ -1,4 +1,13 @@
import { IsBoolean, IsNotEmpty, IsOptional, IsUUID } from "class-validator";
import {
IsBoolean,
IsEnum,
IsInt,
IsNotEmpty,
IsOptional,
IsUUID,
Min,
} from "class-validator";
import { TraceMode } from "../../config/app.config";
export class CreateScenarioRunDto {
@IsUUID()
@@ -8,4 +17,14 @@ export class CreateScenarioRunDto {
@IsBoolean()
@IsOptional()
saveSession?: boolean;
@IsEnum(TraceMode)
@IsOptional()
traceMode?: TraceMode;
/** Scenario version to run; defaults to the latest. */
@IsInt()
@Min(1)
@IsOptional()
version?: number;
}
@@ -3,11 +3,13 @@ import { Type } from "class-transformer";
import {
IsArray,
IsIn,
IsInt,
IsNotEmpty,
IsObject,
IsOptional,
IsString,
IsUUID,
Min,
ValidateNested,
} from "class-validator";
import { EnvironmentData } from "../../environment/environment.entity";
@@ -24,6 +26,12 @@ export class ScenarioStepExportDto {
@IsString()
@IsNotEmpty()
execCode: string | null;
@ApiPropertyOptional()
@IsOptional()
@IsInt()
@Min(1)
timeoutSeconds?: number | null;
}
export class CredentialExportDto {
@@ -117,6 +125,17 @@ export class ScenarioExportDto {
@IsUUID()
environmentId?: string;
@ApiProperty({ description: "Scenario version this export holds" })
@IsInt()
@Min(1)
version: number;
@ApiPropertyOptional()
@IsOptional()
@IsInt()
@Min(1)
timeoutSeconds?: number;
@ApiPropertyOptional({ type: [ScenarioCredentialMappingDto] })
@IsOptional()
@IsArray()
+11 -8
View File
@@ -1,4 +1,7 @@
import { Injectable } from "@nestjs/common";
// archiver is a CommonJS `export =` function without a `default` export, and
// esModuleInterop is off, so TypeScript's import-require form is the one that works
// eslint-disable-next-line @typescript-eslint/no-require-imports
import archiver = require("archiver");
import { resolveUsedSnippetAliases } from "../common/snippet-usage";
import { SnippetService } from "../snippet/snippet.service";
@@ -145,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") });
@@ -232,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.
@@ -32,12 +32,24 @@ export class ScenarioRunStepEntity {
@JoinColumn({ name: "runId" })
run: ScenarioRunEntity;
@Column("text")
scenarioStepId: string;
/** Live step this run step was created from; null once that step is deleted. */
@Column("text", { nullable: true })
scenarioStepId: string | null;
@ManyToOne(() => ScenarioStepEntity, { onDelete: "CASCADE" })
@ManyToOne(() => ScenarioStepEntity, { onDelete: "SET NULL", nullable: true })
@JoinColumn({ name: "scenarioStepId" })
scenarioStep: ScenarioStepEntity;
scenarioStep: ScenarioStepEntity | null;
// Step content copied from the run's scenario version, so the run executes
// (and keeps showing) exactly what it was created with.
@Column({ type: "text", nullable: true })
title: string | null;
@Column({ type: "text", nullable: true })
execCode: string | null;
@Column({ type: "int", nullable: true })
timeoutSeconds: number | null;
@Column({ type: "text", default: "waiting" })
status: RunStepStatus;
@@ -8,6 +8,7 @@ import {
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "typeorm";
import type { TraceMode } from "../config/app.config";
import { EnvironmentEntity } from "../environment/environment.entity";
import { SessionEntity } from "../session/session.entity";
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
@@ -41,12 +42,20 @@ export class ScenarioRunEntity {
@JoinColumn({ name: "sessionId" })
session: SessionEntity | null;
/** Scenario version this run executes; null for runs created before versioning. */
@Column({ type: "int", nullable: true, default: null })
scenarioVersion: number | null;
@Column({ type: "text", default: "pending" })
status: RunStatus;
@Column({ type: "boolean", default: false })
saveSession: boolean;
// Playwright trace mode for this run; null falls back to PLAYWRIGHT_TRACE_MODE
@Column("text", { nullable: true, default: null })
traceMode: TraceMode | null;
@OneToMany(() => ScenarioRunStepEntity, (rs) => rs.run, {
cascade: true,
eager: false,
+148 -14
View File
@@ -1,11 +1,16 @@
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 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";
import { Repository } from "typeorm";
import { type AppConfig, TraceMode } from "../config/app.config";
import { FileStorageService } from "../file/file-storage.service";
import type { ScriptLogger } from "../code-executor/code-executor.service";
import { CodeExecutorService } from "../code-executor/code-executor.service";
@@ -22,13 +27,13 @@ import { SnippetService } from "../snippet/snippet.service";
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario-run.entity";
import { ScenarioStepEntity } from "./scenario-step.entity";
import { ScenarioService } from "./scenario.service";
interface BrowserHandle {
browser: Browser;
context: BrowserContext;
page: Page;
tracing: boolean;
}
@Injectable()
@@ -49,6 +54,9 @@ export class ScenarioSchedulerService implements OnModuleInit {
// Monotonic log sequence per run, since persistLog writes are unawaited and
// can otherwise resolve out of call order
private readonly runLogSeq = new Map<string, number>();
// Cache Playwright trace mode per run
private readonly runTraceModes = new Map<string, TraceMode>();
private readonly defaultTraceMode: TraceMode;
private static readonly DEFAULT_SCENARIO_TIMEOUT_SEC = 600;
private static readonly DEFAULT_STEP_TIMEOUT_SEC = 60;
@@ -69,7 +77,10 @@ export class ScenarioSchedulerService implements OnModuleInit {
private readonly sessionService: SessionService,
private readonly sessionContextService: SessionContextService,
private readonly fileStorageService: FileStorageService,
) {}
configService: ConfigService<AppConfig, true>,
) {
this.defaultTraceMode = configService.get("PLAYWRIGHT_TRACE_MODE");
}
async onModuleInit(): Promise<void> {
const staleRuns = await this.runRepo.find({
@@ -203,11 +214,24 @@ export class ScenarioSchedulerService implements OnModuleInit {
.then((env) => env?.data ?? {})
.catch(() => ({}) as EnvironmentData);
this.runEnvironments.set(run.id, environmentData);
const scenario = await this.scenarioService
.findOne(run.scenarioId)
.catch(() => null);
this.runScenarioTimeouts.set(run.id, scenario?.timeoutSeconds ?? null);
// Legacy runs (no scenarioVersion) fall back to the live scenario
const versioned =
run.scenarioVersion != null
? await this.scenarioService
.findVersion(run.scenarioId, run.scenarioVersion)
.catch(() => null)
: await this.scenarioService
.findOne(run.scenarioId)
.catch(() => null);
this.runScenarioTimeouts.set(
run.id,
versioned?.timeoutSeconds ?? null,
);
this.runScenarioIds.set(run.id, run.scenarioId);
this.runTraceModes.set(
run.id,
run.traceMode ?? this.defaultTraceMode,
);
const traceId = crypto.randomUUID();
void traceStorage.run({ traceId }, () =>
this.processRunToCompletion(run.id),
@@ -221,6 +245,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
this.runEnvironments.delete(run.id);
this.runScenarioTimeouts.delete(run.id);
this.runScenarioIds.delete(run.id);
this.runTraceModes.delete(run.id);
this.runLogSeq.delete(run.id);
this.logger.error(
`Run #${run.id}: setup failed — ${String(err)}`,
@@ -246,7 +271,6 @@ export class ScenarioSchedulerService implements OnModuleInit {
try {
let stepRun = await this.runStepRepo.findOne({
where: { runId, status: "pending" },
relations: ["scenarioStep"],
order: { order: "ASC" },
});
while (stepRun) {
@@ -263,7 +287,6 @@ export class ScenarioSchedulerService implements OnModuleInit {
await this.executeStepRun(stepRun);
stepRun = await this.runStepRepo.findOne({
where: { runId, status: "pending" },
relations: ["scenarioStep"],
order: { order: "ASC" },
});
}
@@ -279,12 +302,15 @@ export class ScenarioSchedulerService implements OnModuleInit {
);
await this.failRun(runId, `Unexpected error: ${(err as Error).message}`);
} finally {
// Before runLogSeq is cleared, so the trace log entry keeps its order
await this.saveTrace(runId);
this.activeRuns.delete(runId);
this.runCredentials.delete(runId);
this.runSnippets.delete(runId);
this.runEnvironments.delete(runId);
this.runScenarioTimeouts.delete(runId);
this.runScenarioIds.delete(runId);
this.runTraceModes.delete(runId);
this.runLogSeq.delete(runId);
await this.maybePreserveSession(runId);
}
@@ -345,12 +371,13 @@ export class ScenarioSchedulerService implements OnModuleInit {
}
private async executeStepRun(stepRun: ScenarioRunStepEntity): Promise<void> {
const step = stepRun.scenarioStep as ScenarioStepEntity;
// Run steps carry a copy of the step from the run's scenario version
const step = stepRun;
stepRun.status = "in_progress";
await this.runStepRepo.save(stepRun);
this.logger.log(
`StepRun #${stepRun.id} (run #${stepRun.runId}, step #${step.id} order=${step.order}) → in_progress`,
`StepRun #${stepRun.id} (run #${stepRun.runId}, step #${step.scenarioStepId ?? "deleted"} order=${step.order}) → in_progress`,
);
this.persistLog(
stepRun.runId,
@@ -431,9 +458,21 @@ export class ScenarioSchedulerService implements OnModuleInit {
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
});
const context = await browser.newContext();
let tracing = false;
const traceMode = this.runTraceModes.get(runId) ?? this.defaultTraceMode;
if (traceMode !== TraceMode.Off) {
try {
await context.tracing.start({ screenshots: true, snapshots: true });
tracing = true;
} catch (err) {
this.logger.warn(
`Run #${runId}: failed to start tracing — ${(err as Error).message}`,
);
}
}
const page = await context.newPage();
const handle: BrowserHandle = { browser, context, page };
const handle: BrowserHandle = { browser, context, page, tracing };
this.runBrowsers.set(runId, handle);
this.logger.log(`Run #${runId}: browser created`);
return handle;
@@ -453,6 +492,53 @@ export class ScenarioSchedulerService implements OnModuleInit {
}
}
/**
* Stops the run's Playwright trace and uploads it as a `trace.zip` run
* artifact. In retain-on-failure mode, traces of runs that did not fail are
* discarded.
*/
private async saveTrace(runId: string): Promise<void> {
const handle = this.runBrowsers.get(runId);
if (!handle?.tracing) return;
handle.tracing = false;
const run = await this.runRepo.findOneBy({ id: runId }).catch(() => null);
const traceMode = this.runTraceModes.get(runId) ?? this.defaultTraceMode;
const keep = traceMode === TraceMode.On || run?.status === "fail";
let tmpDir: string | null = null;
try {
if (!keep) {
await handle.context.tracing.stop();
return;
}
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "liqa-trace-"));
const tracePath = path.join(tmpDir, "trace.zip");
await handle.context.tracing.stop({ path: tracePath });
const trace = await fs.readFile(tracePath);
await this.fileStorageService.createAndSaveRunArtifact(
runId,
trace,
"trace.zip",
"application/zip",
);
this.persistLog(
runId,
null,
"log",
"Playwright trace uploaded to run files (open with `npx playwright show-trace trace.zip` or https://trace.playwright.dev)",
);
} catch (err) {
this.logger.warn(
`Run #${runId}: failed to save trace — ${(err as Error).message}`,
);
} finally {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {});
}
}
}
// ── Pass / fail helpers ────────────────────────────────────────────────────
private async passStepRun(
@@ -470,7 +556,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.runId,
stepRun.id,
"log",
`Step ${(stepRun.scenarioStep as ScenarioStepEntity | undefined)?.order ?? "?"} passed`,
`Step ${stepRun.order} passed`,
);
// Find the next waiting step in this run (next by order)
@@ -512,9 +598,11 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.runId,
stepRun.id,
"error",
`Step ${(stepRun.scenarioStep as ScenarioStepEntity | undefined)?.order ?? "?"} failed: ${description}`,
`Step ${stepRun.order} failed: ${description}`,
);
await this.dumpPageOnFailure(stepRun);
// Cancel all remaining waiting/pending step runs in this run
await this.runStepRepo
.createQueryBuilder()
@@ -534,4 +622,50 @@ export class ScenarioSchedulerService implements OnModuleInit {
await this.runRepo.update(stepRun.runId, { status: "fail" });
this.logger.log(`Run #${stepRun.runId} → fail`);
}
// ── Page dump on failure ────────────────────────────────────────────────────
private async dumpPageOnFailure(
stepRun: ScenarioRunStepEntity,
): Promise<void> {
const handle = this.runBrowsers.get(stepRun.runId);
if (!handle) return; // no browser was ever opened for this run (e.g. setup error)
const namePrefix = `step-${stepRun.order}-failure`;
try {
const screenshot = await handle.page.screenshot({ fullPage: true });
await this.fileStorageService.createAndSaveRunArtifact(
stepRun.runId,
screenshot,
`${namePrefix}.png`,
"image/png",
);
} catch (err) {
this.logger.warn(
`StepRun #${stepRun.id}: failed to capture failure screenshot — ${(err as Error).message}`,
);
}
try {
const html = await handle.page.content();
await this.fileStorageService.createAndSaveRunArtifact(
stepRun.runId,
Buffer.from(html, "utf-8"),
`${namePrefix}.html`,
"text/html",
);
} catch (err) {
this.logger.warn(
`StepRun #${stepRun.id}: failed to capture failure page HTML — ${(err as Error).message}`,
);
}
this.persistLog(
stepRun.runId,
stepRun.id,
"log",
`Step ${stepRun.order} failed: page dump uploaded to run files`,
);
}
}
@@ -0,0 +1,59 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";
import { ScenarioEntity } from "./scenario.entity";
/** A step as it was at a given scenario version. */
export interface ScenarioStepSnapshot {
/** Id of the live step this snapshot was taken from (may no longer exist). */
id: string | null;
order: number;
title: string | null;
execCode: string | null;
timeoutSeconds: number | null;
}
/**
* Immutable snapshot of a scenario's content at one version. The `scenarios`
* and `scenario_steps` tables always hold the latest version.
*/
@Entity("scenario_versions")
@Unique(["scenarioId", "version"])
export class ScenarioVersionEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column("text")
scenarioId: string;
@ManyToOne(() => ScenarioEntity, { onDelete: "CASCADE" })
@JoinColumn({ name: "scenarioId" })
scenario: ScenarioEntity;
@Column({ type: "int" })
version: number;
@Column()
name: string;
@Column("text", { nullable: true })
description: string | null;
@Column({ type: "text", nullable: true })
environmentId: string | null;
@Column({ type: "int", nullable: true })
timeoutSeconds: number | null;
@Column("simple-json")
steps: ScenarioStepSnapshot[];
@CreateDateColumn()
createdAt: Date;
}
+58 -11
View File
@@ -5,6 +5,7 @@ import {
Get,
HttpCode,
Param,
ParseIntPipe,
ParseUUIDPipe,
Patch,
Post,
@@ -24,7 +25,7 @@ import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
import { CreateScenarioDto } from "./dto/create-scenario.dto";
import { ScenarioQueryDto } from "./dto/pagination-query.dto";
import { RunsQueryDto } from "./dto/runs-query.dto";
import { ExportEntity } from "./dto/scenario-export.dto";
import { ExportEntity, ScenarioExportDto } from "./dto/scenario-export.dto";
import { UpdateScenarioStepDto } from "./dto/update-scenario-step.dto";
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
import { ScenarioService } from "./scenario.service";
@@ -51,7 +52,9 @@ export class ScenarioController {
@ApiOperation({ summary: "Import a scenario from an export payload (single object or array)" })
@ApiResponse({ status: 201, description: "Scenario imported" })
importScenario(@Body() payload: unknown) {
return this.scenarioService.importScenario(payload as any);
return this.scenarioService.importScenario(
payload as ScenarioExportDto | ExportEntity[],
);
}
@Get()
@@ -70,6 +73,13 @@ export class ScenarioController {
return this.scenarioService.findAllRuns(query);
}
@Get("run-defaults")
@ApiOperation({ summary: "Get server-configured defaults for new runs" })
@ApiResponse({ status: 200 })
getRunDefaults() {
return this.scenarioService.getRunDefaults();
}
@Get(":id")
@ApiOperation({ summary: "Get a scenario with its steps" })
@ApiResponse({ status: 200 })
@@ -98,6 +108,25 @@ export class ScenarioController {
return this.scenarioService.remove(id);
}
@Get(":id/versions")
@ApiOperation({ summary: "List a scenario's versions, newest first" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
listVersions(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.listVersions(id);
}
@Get(":id/versions/:version")
@ApiOperation({ summary: "Get one version of a scenario, with its steps" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or version not found" })
findVersion(
@Param("id", ParseUUIDPipe) id: string,
@Param("version", ParseIntPipe) version: number,
) {
return this.scenarioService.findVersion(id, version);
}
@Get(":id/export")
@ApiOperation({ summary: "Export a scenario as portable YAML" })
@ApiResponse({ status: 200 })
@@ -106,6 +135,8 @@ export class ScenarioController {
@Param("id", ParseUUIDPipe) id: string,
@Query("includeEnvironment") includeEnvironment: string,
@Query("credentialIds") credentialIds: string | string[] | undefined,
@Query("version", new ParseIntPipe({ optional: true }))
version: number | undefined,
@Res({ passthrough: true }) res: Response,
) {
const credIds: string[] = credentialIds
@@ -116,22 +147,23 @@ export class ScenarioController {
const entities = await this.scenarioService.exportScenario(id, {
includeEnvironment: includeEnvironment === "true",
credentialIds: credIds,
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<string, string> = {
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 as any).name ?? (entity as any).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")
@@ -276,6 +308,8 @@ export class ScenarioController {
id,
dto.environmentId,
dto.saveSession,
dto.traceMode,
dto.version,
);
}
@@ -402,4 +436,17 @@ export class ScenarioController {
) {
return this.scenarioService.getRunFileContent(id, runId, fileId, res);
}
@Delete(":id/runs/:runId/files/:fileId")
@HttpCode(204)
@ApiOperation({ summary: "Delete a file from a run" })
@ApiResponse({ status: 204, description: "File deleted" })
@ApiResponse({ status: 404, description: "Scenario, run, or file not found" })
deleteRunFile(
@Param("id", ParseUUIDPipe) id: string,
@Param("runId", ParseUUIDPipe) runId: string,
@Param("fileId", ParseUUIDPipe) fileId: string,
) {
return this.scenarioService.deleteRunFile(id, runId, fileId);
}
}
+4
View File
@@ -29,6 +29,10 @@ export class ScenarioEntity {
@Column({ type: "int", nullable: true })
timeoutSeconds: number | null;
/** Latest version number; incremented whenever the scenario content changes. */
@Column({ type: "int", default: 1 })
version: number;
@ManyToOne(() => EnvironmentEntity, { nullable: true, onDelete: "SET NULL", eager: false })
@JoinColumn({ name: "environmentId" })
environment: EnvironmentEntity | null;
+2
View File
@@ -14,6 +14,7 @@ import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario-run.entity";
import { ScenarioSchedulerService } from "./scenario-scheduler.service";
import { ScenarioVersionEntity } from "./scenario-version.entity";
import { ScenarioStepEntity } from "./scenario-step.entity";
import { ScenarioController } from "./scenario.controller";
import { ScenarioEntity } from "./scenario.entity";
@@ -25,6 +26,7 @@ import { E2eExportService } from "./e2e-export.service";
TypeOrmModule.forFeature([
ScenarioEntity,
ScenarioStepEntity,
ScenarioVersionEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
+304 -61
View File
@@ -3,19 +3,28 @@ import {
ConflictException,
Injectable,
NotFoundException,
OnModuleInit,
} 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,
PaginationQueryDto,
} from "../common/dto/pagination.dto";
import { resolveUsedSnippetAliases } from "../common/snippet-usage";
import {
parseSnippetRef,
resolveUsedSnippetAliases,
} from "../common/snippet-usage";
import type { AppConfig, TraceMode } from "../config/app.config";
import { CredentialEntity } from "../credential/credential.entity";
import { EnvironmentEntity } from "../environment/environment.entity";
import { FileStorageService } from "../file/file-storage.service";
import {
FileMetadata,
FileStorageService,
} from "../file/file-storage.service";
import { ScenarioFileEntity } from "../file/scenario-file.entity";
import { ScenarioRunFileEntity } from "../file/scenario-run-file.entity";
import { SnippetEntity } from "../snippet/snippet.entity";
@@ -37,6 +46,10 @@ import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario-run.entity";
import { ScenarioStepEntity } from "./scenario-step.entity";
import {
ScenarioStepSnapshot,
ScenarioVersionEntity,
} from "./scenario-version.entity";
import { ScenarioEntity } from "./scenario.entity";
import { Response } from "express";
import type { File as MulterFile } from "multer";
@@ -44,13 +57,55 @@ import type { File as MulterFile } from "multer";
export { PaginatedResult } from "../common/dto/pagination.dto";
export type ScenarioOrderBy = "id" | "name" | "createdAt" | "updatedAt";
/** The versioned content of a scenario. */
type ScenarioContent = Pick<
ScenarioVersionEntity,
"name" | "description" | "environmentId" | "timeoutSeconds" | "steps"
>;
function contentKey(c: ScenarioContent): string {
return JSON.stringify([
c.name,
c.description ?? null,
c.environmentId ?? null,
c.timeoutSeconds ?? null,
// Step ids are not content: re-created steps with equal content are equal
c.steps.map((s) => [s.order, s.title, s.execCode, s.timeoutSeconds]),
]);
}
function stepsSnapshot(steps: ScenarioStepEntity[]): ScenarioStepSnapshot[] {
return [...steps]
.sort((a, b) => a.order - b.order)
.map((s) => ({
id: s.id,
order: s.order,
title: s.title,
execCode: s.execCode,
timeoutSeconds: s.timeoutSeconds,
}));
}
/** File metadata as returned by the scenario/run file listing endpoints. */
export interface FileListResult {
items: Omit<FileMetadata, "path">[];
total: number;
}
export type UsedSnippet = SnippetEntity & {
/** Version pinned via `runSnippet("alias@N")`, or null for the latest. */
pinnedVersion: number | null;
};
@Injectable()
export class ScenarioService {
export class ScenarioService implements OnModuleInit {
constructor(
@InjectRepository(ScenarioEntity)
private readonly scenarioRepo: Repository<ScenarioEntity>,
@InjectRepository(ScenarioStepEntity)
private readonly stepRepo: Repository<ScenarioStepEntity>,
@InjectRepository(ScenarioVersionEntity)
private readonly versionRepo: Repository<ScenarioVersionEntity>,
@InjectRepository(ScenarioRunEntity)
private readonly runRepo: Repository<ScenarioRunEntity>,
@InjectRepository(ScenarioRunStepEntity)
@@ -69,12 +124,96 @@ export class ScenarioService {
private readonly scenarioRunFileRepo: Repository<ScenarioRunFileEntity>,
private readonly fileStorageService: FileStorageService,
private readonly snippetService: SnippetService,
private readonly configService: ConfigService<AppConfig, true>,
) {}
async onModuleInit(): Promise<void> {
await this.backfillVersions();
await this.backfillRunStepSnapshots();
}
/** Records the current content of scenarios that have no row for their current version yet. */
private async backfillVersions(): Promise<void> {
const scenarios = await this.scenarioRepo.find({ select: { id: true } });
for (const { id } of scenarios) await this.commitVersion(id);
}
/**
* Run steps created before versioning point at the live step instead of
* holding a copy of it; copy the live step content into them.
*/
private async backfillRunStepSnapshots(): Promise<void> {
await this.runStepRepo.query(`
UPDATE scenario_run_steps SET
title = (SELECT title FROM scenario_steps WHERE id = scenarioStepId),
execCode = (SELECT execCode FROM scenario_steps WHERE id = scenarioStepId),
timeoutSeconds = (SELECT timeoutSeconds FROM scenario_steps WHERE id = scenarioStepId)
WHERE title IS NULL AND execCode IS NULL AND timeoutSeconds IS NULL
AND scenarioStepId IS NOT NULL
AND runId IN (SELECT id FROM scenario_runs WHERE scenarioVersion IS NULL)
`);
}
// ── Versions ──────────────────────────────────────────────────────────────
/**
* Snapshots the scenario's current content. The first snapshot is stored as
* the scenario's current version; later ones bump the version, unless the
* content is unchanged since the latest snapshot.
*/
private async commitVersion(scenarioId: string): Promise<void> {
const scenario = await this.findOne(scenarioId);
const content: ScenarioContent = {
name: scenario.name,
description: scenario.description,
environmentId: scenario.environmentId,
timeoutSeconds: scenario.timeoutSeconds,
steps: stepsSnapshot(scenario.steps),
};
const latest = await this.versionRepo.findOneBy({
scenarioId,
version: scenario.version,
});
if (latest && contentKey(latest) === contentKey(content)) return;
const version = latest ? scenario.version + 1 : scenario.version;
if (version !== scenario.version) {
await this.scenarioRepo.update(scenarioId, { version });
}
await this.versionRepo.save(
this.versionRepo.create({ scenarioId, version, ...content }),
);
}
async listVersions(scenarioId: string): Promise<ScenarioVersionEntity[]> {
await this.findOne(scenarioId); // 404 guard
return this.versionRepo.find({
where: { scenarioId },
order: { version: "DESC" },
});
}
async findVersion(
scenarioId: string,
version: number,
): Promise<ScenarioVersionEntity> {
await this.findOne(scenarioId); // 404 guard
const row = await this.versionRepo.findOneBy({ scenarioId, version });
if (!row) {
throw new NotFoundException(
`Scenario ${scenarioId} has no version ${version}`,
);
}
return row;
}
// ── Scenarios ─────────────────────────────────────────────────────────────
create(dto: CreateScenarioDto): Promise<ScenarioEntity> {
return this.scenarioRepo.save(this.scenarioRepo.create(dto));
async create(dto: CreateScenarioDto): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ ...dto, version: 1 }),
);
await this.commitVersion(scenario.id);
return this.findOne(scenario.id);
}
async findAll(
@@ -142,7 +281,9 @@ export class ScenarioService {
async update(id: string, dto: UpdateScenarioDto): Promise<ScenarioEntity> {
const scenario = await this.findOne(id);
Object.assign(scenario, dto);
return this.scenarioRepo.save(scenario);
await this.scenarioRepo.save(scenario);
await this.commitVersion(id);
return this.findOne(id);
}
async remove(id: string): Promise<void> {
@@ -182,6 +323,7 @@ export class ScenarioService {
}),
);
await this.normalizeStepOrder(scenarioId);
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, created.id);
}
@@ -240,6 +382,7 @@ export class ScenarioService {
await this.normalizeStepOrder(scenarioId);
}
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, stepId);
}
@@ -247,6 +390,7 @@ export class ScenarioService {
await this.findStep(scenarioId, stepId);
await this.stepRepo.delete(stepId);
await this.normalizeStepOrder(scenarioId);
await this.commitVersion(scenarioId);
}
// ── Scenario Credentials ──────────────────────────────────────────────────
@@ -340,19 +484,22 @@ export class ScenarioService {
* Returns the snippets referenced (directly, or transitively via other snippets)
* by this scenario's step code, in discovery order - for linking to them from the UI.
*/
async getUsedSnippets(scenarioId: string): Promise<SnippetEntity[]> {
async getUsedSnippets(scenarioId: string): Promise<UsedSnippet[]> {
const scenario = await this.findOne(scenarioId);
const snippetMap = await this.snippetService.buildSnippetMap();
const usedAliases = resolveUsedSnippetAliases(
const usedRefs = resolveUsedSnippetAliases(
scenario.steps.map((s) => s.execCode ?? ""),
snippetMap,
);
if (usedAliases.length === 0) return [];
const snippets = await this.snippetService.findByAliases(usedAliases);
).map(parseSnippetRef);
if (usedRefs.length === 0) return [];
const snippets = await this.snippetService.findByAliases([
...new Set(usedRefs.map((r) => r.alias)),
]);
const byAlias = new Map(snippets.map((s) => [s.alias, s]));
return usedAliases
.map((alias) => byAlias.get(alias))
.filter((s): s is SnippetEntity => s != null);
return usedRefs.flatMap(({ alias, version }) => {
const snippet = byAlias.get(alias);
return snippet ? [{ ...snippet, pinnedVersion: version }] : [];
});
}
// ── Runs ──────────────────────────────────────────────────────────────────
@@ -488,12 +635,22 @@ export class ScenarioService {
return this.findRun(scenarioId, runId);
}
getRunDefaults(): { traceMode: TraceMode } {
return { traceMode: this.configService.get("PLAYWRIGHT_TRACE_MODE") };
}
async createRun(
scenarioId: string,
environmentId: string,
saveSession = false,
traceMode?: TraceMode,
version?: number,
): Promise<ScenarioRunEntity> {
const scenario = await this.findOne(scenarioId);
const scenarioVersion = await this.findVersion(
scenarioId,
version ?? scenario.version,
);
const environment = await this.environmentRepo.findOneBy({
id: environmentId,
});
@@ -507,14 +664,20 @@ export class ScenarioService {
environmentId,
status: "pending",
saveSession,
traceMode: traceMode ?? this.getRunDefaults().traceMode,
scenarioVersion: scenarioVersion.version,
}),
);
const stepRuns = scenario.steps.map((step, index) =>
const liveStepIds = new Set(scenario.steps.map((s) => s.id));
const stepRuns = scenarioVersion.steps.map((step, index) =>
this.runStepRepo.create({
runId: run.id,
scenarioStepId: step.id,
scenarioStepId: step.id && liveStepIds.has(step.id) ? step.id : null,
order: step.order,
title: step.title,
execCode: step.execCode,
timeoutSeconds: step.timeoutSeconds,
status: index === 0 ? "pending" : "waiting",
description: null,
}),
@@ -533,9 +696,18 @@ export class ScenarioService {
async exportScenario(
id: string,
options: { includeEnvironment: boolean; credentialIds: string[] },
options: {
includeEnvironment: boolean;
credentialIds: string[];
/** Version to export; defaults to the latest. */
version?: number;
},
): Promise<ExportEntity[]> {
const scenario = await this.findOne(id);
const content = await this.findVersion(
id,
options.version ?? scenario.version,
);
const entities: ExportEntity[] = [];
// Include environment entity if requested and linked
@@ -582,13 +754,16 @@ export class ScenarioService {
const scenarioDto: ScenarioExportDto = {
kind: "scenario",
id: scenario.id,
name: scenario.name,
description: scenario.description ?? undefined,
environmentId: scenario.environmentId ?? undefined,
version: content.version,
name: content.name,
description: content.description ?? undefined,
environmentId: content.environmentId ?? undefined,
timeoutSeconds: content.timeoutSeconds ?? undefined,
credentials: credentialMappings.length > 0 ? credentialMappings : undefined,
steps: scenario.steps.map((s) => ({
steps: content.steps.map((s) => ({
title: s.title,
execCode: s.execCode,
...(s.timeoutSeconds != null ? { timeoutSeconds: s.timeoutSeconds } : {}),
})),
};
entities.push(scenarioDto);
@@ -669,46 +844,72 @@ export class ScenarioService {
throw new BadRequestException("steps must be an array");
}
if (!Number.isInteger(scenarioDto.version) || scenarioDto.version < 1) {
throw new BadRequestException("version must be a positive integer");
}
// Restore the exported version: upsert by id + version. An existing
// version is left untouched; a new one is stored under its number and
// becomes the latest when it is higher than the current latest.
const dto = scenarioDto;
const content: ScenarioContent = {
name: dto.name,
description: dto.description ?? null,
environmentId: dto.environmentId ?? null,
timeoutSeconds: dto.timeoutSeconds ?? null,
steps: dto.steps.map((s, index) => ({
id: null,
order: index + 1,
title: s.title ?? null,
execCode: s.execCode ?? null,
timeoutSeconds: s.timeoutSeconds ?? null,
})),
};
const existing = dto.id
? await this.scenarioRepo.findOneBy({ id: dto.id })
: null;
let scenario: ScenarioEntity;
if (dto.id) {
const existing = await this.scenarioRepo.findOneBy({ id: dto.id });
if (existing) {
existing.name = dto.name;
existing.description = dto.description ?? null;
existing.environmentId = dto.environmentId ?? existing.environmentId;
scenario = await this.scenarioRepo.save(existing);
await this.stepRepo.delete({ scenarioId: scenario.id });
} else {
scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({
id: dto.id,
name: dto.name,
description: dto.description ?? null,
environmentId: dto.environmentId ?? null,
}),
);
}
} else {
if (!existing) {
scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({
name: dto.name,
description: dto.description ?? null,
environmentId: dto.environmentId ?? null,
...(dto.id ? { id: dto.id } : {}),
name: content.name,
description: content.description,
environmentId: content.environmentId,
timeoutSeconds: content.timeoutSeconds,
version: dto.version,
}),
);
}
if (dto.steps.length > 0) {
const steps = dto.steps.map((s, index) =>
this.stepRepo.create({
scenarioId: scenario.id,
order: index,
title: s.title ?? null,
execCode: s.execCode ?? null,
}),
);
await this.stepRepo.save(steps);
await this.normalizeStepOrder(scenario.id);
await this.replaceSteps(scenario.id, content.steps);
await this.commitVersion(scenario.id);
} else {
scenario = existing;
const hasVersion = await this.versionRepo.existsBy({
scenarioId: existing.id,
version: dto.version,
});
if (!hasVersion) {
if (dto.version > existing.version) {
Object.assign(existing, {
name: content.name,
description: content.description,
environmentId: content.environmentId ?? existing.environmentId,
timeoutSeconds: content.timeoutSeconds,
version: dto.version,
});
scenario = await this.scenarioRepo.save(existing);
await this.replaceSteps(scenario.id, content.steps);
await this.commitVersion(scenario.id);
} else {
await this.versionRepo.save(
this.versionRepo.create({
scenarioId: existing.id,
version: dto.version,
...content,
}),
);
}
}
}
// Restore credential alias mappings
if (dto.credentials && dto.credentials.length > 0) {
@@ -725,13 +926,33 @@ export class ScenarioService {
return this.findOne(scenario.id);
}
private async replaceSteps(
scenarioId: string,
steps: ScenarioStepSnapshot[],
): Promise<void> {
await this.stepRepo.delete({ scenarioId });
if (steps.length === 0) return;
await this.stepRepo.save(
steps.map((s) =>
this.stepRepo.create({
scenarioId,
order: s.order,
title: s.title,
execCode: s.execCode,
timeoutSeconds: s.timeoutSeconds,
}),
),
);
await this.normalizeStepOrder(scenarioId);
}
// ── Files ─────────────────────────────────────────────────────────────────
async uploadFile(
scenarioId: string,
file: MulterFile,
file: Pick<MulterFile, "buffer" | "originalname" | "mimetype">,
expiresAtStr?: string,
): Promise<any> {
): Promise<Omit<FileMetadata, "path" | "sha256">> {
// Verify scenario exists
await this.findOne(scenarioId);
@@ -778,7 +999,7 @@ export class ScenarioService {
scenarioId: string,
limit?: number,
offset?: number,
): Promise<any> {
): Promise<FileListResult> {
// Verify scenario exists
await this.findOne(scenarioId);
@@ -876,7 +1097,7 @@ export class ScenarioService {
runId: string,
limit?: number,
offset?: number,
): Promise<any> {
): Promise<FileListResult> {
// Verify scenario and run exist
await this.findOne(scenarioId);
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
@@ -904,6 +1125,28 @@ export class ScenarioService {
};
}
/**
* Removes a file from a run. The stored file itself is deleted once no
* scenario or run references it any more.
*/
async deleteRunFile(
scenarioId: string,
runId: string,
fileId: string,
): Promise<void> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run) {
throw new NotFoundException(`Run ${runId} not found in scenario ${scenarioId}`);
}
const link = await this.scenarioRunFileRepo.findOneBy({ runId, fileId });
if (!link) {
throw new NotFoundException(`File ${fileId} not found in run ${runId}`);
}
await this.scenarioRunFileRepo.delete(link.id);
await this.fileStorageService.deleteFile(fileId);
}
async getRunFileContent(
scenarioId: string,
runId: string,
@@ -1,6 +1,8 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import {
IsIn,
IsInt,
Min,
IsNotEmpty,
IsOptional,
IsString,
@@ -18,6 +20,11 @@ export class SnippetExportDto {
@IsUUID()
id?: string;
@ApiProperty({ description: "Snippet version this export holds" })
@IsInt()
@Min(1)
version: number;
@ApiPropertyOptional()
@IsOptional()
@IsString()
@@ -0,0 +1,47 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";
import { SnippetEntity } from "./snippet.entity";
/**
* Immutable snapshot of a snippet at one version. The `snippets` table always
* holds the latest version. Step code pins a version with
* `runSnippet("alias@<version>")`.
*/
@Entity("snippet_versions")
@Unique(["snippetId", "version"])
export class SnippetVersionEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column("text")
snippetId: string;
@ManyToOne(() => SnippetEntity, { onDelete: "CASCADE" })
@JoinColumn({ name: "snippetId" })
snippet: SnippetEntity;
@Column({ type: "int" })
version: number;
@Column()
alias: string;
@Column({ default: "" })
title: string;
@Column("text", { nullable: true })
description: string | null;
@Column("text")
code: string;
@CreateDateColumn()
createdAt: Date;
}
+25 -2
View File
@@ -5,6 +5,7 @@ import {
Get,
HttpCode,
Param,
ParseIntPipe,
ParseUUIDPipe,
Patch,
Post,
@@ -48,9 +49,31 @@ export class SnippetController {
@ApiOperation({ summary: "Export a snippet as a plain object" })
@ApiResponse({ status: 200, description: "Snippet export payload" })
@ApiResponse({ status: 404, description: "Snippet not found" })
async exportSnippet(@Param("id", ParseUUIDPipe) id: string) {
async exportSnippet(
@Param("id", ParseUUIDPipe) id: string,
@Query("version", new ParseIntPipe({ optional: true })) version?: number,
) {
const snippet = await this.snippetService.findOne(id);
return this.snippetService.exportSnippet(snippet);
return this.snippetService.exportSnippet(snippet, version);
}
@Get(":id/versions")
@ApiOperation({ summary: "List a snippet's versions, newest first" })
@ApiResponse({ status: 200, description: "Snippet versions" })
@ApiResponse({ status: 404, description: "Snippet not found" })
listVersions(@Param("id", ParseUUIDPipe) id: string) {
return this.snippetService.listVersions(id);
}
@Get(":id/versions/:version")
@ApiOperation({ summary: "Get one version of a snippet" })
@ApiResponse({ status: 200, description: "Snippet version" })
@ApiResponse({ status: 404, description: "Snippet or version not found" })
findVersion(
@Param("id", ParseUUIDPipe) id: string,
@Param("version", ParseIntPipe) version: number,
) {
return this.snippetService.findVersion(id, version);
}
@Get(":id")
+4
View File
@@ -30,6 +30,10 @@ export class SnippetEntity {
@Column("text")
code: string;
/** Latest version number; incremented whenever the snippet changes. */
@Column({ type: "int", default: 1 })
version: number;
@CreateDateColumn()
createdAt: Date;
+2 -1
View File
@@ -2,10 +2,11 @@ import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { SnippetController } from "./snippet.controller";
import { SnippetEntity } from "./snippet.entity";
import { SnippetVersionEntity } from "./snippet-version.entity";
import { SnippetService } from "./snippet.service";
@Module({
imports: [TypeOrmModule.forFeature([SnippetEntity])],
imports: [TypeOrmModule.forFeature([SnippetEntity, SnippetVersionEntity])],
controllers: [SnippetController],
providers: [SnippetService],
exports: [SnippetService],
+154 -35
View File
@@ -5,7 +5,7 @@ import {
OnModuleInit,
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from "typeorm";
import { In, Not, Repository } from "typeorm";
import {
PaginatedResult,
PaginationQueryDto,
@@ -13,8 +13,20 @@ import {
import { CreateSnippetDto } from "./dto/create-snippet.dto";
import { SnippetExportDto } from "./dto/snippet-export.dto";
import { UpdateSnippetDto } from "./dto/update-snippet.dto";
import { SnippetVersionEntity } from "./snippet-version.entity";
import { SnippetEntity } from "./snippet.entity";
type SnippetContent = Pick<
SnippetEntity,
"alias" | "title" | "description" | "code"
>;
const sameContent = (a: SnippetContent, b: SnippetContent): boolean =>
a.alias === b.alias &&
a.title === b.title &&
(a.description ?? null) === (b.description ?? null) &&
a.code === b.code;
export type SnippetOrderBy =
| "id"
| "alias"
@@ -27,6 +39,8 @@ export class SnippetService implements OnModuleInit {
constructor(
@InjectRepository(SnippetEntity)
private readonly repo: Repository<SnippetEntity>,
@InjectRepository(SnippetVersionEntity)
private readonly versionRepo: Repository<SnippetVersionEntity>,
) {}
async onModuleInit(): Promise<void> {
@@ -47,6 +61,32 @@ export class SnippetService implements OnModuleInit {
"UPDATE snippets SET title = COALESCE(alias, '') WHERE title IS NULL OR title = ''",
);
}
await this.backfillVersions();
}
/** Records the current content of snippets that have no row for their current version yet. */
private async backfillVersions(): Promise<void> {
const snippets = await this.repo.find();
for (const snippet of snippets) {
const exists = await this.versionRepo.existsBy({
snippetId: snippet.id,
version: snippet.version,
});
if (!exists) await this.recordVersion(snippet);
}
}
private recordVersion(snippet: SnippetEntity): Promise<SnippetVersionEntity> {
return this.versionRepo.save(
this.versionRepo.create({
snippetId: snippet.id,
version: snippet.version,
alias: snippet.alias,
title: snippet.title,
description: snippet.description,
code: snippet.code,
}),
);
}
async create(dto: CreateSnippetDto): Promise<SnippetEntity> {
@@ -56,14 +96,17 @@ export class SnippetService implements OnModuleInit {
`Snippet alias "${dto.alias}" already exists`,
);
}
return this.repo.save(
const snippet = await this.repo.save(
this.repo.create({
alias: dto.alias,
title: dto.title,
description: dto.description ?? null,
code: dto.code,
version: 1,
}),
);
await this.recordVersion(snippet);
return snippet;
}
async findAll(
@@ -96,8 +139,37 @@ export class SnippetService implements OnModuleInit {
`Snippet alias "${dto.alias}" already exists`,
);
}
Object.assign(snippet, dto);
return this.repo.save(snippet);
const next: SnippetContent = {
alias: dto.alias ?? snippet.alias,
title: dto.title ?? snippet.title,
description:
dto.description !== undefined ? dto.description : snippet.description,
code: dto.code ?? snippet.code,
};
if (sameContent(snippet, next)) return snippet;
Object.assign(snippet, next, { version: snippet.version + 1 });
const saved = await this.repo.save(snippet);
await this.recordVersion(saved);
return saved;
}
// ── Versions ──────────────────────────────────────────────────────────────
async listVersions(id: string): Promise<SnippetVersionEntity[]> {
await this.findOne(id); // 404 guard
return this.versionRepo.find({
where: { snippetId: id },
order: { version: "DESC" },
});
}
async findVersion(id: string, version: number): Promise<SnippetVersionEntity> {
await this.findOne(id); // 404 guard
const row = await this.versionRepo.findOneBy({ snippetId: id, version });
if (!row) {
throw new NotFoundException(`Snippet ${id} has no version ${version}`);
}
return row;
}
async remove(id: string): Promise<void> {
@@ -105,10 +177,25 @@ export class SnippetService implements OnModuleInit {
await this.repo.delete(id);
}
/** Returns an alias→code map for all snippets (used by the executor). */
/**
* Returns the code map used by the executor: `alias` → latest code, plus
* `alias@<version>` → that version's code for every stored version. Pinned
* references use the snippet's current alias.
*/
async buildSnippetMap(): Promise<Record<string, string>> {
const { data } = await this.findAll({ limit: 1000 });
return Object.fromEntries(data.map((s) => [s.alias, s.code]));
const map: Record<string, string> = Object.fromEntries(
data.map((s) => [s.alias, s.code]),
);
if (data.length === 0) return map;
const aliasById = new Map(data.map((s) => [s.id, s.alias]));
const versions = await this.versionRepo.find({
where: { snippetId: In([...aliasById.keys()]) },
});
for (const v of versions) {
map[`${aliasById.get(v.snippetId)}@${v.version}`] = v.code;
}
return map;
}
/** Returns the snippet entities matching the given aliases (used by executor/executor consumers). */
@@ -117,52 +204,84 @@ export class SnippetService implements OnModuleInit {
return this.repo.find({ where: { alias: In(aliases) } });
}
exportSnippet(snippet: SnippetEntity): SnippetExportDto {
/** Exports the latest version, or `version` when given. */
async exportSnippet(
snippet: SnippetEntity,
version?: number,
): Promise<SnippetExportDto> {
const content: SnippetContent & { version: number } =
version === undefined || version === snippet.version
? snippet
: await this.findVersion(snippet.id, version);
return {
kind: "snippet",
id: snippet.id,
alias: snippet.alias,
title: snippet.title,
description: snippet.description,
code: snippet.code,
version: content.version,
alias: content.alias,
title: content.title,
description: content.description,
code: content.code,
};
}
/**
* Restores the exported version: upsert by id + version. An existing version
* is left untouched; a new one is stored under its number and becomes the
* latest when it is higher than the current latest.
*/
async importSnippet(dto: SnippetExportDto): Promise<SnippetEntity> {
const alias = dto.alias ?? dto.name;
if (!alias) {
throw new ConflictException("Snippet alias is required");
}
const title = dto.title ?? alias;
const content: SnippetContent = {
alias,
title: dto.title ?? alias,
description: dto.description ?? null,
code: dto.code,
};
if (dto.id) {
const existing = await this.repo.findOneBy({ id: dto.id });
if (existing) {
Object.assign(existing, {
alias,
title,
description: dto.description ?? null,
code: dto.code,
});
return this.repo.save(existing);
}
return this.repo.save(
const existing = dto.id ? await this.repo.findOneBy({ id: dto.id }) : null;
if (!existing) {
await this.assertAliasFree(alias);
const created = await this.repo.save(
this.repo.create({
id: dto.id,
alias,
title,
description: dto.description ?? null,
code: dto.code,
...(dto.id ? { id: dto.id } : {}),
...content,
version: dto.version,
}),
);
await this.recordVersion(created);
return created;
}
return this.repo.save(
this.repo.create({
alias,
title,
description: dto.description ?? null,
code: dto.code,
const hasVersion = await this.versionRepo.existsBy({
snippetId: existing.id,
version: dto.version,
});
if (hasVersion) return existing;
await this.versionRepo.save(
this.versionRepo.create({
snippetId: existing.id,
version: dto.version,
...content,
}),
);
if (dto.version > existing.version) {
if (alias !== existing.alias) await this.assertAliasFree(alias, existing.id);
Object.assign(existing, content, { version: dto.version });
return this.repo.save(existing);
}
return existing;
}
private async assertAliasFree(alias: string, exceptId?: string): Promise<void> {
const conflict = await this.repo.findOneBy(
exceptId ? { alias, id: Not(exceptId) } : { alias },
);
if (conflict) {
throw new ConflictException(`Snippet alias "${alias}" already exists`);
}
}
}
+10
View File
@@ -12,6 +12,16 @@ const makeContext = () => ({
cookies: jest.fn().mockResolvedValue([]),
addCookies: jest.fn().mockResolvedValue(undefined),
addInitScript: jest.fn().mockResolvedValue(undefined),
tracing: {
start: jest.fn().mockResolvedValue(undefined),
// Mimic Playwright writing the trace archive when a path is given
stop: jest.fn().mockImplementation(async (opts?: { path?: string }) => {
if (opts?.path) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
await require("node:fs/promises").writeFile(opts.path, "fake trace zip");
}
}),
},
});
const makeBrowser = () => ({
+26 -1
View File
@@ -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
@@ -38,6 +46,8 @@ import { SessionEntity } from "../src/session/session.entity";
import { EnvironmentEntity } from "../src/environment/environment.entity";
import { ScenarioEntity } from "../src/scenario/scenario.entity";
import { ScenarioStepEntity } from "../src/scenario/scenario-step.entity";
import { ScenarioVersionEntity } from "../src/scenario/scenario-version.entity";
import { SnippetVersionEntity } from "../src/snippet/snippet-version.entity";
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
import { ScenarioRunStepEntity } from "../src/scenario/scenario-run-step.entity";
import { ScenarioRunLogEntity } from "../src/scenario/scenario-run-log.entity";
@@ -53,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<INestApplication> {
// 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({
@@ -68,12 +83,14 @@ export async function buildTestApp(): Promise<INestApplication> {
EnvironmentEntity,
ScenarioEntity,
ScenarioStepEntity,
ScenarioVersionEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
ScenarioCredentialEntity,
CredentialEntity,
SnippetEntity,
SnippetVersionEntity,
FileEntity,
ScenarioFileEntity,
ScenarioRunFileEntity,
@@ -85,15 +102,23 @@ export async function buildTestApp(): Promise<INestApplication> {
SessionModule,
EnvironmentModule,
ScenarioModule,
ImportModule,
McpModule,
],
controllers: [HealthController],
}).compile();
const app = module.createNestApplication();
const app = module.createNestApplication<NestExpressApplication>();
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;
}
+141
View File
@@ -0,0 +1,141 @@
/**
* Tests serving the built client alongside the server routes: static files,
* the index.html fallback for client-side routes, and that server routes are
* never answered with index.html.
*/
import {
Controller,
Get,
INestApplication,
Module,
Post,
RequestMethod,
} from "@nestjs/common";
import { ServeStaticModule } from "@nestjs/serve-static";
import { NestFactory } from "@nestjs/core";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import request from "supertest";
import { clientStaticOptions } from "../src/common/client-static";
@Controller("things")
class ThingsController {
@Get()
list() {
return [{ id: 1 }];
}
}
@Controller()
class RootRoutesController {
@Get("health")
health() {
return { status: "ok" };
}
@Post("mcp")
mcp() {
return { jsonrpc: "2.0" };
}
}
async function buildApp(clientDir: string): Promise<INestApplication> {
// NestFactory (as in main.ts) rather than Test.createTestingModule: the
// serve-static module picks its HTTP loader while the module compiles,
// which needs the HTTP adapter to exist already.
@Module({
imports: [ServeStaticModule.forRoot(...clientStaticOptions(clientDir))],
controllers: [ThingsController, RootRoutesController],
})
class ClientStaticTestModule {}
const app = await NestFactory.create(ClientStaticTestModule, {
logger: false,
});
// Same prefix setup as main.ts
app.setGlobalPrefix("api/v1", {
exclude: [
{ path: "health", method: RequestMethod.ALL },
{ path: "mcp", method: RequestMethod.ALL },
],
});
await app.init();
return app;
}
describe("Client static serving", () => {
let clientDir: string;
let app: INestApplication;
beforeAll(async () => {
clientDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-client-"));
fs.mkdirSync(path.join(clientDir, "assets"));
fs.writeFileSync(path.join(clientDir, "index.html"), "<html>liqa</html>");
fs.writeFileSync(
path.join(clientDir, "assets", "app-abc123.js"),
"console.log(1);",
);
app = await buildApp(clientDir);
});
afterAll(async () => {
await app.close();
fs.rmSync(clientDir, { recursive: true, force: true });
});
it("serves index.html at / without caching", async () => {
const res = await request(app.getHttpServer()).get("/").expect(200);
expect(res.text).toBe("<html>liqa</html>");
expect(res.headers["cache-control"]).toBe("no-cache");
});
it("serves index.html for client-side routes", async () => {
const res = await request(app.getHttpServer())
.get("/scenarios/123/runs/456")
.expect(200);
expect(res.text).toBe("<html>liqa</html>");
expect(res.headers["cache-control"]).toBe("no-cache");
});
it("serves hashed assets as immutable", async () => {
const res = await request(app.getHttpServer())
.get("/assets/app-abc123.js")
.expect(200);
expect(res.text).toBe("console.log(1);");
expect(res.headers["cache-control"]).toContain("immutable");
});
it("leaves API routes to the server", async () => {
const res = await request(app.getHttpServer())
.get("/api/v1/things")
.expect(200);
expect(res.body).toEqual([{ id: 1 }]);
});
it("returns the server's 404 for unknown API routes, not index.html", async () => {
const res = await request(app.getHttpServer())
.get("/api/v1/nope")
.expect(404);
expect(res.text).not.toContain("liqa</html>");
});
it("leaves /health and /mcp to the server", async () => {
await request(app.getHttpServer())
.get("/health")
.expect(200, { status: "ok" });
await request(app.getHttpServer())
.post("/mcp")
.expect(201, { jsonrpc: "2.0" });
const res = await request(app.getHttpServer()).get("/mcp");
expect(res.text).not.toContain("liqa</html>");
});
it("serves nothing when no client directory is configured", async () => {
const bare = await buildApp("");
await request(bare.getHttpServer()).get("/").expect(404);
await request(bare.getHttpServer()).get("/api/v1/things").expect(200);
await bare.close();
});
});
+13 -5
View File
@@ -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,6 +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 "node:net";
describe("Exec Context File Methods Integration Tests", () => {
let app: INestApplication;
@@ -57,7 +58,7 @@ describe("Exec Context File Methods Integration Tests", () => {
// Start a test HTTP server for downloadFile tests
testServer = await createTestServer();
testServerUrl = `http://localhost:${(testServer.address() as any).port}`;
testServerUrl = `http://localhost:${(testServer.address() as AddressInfo).port}`;
});
afterAll(async () => {
@@ -69,7 +70,7 @@ describe("Exec Context File Methods Integration Tests", () => {
// Clean up files directory
try {
await fs.rm(filesDir, { recursive: true, force: true });
} catch (err) {
} catch {
// Ignore errors if directory doesn't exist
}
@@ -133,11 +134,16 @@ describe("Exec Context File Methods Integration Tests", () => {
scenarioStepId: string,
order = 0,
) {
// Run steps execute from their own copy of the step content
const step = await stepRepo.findOneByOrFail({ id: scenarioStepId });
const runStep = await runStepRepo.save(
runStepRepo.create({
runId,
scenarioStepId,
order,
title: step.title,
execCode: step.execCode,
timeoutSeconds: step.timeoutSeconds,
status: "pending",
}),
);
@@ -218,7 +224,9 @@ describe("Exec Context File Methods Integration Tests", () => {
expect(output.total).toBeGreaterThanOrEqual(1);
// Find the uploaded file in the output
const foundFile = output.items.find((f: any) => f.id === uploadedFile.id);
const foundFile = output.items.find(
(f: { id: string; name: string }) => f.id === uploadedFile.id,
);
expect(foundFile).toBeDefined();
expect(foundFile.name).toBe("test-file.txt");
expect(foundFile.mimeType).toBe("text/plain");
+87 -11
View File
@@ -1,11 +1,12 @@
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";
import { FileEntity } from "../src/file/file.entity";
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
import { ScenarioEntity } from "../src/scenario/scenario.entity";
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
@@ -15,6 +16,7 @@ describe("File Controller Integration Tests", () => {
let fileRepo: Repository<FileEntity>;
let scenarioRepo: Repository<ScenarioEntity>;
let runRepo: Repository<ScenarioRunEntity>;
let runFileRepo: Repository<ScenarioRunFileEntity>;
let filesDir: string;
beforeAll(async () => {
@@ -27,6 +29,9 @@ describe("File Controller Integration Tests", () => {
runRepo = app.get<Repository<ScenarioRunEntity>>(
getRepositoryToken(ScenarioRunEntity),
);
runFileRepo = app.get<Repository<ScenarioRunFileEntity>>(
getRepositoryToken(ScenarioRunFileEntity),
);
// Get the files directory from the service
filesDir = fileStorageService.getAbsolutePath("");
@@ -36,7 +41,7 @@ describe("File Controller Integration Tests", () => {
// Clean up files directory
try {
await fs.rm(filesDir, { recursive: true, force: true });
} catch (err) {
} catch {
// Ignore errors if directory doesn't exist
}
await app.close();
@@ -146,7 +151,9 @@ describe("File Controller Integration Tests", () => {
expect(res.body.items.length).toBeGreaterThanOrEqual(1);
expect(res.body.total).toBeGreaterThanOrEqual(1);
const file = res.body.items.find((f: any) => f.name === "listed.txt");
const file = res.body.items.find(
(f: { id: string; name: string }) => f.name === "listed.txt",
);
expect(file).toBeDefined();
expect(file.id).toBeDefined();
expect(file.mimeType).toBe("text/plain");
@@ -274,7 +281,9 @@ describe("File Controller Integration Tests", () => {
expect(Array.isArray(res.body.items)).toBe(true);
expect(res.body.total).toBeGreaterThanOrEqual(1);
const artifact = res.body.items.find((f: any) => f.id === savedFile.id);
const artifact = res.body.items.find(
(f: { id: string; name: string; size: number }) => f.id === savedFile.id,
);
expect(artifact).toBeDefined();
expect(artifact.name).toBe("artifact.txt");
expect(artifact.size).toBe(fileBuffer.length);
@@ -335,12 +344,83 @@ describe("File Controller Integration Tests", () => {
});
});
// ── DELETE /scenarios/:id/runs/:runId/files/:fileId ───────────────────────
describe("DELETE /scenarios/:id/runs/:runId/files/:fileId", () => {
async function createArtifact(runId: string, name = "to-delete.txt") {
return fileStorageService.createAndSaveRunArtifact(
runId,
Buffer.from("delete me"),
name,
"text/plain",
);
}
it("removes the file from the run and deletes the stored file", async () => {
const scenario = await createScenario("run-file-delete");
const run = await createRun(scenario.id);
const file = await createArtifact(run.id);
const physicalPath = fileStorageService.getAbsolutePath(file.filePath);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(204);
const list = await request(app.getHttpServer())
.get(`/scenarios/${scenario.id}/runs/${run.id}/files`)
.expect(200);
expect(list.body.items).toHaveLength(0);
expect(await fileRepo.findOneBy({ id: file.id })).toBeNull();
await expect(fs.access(physicalPath)).rejects.toThrow();
});
it("keeps the stored file while another run still references it", async () => {
const scenario = await createScenario("run-file-delete-shared");
const run = await createRun(scenario.id);
const otherRun = await createRun(scenario.id);
const file = await createArtifact(run.id, "shared.txt");
await runFileRepo.save(
runFileRepo.create({ runId: otherRun.id, fileId: file.id }),
);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(204);
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
await request(app.getHttpServer())
.get(
`/scenarios/${scenario.id}/runs/${otherRun.id}/files/${file.id}/content`,
)
.expect(200);
});
it("returns 404 for a file that belongs to a different run", async () => {
const scenario = await createScenario("run-file-delete-mismatch");
const run = await createRun(scenario.id);
const otherRun = await createRun(scenario.id);
const file = await createArtifact(otherRun.id);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(404);
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
});
it("returns 404 when the run does not exist", async () => {
const scenario = await createScenario("run-file-delete-no-run");
await request(app.getHttpServer())
.delete(
`/scenarios/${scenario.id}/runs/00000000-0000-0000-0000-000000000000/files/00000000-0000-0000-0000-000000000000`,
)
.expect(404);
});
});
// ── File cleanup ───────────────────────────────────────────────────────────
describe("File cleanup", () => {
it("removes expired files from disk and database", async () => {
const scenario = await createScenario("cleanup-test");
// Upload a file with a past expiration date
const fileBuffer = Buffer.from("to be cleaned up");
const savedFile = await fileStorageService.saveFile(
@@ -367,13 +447,11 @@ describe("File Controller Integration Tests", () => {
fail("File should have been deleted");
} catch (err) {
// Expected: file not found
expect((err as any).code).toBe("ENOENT");
expect((err as NodeJS.ErrnoException).code).toBe("ENOENT");
}
});
it("does not delete files with future expiration dates", async () => {
const scenario = await createScenario("no-cleanup-test");
// Upload a file with a future expiration date
const fileBuffer = Buffer.from("should not be cleaned");
const futureDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
@@ -397,8 +475,6 @@ describe("File Controller Integration Tests", () => {
});
it("does not delete files without expiration dates", async () => {
const scenario = await createScenario("no-expire-test");
// Upload a file without expiration
const fileBuffer = Buffer.from("permanent file");
const savedFile = await fileStorageService.saveFile(
+219
View File
@@ -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<string, unknown>) {
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");
});
});
});
+188 -7
View File
@@ -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,6 +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 "node:net";
/**
* MCP controller integration tests.
@@ -50,7 +51,7 @@ describe("McpController", () => {
// Start a test HTTP server for downloadFile tests
testServer = await createTestServer();
testServerUrl = `http://localhost:${(testServer.address() as any).port}`;
testServerUrl = `http://localhost:${(testServer.address() as AddressInfo).port}`;
});
afterAll(async () => {
@@ -124,11 +125,16 @@ describe("McpController", () => {
}
async function createRunStep(runId: string, scenarioStepId: string, order = 0) {
// Run steps execute from their own copy of the step content
const step = await stepRepo.findOneByOrFail({ id: scenarioStepId });
return runStepRepo.save(
runStepRepo.create({
runId,
scenarioStepId,
order,
title: step.title,
execCode: step.execCode,
timeoutSeconds: step.timeoutSeconds,
status: "pending",
}),
);
@@ -588,11 +594,11 @@ describe("McpController", () => {
total: number;
};
// Traces are off by default, so the script's artifact is the only file
expect(list.total).toBe(1);
expect(list.items).toHaveLength(1);
expect(list.items[0].name).toBe("artifact.bin");
expect(list.items[0].mimeType).toBe("application/octet-stream");
expect(list.items[0].expiresAt).not.toBeNull();
const artifact = list.items.find((f) => f.name === "artifact.bin")!;
expect(artifact.mimeType).toBe("application/octet-stream");
expect(artifact.expiresAt).not.toBeNull();
});
it("returns MCP error for non-existent scenario", async () => {
@@ -606,6 +612,42 @@ describe("McpController", () => {
});
});
describe("delete_run_file", () => {
it("deletes a run artifact so it is no longer listed", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete");
const listFiles = async () =>
JSON.parse(
(
(await mcpCall("list_run_files", { scenarioId: sc.id, runId: run.id }))
.rpc.result as { content: { text: string }[] }
).content[0].text,
) as { items: Array<{ id: string; name: string }>; total: number };
const file = (await listFiles()).items.find((f) => f.name === "artifact.bin")!;
const { status, rpc } = await mcpCall("delete_run_file", {
scenarioId: sc.id,
runId: run.id,
fileId: file.id,
});
expect(status).toBe(200);
const result = rpc.result as { isError?: boolean; content: { text: string }[] };
expect(result.isError).toBeFalsy();
expect(result.content[0].text).toContain(file.id);
expect((await listFiles()).items.map((f) => f.id)).not.toContain(file.id);
});
it("returns an MCP error for a file not in the run", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete-missing");
const { status, rpc } = await mcpCall("delete_run_file", {
scenarioId: sc.id,
runId: run.id,
fileId: "00000000-0000-0000-0000-000000000000",
});
expect(status).toBe(200);
expect((rpc.result as { isError: boolean }).isError).toBe(true);
});
});
describe("get_run_file_content", () => {
it("returns run artifact bytes as base64 for a valid file", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-content-test");
@@ -621,7 +663,7 @@ describe("McpController", () => {
) as {
items: Array<{ id: string; name: string; mimeType: string }>;
};
const file = list.items[0];
const file = list.items.find((f) => f.name === "artifact.bin")!;
const { status, rpc } = await mcpCall("get_run_file_content", {
scenarioId: sc.id,
@@ -843,4 +885,143 @@ describe("McpController", () => {
expect(result.isError).toBe(true);
});
});
// ── Credential CRUD tools ─────────────────────────────────────────────────
async function createCredential(name: string, data?: string) {
const { rpc } = await mcpCall("create_credential", { name, data });
return JSON.parse(
(rpc.result as { content: { text: string }[] }).content[0].text,
) as { id: string; name: string; data: string | null };
}
describe("list_credentials", () => {
it("returns a paginated result with a data array", async () => {
await createCredential("mcp-list-cred");
const { status, rpc } = await mcpCall("list_credentials", {
limit: 100,
});
expect(status).toBe(200);
const result = rpc.result as { content: { text: string }[] };
const body = JSON.parse(result.content[0].text) as {
data: { name: string }[];
total: number;
};
expect(Array.isArray(body.data)).toBe(true);
expect(typeof body.total).toBe("number");
expect(body.data.some((c) => c.name === "mcp-list-cred")).toBe(true);
});
});
describe("create_credential", () => {
it("creates a credential and returns it with name and data", async () => {
const created = await createCredential(
"mcp-test-cred",
'{"token":"abc123"}',
);
expect(typeof created.id).toBe("string");
expect(created.name).toBe("mcp-test-cred");
expect(created.data).toBe('{"token":"abc123"}');
});
it("stores null data when data is omitted", async () => {
const created = await createCredential("mcp-test-cred-nodata");
expect(created.data).toBeNull();
});
});
describe("get_credential", () => {
it("returns the created credential by id", async () => {
const created = await createCredential("mcp-get-cred", '{"a":1}');
const { status, rpc } = await mcpCall("get_credential", {
id: created.id,
});
expect(status).toBe(200);
const result = rpc.result as { content: { text: string }[] };
const fetched = JSON.parse(result.content[0].text) as {
id: string;
name: string;
data: string;
};
expect(fetched.id).toBe(created.id);
expect(fetched.name).toBe("mcp-get-cred");
expect(fetched.data).toBe('{"a":1}');
});
it("returns an MCP error for a non-existent credential id", async () => {
const { status, rpc } = await mcpCall("get_credential", {
id: "00000000-0000-0000-0000-000000000000",
});
expect(status).toBe(200);
const result = rpc.result as { isError: boolean };
expect(result.isError).toBe(true);
});
});
describe("update_credential", () => {
it("updates name and data of an existing credential", async () => {
const created = await createCredential("mcp-upd-cred-orig", '{"v":1}');
const { status, rpc } = await mcpCall("update_credential", {
id: created.id,
name: "mcp-upd-cred-new",
data: '{"v":2}',
});
expect(status).toBe(200);
const result = rpc.result as { content: { text: string }[] };
const updated = JSON.parse(result.content[0].text) as {
name: string;
data: string;
};
expect(updated.name).toBe("mcp-upd-cred-new");
expect(updated.data).toBe('{"v":2}');
});
it("leaves data untouched when only name is updated", async () => {
const created = await createCredential("mcp-upd-cred-partial", '{"k":1}');
const { rpc } = await mcpCall("update_credential", {
id: created.id,
name: "mcp-upd-cred-partial-new",
});
const updated = JSON.parse(
(rpc.result as { content: { text: string }[] }).content[0].text,
) as { name: string; data: string };
expect(updated.name).toBe("mcp-upd-cred-partial-new");
expect(updated.data).toBe('{"k":1}');
});
it("returns an MCP error for a non-existent credential id", async () => {
const { status, rpc } = await mcpCall("update_credential", {
id: "00000000-0000-0000-0000-000000000000",
name: "Ghost",
});
expect(status).toBe(200);
const result = rpc.result as { isError: boolean };
expect(result.isError).toBe(true);
});
});
describe("delete_credential", () => {
it("deletes an existing credential and confirms deletion", async () => {
const created = await createCredential("mcp-del-cred");
const { status, rpc } = await mcpCall("delete_credential", {
id: created.id,
});
expect(status).toBe(200);
const result = rpc.result as { content: { text: string }[] };
expect(result.content[0].text).toContain(created.id);
const getResult = (await mcpCall("get_credential", { id: created.id }))
.rpc.result as { isError: boolean };
expect(getResult.isError).toBe(true);
});
it("returns an MCP error for a non-existent credential id", async () => {
const { status, rpc } = await mcpCall("delete_credential", {
id: "00000000-0000-0000-0000-000000000000",
});
expect(status).toBe(200);
const result = rpc.result as { isError: boolean };
expect(result.isError).toBe(true);
});
});
});
+190
View File
@@ -0,0 +1,190 @@
/**
* 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<string> {
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<string> })
.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 (off) when none is given", async () => {
const runId = await runScenario(
"trace-default",
"throw new Error('boom');",
);
const [row] = await dataSource.query(
`SELECT traceMode FROM scenario_runs WHERE id = ?`,
[runId],
);
expect(row.traceMode).toBe("off");
expect(await traceArtifacts(runId)).toHaveLength(0);
});
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,
false,
TraceMode.On,
);
// Legacy runs have no stored mode; they must use the configured default (off)
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<string> })
.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(0);
});
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: "off" });
});
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);
});
});
});
+10 -1
View File
@@ -676,6 +676,7 @@ describe("ScenarioController", () => {
it("creates a new scenario with all steps", async () => {
const payload = {
name: "imported scenario",
version: 1,
steps: [
{
sessionName: "s",
@@ -750,13 +751,20 @@ describe("ScenarioController", () => {
it("imports with empty steps array", async () => {
const res = await request(app.getHttpServer())
.post("/scenarios/import")
.send({ name: "empty-import", steps: [] })
.send({ name: "empty-import", version: 1, steps: [] })
.expect(201);
expect(res.body.name).toBe("empty-import");
expect(res.body.steps).toHaveLength(0);
});
it("returns 400 when version is missing", async () => {
await request(app.getHttpServer())
.post("/scenarios/import")
.send({ name: "no-version", steps: [] })
.expect(400);
});
it("returns 400 when name is missing", async () => {
await request(app.getHttpServer())
.post("/scenarios/import")
@@ -776,6 +784,7 @@ describe("ScenarioController", () => {
.post("/scenarios/import")
.send({
name: "bad-type",
version: 1,
steps: [{ type: "unknown", sessionName: "s" }],
})
.expect(201);
+393
View File
@@ -0,0 +1,393 @@
/**
* Integration tests for scenario and snippet versioning:
* - versions bump on content changes only
* - 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
*/
import { INestApplication } from "@nestjs/common";
import request from "supertest";
import { DataSource } from "typeorm";
import { buildTestApp } from "./app.harness";
import { CredentialEntity } from "../src/credential/credential.entity";
import { EnvironmentEntity } from "../src/environment/environment.entity";
import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service";
import { ScenarioService } from "../src/scenario/scenario.service";
import { SnippetService } from "../src/snippet/snippet.service";
describe("Versioning", () => {
let app: INestApplication;
let dataSource: DataSource;
let scenarioService: ScenarioService;
let snippetService: SnippetService;
let scheduler: ScenarioSchedulerService;
beforeAll(async () => {
app = await buildTestApp();
dataSource = app.get(DataSource);
scenarioService = app.get(ScenarioService);
snippetService = app.get(SnippetService);
scheduler = app.get(ScenarioSchedulerService);
});
afterAll(async () => {
await app.close();
});
// ── helpers ────────────────────────────────────────────────────────────────
const uniq = () => Math.random().toString(36).slice(2, 8);
async function createEnvironment(): Promise<string> {
const repo = dataSource.getRepository(EnvironmentEntity);
const env = await repo.save(
repo.create({ name: `env-${uniq()}`, data: {} }),
);
return env.id;
}
async function versionNumbers(scenarioId: string): Promise<number[]> {
return (await scenarioService.listVersions(scenarioId)).map(
(v) => v.version,
);
}
/** Creates and processes a run; returns it with its step runs. */
async function runToCompletion(scenarioId: string, version?: number) {
const run = await scenarioService.createRun(
scenarioId,
await createEnvironment(),
false,
undefined,
version,
);
await scheduler.pickUpPendingRuns();
return scenarioService.waitForRun(scenarioId, run.id, 10_000);
}
const outputs = (run: { stepRuns: { output: string | null }[] }) =>
run.stepRuns.map((s) => (s.output ? JSON.parse(s.output) : null));
// ── scenario versions ─────────────────────────────────────────────────────
describe("scenario versions", () => {
it("starts at version 1 with a stored snapshot", async () => {
const sc = await scenarioService.create({ name: `v-${uniq()}` });
expect(sc.version).toBe(1);
expect(await versionNumbers(sc.id)).toEqual([1]);
});
it("bumps on scenario and step changes, not on no-op updates", async () => {
const sc = await scenarioService.create({ name: `bump-${uniq()}` });
await scenarioService.update(sc.id, { name: `${sc.name}-renamed` });
expect((await scenarioService.findOne(sc.id)).version).toBe(2);
// Same content again: no new version
await scenarioService.update(sc.id, { name: `${sc.name}-renamed` });
expect((await scenarioService.findOne(sc.id)).version).toBe(2);
const step = await scenarioService.createStep(sc.id, {
execCode: "return 1;",
});
expect((await scenarioService.findOne(sc.id)).version).toBe(3);
await scenarioService.updateStep(sc.id, step.id, {
execCode: "return 2;",
});
expect((await scenarioService.findOne(sc.id)).version).toBe(4);
await scenarioService.removeStep(sc.id, step.id);
expect((await scenarioService.findOne(sc.id)).version).toBe(5);
expect(await versionNumbers(sc.id)).toEqual([5, 4, 3, 2, 1]);
const v4 = await scenarioService.findVersion(sc.id, 4);
expect(v4.steps.map((s) => s.execCode)).toEqual(["return 2;"]);
});
it("does not bump on credential assignment", async () => {
const sc = await scenarioService.create({ name: `cred-${uniq()}` });
const credRepo = dataSource.getRepository(CredentialEntity);
const cred = await credRepo.save(
credRepo.create({ name: "c", data: null }),
);
await scenarioService.addScenarioCredential(sc.id, {
credentialId: cred.id,
alias: "c",
});
expect((await scenarioService.findOne(sc.id)).version).toBe(1);
});
it("exposes versions over HTTP", async () => {
const sc = await scenarioService.create({ name: `http-${uniq()}` });
await scenarioService.createStep(sc.id, { execCode: "return 1;" });
const list = await request(app.getHttpServer())
.get(`/scenarios/${sc.id}/versions`)
.expect(200);
expect(list.body.map((v: { version: number }) => v.version)).toEqual([
2, 1,
]);
const one = await request(app.getHttpServer())
.get(`/scenarios/${sc.id}/versions/2`)
.expect(200);
expect(one.body.steps).toHaveLength(1);
await request(app.getHttpServer())
.get(`/scenarios/${sc.id}/versions/99`)
.expect(404);
});
});
// ── runs ──────────────────────────────────────────────────────────────────
describe("runs", () => {
it("runs the latest version by default and records it", async () => {
const sc = await scenarioService.create({ name: `run-${uniq()}` });
await scenarioService.createStep(sc.id, { execCode: "return 'latest';" });
const run = await runToCompletion(sc.id);
expect(run.scenarioVersion).toBe(2);
expect(outputs(run)).toEqual(["latest"]);
});
it("runs an older version's steps when asked", async () => {
const sc = await scenarioService.create({ name: `old-${uniq()}` });
const step = await scenarioService.createStep(sc.id, {
title: "first",
execCode: "return 'v2';",
});
await scenarioService.updateStep(sc.id, step.id, {
execCode: "return 'v3';",
});
await scenarioService.createStep(sc.id, { execCode: "return 'extra';" });
const run = await runToCompletion(sc.id, 2);
expect(run.scenarioVersion).toBe(2);
expect(run.stepRuns).toHaveLength(1);
expect(run.stepRuns[0].title).toBe("first");
expect(run.stepRuns[0].execCode).toBe("return 'v2';");
expect(outputs(run)).toEqual(["v2"]);
});
it("rejects a version that does not exist", async () => {
const sc = await scenarioService.create({ name: `nov-${uniq()}` });
const environmentId = await createEnvironment();
await request(app.getHttpServer())
.post(`/scenarios/${sc.id}/run`)
.send({ environmentId, version: 7 })
.expect(404);
});
it("keeps run history when a step is deleted", async () => {
const sc = await scenarioService.create({ name: `hist-${uniq()}` });
const step = await scenarioService.createStep(sc.id, {
execCode: "return 1;",
});
const run = await runToCompletion(sc.id);
await scenarioService.removeStep(sc.id, step.id);
const after = await scenarioService.findRun(sc.id, run.id);
expect(after.stepRuns).toHaveLength(1);
expect(after.stepRuns[0].scenarioStepId).toBeNull();
expect(after.stepRuns[0].execCode).toBe("return 1;");
});
});
// ── snippet versions ──────────────────────────────────────────────────────
describe("snippet versions", () => {
it("bumps on change only and keeps every version", async () => {
const alias = `snip-${uniq()}`;
const sn = await snippetService.create({
alias,
title: "t",
code: "return 1;",
});
expect(sn.version).toBe(1);
await snippetService.update(sn.id, { code: "return 2;" });
await snippetService.update(sn.id, { code: "return 2;" }); // no-op
expect((await snippetService.findOne(sn.id)).version).toBe(2);
const versions = await snippetService.listVersions(sn.id);
expect(versions.map((v) => [v.version, v.code])).toEqual([
[2, "return 2;"],
[1, "return 1;"],
]);
});
it("runSnippet uses the latest by default and alias@N pins a version", async () => {
const alias = `pin-${uniq()}`;
const sn = await snippetService.create({
alias,
title: "t",
code: "return 'one';",
});
await snippetService.update(sn.id, { code: "return 'two';" });
const sc = await scenarioService.create({ name: `pinned-${uniq()}` });
await scenarioService.createStep(sc.id, {
execCode: `return [await context.runSnippet("${alias}"), await context.runSnippet("${alias}@1")];`,
});
const run = await runToCompletion(sc.id);
expect(run.status).toBe("pass");
expect(outputs(run)).toEqual([["two", "one"]]);
const used = await scenarioService.getUsedSnippets(sc.id);
expect(used.map((u) => [u.alias, u.pinnedVersion])).toEqual([
[alias, null],
[alias, 1],
]);
});
it("fails the step for a snippet version that does not exist", async () => {
const alias = `missing-${uniq()}`;
await snippetService.create({ alias, title: "t", code: "return 1;" });
const sc = await scenarioService.create({ name: `miss-${uniq()}` });
await scenarioService.createStep(sc.id, {
execCode: `return context.runSnippet("${alias}@5");`,
});
const run = await runToCompletion(sc.id);
expect(run.status).toBe("fail");
});
});
// ── export / import ───────────────────────────────────────────────────────
describe("scenario export/import", () => {
it("exports the latest version, or the one asked for", async () => {
const sc = await scenarioService.create({ name: `exp-${uniq()}` });
await scenarioService.createStep(sc.id, { execCode: "return 1;" });
const latest = await scenarioService.exportScenario(sc.id, {
includeEnvironment: false,
credentialIds: [],
});
expect(latest[latest.length - 1]).toMatchObject({
version: 2,
steps: [{ execCode: "return 1;" }],
});
const first = await scenarioService.exportScenario(sc.id, {
includeEnvironment: false,
credentialIds: [],
version: 1,
});
expect(first[first.length - 1]).toMatchObject({ version: 1, steps: [] });
});
it("restores exact versions by id + version", async () => {
const id = crypto.randomUUID();
const base = { kind: "scenario" as const, id, name: "imported" };
// New scenario: stored under the given version
let sc = await scenarioService.importScenario({
...base,
version: 3,
steps: [{ title: null, execCode: "return 3;" }],
});
expect(sc.version).toBe(3);
expect(await versionNumbers(id)).toEqual([3]);
// Same version again: left untouched
sc = await scenarioService.importScenario({
...base,
name: "changed",
version: 3,
steps: [],
});
expect(sc.name).toBe("imported");
expect(sc.steps).toHaveLength(1);
// Lower, missing version: stored, latest unchanged
sc = await scenarioService.importScenario({
...base,
version: 1,
steps: [{ title: null, execCode: "return 1;" }],
});
expect(sc.version).toBe(3);
expect(await versionNumbers(id)).toEqual([3, 1]);
expect((await scenarioService.findVersion(id, 1)).steps[0].execCode).toBe(
"return 1;",
);
// Higher version: becomes the latest
sc = await scenarioService.importScenario({
...base,
name: "v5",
version: 5,
steps: [{ title: null, execCode: "return 5;" }],
});
expect(sc.version).toBe(5);
expect(sc.name).toBe("v5");
expect(sc.steps.map((s) => s.execCode)).toEqual(["return 5;"]);
expect(await versionNumbers(id)).toEqual([5, 3, 1]);
});
});
describe("snippet export/import", () => {
it("export includes the version and import requires it", async () => {
const alias = `sexp-${uniq()}`;
const sn = await snippetService.create({
alias,
title: "t",
code: "return 1;",
});
await snippetService.update(sn.id, { code: "return 2;" });
const latest = await request(app.getHttpServer())
.get(`/snippets/${sn.id}/export`)
.expect(200);
expect(latest.body).toMatchObject({ version: 2, code: "return 2;" });
const first = await request(app.getHttpServer())
.get(`/snippets/${sn.id}/export?version=1`)
.expect(200);
expect(first.body).toMatchObject({ version: 1, code: "return 1;" });
await request(app.getHttpServer())
.post("/snippets/import")
.send({ alias: `noversion-${uniq()}`, code: "return 0;" })
.expect(400);
});
it("restores exact versions by id + version", async () => {
const id = crypto.randomUUID();
const alias = `simp-${uniq()}`;
const base = { kind: "snippet" as const, id, alias, title: "t" };
let sn = await snippetService.importSnippet({
...base,
version: 2,
code: "return 2;",
});
expect(sn.version).toBe(2);
sn = await snippetService.importSnippet({
...base,
version: 2,
code: "changed",
});
expect(sn.code).toBe("return 2;");
sn = await snippetService.importSnippet({
...base,
version: 1,
code: "return 1;",
});
expect(sn.version).toBe(2);
sn = await snippetService.importSnippet({
...base,
version: 4,
code: "return 4;",
});
expect(sn.version).toBe(4);
expect(sn.code).toBe("return 4;");
expect(
(await snippetService.listVersions(id)).map((v) => v.version),
).toEqual([4, 2, 1]);
});
});
});