Compare commits

...
23 Commits
Author SHA1 Message Date
ars9 6e9b99e415 fix(ui): meet wcag aa text contrast in both themes
- add --color-primary-text for lime used as text or icon color
- stop dimming the idle refresh label with opacity
- darken the light danger button and lighten the dark danger hover
- define syntax highlighting colors per theme as --hl-* tokens
- darken light muted text slightly
- add a storybook contrast page, fail storybook tests on a11y violations
  and show live token values in the colors story
- bump version to 1.20.2
2026-09-30 22:27:06 +03:00
ars9 8aadbd25fb chore(deps): update dependencies and bring storybook up to date
- update client and server dependencies within their ranges, keeping
  all playwright packages aligned at 1.63
- fix code flagged by react-hooks 7.1: refs written during render and
  synchronous setState in effects
- add stories for the ui components that had none, with interaction
  tests, and cover all DescriptionList layouts
- drop the unused mdx stories pattern
- run the storybook tests as part of the pre-commit checks
- bump version to 1.20.1
2026-09-30 22:05:21 +03:00
ars9 9f341719ef feat(database): manage the schema with typeorm migrations
- replace synchronize with migrations, starting with a generated initial
  migration that adopts databases created by synchronize
- apply pending migrations in the docker container before the server,
  and refuse to start the server while migrations are pending
- share one entity list and data source between app, migration runner,
  typeorm cli and tests; tests run the real migrations
- add migration:run, migration:show and migration:generate scripts and
  a test that fails when entities and migrations differ
- exit with code 1 when startup fails
- bump version to 1.20.0
2026-09-30 21:45:42 +03:00
ars9 5de9272281 feat(server): choose cors origins by NODE_ENV and run docker in production
- allow the vite dev server origins in development and the compose port
  origins in production; CORS_ORIGINS overrides both
- set NODE_ENV=production in the runtime docker image
- document both settings in .env.example and the development docs
- bump version to 1.19.3
2026-09-30 21:31:48 +03:00
ars9 a6d468bd2c chore: make .env optional and comment out all example settings
- load .env in compose only when it exists; every setting has a default
- comment out every setting in .env.example, with ## for descriptions
  and # VAR=default for settings to uncomment
- update README, development docs and CLAUDE.md accordingly
2026-09-30 21:23:18 +03:00
ars9 b0f2c72b5f docs: bring docs up to date and drop unused keys setting
- rewrite architecture, development, mcp and scenario docs to match the
  current code: single container, all mcp tools, runs, versioning,
  timeouts, settings, client routing and dev setup
- rewrite README as a general overview without liquio-specific wording
- remove the unused KEYS_DIR setting and ./keys compose mount
- remove the unused result parameter from step code execution
- complete .env.example with the file storage settings and fix the
  DB_PATH default
2026-09-30 21:18:41 +03:00
ars9 e586ab9bd9 fix(scenarios): preselect default environment when opening run modal
- choose the scenario's default environment when the run modal opens on
  the scenario detail page, instead of in a load effect that raced with
  loading the scenario and could fall back to the first environment
- set the run to a final status directly in the status filter test so
  the scheduler can't change it mid-test
- bump version to 1.19.2
2026-09-30 21:07:15 +03:00
ars9 68415f805b chore: upgrade to node 24 and read app version from workspace root
- base the docker image on node:24-slim and require node >=24 via
  engines in the root package.json
- read app name and version from the workspace root package.json for
  config, swagger, startup log and mcp server info; drop the server
  workspace version
- bind test http servers to 127.0.0.1 so tests pass in containers
  without ipv6 loopback
- bump version to 1.19.1
2026-09-30 20:57:33 +03:00
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
ars9 1eea4eb8d8 feat(scenario): export scenarios as standalone playwright e2e test projects
- Added E2eExportService to package scenarios as plug-and-play zip archives
  containing package.json, playwright.config.ts, test.spec.ts, .env, credentials.json,
  snippets, and referenced scenario files
- Only bundles credentials, snippets, and files actually referenced (directly or
  transitively) by the scenario's steps, reducing archive size
- Exported test runs entirely offline using a context shim that mirrors liqa's API
  (page, getCredential, runSnippet, getScenarioFiles, downloadFile, assert, etc.)
- Added GET /scenarios/:id/export-e2e HTTP endpoint and export_e2e_test MCP tool
- Added getUsedSnippets() endpoint to list snippets referenced by a scenario
- Added "Used Snippets" section on scenario detail page
- Added archiver@^7.0.1 dependency for zip archive creation
- Bumped version to 1.11.0
2026-09-16 12:33:16 +03:00
ars9 385f51f3ba fix(scenario-logs): fix out-of-order run log persistence
- Add seq column assigned in call order to fix logs racing each other
  as fire-and-forget writes (e.g. a snippet's "started" entry could be
  saved after its "finished" entry)
- Log snippet start/finish/failure via context.runSnippet, matching
  existing step start/pass/fail logging
- Rework Markdown log export to per-entry sections instead of a table
- Version bump: 1.10.0 -> 1.10.1
2026-09-15 13:17:29 +03:00
ars9 540a7ec96b feat(logs): add scenario run logs export
- Backend: logs export endpoint for CSV and Markdown formats
- Frontend: ExportLogsModal with download button on run detail page
- Updated i18n and CHANGELOG
- Version bump: 1.9.1 -> 1.10.0
2026-09-15 13:04:59 +03:00
115 changed files with 11735 additions and 4175 deletions

No files matched your search

+1
View File
@@ -7,3 +7,4 @@ node_modules
**/*.log
data
keys
**/*.tsbuildinfo
+36 -12
View File
@@ -1,17 +1,41 @@
NODE_ENV=development
PORT=3000
## Optional overrides: copy to .env and uncomment the settings you need.
## Every setting has a default (shown below), so liqa runs without a .env file.
##
## Lines starting with "##" are descriptions.
## Lines starting with "# " are settings: remove the "# " to enable one.
# Directory (relative to cwd) containing *.json key descriptors
KEYS_DIR=keys
## production in the Docker image; also selects the default CORS origins
# NODE_ENV=development
# PORT=3000
# SQLite database path
DB_PATH=data/sessions.db
## Origins allowed to call the API cross-origin (comma separated). Default: the
## Vite dev server (http://localhost:5173) in development, the compose port
## (http://localhost:13000) in production
# CORS_ORIGINS=http://localhost:5173,http://127.0.0.1:5173
# Playwright executable (set in Docker; leave empty to use system default)
## SQLite database path
# DB_PATH=data/liqa.db
## Stored scenario files and run artifacts (downloads, failure dumps, traces)
# FILES_DIR=data/files
## Days to keep run artifacts before they are deleted
# FILE_RUN_EXPIRATION_DAYS=7
## When expired files are deleted (cron expression)
# FILE_CLEANUP_CRON=0 3 * * *
## 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
## 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
## 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
## Directory with the built client to serve at / (the Docker image sets it to
## "client"; leave unset to not serve the UI, e.g. when using the Vite dev server)
# CLIENT_DIR=client
+1
View File
@@ -1,5 +1,6 @@
# runtime
/data
server/data/
.env
*.log
+225
View File
@@ -2,6 +2,231 @@
All notable changes to this project will be documented in this file.
## 1.20.2 - 2026-09-30
Changes since 1.20.1:
### Fixed
- Text contrast now meets WCAG AA (4.5:1) in both themes:
- the "Live" refresh label and the table sort icon use a new `--color-primary-text` token (dark lime in the light theme) instead of the light lime brand color;
- the idle refresh label is no longer dimmed with opacity;
- the light theme's danger button is a little darker, and the dark theme's danger hover is lighter so its dark text stays readable;
- syntax highlighting colors are defined per theme (`--hl-*` tokens), darker in the light theme;
- muted text is slightly darker in the light theme.
### Added
- A Storybook "Accessibility / Contrast" page that measures the contrast of the most contrast-prone components in both themes.
- Storybook accessibility checks now fail the tests on violations (`a11y.test: 'error'`). The Colors story shows each token's current value per theme and includes the syntax colors.
## 1.20.1 - 2026-09-30
Changes since 1.20.0:
### Added
- Storybook stories for AutoRefreshIndicator, CodeBlock, CodeEditor, MarkdownContent, Modal, Notification, Search, Textarea, Toast and UuidBadge, with interaction tests where the component has behavior. The DescriptionList story covers all four layouts.
### Changed
- Updated all client and server dependencies to the latest versions within their ranges, including React 19.3, Storybook 10.6, NestJS 11.2, TypeORM 0.3.31 and Playwright 1.63 (the Docker image now ships HeadlessChrome 153).
- The Storybook tests are part of the pre-commit checks.
### Fixed
- Code flagged by the stricter React hooks lint rules: the runs list no longer writes refs during render, and the scenario list, scenario files and sessions list no longer set state synchronously inside effects.
## 1.20.0 - 2026-09-30
Changes since 1.19.3:
### Added
- Database migrations: the SQLite schema is now managed by TypeORM migrations (`server/src/database/migrations/`) instead of `synchronize`. The Docker container applies pending migrations before starting the server, and the server refuses to start while migrations are pending.
- `npm -w server run migration:run`, `migration:show` and `migration:generate` scripts; the workflow for schema changes is described in `docs/development.md`.
- A test that fails when an entity changes without a matching migration.
### Changed
- Databases created before migrations (by `synchronize`) are adopted automatically: the initial migration is recorded as applied without changing them.
- Tests now build their database from the migrations instead of `synchronize`.
### Fixed
- Startup failures were logged but the process exited with code 0; they now exit with code 1.
## 1.19.3 - 2026-09-30
Changes since 1.19.2:
### Changed
- The Docker image runs with `NODE_ENV=production`.
- The origins allowed to call the API cross-origin depend on `NODE_ENV`: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) in development, the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) in production. The new `CORS_ORIGINS` setting (comma separated) overrides both. A `NODE_ENV=development` in `.env` overrides the image's production mode.
- `.env` is optional: compose loads it only when it exists, and every setting in `.env.example` is commented out and shows its default.
### Removed
- The unused `KEYS_DIR` setting and the `./keys` mount in `compose.yaml`.
## 1.19.2 - 2026-09-30
Changes since 1.19.1:
### Fixed
- Running a scenario from its detail page could preselect the first environment in the list instead of the scenario's default environment, depending on whether the environment list or the scenario loaded first. The default environment is now chosen when the run modal opens.
- Made the run status filter test deterministic; it could fail when the scheduler picked up the test's pending run mid-test.
## 1.19.1 - 2026-09-30
Changes since 1.19.0:
### Changed
- Upgraded to Node.js 24: the Docker image is based on `node:24-slim`, and the root `package.json` declares `"engines": { "node": ">=24" }`.
- The server reads the app name and version (startup log, Swagger, MCP server info, `APP_NAME`/`APP_VERSION`) from the workspace root `package.json`. Previously local runs reported the server workspace's own `liqa-server` / `1.0.0`; the server workspace no longer has a version.
### Fixed
- Tests using a local HTTP server bind to `127.0.0.1` instead of `localhost`, which could resolve to an unreachable `::1` inside Docker containers.
## 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:
### Added
- Added export of a scenario as a standalone, plug-and-play Playwright e2e test project (zip archive with `package.json`, `playwright.config.ts`, `test.spec.ts`, `README.md`, `.env`, `credentials.json`, `snippets/*.js`, and any attached scenario files) that runs entirely independently of the liqa backend. Available as a "Export e2e test" button on the scenario detail page, `GET /scenarios/:id/export-e2e`, and the `export_e2e_test` MCP tool.
- The exported test only bundles the credentials, snippets, and attached files actually referenced (directly or transitively) by the scenario's steps.
- Added a "Used Snippets" section on the scenario detail page listing the snippets referenced by the scenario's steps, linking to each snippet's detail page. Backed by `GET /scenarios/:id/used-snippets`.
## 1.10.1 - 2026-09-15
Changes since 1.10.0:
### Added
- `context.runSnippet()` now logs "Snippet started"/"finished"/"failed" entries, matching the automatic step start/pass/fail logging.
### Changed
- Reworked the Markdown log export format: each log entry is now its own `## [level] timestamp` section with the message as its body, instead of a single large table.
### Fixed
- Run logs could persist out of call order (e.g. a snippet's "started" log appearing after its "finished" log) because writes were fire-and-forget and raced against each other. Log entries now carry a per-run sequence number assigned in call order, and queries/exports sort by it.
## 1.10.0 - 2026-09-15
Changes since 1.9.1:
### Added
- Added a "Download logs" button on the scenario run detail page: opens a modal to choose Markdown (default) or CSV, then downloads the run's logs in that format.
- Added `GET /scenarios/:id/run/:runId/logs/export?format=csv|md` backend endpoint to export a run's logs.
## 1.9.1 - 2026-09-15
Changes since 1.9.0:
+56
View File
@@ -0,0 +1,56 @@
# liqa
liqa is a browser automation and QA service for web applications. 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; the schema is managed by migrations in `server/src/database/migrations/` (see "Database migrations" in `docs/development.md`). Any entity change needs a generated migration - `test/migrations.spec.ts` fails otherwise. 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` if present (optional - every setting has a default) and mounts `./data` (SQLite DB `data/liqa.db` + stored files).
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`. The container applies pending migrations before starting the server, so back it up before deploying a release with new migrations.
## 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 && npm run test:storybook
```
- Lint must stay clean in both packages (no errors, no warnings).
- `test:storybook` runs every story in `client/.storybook/stories/` in headless Chromium (needs `npx playwright install --only-shell chromium` once, and again after Playwright updates). Add or update a story when adding or changing a `client/src/ui/` component.
- 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.
+92
View File
@@ -0,0 +1,92 @@
# 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:24-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:24-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:24-slim AS runtime
# Runtime only: in the build stages it would make npm ci skip devDependencies
ENV NODE_ENV=production
# 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 directory (SQLite DB and stored files, mounted as a volume)
RUN mkdir -p data
EXPOSE 3000
# Apply pending database migrations, then start the server (which refuses to
# start while migrations are pending)
CMD ["sh", "-c", "node dist/database/migrate && exec node dist/main"]
+28 -34
View File
@@ -1,46 +1,40 @@
# liquio-qa-bot
# liqa
A headless QA automation service for Liquio environments. Exposes an **MCP (Model Context Protocol)** server that lets AI agents define, run, and monitor multi-step browser test scenarios using [Playwright](https://playwright.dev/).
A browser automation and QA service for web applications. It stores multi-step browser scenarios, runs them with [Playwright](https://playwright.dev/) in headless Chromium, and keeps their results, logs and artifacts. Scenarios are managed from a web UI, a REST API, or an **MCP (Model Context Protocol)** server that lets AI agents define, run and debug them.
---
## Quick start
```bash
docker compose up -d --build
```
No configuration is needed: every setting has a default. To override one, copy `.env.example` to `.env` and uncomment it.
Everything is served from one container at **http://localhost:13000**: the UI at `/`, the REST API at `/api/v1`, MCP at `/mcp`, Swagger at `/docs`. Data is kept in `./data/`.
---
## Overview
The bot manages four core concepts:
| Concept | Description |
|---|---|
| **Environment** | A named set of URLs for a deployment target (e.g. `liquio-diia-stg`) |
| **Key** | A JSON descriptor in `keys/` that points to an EDS key file + password |
| **Session** | Saved browser state (cookies + localStorage) produced by a login |
| **Scenario** | An ordered list of steps that run in sequence against a shared browser instance |
| **Scenario** | An ordered list of steps (async Playwright JS) that run in sequence in one browser. Versioned: every content change creates a new version |
| **Run** | One execution of a scenario version in an environment, with per-step status and output, logs, and run files (downloads, failure screenshots, Playwright traces) |
| **Snippet** | Reusable, versioned code called from steps with `context.runSnippet("alias")` (or `"alias@N"` for a specific version) |
| **Environment** | A named set of key/value settings for a deployment target (URLs etc.), read with `context.getEnv(key)` |
| **Credential** | Secret data (e.g. a key file and password), assigned to scenarios under an alias and read with `context.getCredential(alias)` |
| **Session** | Saved browser state (cookies + localStorage), e.g. kept from a run with "save session" |
Scenarios, snippets, environments and credentials can be exported as YAML and imported back (one file may hold several of them).
---
## Architecture
## Documentation
See [docs/architecture.md](docs/architecture.md).
---
## Key descriptors
See [docs/key-descriptors.md](docs/key-descriptors.md).
---
## MCP tools
See [docs/mcp.md](docs/mcp.md).
---
## Scenario execution
See [docs/scenario.md](docs/scenario.md).
---
## Development
See [docs/development.md](docs/development.md) for setup, running, testing, linting, and Docker instructions.
- [docs/architecture.md](docs/architecture.md) - components, data model, client
- [docs/scenario.md](docs/scenario.md) - runs, the script `context` API, versioning, import/export, Playwright traces
- [docs/mcp.md](docs/mcp.md) - MCP tool reference
- [docs/development.md](docs/development.md) - setup, configuration, running, testing, linting, Docker
- [CHANGELOG.md](CHANGELOG.md) - release notes
+1 -1
View File
@@ -12,7 +12,7 @@ function getAbsolutePath(value: string) {
return dirname(fileURLToPath(import.meta.resolve(`${value}/package.json`)));
}
const config: StorybookConfig = {
stories: ['./stories/**/*.mdx', './stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
stories: ['./stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
getAbsolutePath('@storybook/addon-vitest'),
getAbsolutePath('@storybook/addon-a11y'),
+2 -1
View File
@@ -37,7 +37,8 @@ const preview: Preview = {
},
},
a11y: {
test: 'todo',
// Accessibility violations (e.g. color contrast) fail the story's test
test: 'error',
},
},
};
@@ -0,0 +1,25 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, fn, userEvent, within } from 'storybook/test';
import { AutoRefreshIndicator } from '../../src/ui/AutoRefreshIndicator/AutoRefreshIndicator';
const meta: Meta<typeof AutoRefreshIndicator> = {
title: 'UI/AutoRefreshIndicator',
component: AutoRefreshIndicator,
parameters: { layout: 'centered' },
tags: ['autodocs'],
args: { onClick: fn() },
};
export default meta;
type Story = StoryObj<typeof AutoRefreshIndicator>;
export const Active: Story = { args: { active: true, pulseKey: 1 } };
export const Idle: Story = { args: { active: false } };
export const Errored: Story = { args: { active: false, error: true } };
export const ClickToRefresh: Story = {
args: { active: false },
play: async ({ args, canvasElement }) => {
await userEvent.click(within(canvasElement).getByRole('button'));
await expect(args.onClick).toHaveBeenCalled();
},
};
@@ -0,0 +1,33 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect } from 'storybook/test';
import { CodeBlock } from '../../src/ui/CodeBlock/CodeBlock';
const meta: Meta<typeof CodeBlock> = {
title: 'UI/CodeBlock',
component: CodeBlock,
parameters: { layout: 'padded' },
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof CodeBlock>;
export const JavaScript: Story = {
args: {
language: 'javascript',
code: "const url = context.getEnv('BASE_URL');\nawait context.page.goto(url);\nreturn await context.page.title();",
},
play: async ({ canvasElement }) => {
// Highlighting splits the code into many spans: check the whole text and
// that highlighting actually happened
const code = canvasElement.querySelector('code');
await expect(code).toHaveTextContent('await context.page.goto(url);');
await expect(code?.querySelector('.hljs-keyword')).not.toBeNull();
},
};
export const Json: Story = {
args: {
language: 'json',
code: JSON.stringify({ status: 'pass', output: { title: 'Dashboard' } }, null, 2),
},
};
@@ -0,0 +1,35 @@
import React, { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { CodeEditor, type CodeEditorProps } from '../../src/ui/CodeEditor/CodeEditor';
// Monaco is loaded from a CDN at runtime, so these stories only check that the
// editor mounts; they have no interaction tests.
function ControlledEditor(
props: Omit<CodeEditorProps, 'value' | 'onChange'> & { initial: string },
) {
const { initial, ...rest } = props;
const [value, setValue] = useState(initial);
return <CodeEditor {...rest} value={value} onChange={setValue} />;
}
const meta: Meta<typeof ControlledEditor> = {
title: 'UI/CodeEditor',
component: ControlledEditor,
parameters: { layout: 'padded' },
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof ControlledEditor>;
export const JavaScript: Story = {
args: {
language: 'javascript',
rows: 8,
initial:
"await context.page.goto(context.getEnv('BASE_URL'));\nreturn await context.page.title();",
},
};
export const WithError: Story = {
args: { language: 'javascript', rows: 4, error: true, initial: 'await context.page.goto(' },
};
+45 -4
View File
@@ -1,10 +1,16 @@
import React from 'react';
import React, { useLayoutEffect, useRef, useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
const palette = [
{ name: 'Atlantis', token: '--color-primary', hex: '#9DD341', role: 'Primary / Action' },
{
name: 'Atlantis text',
token: '--color-primary-text',
hex: '#496904',
role: 'Primary as text / icon',
},
{ name: 'Outer Space', token: '--color-text', hex: '#2D3339', role: 'Text / Surface' },
{ name: 'Kimberly', token: '--color-text-muted', hex: '#6650C0', role: 'Muted text / Info' },
{ name: 'Kimberly', token: '--color-text-muted', hex: '#634DBD', role: 'Muted text / Info' },
{ name: 'Powder Ash', token: '--color-border', hex: '#AFBFB9', role: 'Border / Neutral' },
];
@@ -23,12 +29,23 @@ const semanticColors = [
{ name: 'Running fg', token: '--color-running-fg', hex: '#6B4A00' },
];
const syntaxColors = [
{ name: 'Comment', token: '--hl-comment', hex: '#4F6557' },
{ name: 'Keyword', token: '--hl-keyword', hex: '#764E99' },
{ name: 'String / title', token: '--hl-string', hex: '#4F6821' },
{ name: 'Number', token: '--hl-number', hex: '#895403' },
{ name: 'Function', token: '--hl-function', hex: '#056980' },
{ name: 'Variable / params', token: '--hl-variable', hex: '#815354' },
{ name: 'Property', token: '--hl-property', hex: '#1D6971' },
{ name: 'Meta', token: '--hl-meta', hex: '#615B7E' },
];
const surfaceColors = [
{ name: 'Background', token: '--color-bg', hex: '#E6EDEA' },
{ name: 'Background subtle', token: '--color-bg-subtle', hex: '#D8E2DE' },
{ name: 'Secondary', token: '--color-secondary', hex: '#EDF1EF' },
{ name: 'Secondary hover', token: '--color-secondary-hover', hex: '#DFE6E3' },
{ name: 'Danger', token: '--color-danger', hex: '#D94F4F' },
{ name: 'Danger', token: '--color-danger', hex: '#CE4546' },
{ name: 'Primary fg', token: '--color-primary-fg', hex: '#2D3339' },
];
@@ -43,8 +60,27 @@ function Swatch({
hex: string;
role?: string;
}) {
// Show the token's current value (per theme) rather than a hard-coded copy
const ref = useRef<HTMLDivElement>(null);
const [value, setValue] = useState(hex);
useLayoutEffect(() => {
const read = () => {
if (!ref.current) return;
const resolved = getComputedStyle(ref.current).getPropertyValue(token).trim();
if (resolved) setValue(resolved.toUpperCase());
};
read();
// Re-read when the theme toolbar switches data-theme on <html>
const observer = new MutationObserver(read);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme'],
});
return () => observer.disconnect();
}, [token]);
return (
<div
ref={ref}
style={{
display: 'flex',
flexDirection: 'column',
@@ -72,7 +108,7 @@ function Swatch({
</div>
)}
<div style={{ fontSize: 11, fontFamily: 'monospace', color: 'var(--color-text-muted)' }}>
{hex}
{value}
</div>
<div style={{ fontSize: 11, fontFamily: 'monospace', color: 'var(--color-text-muted)' }}>
{token}
@@ -128,6 +164,11 @@ function ColorPalette() {
<Swatch key={c.token} {...c} />
))}
</Section>
<Section title="Syntax highlighting">
{syntaxColors.map((c) => (
<Swatch key={c.token} {...c} />
))}
</Section>
</div>
);
}
@@ -13,7 +13,7 @@ export default meta;
type Story = StoryObj<typeof ContextMenu>;
const trigger = (
<Button variant="ghost" size="sm">
<Button variant="ghost" size="sm" aria-label="Actions">
<Settings size={16} />
</Button>
);
@@ -0,0 +1,330 @@
import React, { useLayoutEffect, useRef, useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { AutoRefreshIndicator } from '../../src/ui/AutoRefreshIndicator/AutoRefreshIndicator';
import { Button } from '../../src/ui/Button/Button';
import { CodeBlock } from '../../src/ui/CodeBlock/CodeBlock';
/*
* Text contrast review page (WCAG AA: 4.5:1 for normal text, 3:1 for large
* text) for the components where contrast is easiest to get wrong: light
* brand colors used as text, dimmed labels, colored buttons and syntax
* highlighting. Every sample is the real component, shown in both themes, and
* the contrast of each piece of text is measured live from the rendered
* colors - so this page stays accurate as the design tokens change.
*/
// ── Contrast measurement ─────────────────────────────────────────────────────
type Rgba = [number, number, number, number];
function parseColor(value: string): Rgba {
const parts = value.match(/[\d.]+/g)?.map(Number) ?? [0, 0, 0];
return [parts[0], parts[1], parts[2], parts[3] ?? 1];
}
function luminance([r, g, b]: Rgba): number {
const channel = (c: number) => {
const s = c / 255;
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
};
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrast(a: Rgba, b: Rgba): number {
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
/** Blends `top` over an opaque `bottom`. */
function blend(top: Rgba, bottom: Rgba): Rgba {
const a = top[3];
return [0, 1, 2].map((i) => top[i] * a + bottom[i] * (1 - a)).concat(1) as Rgba;
}
/** Product of the `opacity` of `el` and its ancestors (e.g. a dimmed idle label). */
function opacityChain(el: Element): number {
let opacity = 1;
for (let node: Element | null = el; node; node = node.parentElement) {
opacity *= parseFloat(getComputedStyle(node).opacity);
}
return opacity;
}
/** The opaque color behind `el`, from its own and its ancestors' backgrounds. */
function effectiveBackground(el: Element): Rgba {
const layers: Rgba[] = [];
for (let node: Element | null = el; node; node = node.parentElement) {
const bg = parseColor(getComputedStyle(node).backgroundColor);
bg[3] *= opacityChain(node);
if (bg[3] > 0) layers.push(bg);
if (bg[3] >= 1) break;
}
return layers.reduceRight<Rgba>((under, layer) => blend(layer, under), [255, 255, 255, 1]);
}
const hex = ([r, g, b]: Rgba) =>
'#' + [r, g, b].map((c) => Math.round(c).toString(16).padStart(2, '0')).join('');
interface Measurement {
text: string;
fg: string;
bg: string;
ratio: number;
required: number;
}
/** Measures every element that directly contains visible text. */
function measure(root: HTMLElement): Measurement[] {
const seen = new Set<string>();
const results: Measurement[] = [];
for (const el of Array.from(root.querySelectorAll<HTMLElement>('*'))) {
const text = Array.from(el.childNodes)
.filter((n) => n.nodeType === Node.TEXT_NODE)
.map((n) => n.textContent ?? '')
.join('')
.trim();
if (!text || el.closest('[aria-hidden="true"]')) continue;
const style = getComputedStyle(el);
const fontSize = parseFloat(style.fontSize);
const bold = Number(style.fontWeight) >= 700;
// WCAG "large text": at least 24px, or 18.66px bold
const required = fontSize >= 24 || (bold && fontSize >= 18.66) ? 3 : 4.5;
const bgColor = effectiveBackground(el);
const fg = parseColor(style.color);
fg[3] *= opacityChain(el);
const fgColor = blend(fg, bgColor);
const key = `${hex(fgColor)}|${hex(bgColor)}|${required}`;
if (seen.has(key)) continue;
seen.add(key);
results.push({
text: text.length > 28 ? `${text.slice(0, 28)}…` : text,
fg: hex(fgColor),
bg: hex(bgColor),
ratio: contrast(fgColor, bgColor),
required,
});
}
return results.sort((a, b) => a.ratio / a.required - b.ratio / b.required);
}
function Readout({ items }: { items: Measurement[] }) {
return (
<table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 12, marginTop: 8 }}>
<tbody>
{items.map((m) => {
const pass = m.ratio >= m.required;
return (
<tr
key={`${m.fg}${m.bg}${m.required}`}
style={{ borderTop: '1px solid var(--color-border)' }}
>
<td style={{ padding: '4px 6px' }}>
<span
style={{
display: 'inline-block',
padding: '1px 6px',
borderRadius: 3,
background: m.bg,
color: m.fg,
fontFamily: 'monospace',
}}
>
{m.text}
</span>
</td>
<td
style={{
padding: '4px 6px',
fontFamily: 'monospace',
color: 'var(--color-text-muted)',
}}
>
{m.fg} on {m.bg}
</td>
<td
style={{
padding: '4px 6px',
textAlign: 'right',
fontWeight: 700,
whiteSpace: 'nowrap',
color: pass ? 'var(--color-text)' : 'var(--color-danger)',
}}
>
{m.ratio.toFixed(2)}:1 {pass ? '✓' : `✗ needs ${m.required}:1`}
</td>
</tr>
);
})}
</tbody>
</table>
);
}
/** Renders a sample and measures its text contrast after layout. */
function Sample({ children }: { children: React.ReactNode }) {
const ref = useRef<HTMLDivElement>(null);
const [items, setItems] = useState<Measurement[]>([]);
useLayoutEffect(() => {
const el = ref.current;
if (!el) return;
// Measure after the theme/styles have applied
const frame = requestAnimationFrame(() => setItems(measure(el)));
return () => cancelAnimationFrame(frame);
}, []);
return (
<div>
<div ref={ref}>{children}</div>
<Readout items={items} />
</div>
);
}
// ── Page ─────────────────────────────────────────────────────────────────────
// Covers every syntax color: comment, keyword, string, number, function
// (title), params, property
const JS_CODE = [
'// Open the app and wait for the dashboard',
'async function openDashboard(context, timeoutMs) {',
" const url = context.getEnv('BASE_URL');",
' await context.page.goto(url, { timeout: timeoutMs ?? 30000 });',
' return context.page.title();',
'}',
].join('\n');
const JSON_CODE = JSON.stringify(
{ status: 'pass', attempts: 2, cached: false, output: { title: 'Dashboard' } },
null,
2,
);
interface Issue {
title: string;
note: string;
render: () => React.ReactNode;
}
const ISSUES: Issue[] = [
{
title: 'AutoRefreshIndicator label',
note: 'Active ("Live", --color-primary-text), idle and error labels (runs pages).',
render: () => (
<div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
<AutoRefreshIndicator active pulseKey={1} />
<AutoRefreshIndicator active={false} />
<AutoRefreshIndicator active={false} error />
</div>
),
},
{
title: 'Danger button',
note: 'Danger, primary and secondary buttons (text on colored backgrounds).',
render: () => (
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
<Button variant="danger">Delete</Button>
<Button variant="danger" size="sm">
Delete
</Button>
<Button variant="primary">Run</Button>
<Button variant="secondary">Cancel</Button>
</div>
),
},
{
title: 'Code highlighting - JavaScript',
note: 'All --hl-* syntax colors on the page background (step code, snippets).',
render: () => <CodeBlock language="javascript" code={JS_CODE} />,
},
{
title: 'Code highlighting - JSON',
note: 'Keys, strings, numbers and literals in JSON step outputs.',
render: () => <CodeBlock language="json" code={JSON_CODE} />,
},
{
title: 'Muted text',
note: '--color-text-muted on both background shades (hints, table headers, descriptions).',
render: () => (
<div style={{ display: 'flex', gap: 8 }}>
{['var(--color-bg)', 'var(--color-bg-subtle)'].map((bg) => (
<span
key={bg}
style={{
background: bg,
color: 'var(--color-text-muted)',
padding: '4px 8px',
fontSize: 13,
}}
>
Last run 2 minutes ago
</span>
))}
</div>
),
},
];
function Panel({ theme }: { theme: 'light' | 'dark' }) {
return (
<section
// The dark tokens apply under [data-theme="dark"]; light is the page default
data-theme={theme === 'dark' ? 'dark' : undefined}
style={{
flex: '1 1 420px',
minWidth: 0,
padding: 16,
borderRadius: 8,
background: 'var(--color-bg)',
color: 'var(--color-text)',
border: '1px solid var(--color-border)',
}}
>
<h2 style={{ margin: '0 0 12px', fontSize: 16 }}>
{theme === 'dark' ? 'Dark theme' : 'Light theme'}
</h2>
{ISSUES.map((issue) => (
<div key={issue.title} style={{ marginBottom: 24 }}>
<h3 style={{ margin: '0 0 2px', fontSize: 14 }}>{issue.title}</h3>
<p style={{ margin: '0 0 8px', fontSize: 12, color: 'var(--color-text-muted)' }}>
{issue.note}
</p>
<Sample>{issue.render()}</Sample>
</div>
))}
</section>
);
}
function ContrastPage() {
return (
<div style={{ padding: 16 }}>
<p
style={{
margin: '0 0 16px',
fontSize: 13,
color: 'var(--color-text-muted)',
maxWidth: 820,
}}
>
Text contrast of the components where it's easiest to get wrong (WCAG AA: 4.5:1 for normal
text, 3:1 for large text). Each row is a piece of text in the sample, measured from its
rendered colors, worst first.
</p>
<div style={{ display: 'flex', gap: 16, flexWrap: 'wrap', alignItems: 'flex-start' }}>
<Panel theme="light" />
<Panel theme="dark" />
</div>
</div>
);
}
const meta: Meta<typeof ContrastPage> = {
title: 'Accessibility/Contrast',
component: ContrastPage,
// a11y checks use the project default ('error' in preview.tsx)
parameters: { layout: 'fullscreen' },
// The light panel relies on the page default (light) theme
globals: { theme: 'light' },
};
export default meta;
type Story = StoryObj<typeof ContrastPage>;
export const Overview: Story = {};
@@ -15,7 +15,7 @@ const meta: Meta<typeof DescriptionList> = {
),
],
argTypes: {
layout: { control: 'select', options: ['inline', 'compact'] },
layout: { control: 'select', options: ['inline', 'compact', 'comfortable', 'grid'] },
},
};
export default meta;
@@ -35,6 +35,14 @@ export const Compact: Story = {
args: { items: urlItems, layout: 'compact' },
};
export const Comfortable: Story = {
args: { items: urlItems, layout: 'comfortable' },
};
export const Grid: Story = {
args: { items: urlItems, layout: 'grid' },
};
export const WithLinks: Story = {
args: {
layout: 'inline',
@@ -0,0 +1,35 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, within } from 'storybook/test';
import { MarkdownContent } from '../../src/ui/MarkdownContent/MarkdownContent';
const meta: Meta<typeof MarkdownContent> = {
title: 'UI/MarkdownContent',
component: MarkdownContent,
parameters: { layout: 'padded' },
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof MarkdownContent>;
export const Description: Story = {
args: {
content: [
'## Login flow',
'',
'Logs in with the **test user** and checks the dashboard.',
'',
'- Uses the `BASE_URL` environment value',
'- Needs the `user` credential alias',
'',
'```js',
"await context.page.goto(context.getEnv('BASE_URL'));",
'```',
].join('\n'),
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(canvas.getByRole('heading', { name: 'Login flow' })).toBeInTheDocument();
await expect(canvas.getByText('test user').tagName).toBe('STRONG');
await expect(canvas.getAllByRole('listitem')).toHaveLength(2);
},
};
@@ -0,0 +1,63 @@
import React, { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, userEvent, within } from 'storybook/test';
import { Modal } from '../../src/ui/Modal/Modal';
import { Button } from '../../src/ui/Button/Button';
function ModalDemo({ title, body }: { title: string; body: string }) {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>Open modal</Button>
<Modal
open={open}
title={title}
onClose={() => setOpen(false)}
footer={
<>
<Button variant="secondary" onClick={() => setOpen(false)}>
Cancel
</Button>
<Button onClick={() => setOpen(false)}>Confirm</Button>
</>
}
>
{body}
</Modal>
</>
);
}
const meta: Meta<typeof ModalDemo> = {
title: 'UI/Modal',
component: ModalDemo,
parameters: { layout: 'centered' },
tags: ['autodocs'],
args: { title: 'Delete scenario', body: 'This deletes the scenario, its versions and its runs.' },
};
export default meta;
type Story = StoryObj<typeof ModalDemo>;
export const Default: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.click(canvas.getByRole('button', { name: 'Open modal' }));
const dialog = canvas.getByRole('dialog', { name: 'Delete scenario' });
await expect(dialog).toHaveTextContent('its versions and its runs');
await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }));
await expect(canvas.queryByRole('dialog')).not.toBeInTheDocument();
},
};
export const ClosesOnEscapeAndBackdrop: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.click(canvas.getByRole('button', { name: 'Open modal' }));
await userEvent.keyboard('{Escape}');
await expect(canvas.queryByRole('dialog')).not.toBeInTheDocument();
await userEvent.click(canvas.getByRole('button', { name: 'Open modal' }));
await userEvent.click(canvas.getByRole('presentation'));
await expect(canvas.queryByRole('dialog')).not.toBeInTheDocument();
},
};
@@ -0,0 +1,30 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { Notification } from '../../src/ui/Notification/Notification';
const meta: Meta<typeof Notification> = {
title: 'UI/Notification',
component: Notification,
parameters: { layout: 'padded' },
tags: ['autodocs'],
argTypes: {
variant: { control: 'select', options: ['info', 'success', 'error', 'warning', 'note'] },
},
};
export default meta;
type Story = StoryObj<typeof Notification>;
export const Info: Story = {
args: {
variant: 'info',
title: 'Traces are off',
children: 'Enable them per run in the run modal.',
},
};
export const Success: Story = { args: { variant: 'success', children: 'Scenario imported.' } };
export const Error: Story = {
args: { variant: 'error', title: 'Run failed', children: 'Step 2 timed out after 60s.' },
};
export const Warning: Story = {
args: { variant: 'warning', children: 'The environment of this run no longer exists.' },
};
export const Note: Story = { args: { variant: 'note', children: 'Pinned snippet version: v3.' } };
@@ -0,0 +1,34 @@
import React, { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, userEvent, within } from 'storybook/test';
import { Search } from '../../src/ui/Search/Search';
function ControlledSearch({ placeholder }: { placeholder?: string }) {
const [value, setValue] = useState('');
return (
<div>
<Search value={value} onChange={setValue} placeholder={placeholder} />
<p data-testid="query">{value}</p>
</div>
);
}
const meta: Meta<typeof ControlledSearch> = {
title: 'UI/Search',
component: ControlledSearch,
parameters: { layout: 'centered' },
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof ControlledSearch>;
export const Default: Story = {};
export const Typing: Story = {
args: { placeholder: 'Filter logs…' },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.type(canvas.getByPlaceholderText('Filter logs…'), 'timeout');
await expect(canvas.getByTestId('query')).toHaveTextContent('timeout');
},
};
@@ -0,0 +1,37 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, within } from 'storybook/test';
import { Textarea } from '../../src/ui/Textarea/Textarea';
const meta: Meta<typeof Textarea> = {
title: 'UI/Textarea',
component: Textarea,
parameters: { layout: 'centered' },
tags: ['autodocs'],
decorators: [
(Story) => (
<div style={{ width: 360 }}>
<Story />
</div>
),
],
};
export default meta;
type Story = StoryObj<typeof Textarea>;
export const Default: Story = {
args: { label: 'Description', placeholder: 'Describe this scenario', rows: 4 },
};
export const WithHint: Story = {
args: { label: 'Description', hint: 'Markdown is supported.', rows: 4 },
};
export const WithError: Story = {
args: { label: 'Description', error: 'Description is too long.', rows: 4 },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const textarea = canvas.getByLabelText('Description');
await expect(textarea).toHaveAttribute('aria-invalid', 'true');
await expect(canvas.getByRole('alert')).toHaveTextContent('Description is too long.');
},
};
@@ -0,0 +1,41 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, userEvent, within } from 'storybook/test';
import { ToastProvider } from '../../src/ui/Toast/ToastProvider';
import { useToast } from '../../src/ui/Toast/useToast';
import { Button } from '../../src/ui/Button/Button';
function ToastButtons() {
const toast = useToast();
return (
<div style={{ display: 'flex', gap: 8 }}>
<Button onClick={() => toast.success('Scenario run started')}>Success</Button>
<Button variant="danger" onClick={() => toast.error('Environment not found')}>
Error
</Button>
<Button variant="secondary" onClick={() => toast.info('Traces are off')}>
Info
</Button>
</div>
);
}
const meta: Meta<typeof ToastProvider> = {
title: 'UI/Toast',
component: ToastProvider,
parameters: { layout: 'centered' },
tags: ['autodocs'],
args: { durationMs: 3500, children: <ToastButtons /> },
};
export default meta;
type Story = StoryObj<typeof ToastProvider>;
export const Default: Story = {};
export const ShowsToast: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.click(canvas.getByRole('button', { name: 'Success' }));
await expect(await within(document.body).findByText('Scenario run started')).toBeVisible();
},
};
@@ -0,0 +1,21 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, within } from 'storybook/test';
import { UuidBadge } from '../../src/ui/UuidBadge/UuidBadge';
const meta: Meta<typeof UuidBadge> = {
title: 'UI/UuidBadge',
component: UuidBadge,
parameters: { layout: 'centered' },
tags: ['autodocs'],
};
export default meta;
type Story = StoryObj<typeof UuidBadge>;
export const Default: Story = {
args: { id: '61d2b1c2-7664-4dca-974f-436c2ee90b2e' },
play: async ({ canvasElement }) => {
// Shows a shortened id and keeps the full one in the tooltip
const badge = within(canvasElement).getByTitle('61d2b1c2-7664-4dca-974f-436c2ee90b2e');
await expect(badge).toHaveTextContent('61d2…0b2e');
},
};
-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 />} />
+53 -25
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,11 +259,11 @@ 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<UsedSnippet[]> {
return request(`/scenarios/${id}/used-snippets`);
},
};
@@ -292,6 +296,9 @@ export const runs = {
const qs = q?.trim() ? `?q=${encodeURIComponent(q.trim())}` : '';
return request(`/scenarios/${scenarioId}/run/${runId}${qs}`);
},
exportLogs(scenarioId: string, runId: string, format: 'csv' | 'md'): Promise<string> {
return request(`/scenarios/${scenarioId}/run/${runId}/logs/export?format=${format}`);
},
};
// ── Scenario Credentials ──────────────────────────────────────────────────────
@@ -379,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[];
}
@@ -0,0 +1,27 @@
.section {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.sectionTitle {
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-muted, #888);
margin: 0;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.radioRow {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
cursor: pointer;
}
.radioRow input[type='radio'] {
cursor: pointer;
accent-color: var(--color-primary);
}
@@ -0,0 +1,68 @@
import { useTranslation } from 'react-i18next';
import { X, Download } from 'lucide-react';
import { Modal, Button } from '../../ui';
import styles from './ExportLogsModal.module.css';
export type LogExportFormat = 'md' | 'csv';
export interface ExportLogsModalProps {
open: boolean;
format: LogExportFormat;
onFormatChange: (format: LogExportFormat) => void;
onClose: () => void;
onExport: () => void;
isExporting: boolean;
}
export function ExportLogsModal({
open,
format,
onFormatChange,
onClose,
onExport,
isExporting,
}: ExportLogsModalProps) {
const { t } = useTranslation();
return (
<Modal
open={open}
title={t('runs.export_logs_modal_title')}
onClose={() => !isExporting && onClose()}
footer={
<>
<Button variant="secondary" onClick={onClose} disabled={isExporting}>
<X size={14} />
{t('common.button_cancel')}
</Button>
<Button variant="primary" onClick={onExport} disabled={isExporting}>
<Download size={14} />
{t('runs.action_export')}
</Button>
</>
}
>
<div className={styles.section}>
<h4 className={styles.sectionTitle}>{t('runs.export_logs_format')}</h4>
<label className={styles.radioRow}>
<input
type="radio"
name="log-export-format"
checked={format === 'md'}
onChange={() => onFormatChange('md')}
/>
<span>{t('runs.export_logs_format_md')}</span>
</label>
<label className={styles.radioRow}>
<input
type="radio"
name="log-export-format"
checked={format === 'csv'}
onChange={() => onFormatChange('csv')}
/>
<span>{t('runs.export_logs_format_csv')}</span>
</label>
</div>
</Modal>
);
}
@@ -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>
+2
View File
@@ -4,3 +4,5 @@ export { ExportScenarioModal } from './ExportScenarioModal';
export type { ExportScenarioModalProps } from './ExportScenarioModal';
export { RunScenarioModal } from './RunScenarioModal';
export type { RunScenarioModalProps } from './RunScenarioModal';
export { ExportLogsModal } from './ExportLogsModal';
export type { ExportLogsModalProps, LogExportFormat } from './ExportLogsModal';
+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;
}
+49 -2
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",
@@ -149,6 +168,7 @@
"action_edit": "Edit",
"action_runs": "Runs",
"action_export": "Export",
"action_export_e2e": "Export e2e test",
"action_import": "Import",
"import_error": "Failed to import scenario",
"action_save": "Create",
@@ -165,6 +185,9 @@
"form_name_placeholder": "e.g. Login flow",
"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",
@@ -183,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",
@@ -240,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",
@@ -257,7 +291,15 @@
"step_description": "Result",
"log_level": "Level",
"log_message": "Message",
"log_time": "Time"
"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)",
"export_logs_format_csv": "CSV (.csv)",
"action_export": "Export"
},
"snippets": {
"title": "Snippets",
@@ -287,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",
@@ -308,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'),
+164 -13
View File
@@ -14,6 +14,11 @@ import type {
FileMetadata,
} from '../../api';
import { scenarios } from '../../api';
import {
DeleteConfirmationModal,
ExportLogsModal,
type LogExportFormat,
} from '../../components/modals';
import {
AutoRefreshIndicator,
Badge,
@@ -27,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> = {
@@ -65,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);
@@ -84,6 +105,12 @@ export function RunDetailPage() {
const [artifacts, setArtifacts] = useState<FileMetadata[]>([]);
const [artifactsTotal, setArtifactsTotal] = useState(0);
const [artifactsLoading, setArtifactsLoading] = useState(false);
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);
@@ -106,6 +133,62 @@ export function RunDetailPage() {
const isFileExpired = (file: FileMetadata) =>
file.expiresAt != null && new Date(file.expiresAt).getTime() <= Date.now();
const handleExportLogs = async () => {
if (!id || !runId) return;
setExportingLogs(true);
try {
const content = await runs.exportLogs(id, runId, exportLogsFormat);
const mimeType = exportLogsFormat === 'csv' ? 'text/csv' : 'text/markdown';
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `run-${runId}-logs.${exportLogsFormat}`;
a.click();
URL.revokeObjectURL(url);
setExportLogsOpen(false);
} catch {
// error toast is already emitted by the API client
} finally {
setExportingLogs(false);
}
};
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
@@ -198,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',
@@ -359,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>}
@@ -388,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',
@@ -436,11 +548,17 @@ export function RunDetailPage() {
<div className={styles.stepsSection}>
<div className={styles.sectionHeadingRow}>
<h2 className={styles.sectionHeading}>{t('runs.logs_heading')}</h2>
<Search
value={logSearch}
onChange={setLogSearch}
placeholder={t('runs.logs_search_placeholder')}
/>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<Search
value={logSearch}
onChange={setLogSearch}
placeholder={t('runs.logs_search_placeholder')}
/>
<Button variant="secondary" size="sm" onClick={() => setExportLogsOpen(true)}>
<Download size={14} />
{t('runs.action_download_logs')}
</Button>
</div>
</div>
<Table
columns={logColumns}
@@ -515,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')}
@@ -523,6 +656,24 @@ export function RunDetailPage() {
)}
</>
)}
<ExportLogsModal
open={exportLogsOpen}
format={exportLogsFormat}
onFormatChange={setExportLogsFormat}
onClose={() => setExportLogsOpen(false)}
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>
);
}
+17 -4
View File
@@ -68,10 +68,12 @@ export function RunsPage() {
const sortDirRef = useRef(sortDir);
const pageRef = useRef(page);
const pageSizeRef = useRef(pageSize);
sortByRef.current = sortBy;
sortDirRef.current = sortDir;
pageRef.current = page;
pageSizeRef.current = pageSize;
useEffect(() => {
sortByRef.current = sortBy;
sortDirRef.current = sortDir;
pageRef.current = page;
pageSizeRef.current = pageSize;
}, [sortBy, sortDir, page, pageSize]);
const load = useCallback(
(orderBy: string, orderDir: SortDirection, p: number, limit: number) => {
@@ -161,6 +163,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'),
+123 -55
View File
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState, useCallback, SubmitEvent } from 'react';
import { useEffect, useState, useCallback, SubmitEvent } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { usePageTitle } from '../../hooks/usePageTitle';
@@ -12,6 +12,7 @@ import {
Upload,
GripVertical,
ClipboardList,
FileArchive,
} from 'lucide-react';
import {
environments,
@@ -28,6 +29,9 @@ import type {
Credential,
Environment,
FileMetadata,
ScenarioVersion,
TraceMode,
UsedSnippet,
} from '../../api';
import {
Breadcrumbs,
@@ -44,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() {
@@ -71,9 +76,15 @@ 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);
const [pendingDelete, setPendingDelete] = useState<
{ type: 'scenario' } | { type: 'step'; id: string } | { type: 'credential'; id: string } | null
>(null);
@@ -87,25 +98,31 @@ export function ScenarioDetailPage() {
// Files state
const [files, setFiles] = useState<FileMetadata[]>([]);
const [filesTotal, setFilesTotal] = useState(0);
const [filesLoading, setFilesLoading] = useState(false);
const [filesLoading, setFilesLoading] = useState(true);
const [uploadOpen, setUploadOpen] = useState(false);
const [uploadFile, setUploadFile] = useState<File | null>(null);
const [uploadExpiresAt, setUploadExpiresAt] = useState('');
const [uploading, setUploading] = useState(false);
const loadFiles = useCallback(async () => {
const [usedSnippets, setUsedSnippets] = useState<UsedSnippet[]>([]);
// Fetches the files; `filesLoading` starts as true for the fetch from the
// load effect, and `loadFiles` sets it for reloads (e.g. after an upload)
const fetchFiles = useCallback(async () => {
if (!id) return;
setFilesLoading(true);
try {
const result = await scenarioFiles.list(id);
setFiles(result.items);
setFilesTotal(result.total);
} finally {
setFilesLoading(false);
}
}, [id]);
const loadFiles = async () => {
setFilesLoading(true);
await fetchFiles();
};
useEffect(() => {
if (!id) return;
void scenarios
@@ -115,7 +132,11 @@ export function ScenarioDetailPage() {
setScenarioCreds(s.scenarioCredentials ?? []);
})
.finally(() => setLoading(false));
void loadFiles();
void fetchFiles();
scenarios
.usedSnippets(id)
.then(setUsedSnippets)
.catch(() => {});
credentialsApi
.list(1, 200)
.then((r) => setAllCredentials(r.data))
@@ -126,22 +147,35 @@ export function ScenarioDetailPage() {
setEnvs(r.data);
})
.catch(() => {});
}, [id, loadFiles]);
}, [id, fetchFiles]);
// Default selected env to scenario's linked environment (or first env) once both are loaded
useEffect(() => {
if (envInitRef.current || envs.length === 0) return;
envInitRef.current = true;
const openRunModal = () => {
// Preselect the scenario's default environment (or the first one) when the
// modal opens: by then both are loaded, and it follows default env edits
const preferred = scenario?.environmentId ?? null;
const exists = preferred ? envs.some((e) => e.id === preferred) : false;
setSelectedEnvId(exists ? preferred! : envs[0].id);
}, [scenario, envs]);
setSelectedEnvId(exists ? preferred! : (envs[0]?.id ?? ''));
setTraceModeOverride(null);
setRunVersion(null);
setRunVersions([]);
if (id) {
scenarios
.versions(id)
.then(setRunVersions)
.catch(() => undefined);
}
setRunModalOpen(true);
};
const reloadScenario = async () => {
if (!id) return;
const s = await scenarios.get(id);
setScenario(s);
setScenarioCreds(s.scenarioCredentials ?? []);
scenarios
.usedSnippets(id)
.then(setUsedSnippets)
.catch(() => {});
};
const handleRun = async () => {
@@ -152,7 +186,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);
@@ -444,7 +484,7 @@ export function ScenarioDetailPage() {
/>
{scenario && (
<div className={styles.toolbarActions}>
<Button size="sm" onClick={() => setRunModalOpen(true)}>
<Button size="sm" onClick={openRunModal}>
<Play size={14} />
{t('scenarios.action_run')}
</Button>
@@ -456,6 +496,16 @@ export function ScenarioDetailPage() {
<Upload size={14} />
{t('scenarios.action_export')}
</Button>
<Button
variant="secondary"
size="sm"
onClick={() =>
handleFileDownload(scenarios.exportE2eTestUrl(id!), `${scenario.name}-e2e-test.zip`)
}
>
<FileArchive size={14} />
{t('scenarios.action_export_e2e')}
</Button>
<Button variant="secondary" size="sm" onClick={() => navigate(`/scenarios/${id}/edit`)}>
<Pencil size={14} />
{t('scenarios.action_edit')}
@@ -482,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
? [
{
@@ -651,6 +702,44 @@ export function ScenarioDetailPage() {
)}
</div>
{/* ── Used snippets section ───────────────────────────────────────── */}
{usedSnippets.length > 0 && (
<div className={styles.stepsSection}>
<div className={styles.sectionToolbar}>
<h2 className={styles.sectionHeading}>{t('scenarios.used_snippets_heading')}</h2>
</div>
<Table<UsedSnippet>
data={usedSnippets}
rowKey={(s) => `${s.id}@${s.pinnedVersion ?? 'latest'}`}
loading={false}
emptyMessage=""
onRowClick={(s) => navigate(`/snippets/${s.id}`)}
columns={
[
{
key: 'alias',
header: t('snippets.field_alias'),
render: (s) => s.alias,
},
{
key: 'title',
header: t('snippets.field_title'),
render: (s) => s.title,
},
{
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>
)}
{/* ── Files section ───────────────────────────────────────────────── */}
<div className={styles.stepsSection}>
<div className={styles.sectionToolbar}>
@@ -774,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}
+58 -58
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('');
@@ -60,9 +68,10 @@ export function ScenariosPage() {
const [pageSize, setPageSize] = useState(10);
const [total, setTotal] = useState(0);
const load = useCallback(
// Fetches a page; `loading` starts as true for the initial fetch from the
// effect, and `load` sets it for later ones (no setState inside the effect)
const fetchPage = useCallback(
(orderBy: string, orderDir: SortDirection, p: number, limit: number) => {
setLoading(true);
Promise.all([
scenarios.list(p, limit, orderBy, orderDir === 'asc' ? 'ASC' : 'DESC'),
environments.list(1, 200),
@@ -80,9 +89,14 @@ export function ScenariosPage() {
[toast],
);
const load = (orderBy: string, orderDir: SortDirection, p: number, limit: number) => {
setLoading(true);
fetchPage(orderBy, orderDir, p, limit);
};
useEffect(() => {
load('updatedAt', 'desc', 1, 10);
}, [load]);
fetchPage('updatedAt', 'desc', 1, 10);
}, [fetchPage]);
const handleSort = (key: string, dir: SortDirection) => {
setSortBy(key);
@@ -104,6 +118,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 +135,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 +165,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 +292,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>
);
}
+8 -3
View File
@@ -23,7 +23,9 @@ export function SessionsPage() {
usePageTitle(t('sessions.title'));
const navigate = useNavigate();
const [items, setItems] = useState<Session[]>([]);
const [loading, setLoading] = useState(true);
// Loading until the current query's result has arrived (derived, so the
// loading effect doesn't have to set state synchronously)
const [loadedQuery, setLoadedQuery] = useState<string | null>(null);
const [deleteId, setDeleteId] = useState<string | null>(null);
const [deleting, setDeleting] = useState(false);
const [sortBy, setSortBy] = useState('id');
@@ -32,15 +34,18 @@ export function SessionsPage() {
const [pageSize, setPageSize] = useState(10);
const [total, setTotal] = useState(0);
const query = `${sortBy}|${sortDir}|${page}|${pageSize}`;
const loading = loadedQuery !== query;
const load = useCallback((orderBy: string, orderDir: SortDirection, p: number, limit: number) => {
setLoading(true);
const key = `${orderBy}|${orderDir}|${p}|${limit}`;
sessions
.list(p, limit, orderBy, orderDir === 'asc' ? 'ASC' : 'DESC')
.then((res) => {
setItems(res.data);
setTotal(res.total);
})
.finally(() => setLoading(false));
.finally(() => setLoadedQuery(key));
}, []);
useEffect(() => {
@@ -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>
@@ -71,7 +71,7 @@
}
.active .label {
color: var(--color-primary);
color: var(--color-primary-text);
font-weight: 600;
}
@@ -83,8 +83,8 @@
}
.inactive .label {
/* Not dimmed with opacity: muted text already reads as inactive and keeps 4.5:1 */
color: var(--color-text-muted);
opacity: 0.5;
}
/* ── Error state ──────────────────────────────────────────── */
+8 -8
View File
@@ -18,7 +18,7 @@
.code :global(.hljs-comment),
.code :global(.hljs-quote) {
color: var(--hl-comment, #6A8072);
color: var(--hl-comment);
font-style: italic;
}
@@ -27,7 +27,7 @@
.code :global(.hljs-built_in),
.code :global(.hljs-name),
.code :global(.hljs-tag) {
color: var(--hl-keyword, #A67DCC);
color: var(--hl-keyword);
}
.code :global(.hljs-string),
@@ -39,32 +39,32 @@
.code :global(.hljs-template-variable),
.code :global(.hljs-type),
.code :global(.hljs-addition) {
color: var(--hl-string, #9BB870);
color: var(--hl-string);
}
.code :global(.hljs-number),
.code :global(.hljs-regexp),
.code :global(.hljs-link) {
color: var(--hl-number, #E8A85F);
color: var(--hl-number);
}
.code :global(.hljs-function),
.code :global(.hljs-title.hljs-function_) {
color: var(--hl-function, #5BB8D4);
color: var(--hl-function);
}
.code :global(.hljs-variable),
.code :global(.hljs-params) {
color: var(--hl-variable, #D4A0A0);
color: var(--hl-variable);
}
.code :global(.hljs-property) {
color: var(--hl-property, #79C0C8);
color: var(--hl-property);
}
.code :global(.hljs-deletion),
.code :global(.hljs-meta) {
color: var(--hl-meta, #A9A3C9);
color: var(--hl-meta);
}
.code :global(.hljs-emphasis) {
+1 -1
View File
@@ -49,7 +49,7 @@
.sortIconActive {
opacity: 1;
color: var(--color-primary);
color: var(--color-primary-text);
}
[data-theme="dark"] .th {
+27 -4
View File
@@ -11,6 +11,8 @@
--color-primary-hover: #8ABD2E;
--color-primary-active: #77A320;
--color-primary-fg: #2D3339; /* Outer Space on bright green */
/* Primary as text/icon color on light surfaces (Atlantis is too light for text) */
--color-primary-text: #496904;
/* Secondary — Kimberly tint */
--color-secondary: #EDEAF8;
@@ -19,8 +21,8 @@
--color-secondary-fg: #6650C0; /* Kimberly */
--color-secondary-border: #B8B0E0;
/* Danger */
--color-danger: #D94F4F;
/* Danger (white text needs >= 4.5:1) */
--color-danger: #CE4546;
--color-danger-hover: #C03A3A;
--color-danger-fg: #ffffff;
@@ -43,11 +45,21 @@
--color-link-hover: #3D2F80;
--color-text: #2D3339;
--color-text-muted: #6650C0; /* Kimberly */
--color-text-muted: #634DBD; /* Kimberly, a touch darker for 4.5:1 on bg-subtle */
--color-border: #AFBFB9; /* Powder Ash */
--color-bg: #E6EDEA; /* Powder Ash tint */
--color-bg-subtle: #D8E2DE; /* Powder Ash tint, 1 step darker */
/* Syntax highlighting (CodeBlock); each >= 4.5:1 on bg and bg-subtle */
--hl-comment: #4F6557;
--hl-keyword: #764E99;
--hl-string: #4F6821;
--hl-number: #895403;
--hl-function: #056980;
--hl-variable: #815354;
--hl-property: #1D6971;
--hl-meta: #615B7E;
/* Spacing */
--space-1: 4px;
--space-2: 8px;
@@ -112,6 +124,7 @@ a:hover {
--color-primary-hover: #AEDE55;
--color-primary-active: #8ABD2E;
--color-primary-fg: #1A1F24;
--color-primary-text: #9DD341;
/* Secondary — dark Kimberly tint */
--color-secondary: #2A2548;
@@ -122,7 +135,7 @@ a:hover {
/* Danger */
--color-danger: #E06060;
--color-danger-hover: #CC4A4A;
--color-danger-hover: #F06E6D; /* lighter, so the dark text keeps >= 4.5:1 */
--color-danger-fg: #1A1F24;
/* Semantic */
@@ -148,4 +161,14 @@ a:hover {
--color-border: #3D4850;
--color-bg: #1A1F24; /* deeper than Outer Space */
--color-bg-subtle: #2D3339; /* Outer Space */
/* Syntax highlighting (CodeBlock); each >= 4.5:1 on bg and bg-subtle */
--hl-comment: #8AA192;
--hl-keyword: #B38ADA;
--hl-string: #9BB870;
--hl-number: #E8A85F;
--hl-function: #5BB8D4;
--hl-variable: #D4A0A0;
--hl-property: #79C0C8;
--hl-meta: #A9A3C9;
}
+17
View File
@@ -0,0 +1,17 @@
services:
liqa:
build:
context: .
ports:
# UI, REST API (/api/v1), MCP (/mcp) and Swagger (/docs).
# The production CORS origins (server/src/common/cors.ts) use this port;
# set CORS_ORIGINS in .env when publishing it elsewhere.
- "13000:3000"
# Optional overrides (see .env.example); every setting has a default
env_file:
- path: .env
required: false
volumes:
# persist SQLite database across restarts
- ./data:/app/data
restart: unless-stopped
-23
View File
@@ -1,23 +0,0 @@
services:
server:
build:
context: .
dockerfile: server/Dockerfile
ports:
- "13000:3000"
env_file:
- .env
volumes:
# persist SQLite database across restarts
- ./data:/app/data
# mount key files into the container
- ./keys:/app/keys
restart: unless-stopped
client:
build:
context: .
dockerfile: client/Dockerfile
ports:
- "13001:80"
restart: unless-stopped
+67 -38
View File
@@ -1,50 +1,79 @@
# Architecture
```
┌──────────────────────────────────────────────────────┐
│ Browser (React SPA - client/) │
│ │
│ Environments Credentials Snippets │
│ Sessions Scenarios Runs │
└─────────────────────────────┬────────────────────────┘
│ HTTP REST (:13000)
│
MCP client (AI agent / VS Code Copilot)
│ HTTP /mcp (Streamable MCP)
▼
┌──────────────────────────────────────────────────────┐
│ NestJS app (server/src/) │
│ │
│ McpService all MCP tools │
│ SessionService session persistence │
│ SessionContextService live browser contexts │
│ EnvironmentService environment CRUD + import │
│ CredentialService credential CRUD + import │
│ SnippetService snippet CRUD + import │
│ BrowserService ad-hoc open/exec │
│ CodeExecutorService sandboxed JS runtime │
│ ScenarioService scenarios/steps/runs CRUD │
│ ScenarioSchedulerService @Interval(1 s) run worker │
└───────────────────────┬──────────────────────────────┘
│ better-sqlite3 (TypeORM)
▼
data/sessions.db
Browser (React SPA) MCP client (AI agent, IDE)
│ / and /api/v1 │ /mcp (Streamable HTTP)
└──────────────┬──────────────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ liqa container (:13000 → 3000) NestJS app, server/src/ │
│ │
│ Routes / built client (ServeStaticModule) │
│ /api/v1 REST API /docs Swagger UI │
│ /mcp MCP tools /health health check │
│ │
│ McpService MCP tools → services below │
│ ScenarioService scenarios, steps, versions, runs │
│ ScenarioSchedulerService @Interval(1 s) run worker │
│ E2eExportService standalone Playwright test zip │
│ SnippetService snippets + versions │
│ EnvironmentService environments │
│ CredentialService credentials │
│ ImportService central YAML import │
│ FileStorageService scenario files + run artifacts │
│ SessionService saved browser sessions │
│ SessionContextService live Playwright contexts │
│ SessionSchedulerService @Interval(60 s) session cleanup │
│ BrowserService ad-hoc open / exec │
│ CodeExecutorService runs step / snippet JS │
└───────────────┬─────────────────────────────┬───────────────┘
│ TypeORM (better-sqlite3) │ Playwright
▼ ▼
data/liqa.db, data/files/ chromium-headless-shell
```
- **McpService** registers MCP tools and delegates to session, environment, browser, and scenario services.
- **SessionService** persists session records in SQLite. **SessionContextService** manages live Playwright contexts for open sessions.
- **EnvironmentService**, **CredentialService**, and **SnippetService** provide CRUD plus import/export upserts.
- **BrowserService** handles stateless ad-hoc automation (`/open`, `/exec`) and session-aware browsing.
- **CodeExecutorService** validates and executes user JS with `page`, `context`, and helper APIs.
- **ScenarioService** manages scenarios, steps, credential aliases, runs, logs, and scenario import/export.
- **ScenarioSchedulerService** polls pending runs every second and executes step runs to completion.
Everything runs in one container built from the root `Dockerfile`: the server serves the API, MCP and Swagger, and also the built client. See [development.md](development.md) for running it.
## Server components (`server/src/`)
- **McpService** (`mcp/`) registers the MCP tools and delegates to the services below. Each request gets a fresh MCP server over the Streamable HTTP transport.
- **ScenarioService** (`scenario/`) manages scenarios, steps, credential aliases, versions, runs, run logs, scenario/run files, and scenario export/import.
- **ScenarioSchedulerService** picks up pending runs every second (up to 10 in parallel) and executes their steps in order in one Playwright browser per run. It handles step and scenario timeouts, failure page dumps (screenshot + HTML), Playwright traces, and saving the browser as a session (`saveSession`).
- **E2eExportService** builds a standalone Playwright test project (zip) from a scenario, bundling the snippets, credentials and files it uses.
- **SnippetService** (`snippet/`), **EnvironmentService** (`environment/`) and **CredentialService** (`credential/`) provide CRUD plus export/import.
- **ImportService** (`import/`) implements `POST /import`: validates a YAML stream of mixed items, then imports them in dependency order (environments, credentials, snippets, scenarios).
- **FileStorageService** (`file/`) stores uploaded scenario files and run artifacts under `FILES_DIR`, and deletes expired files on a cron schedule (`FILE_CLEANUP_CRON`).
- **SessionService** (`session/`) persists saved sessions (cookies, localStorage); **SessionContextService** keeps their live Playwright contexts; **SessionSchedulerService** closes idle sessions and deletes old closed ones every minute.
- **BrowserService** (`browser/`) handles ad-hoc automation (`POST /api/v1/open`, `POST /api/v1/exec`, and the `open_url` / `exec_code` MCP tools), optionally in a saved session.
- **CodeExecutorService** (`code-executor/`) checks step and snippet code with acorn, then runs it with the `context` API (page, environment values, credentials, snippets, files, assertions, logs). It is not a security sandbox: step code is trusted.
Cross-cutting pieces:
- `config/app.config.ts` - all settings, validated at startup (see `.env.example`).
- `database/` - the entity list, TypeORM data source options (shared by the app, the migration runner and the TypeORM CLI), the migration runner and the migrations.
- `common/app-package.ts` - app name and version, read from the workspace root `package.json`.
- `common/client-static.ts` - serving the built client, with the `index.html` fallback for client-side routes.
- `common/body-parsers.ts` - YAML request bodies (`application/yaml`) for the import endpoint.
- `interceptors/` - a trace id per request (kept in `AsyncLocalStorage` and added to log lines) and request/response logging; `filters/` - consistent error responses.
## Data
SQLite via TypeORM. The schema is managed by migrations (`server/src/database/migrations/`, registered in `migrations/index.ts`); `synchronize` is off. The Docker container applies pending migrations (`node dist/database/migrate`) before starting the server, and the server refuses to start while migrations are pending. See "Database migrations" in [development.md](development.md). The main tables:
- `scenarios` / `scenario_steps` hold the **latest** version of each scenario; `scenario_versions` holds a snapshot of every version (content + steps).
- `snippets` holds the latest version of each snippet; `snippet_versions` holds every version. Step code pins a version with `runSnippet("alias@N")`.
- `scenario_runs` record the scenario version, environment, trace mode and status; `scenario_run_steps` keep their own copy of each step's code, so a run always shows what it executed; `scenario_run_logs` hold ordered run logs.
- `files` + `scenario_files` / `scenario_run_files` map stored files to scenarios and runs.
- `environments`, `credentials`, `scenario_credentials` (alias → credential per scenario), `sessions`.
See [scenario.md](scenario.md) for versioning, runs and import/export rules.
## 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. During development it's served by the Vite dev server; in the Docker image the server serves the built client 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).
Pages: scenarios (with steps, versions, credentials, used snippets and files), runs, snippets, credentials, environments, sessions, and the central import page. The sidebar footer contains the theme switcher and the app version (injected from the workspace root `package.json` at build time).
See [development.md](development.md) for the full client tech stack and directory layout.
+79 -31
View File
@@ -2,7 +2,7 @@
## Prerequisites
- Node.js 20+
- Node.js 24+ (required by `engines` in `package.json`; the Docker image uses Node.js 24)
- Docker + Docker Compose
The repository is a **npm workspace monorepo** with two packages:
@@ -16,21 +16,29 @@ Install all dependencies from the repo root:
```bash
npm install
npx playwright install chromium # first time only (server)
npx playwright install --only-shell chromium # first time only: headless Chromium for the server
```
Copy `.env.example` to `.env` and fill in the required values before running.
No `.env` is needed: every setting below has a default. To override settings, copy `.env.example` to `.env` and uncomment the ones you need. Docker Compose loads `.env` when it exists.
### Environment variables (server)
All settings are validated at startup by `server/src/config/app.config.ts`.
| Variable | Default | Description |
|---|---|---|
| `NODE_ENV` | `development` | `production` in the Docker image. Shown in the startup log, and selects the default CORS origins |
| `CORS_ORIGINS` | *(empty)* | Comma-separated origins allowed to call the API cross-origin. When empty: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) unless `NODE_ENV=production`, then the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) |
| `PORT` | `3000` | HTTP port |
| `KEYS_DIR` | `keys` | Directory for key material used by credential/snippet workflows |
| `DB_PATH` | `data/sessions.db` | SQLite database file |
| `DB_PATH` | `data/liqa.db` | SQLite database file |
| `FILES_DIR` | `data/files` | Where scenario files and run artifacts are stored |
| `FILE_RUN_EXPIRATION_DAYS` | `7` | How long run artifacts (downloads, failure dumps, traces) are kept |
| `FILE_CLEANUP_CRON` | `0 3 * * *` | When expired files are deleted |
| `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`). |
| `PLAYWRIGHT_TRACE_MODE` | `off` | Default Playwright trace mode for runs: `on`, `retain-on-failure` or `off` (see [scenario.md](scenario.md)) |
| `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`. |
---
@@ -61,13 +69,12 @@ The server listens on port **3000** by default.
npm -w client run dev
```
The Vite dev server runs on **http://localhost:5173** and proxies these API prefixes to `localhost:13000` (or `localhost:3000` when running server locally):
The Vite dev server runs on **http://localhost:5173**. It has no API proxy: the client calls `VITE_API_URL` (default `/api/v1` on its own origin), and the server allows CORS from `http://localhost:5173`. So point it at a running server:
- `/environments`
- `/credentials`
- `/snippets`
- `/sessions`
- `/scenarios`
```bash
npm -w client run dev:compose-api # API of the compose container (localhost:13000)
VITE_API_URL=http://localhost:3000/api/v1 npm -w client run dev # API of a locally started server
```
**Production build**:
@@ -79,6 +86,36 @@ The built files are emitted to `client/dist/`.
---
## Database migrations
The SQLite schema is managed by TypeORM migrations in `server/src/database/migrations/`; `synchronize` is off.
- **Docker:** the container applies pending migrations before starting the server.
- **Locally:** build, then apply them before starting the server (it refuses to start while migrations are pending):
```bash
npm -w server run build
npm -w server run migration:run # apply pending migrations to DB_PATH
npm -w server run migration:show # list migrations and whether they're applied
```
The scripts run from `server/`, so `DB_PATH` (default `data/liqa.db`) is relative to it, and they read `server/.env` if present, like the server does.
**Changing the schema** (adding or changing an entity):
1. Change the entity and build (`npm -w server run build`).
2. Make sure your local database is on the latest migration (`migration:run`).
3. Generate a migration from the difference between the entities and that database:
```bash
npm -w server run migration:generate -- src/database/migrations/AddSomething
```
4. Review the generated file, then add its class to `MIGRATIONS` in `src/database/migrations/index.ts` (they're listed explicitly, oldest first).
5. Run the tests: `test/migrations.spec.ts` checks that the migrations produce exactly the schema the entities describe, and fails if an entity change has no migration.
The initial migration (`Initial…`) adopts databases created before migrations existed (by `synchronize`): if the tables already exist, it's only recorded as applied.
## Running tests
### Server (Jest)
@@ -99,7 +136,7 @@ Watch mode:
npm -w server run test:watch
```
Enable verbose NestJS log output during tests:
Print the NestJS logs of the test app (they're silenced by default; `DEBUG=test`):
```bash
npm -w server run test:debug
@@ -163,6 +200,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,9 +230,11 @@ 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.
SQLite data is persisted in `./data/` and key files are mounted from `./keys/` — both directories are volume-mounted into the container.
When switching from the old two-container setup (`server` + `client`), remove the old containers with `docker compose up -d --build --remove-orphans`.
The SQLite database and stored files are persisted in `./data/`, which is volume-mounted into the container.
Stop and remove containers:
@@ -209,7 +252,7 @@ docker compose logs -f
## Client overview
The client (`client/`) is a React 19 + TypeScript SPA built with Vite. It provides a browser UI for managing environments, credentials, snippets, sessions, scenarios, and runs.
The client (`client/`) is a React 19 + TypeScript SPA built with Vite. It provides a browser UI for managing scenarios, runs, snippets, credentials, environments and sessions, and for importing exported YAML.
### Tech stack
@@ -217,9 +260,13 @@ The client (`client/`) is a React 19 + TypeScript SPA built with Vite. It provid
|---|---|
| React 19 + TypeScript | UI framework |
| Vite 6 | Dev server and bundler |
| react-router-dom (HashRouter) | Client-side routing |
| react-router-dom 7 (`BrowserRouter`) | Client-side routing |
| @tanstack/react-query | Data fetching (cross-scenario runs list) |
| i18next + react-i18next | Internationalisation |
| lucide-react | Icon library |
| @monaco-editor/react | Code editor for steps and snippets |
| react-markdown + highlight.js | Markdown descriptions and code highlighting |
| yaml | YAML export of entities |
| moment | Relative timestamp formatting |
| ESLint 10 + Prettier | Linting and formatting |
| Storybook 10 + Vitest | Component development and testing |
@@ -229,29 +276,30 @@ The client (`client/`) is a React 19 + TypeScript SPA built with Vite. It provid
```
client/
src/
api/ API client modules (environments, credentials, snippets, sessions, scenarios)
hooks/ useTheme (light/dark persistence)
api/ API client (client.ts) and types (types.ts)
components/ modals/ - run, export, export logs and delete confirmation modals
hooks/ useTheme, usePageTitle, useRunDefaults, useImportFile
i18n/ i18next bootstrap + locales/en.json
pages/ Environment/Credential/Snippet/Session/Scenario/Run pages
ui/ Reusable component library
Badge, Breadcrumbs, Button, Card, Input, Select,
SidePanel, Table, ThemeSwitcher, Timestamp
lib/ highlight.js setup, API error toast events
pages/ scenario/, run/, snippet/, credential/, environment/, session/, import/
ui/ Reusable component library (Badge, Button, Card, CodeEditor, Modal,
Select, SidePanel, Table, Toast, ... - see ui/index.ts)
.storybook/
stories/ *.stories.tsx for all ui/ components
stories/ *.stories.tsx for ui/ components
```
### Routing
Hash-based routing (`/#/path`) avoids conflicts with the Vite dev-server proxy. Default route redirects to `/#/scenarios`.
`BrowserRouter` with plain paths. The root and unknown paths redirect to `/scenarios`. The server answers these paths with the client's `index.html` (see [architecture.md](architecture.md)), so they work on refresh.
| Route | Page |
|---|---|
| `/#/environments` | Environment list |
| `/#/credentials` | Credential list |
| `/#/snippets` | Snippet list |
| `/#/sessions` | Session list |
| `/#/scenarios` | Scenario list |
| `/#/runs` | Cross-scenario runs list |
| `/scenarios` | Scenario list |
| `/scenarios/:id` | Scenario details (steps, versions, credentials, used snippets, files) |
| `/scenarios/:id/runs`, `/scenarios/:id/runs/:runId` | Runs of a scenario, run details |
| `/runs` | Cross-scenario runs list |
| `/snippets`, `/credentials`, `/environments`, `/sessions` | Entity lists (plus `/:id`, `/new`, `/:id/edit` where applicable) |
| `/import` | Central import |
### Theming
+77 -39
View File
@@ -1,78 +1,116 @@
# MCP tools
The MCP endpoint is exposed at `/mcp` (Streamable HTTP transport).
The MCP endpoint is exposed at `/mcp` (Streamable HTTP transport), e.g. `http://localhost:13000/mcp` with the compose setup. Each request is served by a fresh MCP server, so there is no MCP session state between calls.
The server currently registers the following tools.
The server registers the following tools. All entity IDs are UUIDs; list tools are paginated (`page`, `limit`, and where noted `orderBy` / `orderDir`). Errors are returned as MCP tool errors (`isError: true`) with the message as text.
## Sessions
| Tool | Description |
|---|---|
| `list_sessions` | List stored sessions (paginated) |
| `delete_session` | Delete a session by UUID |
| `list_sessions` | List saved browser sessions (`orderBy`/`orderDir`) |
| `delete_session` | Delete a session by `id` |
## Environments
| Tool | Description |
|---|---|
| `list_environments` | List environments (paginated) |
| `get_environment` | Get one environment by UUID |
| `create_environment` | Create environment (`name`, `urls`) |
| `update_environment` | Update environment (`id`, optional `name`/`urls`) |
| `delete_environment` | Delete environment by UUID |
| `list_environments` | List environments (`orderBy`/`orderDir`) |
| `get_environment` | Get one environment by `id` |
| `create_environment` | Create an environment: `name`, optional `description`, `data` |
| `update_environment` | Update `name`, `description` and/or `data` of environment `id` |
| `delete_environment` | Delete an environment by `id` |
`urls` is a free-form key/value map. Common keys: `id_url`, `cabinet_url`, `admin_url`.
`data` is a free-form map of string keys to string values, read in step code with `context.getEnv(key)`.
## Credentials
| Tool | Description |
|---|---|
| `list_credentials` | List credentials (`orderBy`/`orderDir`). Returns the full `data`, secrets included |
| `get_credential` | Get one credential by `id` (full `data`) |
| `create_credential` | Create a credential: `name`, optional `data` (JSON-encoded string) |
| `update_credential` | Update `name` and/or `data`; omitted fields are left unchanged |
| `delete_credential` | Delete a credential; also removes it from scenarios it's assigned to |
## Browser (ad-hoc)
| Tool | Description |
|---|---|
| `open_url` | Open URL with optional `sessionName`, return page/title/content |
| `exec_code` | Execute Playwright JS — script receives a single `context` argument (see `scenario.md`). Optional `environmentId` (UUID) resolves environment from DB for `context.getEnv()`. Optional `credentials` (`{ alias: credentialUUID }`) resolves credentials from DB for `context.getCredential(alias)`. |
| `open_url` | Open `url`, optionally in saved session `sessionName`; returns title and content (`readerMode` for readable text, `selector` to limit to one element) |
| `exec_code` | Run Playwright JS that receives a single `context` argument (see [scenario.md](scenario.md)), optionally after opening `url` and/or in session `sessionName`. `environmentId` loads an environment for `context.getEnv()`; `credentials` (`{ alias: credentialId }`) loads credentials for `context.getCredential(alias)` |
## Scenarios
| Tool | Description |
|---|---|
| `list_scenarios` | List scenarios (paginated) |
| `get_scenario` | Get scenario with steps |
| `create_scenario` | Create scenario by `name` |
| `update_scenario` | Rename scenario |
| `delete_scenario` | Delete scenario |
| `create_scenario_step` | Add step to scenario |
| `list_scenarios` | List scenarios (`orderBy`/`orderDir`) |
| `get_scenario` | Get a scenario (latest version) with its steps |
| `create_scenario` | Create a scenario: `name`, optional `description`, `environmentId` (default environment), `timeoutSeconds` |
| `update_scenario` | Update scenario fields; bumps the version when content changes |
| `delete_scenario` | Delete a scenario with its steps, versions and runs |
| `create_scenario_step` | Append a step: `execCode`, optional `title` and `timeoutSeconds`. The schema also requires `order` and `type`, but both are ignored: new steps always go last (use `update_scenario_step` with `order` to move one) |
| `get_scenario_step` | Get one step |
| `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 |
| `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 |
| `update_scenario_step` | Update step fields (including `order` to move it) |
| `delete_scenario_step` | Delete a step |
| `list_scenario_versions` | List a scenario's versions, newest first |
| `get_scenario_version` | Get one scenario version with its steps |
| `list_scenario_credentials` | List credentials assigned to a scenario (alias → credential) |
| `add_scenario_credential` | Assign credential `credentialId` to a scenario under `alias` |
| `remove_scenario_credential` | Remove an assignment by `scenarioCredentialId` (the assignment id, not the credential id) |
| `export_scenario` | Export one scenario version (optional `version`, defaults to the latest) |
| `import_scenario` | Import one scenario version; `version` is required (see "Versioning" in [scenario.md](scenario.md)) |
| `import_yaml` | Import any mix of environments, credentials, snippets and scenarios from YAML (see "Import" in [scenario.md](scenario.md)) |
| `export_e2e_test` | Export a scenario as a standalone Playwright test project (zip, base64) |
## Scenario Files
Changes to scenario content (fields and steps) create a new scenario version; credential assignments and files are not versioned.
## Runs
| Tool | Description |
|---|---|
| `upload_scenario_file` | Upload a file to a scenario; accepts optional `expiresAt` (ISO 8601 date string) |
| `list_scenario_files` | List scenario files (paginated) |
| `get_scenario_file_content` | Retrieve file content as base64 with metadata |
| `run_scenario` | Start a run of scenario `id` in `environmentId`; optional `version` (defaults to the latest), `saveSession`, `traceMode` (`on`, `retain-on-failure`, `off`; defaults to `PLAYWRIGHT_TRACE_MODE`) |
| `list_scenario_runs` | List runs of a scenario, optionally filtered by `status` |
| `list_all_runs` | List runs across all scenarios, optionally filtered by `status` |
| `get_scenario_run` | Get a run with its step runs (outputs) and logs |
| `wait_for_scenario_run` | Wait until a run is `pass` or `fail` (up to 5 minutes), then return it |
## Run Artifact Files
## Snippets
| 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 |
| `list_snippets` | List snippets (`orderBy`/`orderDir`) |
| `get_snippet` | Get one snippet (latest version) |
| `create_snippet` | Create a snippet: `alias`, `title`, `code`, optional `description` |
| `update_snippet` | Update snippet fields; bumps its version when content changes |
| `delete_snippet` | Delete a snippet and all its versions |
| `list_snippet_versions` | List a snippet's versions, newest first |
| `get_snippet_version` | Get one snippet version |
## Binary Content Handling
Step code runs a snippet with `context.runSnippet("alias", ...args)` (latest version) or `context.runSnippet("alias@3", ...args)` (version 3).
File tools transport binary payloads as base64-encoded strings in a `contentBase64` field with an accompanying `encoding: "base64"` marker in the response. When retrieving file content via `get_scenario_file_content` or `get_run_file_content`, decode the base64 to recover the original bytes.
## Scenario files
Scenario files uploaded via `upload_scenario_file` must have their content pre-encoded as base64. Run artifacts are created implicitly through scenario execution (via `context.downloadFile()` during step execution) and cannot be uploaded via MCP.
| Tool | Description |
|---|---|
| `upload_scenario_file` | Upload a file to a scenario: `name`, base64 `contentBase64`, optional `mimeType` and `expiresAt` (ISO 8601) |
| `list_scenario_files` | List a scenario's files (`limit`, `offset`) |
| `get_scenario_file_content` | Get a file's content as base64, with metadata |
## Run files
| Tool | Description |
|---|---|
| `list_run_files` | List files created during a run (`limit`, `offset`): downloads, failure screenshots/HTML, `trace.zip` |
| `get_run_file_content` | Get a run file's content as base64, with metadata |
| `delete_run_file` | Delete a file from a run; the stored file is removed once no scenario or run references it (also `DELETE /api/v1/scenarios/:id/runs/:runId/files/:fileId` and the delete button on the run detail page) |
## Binary content handling
File tools carry binary content as base64 strings in a `contentBase64` field, with `encoding: "base64"` in the response. Decode it to get the original bytes.
Content for `upload_scenario_file` must be base64-encoded. Run files are only created by runs themselves (`context.downloadFile()`, failure page dumps, traces) and can't be uploaded.
## Notes
- Tool IDs and entity IDs are UUIDs.
- `create_scenario_step` schema still accepts legacy `type`/`sessionName` fields for compatibility. Current scheduler executes `execCode` plus optional `validateCode` and does not branch by step type.
- The step `type` field (`login`, `exec`, `sign`) in `create_scenario_step`, `update_scenario_step` and `import_scenario` is a legacy field kept for compatibility; it's ignored, and every step runs its `execCode`.
+87 -34
View File
@@ -1,23 +1,23 @@
# Scenario execution
When a scenario run is created, the server creates one `ScenarioRunStep` per scenario step.
API paths below are relative to the REST prefix `/api/v1` (e.g. `POST /api/v1/scenarios/:id/run`).
- Step runs are ordered by `step.order`.
- First step run starts as `pending`; the rest start as `waiting`.
- Scheduler tick (`@Interval(1000)`) picks pending runs and processes steps in order.
- Steps in the same run share one Playwright browser/context/page.
## Runs
## Execution model
Creating a run (`POST /scenarios/:id/run`, the Run button, or the `run_scenario` MCP tool) takes an environment, and optionally a scenario version (latest by default), `saveSession` and a trace mode.
Current scheduler behavior is exec-centric:
- `execCode` is required for a step to pass.
- `validateCode` is optional and runs after `execCode`.
- Legacy step type metadata may still appear in MCP payloads, but run execution is not branched by step type.
- The run gets one step run per step of that scenario version, each with its own copy of the step's title, code and timeout. Editing or deleting steps later doesn't change existing runs.
- The first step run starts as `pending`, the rest as `waiting`.
- A scheduler tick (`@Interval(1000)`) picks up pending runs (up to 10 at a time) and executes their steps in order. All steps of a run share one Playwright browser, context and page.
- A step passes when its code returns, and fails when it throws or times out. Its return value is stored as the step output (JSON) and is available to later steps via `context.getStepOutput()`.
- When a step fails, the run is marked `fail`, the remaining steps are `cancelled`, and a screenshot and the page HTML are saved to the run's files (`step-<order>-failure.png` / `.html`).
- Timeouts: each step gets its own `timeoutSeconds`, otherwise the scenario's `timeoutSeconds`, otherwise 60 s. The run as a whole fails once it has run longer than the scenario's `timeoutSeconds` (default 600 s); this is checked before each step.
- With `saveSession`, the run's browser is kept after the run as a saved session named `run-<runId>` (see Sessions in the UI), instead of being closed.
- Runs left `in_progress` when the server stops are marked `fail` on the next start.
## Script runtime
`execCode` and `validateCode` execute as async JavaScript with a single `context` argument:
Step code (`execCode`) and snippet code run as an async JavaScript function body with a single `context` argument:
```js
async (context) => {
@@ -25,40 +25,93 @@ async (context) => {
}
```
Available in scope:
Available on `context`:
- `context.page`: Playwright `Page`
- `context.browser`: Playwright `BrowserContext`
- `context.env`: shallow copy of environment key/value map
- `context.getEnv(key)`: required environment value lookup (throws if missing)
- `context.getCredential(alias)`: credential payload assigned to the scenario alias
- `context.getStepOutput(order)`: prior step output by absolute order (`1`, `2`, ...) or relative (`-1` = previous step)
- `context.dumpDom(selector?)`: simplified DOM snapshot
- `context.log(...args)`, `context.warn(...args)`, `context.error(...args)`: structured step logs
- `context.env`: shallow copy of the environment's key/value map
- `context.getEnv(key)`: environment value lookup (throws if missing)
- `context.getCredential(alias)`: payload of the credential assigned to the scenario under `alias` (parsed JSON when the data is JSON; throws if missing)
- `context.getStepOutput(order)`: output of a previous step by absolute order (`1`, `2`, ...) or relative (`-1` = previous step); `null` if there is none
- `context.dumpDom(selector?)`: simplified DOM snapshot of the page (or of one element)
- `context.log(...args)`, `context.warn(...args)`, `context.error(...args)`: run log entries
- `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.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.
- `context.runSnippet(name, ...args)`: runs stored snippet code with the same context; the snippet receives the extra arguments as `...args`. `name` is the snippet alias for its latest version, or `alias@<version>` (e.g. `login@3`) to pin a specific version. Snippet start/finish/failure is logged
- `context.getScenarioFiles(opts?)`: list files uploaded to the scenario (`limit`, `offset`), including their path on disk
- `context.downloadFile(url, opts?)`: fetch `url` and save the result as a run file (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.
Also in scope:
- `console`: proxied to run logs (`log`, `warn`, `error`, etc.)
- `expect`: Playwright's `expect` (e.g. `await expect(context.page.locator("h1")).toHaveText("Done")`)
`validateCode` additionally receives `result` in scope, which is the value returned by `execCode`.
Code is syntax-checked before it runs, and executes in the server process: it's trusted code, not sandboxed.
## Validation contract
Step run logs also include automatic "Step N started / passed / failed" entries.
`validateCode` may return:
## Versioning
```js
return { success: true, description: 'ok' };
// or
return false;
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"));
```
Interpretation:
## Playwright traces
- Boolean: `true` = pass, `false` = fail
- Object: `success` controls pass/fail; `description` is persisted as step description
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.
On validation failure, the step fails and the run is marked `fail`.
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.
## Statuses
@@ -83,7 +136,7 @@ Run-step status:
1. Create environment
2. Create credentials and snippets (optional but common)
3. Create scenario
4. Add ordered steps (`execCode`, optional `validateCode`)
4. Add ordered steps (`execCode`, optional `title` and `timeoutSeconds`)
5. Assign credentials to scenario aliases when needed
6. Run scenario
7. Wait for run completion and inspect run logs/outputs
+5102 -3378
View File
File diff suppressed because it is too large. Load diff
+4 -1
View File
@@ -1,7 +1,10 @@
{
"name": "liqa",
"version": "1.9.1",
"version": "1.20.2",
"private": true,
"engines": {
"node": ">=24"
},
"workspaces": [
"server",
"client"
-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"]
+7 -1
View File
@@ -1,12 +1,15 @@
{
"name": "liqa-server",
"version": "1.0.0",
"private": true,
"main": "index.js",
"scripts": {
"build": "nest build",
"start": "nest start",
"start:dev": "nest start --watch",
"start:prod": "node dist/main",
"migration:run": "node dist/database/migrate",
"migration:show": "typeorm migration:show -d dist/database/data-source.js",
"migration:generate": "typeorm migration:generate -d dist/database/data-source.js",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"lint:fix": "eslint --fix \"src/**/*.ts\" \"test/**/*.ts\"",
@@ -25,10 +28,12 @@
"@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",
"acorn": "^8.16.0",
"archiver": "^7.0.1",
"better-sqlite3": "^12.8.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.4",
@@ -46,6 +51,7 @@
"@nestjs/cli": "^11.0.17",
"@nestjs/testing": "^11.1.18",
"@types/acorn": "^4.0.6",
"@types/archiver": "^6.0.4",
"@types/better-sqlite3": "^7.6.13",
"@types/debug": "^4.1.13",
"@types/express": "^5.0.6",
+12 -33
View File
@@ -1,28 +1,19 @@
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 { dataSourceOptions } from "./database/data-source";
import { AppConfig, validateAppConfig } from "./config/app.config";
import { CredentialEntity } from "./credential/credential.entity";
import { CredentialModule } from "./credential/credential.module";
import { EnvironmentEntity } from "./environment/environment.entity";
import { EnvironmentModule } from "./environment/environment.module";
import { FileEntity } from "./file/file.entity";
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 { ScenarioEntity } from "./scenario/scenario.entity";
import { ScenarioModule } from "./scenario/scenario.module";
import { SessionEntity } from "./session/session.entity";
import { SnippetEntity } from "./snippet/snippet.entity";
import { SnippetModule } from "./snippet/snippet.module";
@Module({
@@ -33,28 +24,15 @@ 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>) => ({
type: "better-sqlite3",
database: config.get("DB_PATH"),
entities: [
SessionEntity,
EnvironmentEntity,
CredentialEntity,
ScenarioEntity,
ScenarioStepEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
ScenarioCredentialEntity,
SnippetEntity,
FileEntity,
ScenarioFileEntity,
ScenarioRunFileEntity,
],
synchronize: true,
}),
useFactory: (config: ConfigService<AppConfig, true>) =>
dataSourceOptions(config.get("DB_PATH")),
}),
BrowserModule,
EnvironmentModule,
@@ -62,6 +40,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";
@@ -74,7 +74,6 @@ export interface ExecContext {
credentials?: Record<string, unknown>;
environment?: EnvironmentData | null;
snippets?: Record<string, string> | null;
result?: unknown;
scenarioId?: string;
runId?: string;
fileService?: FileStorageService;
@@ -115,7 +114,6 @@ export class CodeExecutorService {
credentials,
environment,
snippets,
result,
scenarioId,
runId,
fileService,
@@ -199,6 +197,7 @@ export class CodeExecutorService {
if (snippetCode == null) {
throw new Error(`Snippet alias "${alias}" not found`);
}
scriptLog("log", `Snippet "${alias}" started`);
const snippetFn = new Function(
"context",
"console",
@@ -206,7 +205,22 @@ export class CodeExecutorService {
"expect",
`return (async (context, ...args) => { ${snippetCode} })(context, ...snippetArgs)`,
);
return snippetFn(scriptContext, fakeConsole, args, playwrightExpect);
try {
const snippetResult = await snippetFn(
scriptContext,
fakeConsole,
args,
playwrightExpect,
);
scriptLog("log", `Snippet "${alias}" finished`);
return snippetResult;
} catch (err) {
scriptLog(
"error",
`Snippet "${alias}" failed: ${(err as Error).message}`,
);
throw err;
}
},
getScenarioFiles: async (opts?: { limit?: number; offset?: number }) => {
if (!fileService || !scenarioId) {
@@ -262,12 +276,11 @@ export class CodeExecutorService {
},
};
// `console`, `result`, and `expect` are injected as named parameters so
// existing scripts that reference them directly still work.
// `console` and `expect` are injected as named parameters so scripts
// can reference them directly.
const fn = new Function(
"context",
"console",
"result",
"expect",
`return (async (context) => { ${code} })(context)`,
);
@@ -275,7 +288,6 @@ export class CodeExecutorService {
const execResult = await fn(
scriptContext,
fakeConsole,
result,
playwrightExpect,
);
return { result: execResult };
@@ -46,11 +46,6 @@ export class ExecContextBuilder {
return this;
}
result(result: unknown): this {
this.ctx.result = result;
return this;
}
scenarioId(scenarioId: string): this {
this.ctx.scenarioId = scenarioId;
return this;
+40
View File
@@ -0,0 +1,40 @@
import * as fs from "node:fs";
import * as path from "node:path";
export interface AppPackage {
name: string;
version: string;
}
let cached: AppPackage | undefined;
/**
* Name and version of the application, read from the workspace root
* `package.json` (the one declaring `workspaces`) - the single source of the
* app version. Found by walking up from this file, which covers both the repo
* (`server/src/common` → repo root) and the Docker image
* (`/app/dist/common` → `/app/package.json`).
*/
export function appPackage(): AppPackage {
if (cached) return cached;
let dir = __dirname;
for (;;) {
const file = path.join(dir, "package.json");
if (fs.existsSync(file)) {
const pkg = JSON.parse(fs.readFileSync(file, "utf8")) as Partial<
AppPackage & { workspaces: unknown }
>;
if (pkg.workspaces && pkg.name && pkg.version) {
cached = { name: pkg.name, version: pkg.version };
return cached;
}
}
const parent = path.dirname(dir);
if (parent === dir) {
throw new Error(
`Workspace root package.json (with "workspaces", "name" and "version") not found above ${__dirname}`,
);
}
dir = parent;
}
}
+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);
},
},
},
];
}
+23
View File
@@ -0,0 +1,23 @@
/** Vite dev server (`npm -w client run dev`), calling the API cross-origin. */
const DEV_ORIGINS = ["http://localhost:5173", "http://127.0.0.1:5173"];
/**
* The port compose.yaml publishes. The UI is served from the same origin in
* production, so these only matter for other tools calling the API from a
* page on that origin; keep in sync with compose.yaml.
*/
const PRODUCTION_ORIGINS = ["http://localhost:13000", "http://127.0.0.1:13000"];
/**
* Origins allowed to call the API cross-origin. `CORS_ORIGINS` (comma
* separated) overrides the defaults, which depend on NODE_ENV: the compose
* port in production, the Vite dev server otherwise.
*/
export function corsOrigins(nodeEnv: string, override: string): string[] {
const configured = override
.split(",")
.map((origin) => origin.trim())
.filter(Boolean);
if (configured.length > 0) return configured;
return nodeEnv === "production" ? PRODUCTION_ORIGINS : DEV_ORIGINS;
}
+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
+48
View File
@@ -0,0 +1,48 @@
/** Extracts the alias literals passed to `runSnippet("alias", ...)` calls in `code`. */
export function extractRunSnippetAliases(code: string): string[] {
const aliases: string[] = [];
const regex = /runSnippet\s*\(\s*(['"`])((?:(?!\1).)*)\1/g;
let match: RegExpExecArray | null;
while ((match = regex.exec(code)) !== null) {
aliases.push(match[2]);
}
return aliases;
}
/**
* Resolves the transitive closure of snippet aliases referenced (directly, or via
* other snippets) by `stepCodes`, against the full alias -> code map. Aliases with
* no matching entry in `snippetCodeByAlias` are dropped. Returned in discovery order.
*/
export function resolveUsedSnippetAliases(
stepCodes: string[],
snippetCodeByAlias: Record<string, string>,
): string[] {
const used: string[] = [];
const seen = new Set<string>();
const queue = stepCodes.flatMap((code) => extractRunSnippetAliases(code));
while (queue.length > 0) {
const alias = queue.shift()!;
if (seen.has(alias)) continue;
seen.add(alias);
const code = snippetCodeByAlias[alias];
if (code == null) continue;
used.push(alias);
queue.push(...extractRunSnippetAliases(code));
}
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;
+30 -7
View File
@@ -1,25 +1,40 @@
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";
import { appPackage } from "../common/app-package";
export enum TraceMode {
On = "on",
RetainOnFailure = "retain-on-failure",
Off = "off",
}
export class AppConfig {
/** `production` in the Docker image; also selects the default CORS origins. */
@IsString()
NODE_ENV: string = "development";
/** Comma-separated origins allowed to call the API cross-origin; empty = default for NODE_ENV. */
@IsString()
CORS_ORIGINS: string = "";
@IsInt()
@Min(1)
@Max(65535)
PORT: number = 3000;
@IsString()
APP_NAME: string = pkg.name;
APP_NAME: string = appPackage().name;
@IsString()
APP_VERSION: string = pkg.version;
@IsString()
KEYS_DIR: string = "keys";
APP_VERSION: string = appPackage().version;
@IsString()
DB_PATH: string = "data/liqa.db";
@@ -41,6 +56,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 {
+31
View File
@@ -0,0 +1,31 @@
import * as fs from "node:fs";
import { DataSource, type DataSourceOptions } from "typeorm";
import { validateAppConfig } from "../config/app.config";
import { ENTITIES } from "./entities";
import { MIGRATIONS } from "./migrations";
/**
* TypeORM options shared by the app, the migration runner and the TypeORM
* CLI. The schema is managed by migrations only (`synchronize: false`).
*/
export function dataSourceOptions(database: string): DataSourceOptions {
return {
type: "better-sqlite3",
database,
entities: ENTITIES,
migrations: MIGRATIONS,
synchronize: false,
};
}
// Outside Nest (migration runner, TypeORM CLI) nothing loads .env; load it
// like ConfigModule does: optional, and without overriding set variables
if (fs.existsSync(".env")) process.loadEnvFile(".env");
/**
* Data source for the TypeORM CLI and `migrate.ts`, using DB_PATH from the
* environment or .env (default `data/liqa.db`).
*/
export default new DataSource(
dataSourceOptions(validateAppConfig(process.env).DB_PATH),
);
+34
View File
@@ -0,0 +1,34 @@
import { CredentialEntity } from "../credential/credential.entity";
import { EnvironmentEntity } from "../environment/environment.entity";
import { FileEntity } from "../file/file.entity";
import { ScenarioFileEntity } from "../file/scenario-file.entity";
import { ScenarioRunFileEntity } from "../file/scenario-run-file.entity";
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 { ScenarioEntity } from "../scenario/scenario.entity";
import { SessionEntity } from "../session/session.entity";
import { SnippetVersionEntity } from "../snippet/snippet-version.entity";
import { SnippetEntity } from "../snippet/snippet.entity";
/** Every TypeORM entity of the app (used by the app, the migrations CLI and tests). */
export const ENTITIES = [
SessionEntity,
EnvironmentEntity,
CredentialEntity,
ScenarioEntity,
ScenarioStepEntity,
ScenarioVersionEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
ScenarioCredentialEntity,
SnippetEntity,
SnippetVersionEntity,
FileEntity,
ScenarioFileEntity,
ScenarioRunFileEntity,
];
+24
View File
@@ -0,0 +1,24 @@
import dataSource from "./data-source";
/**
* Applies pending migrations to the database at DB_PATH, then exits. Runs
* before the server in the Docker image (`node dist/database/migrate`).
*/
async function migrate(): Promise<void> {
await dataSource.initialize();
try {
const applied = await dataSource.runMigrations({ transaction: "each" });
console.log(
applied.length > 0
? `Applied ${applied.length} migration(s): ${applied.map((m) => m.name).join(", ")}`
: "Database schema is up to date",
);
} finally {
await dataSource.destroy();
}
}
migrate().catch((err: unknown) => {
console.error("Migration failed:", err);
process.exit(1);
});
@@ -0,0 +1,277 @@
import { MigrationInterface, QueryRunner } from "typeorm";
/**
* Initial schema. Databases created before migrations existed (by TypeORM
* `synchronize`) already have this schema: for them this migration only gets
* recorded as applied, and later migrations run from there.
*/
export class Initial1790793447827 implements MigrationInterface {
name = "Initial1790793447827";
public async up(queryRunner: QueryRunner): Promise<void> {
if (await queryRunner.hasTable("scenarios")) return;
await queryRunner.query(
`CREATE TABLE "credentials" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "data" text, "lastUsedAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "environments" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "data" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_9d00da4836959b86d7590f60c57" UNIQUE ("name"))`,
);
await queryRunner.query(
`CREATE TABLE "files" ("id" varchar PRIMARY KEY NOT NULL, "originalName" varchar NOT NULL, "mimeType" varchar NOT NULL, "size" integer NOT NULL, "sha256" varchar NOT NULL, "filePath" varchar NOT NULL, "expiresAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "sessions" ("id" varchar PRIMARY KEY NOT NULL, "sessionName" varchar NOT NULL, "token" text NOT NULL, "cookies" text NOT NULL, "localStorage" text NOT NULL DEFAULT ('{}'), "status" varchar NOT NULL DEFAULT ('closed'), "lastUsedAt" datetime, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_b8f17f1ce30f015f494188c50bb" UNIQUE ("sessionName"))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`CREATE TABLE "scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"))`,
);
await queryRunner.query(
`CREATE TABLE "snippets" ("id" varchar PRIMARY KEY NOT NULL, "alias" varchar, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_bd062c68591ae917b720eb3427c" UNIQUE ("alias"))`,
);
await queryRunner.query(
`CREATE TABLE "snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"))`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"), CONSTRAINT "FK_37f19dadd8e524b22a807ece1a9" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_96676b988693d5ca2923782530c" FOREIGN KEY ("credentialId") REFERENCES "credentials" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_credentials"("id", "scenarioId", "credentialId", "alias", "createdAt") SELECT "id", "scenarioId", "credentialId", "alias", "createdAt" FROM "scenario_credentials"`,
);
await queryRunner.query(`DROP TABLE "scenario_credentials"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_credentials" RENAME TO "scenario_credentials"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_4bab3637cc3bf37677bb01c7446" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_steps"("id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt") SELECT "id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt" FROM "scenario_steps"`,
);
await queryRunner.query(`DROP TABLE "scenario_steps"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_steps" RENAME TO "scenario_steps"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_ffdcf9186d95114f0dab068e20a" FOREIGN KEY ("environmentId") REFERENCES "environments" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenarios"("id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt") SELECT "id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt" FROM "scenarios"`,
);
await queryRunner.query(`DROP TABLE "scenarios"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenarios" RENAME TO "scenarios"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_c41719984235f8984d1c6b92ef5" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_67013a49e7948fb530a7d956e2b" FOREIGN KEY ("fileId") REFERENCES "files" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_files"("id", "scenarioId", "fileId", "createdAt") SELECT "id", "scenarioId", "fileId", "createdAt" FROM "scenario_files"`,
);
await queryRunner.query(`DROP TABLE "scenario_files"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_files" RENAME TO "scenario_files"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_213c27405af3433011121babb58" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_e84cd4c7725bdc5dfd09d18d92f" FOREIGN KEY ("scenarioStepId") REFERENCES "scenario_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_run_steps"("id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt") SELECT "id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt" FROM "scenario_run_steps"`,
);
await queryRunner.query(`DROP TABLE "scenario_run_steps"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_run_steps" RENAME TO "scenario_run_steps"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_1d2c2595dfff133de062f9c692a" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_5076ffbd1b4fa71f5de011eba1f" FOREIGN KEY ("environmentId") REFERENCES "environments" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_068b01cdfa49095433f245d6708" FOREIGN KEY ("sessionId") REFERENCES "sessions" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_runs"("id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt") SELECT "id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt" FROM "scenario_runs"`,
);
await queryRunner.query(`DROP TABLE "scenario_runs"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_runs" RENAME TO "scenario_runs"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_5fc35a4bfc42d7566a0f7706902" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_637619025bdb0b6a89fae6e178d" FOREIGN KEY ("fileId") REFERENCES "files" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_run_files"("id", "runId", "fileId", "createdAt") SELECT "id", "runId", "fileId", "createdAt" FROM "scenario_run_files"`,
);
await queryRunner.query(`DROP TABLE "scenario_run_files"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_run_files" RENAME TO "scenario_run_files"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "seq", "createdAt") SELECT "id", "runId", "stepRunId", "level", "message", "seq", "createdAt" FROM "scenario_run_logs"`,
);
await queryRunner.query(`DROP TABLE "scenario_run_logs"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_run_logs" RENAME TO "scenario_run_logs"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"), CONSTRAINT "FK_f5ccfb45c7847cfab74cf4f17b6" FOREIGN KEY ("scenarioId") REFERENCES "scenarios" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_versions"("id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt") SELECT "id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt" FROM "scenario_versions"`,
);
await queryRunner.query(`DROP TABLE "scenario_versions"`);
await queryRunner.query(
`ALTER TABLE "temporary_scenario_versions" RENAME TO "scenario_versions"`,
);
await queryRunner.query(
`CREATE TABLE "temporary_snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"), CONSTRAINT "FK_0d3a004957c9553619be11635fd" FOREIGN KEY ("snippetId") REFERENCES "snippets" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_snippet_versions"("id", "snippetId", "version", "alias", "title", "description", "code", "createdAt") SELECT "id", "snippetId", "version", "alias", "title", "description", "code", "createdAt" FROM "snippet_versions"`,
);
await queryRunner.query(`DROP TABLE "snippet_versions"`);
await queryRunner.query(
`ALTER TABLE "temporary_snippet_versions" RENAME TO "snippet_versions"`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "snippet_versions" RENAME TO "temporary_snippet_versions"`,
);
await queryRunner.query(
`CREATE TABLE "snippet_versions" ("id" varchar PRIMARY KEY NOT NULL, "snippetId" varchar NOT NULL, "version" integer NOT NULL, "alias" varchar NOT NULL, "title" varchar NOT NULL DEFAULT (''), "description" text, "code" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_2487f4aabeb20c4192ee30e09a2" UNIQUE ("snippetId", "version"))`,
);
await queryRunner.query(
`INSERT INTO "snippet_versions"("id", "snippetId", "version", "alias", "title", "description", "code", "createdAt") SELECT "id", "snippetId", "version", "alias", "title", "description", "code", "createdAt" FROM "temporary_snippet_versions"`,
);
await queryRunner.query(`DROP TABLE "temporary_snippet_versions"`);
await queryRunner.query(
`ALTER TABLE "scenario_versions" RENAME TO "temporary_scenario_versions"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_versions" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "version" integer NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" text, "timeoutSeconds" integer, "steps" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_e1a56dce2b7e0379bb5bbdf8c1f" UNIQUE ("scenarioId", "version"))`,
);
await queryRunner.query(
`INSERT INTO "scenario_versions"("id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt") SELECT "id", "scenarioId", "version", "name", "description", "environmentId", "timeoutSeconds", "steps", "createdAt" FROM "temporary_scenario_versions"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_versions"`);
await queryRunner.query(
`ALTER TABLE "scenario_run_logs" RENAME TO "temporary_scenario_run_logs"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "seq" integer NOT NULL DEFAULT (0), "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "seq", "createdAt") SELECT "id", "runId", "stepRunId", "level", "message", "seq", "createdAt" FROM "temporary_scenario_run_logs"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`);
await queryRunner.query(
`ALTER TABLE "scenario_run_files" RENAME TO "temporary_scenario_run_files"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_files" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_run_files"("id", "runId", "fileId", "createdAt") SELECT "id", "runId", "fileId", "createdAt" FROM "temporary_scenario_run_files"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_run_files"`);
await queryRunner.query(
`ALTER TABLE "scenario_runs" RENAME TO "temporary_scenario_runs"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_runs" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "environmentId" varchar, "sessionId" varchar, "scenarioVersion" integer, "status" text NOT NULL DEFAULT ('pending'), "saveSession" boolean NOT NULL DEFAULT (0), "traceMode" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_runs"("id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt") SELECT "id", "scenarioId", "environmentId", "sessionId", "scenarioVersion", "status", "saveSession", "traceMode", "createdAt", "updatedAt" FROM "temporary_scenario_runs"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_runs"`);
await queryRunner.query(
`ALTER TABLE "scenario_run_steps" RENAME TO "temporary_scenario_run_steps"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_run_steps" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "scenarioStepId" varchar, "title" text, "execCode" text, "timeoutSeconds" integer, "status" text NOT NULL DEFAULT ('waiting'), "order" integer NOT NULL DEFAULT (0), "description" text, "output" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_run_steps"("id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt") SELECT "id", "runId", "scenarioStepId", "title", "execCode", "timeoutSeconds", "status", "order", "description", "output", "createdAt", "updatedAt" FROM "temporary_scenario_run_steps"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_run_steps"`);
await queryRunner.query(
`ALTER TABLE "scenario_files" RENAME TO "temporary_scenario_files"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_files" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "fileId" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_files"("id", "scenarioId", "fileId", "createdAt") SELECT "id", "scenarioId", "fileId", "createdAt" FROM "temporary_scenario_files"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_files"`);
await queryRunner.query(
`ALTER TABLE "scenarios" RENAME TO "temporary_scenarios"`,
);
await queryRunner.query(
`CREATE TABLE "scenarios" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "description" text, "environmentId" varchar, "timeoutSeconds" integer, "version" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenarios"("id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt") SELECT "id", "name", "description", "environmentId", "timeoutSeconds", "version", "createdAt", "updatedAt" FROM "temporary_scenarios"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenarios"`);
await queryRunner.query(
`ALTER TABLE "scenario_steps" RENAME TO "temporary_scenario_steps"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_steps" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "order" integer NOT NULL DEFAULT (0), "title" text, "execCode" text, "timeoutSeconds" integer, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))`,
);
await queryRunner.query(
`INSERT INTO "scenario_steps"("id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt") SELECT "id", "scenarioId", "order", "title", "execCode", "timeoutSeconds", "createdAt", "updatedAt" FROM "temporary_scenario_steps"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_steps"`);
await queryRunner.query(
`ALTER TABLE "scenario_credentials" RENAME TO "temporary_scenario_credentials"`,
);
await queryRunner.query(
`CREATE TABLE "scenario_credentials" ("id" varchar PRIMARY KEY NOT NULL, "scenarioId" varchar NOT NULL, "credentialId" varchar NOT NULL, "alias" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_02756536236c408dbb58b3fc964" UNIQUE ("scenarioId", "alias"))`,
);
await queryRunner.query(
`INSERT INTO "scenario_credentials"("id", "scenarioId", "credentialId", "alias", "createdAt") SELECT "id", "scenarioId", "credentialId", "alias", "createdAt" FROM "temporary_scenario_credentials"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_credentials"`);
await queryRunner.query(`DROP TABLE "snippet_versions"`);
await queryRunner.query(`DROP TABLE "snippets"`);
await queryRunner.query(`DROP TABLE "scenario_versions"`);
await queryRunner.query(`DROP TABLE "scenario_run_logs"`);
await queryRunner.query(`DROP TABLE "scenario_run_files"`);
await queryRunner.query(`DROP TABLE "scenario_runs"`);
await queryRunner.query(`DROP TABLE "scenario_run_steps"`);
await queryRunner.query(`DROP TABLE "sessions"`);
await queryRunner.query(`DROP TABLE "scenario_files"`);
await queryRunner.query(`DROP TABLE "scenarios"`);
await queryRunner.query(`DROP TABLE "scenario_steps"`);
await queryRunner.query(`DROP TABLE "scenario_credentials"`);
await queryRunner.query(`DROP TABLE "files"`);
await queryRunner.query(`DROP TABLE "environments"`);
await queryRunner.query(`DROP TABLE "credentials"`);
}
}
+7
View File
@@ -0,0 +1,7 @@
import { Initial1790793447827 } from "./1790793447827-Initial";
/**
* All migrations, oldest first. Listed explicitly (instead of a glob) so the
* same list works from compiled JS, ts-jest and the TypeORM CLI.
*/
export const MIGRATIONS = [Initial1790793447827];
+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";
+27 -4
View File
@@ -1,9 +1,13 @@
import { RequestMethod, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { DataSource } from "typeorm";
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 { appPackage } from "./common/app-package";
import { configureBodyParsers } from "./common/body-parsers";
import { corsOrigins } from "./common/cors";
import { TraceLogger } from "./common/trace-logger";
import { AppConfig } from "./config/app.config";
import { HttpExceptionFilter } from "./filters/http-exception.filter";
@@ -26,12 +30,22 @@ process.on("unhandledRejection", (reason) => {
});
async function bootstrap() {
const { name: pkgName, version: pkgVersion } = appPackage();
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 }),
});
// The schema is managed by migrations; refuse to run against an outdated one.
// Checked before app.init(), i.e. before any module touches the tables.
if (await app.get(DataSource).showMigrations()) {
throw new Error(
"The database has pending migrations. Run `npm -w server run migration:run` " +
"(the Docker image runs them automatically before starting the server).",
);
}
app.setGlobalPrefix(API_PREFIX, {
exclude: [
{ path: "health", method: RequestMethod.ALL },
@@ -40,6 +54,7 @@ async function bootstrap() {
],
});
configureBodyParsers(app);
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TraceInterceptor(), new LoggingInterceptor());
@@ -49,7 +64,7 @@ async function bootstrap() {
const nodeEnv = config.get("NODE_ENV");
app.enableCors({
origin: ["http://localhost:5173", "http://127.0.0.1:5173"],
origin: corsOrigins(nodeEnv, config.get("CORS_ORIGINS")),
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"],
credentials: true,
});
@@ -72,4 +87,12 @@ async function bootstrap() {
logger.log(`MCP endpoint available at http://localhost:${port}/mcp`);
}
bootstrap();
// Startup failures (pending migrations, port in use, ...) must stop the
// process with an error code; the handlers above only keep a running server alive
bootstrap().catch((err: unknown) => {
processLogger.error(
`Startup failed: ${err instanceof Error ? err.message : String(err)}`,
err instanceof Error ? err.stack : undefined,
);
process.exit(1);
});
+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],
+401 -11
View File
@@ -7,13 +7,16 @@ 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";
import { appPackage } from "../common/app-package";
@Injectable()
export class McpService {
@@ -25,11 +28,14 @@ export class McpService {
private readonly browserService: BrowserService,
private readonly codeExecutor: CodeExecutorService,
private readonly scenarioService: ScenarioService,
private readonly e2eExportService: E2eExportService,
private readonly snippetService: SnippetService,
private readonly importService: ImportService,
) {}
private createServer(): McpServer {
const server = new McpServer({ name: pkg.name, version: pkg.version });
const { name, version } = appPackage();
const server = new McpServer({ name, version });
this.registerTools(server);
return server;
}
@@ -273,6 +279,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(
@@ -777,14 +943,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) }],
@@ -985,19 +1167,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: [
@@ -1017,8 +1254,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()
@@ -1040,10 +1286,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 }[],
@@ -1062,6 +1310,69 @@ 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",
{
description:
"Export a scenario as a standalone, plug-and-play Playwright e2e test project (package.json, playwright.config.ts, test.spec.ts, .env), zipped and returned as base64",
inputSchema: {
id: z.uuid().describe("Scenario ID to export"),
},
},
async ({ id }) => {
try {
const { filename, buffer } =
await this.e2eExportService.buildE2eTestPackage(id);
return {
content: [
{
type: "text" as const,
text: JSON.stringify({
filename,
contentBase64: buffer.toString("base64"),
encoding: "base64",
}),
},
],
};
} catch (err) {
return {
isError: true,
content: [{ type: "text" as const, text: (err as Error).message }],
};
}
},
);
// ── Scenario Files ────────────────────────────────────────────────────────
server.registerTool(
@@ -1128,12 +1439,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(
@@ -1277,6 +1588,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",
{
@@ -1391,7 +1733,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
@@ -1460,6 +1802,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()
+476
View File
@@ -0,0 +1,476 @@
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";
import { ScenarioService } from "./scenario.service";
const DEFAULT_SCENARIO_TIMEOUT_SEC = 600;
const DEFAULT_STEP_TIMEOUT_SEC = 60;
function slugify(name: string): string {
const slug = name
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
return slug || "scenario";
}
function jsStringLiteral(code: string): string {
return JSON.stringify(code);
}
function sanitizeAlias(alias: string): string {
const sanitized = alias.replace(/[^A-Za-z0-9_-]+/g, "_").replace(/^_+|_+$/g, "");
return sanitized || "snippet";
}
/**
* Builds a standalone, plug-and-play Playwright test project (package.json,
* playwright.config.ts, test.spec.ts, .env, README) from a scenario's steps,
* zipped for download. Each step's execCode is embedded verbatim and run
* against a `context` shim that mirrors the shape scripts already use in
* liqa (env, credentials, snippets, assert, ...).
*/
@Injectable()
export class E2eExportService {
constructor(
private readonly scenarioService: ScenarioService,
private readonly snippetService: SnippetService,
) {}
async buildE2eTestPackage(scenarioId: string): Promise<{ filename: string; buffer: Buffer }> {
const scenario = await this.scenarioService.findOne(scenarioId);
const [credentialMap, snippetMap, attachedFiles] = await Promise.all([
this.scenarioService.buildCredentialMap(scenarioId),
this.snippetService.buildSnippetMap(),
this.scenarioService.listScenarioFiles(scenarioId, 1000, 0),
]);
const envData = scenario.environment?.data ?? {};
const slug = slugify(scenario.name);
const files: Record<string, string | Buffer> = {
"package.json": this.buildPackageJson(slug),
"playwright.config.ts": this.buildPlaywrightConfig(scenario.timeoutSeconds),
".env": this.buildEnvFile(envData),
"credentials.json": JSON.stringify(credentialMap, null, 2) + "\n",
".gitignore": [
"node_modules/",
"test-results/",
"playwright-report/",
"downloads/",
".env",
"credentials.json",
].join("\n") + "\n",
"README.md": this.buildReadme(scenario.name, scenario.description),
};
// Bundle any files attached to the scenario so getScenarioFiles works offline.
const fileManifest: { id: string; name: string; mimeType: string; size: number; sha256: string; zipPath: string }[] =
[];
for (const item of attachedFiles.items as {
id: string;
name: string;
mimeType: string;
size: number;
sha256: string;
}[]) {
const { contentBuffer } = await this.scenarioService.getScenarioFileContentAsBuffer(
scenarioId,
item.id,
);
const zipPath = `files/${item.id}__${item.name}`;
files[zipPath] = contentBuffer;
fileManifest.push({
id: item.id,
name: item.name,
mimeType: item.mimeType,
size: item.size,
sha256: item.sha256,
zipPath,
});
}
// Bundle only the snippets actually referenced (directly or transitively via
// other snippets) by this scenario's steps, as individual .js files under snippets/.
const usedAliases = resolveUsedSnippetAliases(
scenario.steps.map((s) => s.execCode ?? ""),
snippetMap,
);
const snippetManifest: { alias: string; filename: string }[] = [];
const usedSnippetFilenames = new Set<string>();
for (const alias of usedAliases) {
const code = snippetMap[alias];
const base = sanitizeAlias(alias);
let filename = `${base}.js`;
let suffix = 2;
while (usedSnippetFilenames.has(filename)) {
filename = `${base}-${suffix}.js`;
suffix += 1;
}
usedSnippetFilenames.add(filename);
files[`snippets/${filename}`] = code;
snippetManifest.push({ alias, filename });
}
files["test.spec.ts"] = this.buildTestSpec(scenario, snippetManifest, fileManifest);
const buffer = await this.zip(files);
return { filename: `${slug}-e2e-test.zip`, buffer };
}
private buildPackageJson(slug: string): string {
return (
JSON.stringify(
{
name: `${slug}-e2e-test`,
version: "1.0.0",
private: true,
scripts: {
test: "playwright test",
"test:headed": "playwright test --headed",
},
devDependencies: {
"@playwright/test": "^1.59.1",
dotenv: "^17.4.2",
},
},
null,
2,
) + "\n"
);
}
private buildPlaywrightConfig(scenarioTimeoutSeconds: number | null): string {
const timeoutMs = (scenarioTimeoutSeconds ?? DEFAULT_SCENARIO_TIMEOUT_SEC) * 1000;
return `import { defineConfig } from "@playwright/test";
import * as dotenv from "dotenv";
import * as path from "node:path";
dotenv.config({ path: path.resolve(__dirname, ".env") });
export default defineConfig({
testDir: ".",
timeout: ${timeoutMs},
use: {
headless: true,
},
});
`;
}
private buildEnvFile(envData: Record<string, string>): string {
const lines = Object.entries(envData).map(([key, value]) => `${key}=${value}`);
return (
[
"# Environment values copied from the scenario's liqa environment.",
"# This file may contain secrets - do not commit it.",
...lines,
].join("\n") + "\n"
);
}
private buildReadme(scenarioName: string, description: string | null): string {
const descriptionSection = description?.trim() ? `${description.trim()}\n\n` : "";
return `# ${scenarioName} - standalone e2e test
${descriptionSection}## Setup
\`\`\`sh
npm install
npx playwright install --with-deps chromium
\`\`\`
## Run
\`\`\`sh
npm test
\`\`\`
Environment values are loaded from \`.env\` (as plain process env vars). Credential
data resolved from this scenario's aliases is stored in \`credentials.json\` and
loaded by \`context.getCredential\`. Snippet bodies are stored as individual \`.js\`
files under \`snippets/\` and loaded by \`context.runSnippet\`. Files attached to
the scenario are bundled under \`files/\` and served locally by
\`context.getScenarioFiles\`. \`context.downloadFile\` performs a real HTTP (or
\`data:\` URL) download and saves the result under \`downloads/\`.
`;
}
private buildTestSpec(
scenario: { name: string; timeoutSeconds: number | null; steps: { order: number; title: string | null; execCode: string | null; timeoutSeconds: number | null }[] },
snippetManifest: { alias: string; filename: string }[],
fileManifest: { id: string; name: string; mimeType: string; size: number; sha256: string; zipPath: string }[],
): string {
const steps = [...scenario.steps].sort((a, b) => a.order - b.order);
const scenarioTimeoutSec = scenario.timeoutSeconds ?? DEFAULT_SCENARIO_TIMEOUT_SEC;
const stepBlocks = steps
.map((step, index) => {
const title = (step.title ?? `Step ${index + 1}`).replace(/`/g, "\\`");
const timeoutSec = step.timeoutSeconds ?? scenarioTimeoutSec ?? DEFAULT_STEP_TIMEOUT_SEC;
const code = step.execCode ?? "";
return ` await test.step(\`${title}\`, async () => {
const __run = new Function(
"context",
"console",
"result",
"expect",
\`return (async (context) => { ${code.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${")} })(context)\`,
);
const __timeoutMs = ${timeoutSec * 1000};
const __output = await Promise.race([
__run(context, console, undefined, expect),
new Promise((_, reject) =>
setTimeout(() => reject(new Error(\`Step timed out after ${timeoutSec}s\`)), __timeoutMs),
),
]);
stepOutputs.push({ order: ${step.order}, output: __output });
});`;
})
.join("\n\n");
return `import { test, expect } from "@playwright/test";
import type { BrowserContext, Page } from "@playwright/test";
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.
const CREDENTIALS: Record<string, unknown> = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "credentials.json"), "utf-8"),
);
// Snippet bodies are stored as individual .js files under snippets/, available
// via context.runSnippet(alias, ...args).
const SNIPPET_FILES: { alias: string; filename: string }[] = ${JSON.stringify(snippetManifest, null, 2)};
// Files attached to the scenario, bundled alongside this test under files/.
const FILES: { id: string; name: string; mimeType: string; size: number; sha256: string; zipPath: string }[] =
${JSON.stringify(fileManifest, null, 2)};
interface StepOutput {
order: number;
output: unknown;
}
// ── Standalone downloadFile: real HTTP/data: URL fetch, no liqa backend involved ──
const MAX_DOWNLOAD_SIZE = 100 * 1024 * 1024; // 100 MB
const DOWNLOAD_TIMEOUT_MS = 30000;
function decodeDataUrl(urlStr: string): Buffer {
const match = /^data:([^,]*),(.*)$/s.exec(urlStr);
if (!match) {
throw new Error("Malformed data: URL");
}
const [, meta, data] = match;
if (meta.endsWith(";base64")) {
return Buffer.from(data, "base64");
}
return Buffer.from(decodeURIComponent(data), "utf-8");
}
function fetchBytes(
urlStr: string,
options?: { method?: string; headers?: Record<string, string>; body?: string },
): Promise<Buffer> {
if (urlStr.startsWith("data:")) {
return Promise.resolve(decodeDataUrl(urlStr));
}
const url = new URL(urlStr);
const protocol = url.protocol === "https:" ? https : http;
const method = options?.method ?? "GET";
const headers = options?.headers ?? {};
return new Promise((resolve, reject) => {
const req = protocol.request(
{ hostname: url.hostname, port: url.port, path: url.pathname + url.search, method, headers, timeout: DOWNLOAD_TIMEOUT_MS },
(res) => {
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
fetchBytes(res.headers.location, options).then(resolve).catch(reject);
return;
}
if (res.statusCode && res.statusCode !== 200) {
reject(new Error(\`HTTP \${res.statusCode}: \${res.statusMessage}\`));
return;
}
const chunks: Buffer[] = [];
let totalSize = 0;
res.on("data", (chunk: Buffer) => {
totalSize += chunk.length;
if (totalSize > MAX_DOWNLOAD_SIZE) {
req.destroy();
reject(new Error(\`File exceeds maximum size of \${MAX_DOWNLOAD_SIZE} bytes\`));
return;
}
chunks.push(chunk);
});
res.on("end", () => resolve(Buffer.concat(chunks)));
res.on("error", reject);
},
);
req.on("timeout", () => {
req.destroy();
reject(new Error("Download timeout"));
});
req.on("error", reject);
if (options?.body && (method === "POST" || method === "PUT")) {
req.write(options.body);
}
req.end();
});
}
const MIME_TYPES: Record<string, string> = {
pdf: "application/pdf",
doc: "application/msword",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
gif: "image/gif",
csv: "text/csv",
txt: "text/plain",
zip: "application/zip",
};
function inferMimeType(filename: string): string {
const ext = filename.split(".").pop()?.toLowerCase() ?? "";
return MIME_TYPES[ext] ?? "application/octet-stream";
}
function buildContext(page: Page, browser: BrowserContext, stepOutputs: StepOutput[]) {
const context: Record<string, unknown> = {
page,
browser,
env: { ...process.env },
getEnv(key: string): string {
const value = process.env[key];
if (value == null) {
throw new Error(\`Environment value "\${key}" is not defined\`);
}
return value;
},
getCredential(alias: string): unknown {
if (!(alias in CREDENTIALS)) {
throw new Error(\`Credential alias "\${alias}" not found in this export\`);
}
return CREDENTIALS[alias];
},
log: (...args: unknown[]) => console.log(...args),
warn: (...args: unknown[]) => console.warn(...args),
error: (...args: unknown[]) => console.error(...args),
assert: async (fn: () => boolean | Promise<boolean>, description: string): Promise<void> => {
let passed: boolean;
let failureReason: string | undefined;
try {
passed = (await fn()) === true;
} catch (err) {
passed = false;
failureReason = (err as Error).message;
}
if (passed) {
console.log(\`Assertion passed: \${description}\`);
return;
}
const message = failureReason
? \`Assertion failed: \${description} (\${failureReason})\`
: \`Assertion failed: \${description}\`;
console.error(message);
throw new Error(message);
},
getStepOutput: async (order: number): Promise<unknown> => {
const targetOrder = order < 0 ? stepOutputs.length + order + 1 : order;
return stepOutputs.find((s) => s.order === targetOrder)?.output ?? null;
},
runSnippet: async (alias: string, ...args: unknown[]): Promise<unknown> => {
const entry = SNIPPET_FILES.find((s) => s.alias === alias);
if (!entry) {
throw new Error(\`Snippet alias "\${alias}" not found\`);
}
const snippetCode = fs.readFileSync(path.resolve(__dirname, "snippets", entry.filename), "utf-8");
const snippetFn = new Function(
"context",
"console",
"snippetArgs",
"expect",
\`return (async (context, ...args) => { \${snippetCode} })(context, ...snippetArgs)\`,
);
return snippetFn(context, console, args, expect);
},
dumpDom: async (): Promise<never> => {
throw new Error("dumpDom is not available in this standalone export");
},
getScenarioFiles: async (opts?: { limit?: number; offset?: number }) => {
const offset = opts?.offset ?? 0;
const limit = opts?.limit ?? FILES.length;
const items = FILES.slice(offset, offset + limit).map((f) => ({
id: f.id,
name: f.name,
mimeType: f.mimeType,
size: f.size,
sha256: f.sha256,
path: path.resolve(__dirname, f.zipPath),
}));
return { items, total: FILES.length };
},
downloadFile: async (
url: string,
opts?: { method?: string; headers?: Record<string, string>; body?: string; filename?: string },
) => {
const buffer = await fetchBytes(url, opts);
const filename =
opts?.filename ??
(url.startsWith("data:") ? "download" : new URL(url).pathname.split("/").pop() || "download");
const downloadsDir = path.resolve(__dirname, "downloads");
fs.mkdirSync(downloadsDir, { recursive: true });
const filePath = path.join(downloadsDir, filename);
fs.writeFileSync(filePath, buffer);
return {
id: randomUUID(),
name: filename,
mimeType: inferMimeType(filename),
size: buffer.length,
sha256: crypto.createHash("sha256").update(buffer).digest("hex"),
path: filePath,
};
},
};
return context;
}
test.describe(${jsStringLiteral(scenario.name)}, () => {
test(${jsStringLiteral(scenario.name)}, async ({ page, context: browserContext }) => {
const stepOutputs: StepOutput[] = [];
const context = buildContext(page, browserContext, stepOutputs);
${stepBlocks}
});
});
`;
}
private zip(files: Record<string, string | Buffer>): Promise<Buffer> {
return new Promise((resolve, reject) => {
const archive = archiver("zip", { zlib: { level: 9 } });
const chunks: Buffer[] = [];
archive.on("data", (chunk: Buffer) => chunks.push(chunk));
archive.on("error", (err) => reject(err));
archive.on("end", () => resolve(Buffer.concat(chunks)));
for (const [name, content] of Object.entries(files)) {
archive.append(content, { name });
}
void archive.finalize();
});
}
}
@@ -39,6 +39,11 @@ export class ScenarioRunLogEntity {
@Column({ type: "text" })
message: string;
// Assigned in call order per run, since concurrent unawaited writes can
// otherwise persist out of order (createdAt alone isn't a reliable tiebreaker).
@Column({ type: "int", default: 0 })
seq: number;
@CreateDateColumn()
createdAt: Date;
}
@@ -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,
+156 -15
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()
@@ -46,6 +51,12 @@ export class ScenarioSchedulerService implements OnModuleInit {
private readonly runScenarioTimeouts = new Map<string, number | null>();
// Cache scenarioId per run
private readonly runScenarioIds = new Map<string, string>();
// 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;
@@ -66,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({
@@ -138,8 +152,10 @@ export class ScenarioSchedulerService implements OnModuleInit {
level: "log" | "warn" | "error",
message: string,
): void {
const seq = (this.runLogSeq.get(runId) ?? 0) + 1;
this.runLogSeq.set(runId, seq);
void this.runLogRepo.save(
this.runLogRepo.create({ runId, stepRunId, level, message }),
this.runLogRepo.create({ runId, stepRunId, level, message, seq }),
);
}
@@ -198,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),
@@ -216,6 +245,8 @@ 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)}`,
);
@@ -240,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) {
@@ -257,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" },
});
}
@@ -273,12 +302,16 @@ 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);
}
}
@@ -338,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,
@@ -424,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;
@@ -446,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(
@@ -463,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)
@@ -505,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()
@@ -527,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;
}
+113 -12
View File
@@ -5,6 +5,7 @@ import {
Get,
HttpCode,
Param,
ParseIntPipe,
ParseUUIDPipe,
Patch,
Post,
@@ -24,15 +25,19 @@ 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";
import { E2eExportService } from "./e2e-export.service";
@ApiTags("scenarios")
@Controller("scenarios")
export class ScenarioController {
constructor(private readonly scenarioService: ScenarioService) {}
constructor(
private readonly scenarioService: ScenarioService,
private readonly e2eExportService: E2eExportService,
) {}
// ── Scenarios ─────────────────────────────────────────────────────────────
@@ -47,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()
@@ -66,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 })
@@ -94,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 })
@@ -102,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
@@ -112,22 +147,40 @@ 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")
@ApiOperation({
summary: "Export a scenario as a standalone, plug-and-play Playwright e2e test (zip)",
})
@ApiResponse({ status: 200, description: "Zip archive" })
@ApiResponse({ status: 404, description: "Scenario not found" })
async exportE2eTest(
@Param("id", ParseUUIDPipe) id: string,
@Res() res: Response,
) {
const { filename, buffer } = await this.e2eExportService.buildE2eTestPackage(id);
res.setHeader("Content-Type", "application/zip");
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
res.setHeader("Content-Length", buffer.length);
res.send(buffer);
}
// ── Steps ─────────────────────────────────────────────────────────────────
@@ -178,6 +231,16 @@ export class ScenarioController {
return this.scenarioService.removeStep(id, stepId);
}
@Get(":id/used-snippets")
@ApiOperation({
summary: "List snippets referenced (directly or transitively) by this scenario's steps",
})
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
getUsedSnippets(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.getUsedSnippets(id);
}
// ── Credentials ───────────────────────────────────────────────────────────
@Get(":id/credentials")
@@ -245,6 +308,8 @@ export class ScenarioController {
id,
dto.environmentId,
dto.saveSession,
dto.traceMode,
dto.version,
);
}
@@ -260,6 +325,29 @@ export class ScenarioController {
return this.scenarioService.findRun(id, runId, q);
}
@Get(":id/run/:runId/logs/export")
@ApiOperation({ summary: "Export run logs as CSV or Markdown" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or run not found" })
async exportRunLogs(
@Param("id", ParseUUIDPipe) id: string,
@Param("runId", ParseUUIDPipe) runId: string,
@Query("format") format: string | undefined,
@Res({ passthrough: true }) res: Response,
) {
const fmt = format === "csv" ? "csv" : "md";
const content = await this.scenarioService.exportRunLogs(id, runId, fmt);
const contentType =
fmt === "csv" ? "text/csv; charset=utf-8" : "text/markdown; charset=utf-8";
const ext = fmt === "csv" ? "csv" : "md";
res.setHeader("Content-Type", contentType);
res.setHeader(
"Content-Disposition",
`attachment; filename="run-${runId}-logs.${ext}"`,
);
return content;
}
@Post(":id/run/:runId/wait")
@HttpCode(200)
@ApiOperation({
@@ -348,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;
+5 -2
View File
@@ -14,16 +14,19 @@ 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";
import { ScenarioService } from "./scenario.service";
import { E2eExportService } from "./e2e-export.service";
@Module({
imports: [
TypeOrmModule.forFeature([
ScenarioEntity,
ScenarioStepEntity,
ScenarioVersionEntity,
ScenarioRunEntity,
ScenarioRunStepEntity,
ScenarioRunLogEntity,
@@ -40,7 +43,7 @@ import { ScenarioService } from "./scenario.service";
FileModule,
],
controllers: [ScenarioController],
providers: [ScenarioService, ScenarioSchedulerService],
exports: [ScenarioService],
providers: [ScenarioService, ScenarioSchedulerService, E2eExportService],
exports: [ScenarioService, E2eExportService],
})
export class ScenarioModule {}
+357 -53
View File
@@ -3,20 +3,32 @@ 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 {
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";
import { SnippetService } from "../snippet/snippet.service";
import { AddScenarioCredentialDto } from "./dto/add-scenario-credential.dto";
import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
import { CreateScenarioDto } from "./dto/create-scenario.dto";
@@ -34,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";
@@ -41,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)
@@ -65,12 +123,97 @@ export class ScenarioService {
@InjectRepository(ScenarioRunFileEntity)
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(
@@ -138,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> {
@@ -178,6 +323,7 @@ export class ScenarioService {
}),
);
await this.normalizeStepOrder(scenarioId);
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, created.id);
}
@@ -236,6 +382,7 @@ export class ScenarioService {
await this.normalizeStepOrder(scenarioId);
}
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, stepId);
}
@@ -243,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 ──────────────────────────────────────────────────
@@ -332,6 +480,28 @@ export class ScenarioService {
return map;
}
/**
* 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<UsedSnippet[]> {
const scenario = await this.findOne(scenarioId);
const snippetMap = await this.snippetService.buildSnippetMap();
const usedRefs = resolveUsedSnippetAliases(
scenario.steps.map((s) => s.execCode ?? ""),
snippetMap,
).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 usedRefs.flatMap(({ alias, version }) => {
const snippet = byAlias.get(alias);
return snippet ? [{ ...snippet, pinnedVersion: version }] : [];
});
}
// ── Runs ──────────────────────────────────────────────────────────────────
async findRuns(
@@ -402,11 +572,49 @@ export class ScenarioService {
);
const logs = await this.runLogRepo.find({
where: q?.trim() ? { runId, message: Like(`%${q.trim()}%`) } : { runId },
order: { createdAt: "ASC" },
order: { seq: "ASC", createdAt: "ASC" },
});
return Object.assign(run, { logs });
}
async exportRunLogs(
scenarioId: string,
runId: string,
format: "csv" | "md",
): Promise<string> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOne({
where: { id: runId, scenarioId },
});
if (!run)
throw new NotFoundException(
`Run ${runId} not found in scenario ${scenarioId}`,
);
const logs = await this.runLogRepo.find({
where: { runId },
order: { seq: "ASC", createdAt: "ASC" },
});
if (format === "csv") {
const escapeCsv = (value: string): string =>
/[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value;
const rows = [
["Timestamp", "Level", "Message"].map(escapeCsv).join(","),
...logs.map((l) =>
[l.createdAt.toISOString(), l.level, l.message]
.map((v) => escapeCsv(String(v)))
.join(","),
),
];
return rows.join("\n");
}
const sections = logs.map(
(l) => `## [${l.level}] ${l.createdAt.toISOString()}\n\n${l.message}`,
);
return [`# Run ${runId} logs`, ...sections].join("\n\n");
}
async waitForRun(
scenarioId: string,
runId: string,
@@ -427,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,
});
@@ -446,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,
}),
@@ -472,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
@@ -521,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);
@@ -608,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) {
@@ -664,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);
@@ -717,7 +999,7 @@ export class ScenarioService {
scenarioId: string,
limit?: number,
offset?: number,
): Promise<any> {
): Promise<FileListResult> {
// Verify scenario exists
await this.findOne(scenarioId);
@@ -815,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 });
@@ -843,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;
Loaded 100 of 115 files, more files were not shown because too many files have changed in this diff. Show more