Merge pull request 'Add log context and data' (#2) from feat/run-log-context into master
CI / changes (push) Successful in 6s
CI / version (push) Successful in 7s
CI / server (push) Successful in 1m4s
CI / client (push) Successful in 1m22s
CI / image (push) Skipped

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
ars9 committed 2026-10-01 14:19:39 +03:00
commit d085ea3ded
17 files changed
+554 -82

No files matched your search

@@ -0,0 +1,121 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { LogOutput } from '../../src/ui/LogOutput/LogOutput';
const meta: Meta<typeof LogOutput> = {
title: 'UI/LogOutput',
component: LogOutput,
tags: ['autodocs'],
parameters: { layout: 'padded' },
};
export default meta;
type Story = StoryObj<typeof LogOutput>;
const secondsAgo = (n: number) => new Date(Date.now() - n * 1000).toISOString();
export const Default: Story = {
args: {
startTime: secondsAgo(60),
entries: [
{
id: 0,
level: 'debug',
context: 'step:1',
message: 'Resolved credential alias "sdJwtKey" to a file credential',
createdAt: secondsAgo(61),
},
{
id: 1,
level: 'log',
context: 'step:1',
message: 'Step 1 started: Login with PKCS',
createdAt: secondsAgo(60),
},
{
id: 2,
level: 'log',
context: 'snippet:loginWithPkcs',
message: 'Snippet "loginWithPkcs" started',
createdAt: secondsAgo(58),
},
{
id: 3,
level: 'log',
context: 'snippet:loginWithPkcs',
message: 'Saved credential as run file:',
data: JSON.stringify({
id: '181d8ae2-4aee-4f33-a657-210efe7fe668',
name: 'credential.sd-jwt',
mimeType: 'application/octet-stream',
size: 1657,
sha256: '9c4226999c700a39bda30279907e4479a5b3920ccde5c75771ea0e264c638c0',
path: 'data/files/18/181d/181d8ae2-4aee-4f33-a657-210efe7fe668',
}),
createdAt: secondsAgo(57),
},
{
id: 4,
level: 'log',
context: 'snippet:loginWithPkcs',
message: 'Navigating to ID login: https://id.example.com',
createdAt: secondsAgo(57),
},
{
id: 5,
level: 'log',
context: 'assertion:pass',
message: 'Assertion passed: page must have redirected to cabinet URL',
createdAt: secondsAgo(40),
},
{
id: 6,
level: 'warn',
context: 'snippet:loginWithPkcs',
message: 'Retrying file upload after a transient error',
createdAt: secondsAgo(39),
},
{
id: 7,
level: 'log',
context: 'step:1',
message: 'Step 1 passed',
createdAt: secondsAgo(30),
},
{
id: 8,
level: 'error',
context: 'assertion:fail',
message: 'Assertion failed: Document validation must pass (got 422)',
createdAt: secondsAgo(10),
},
{
id: 9,
level: 'error',
context: 'step:2',
message: 'Step 2 failed: Document validation must pass (got 422)',
createdAt: secondsAgo(9),
},
],
},
};
export const Scrollable: Story = {
args: {
maxHeight: 160,
startTime: secondsAgo(60),
entries: Array.from({ length: 30 }, (_, i) => ({
id: i,
level: (['log', 'log', 'log', 'warn', 'error'] as const)[i % 5],
context: `step:${Math.floor(i / 6) + 1}`,
message: `Log entry ${i + 1}`,
createdAt: secondsAgo(60 - i * 2),
})),
},
};
export const Empty: Story = {
args: {
entries: [],
emptyMessage: 'No logs yet.',
},
};
+5 -1
View File
@@ -147,7 +147,7 @@ export interface RunDefaults {
export type RunStepStatus = 'waiting' | 'pending' | 'in_progress' | 'pass' | 'fail' | 'cancelled';
export type LogLevel = 'log' | 'warn' | 'error';
export type LogLevel = 'debug' | 'log' | 'warn' | 'error';
export interface ScenarioRun {
id: string;
@@ -189,7 +189,11 @@ export interface ScenarioRunLog {
runId: string;
stepRunId: string | null;
level: LogLevel;
/** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */
context: string | null;
message: string;
/** JSON payload attached to the message (e.g. a created file's metadata), as a raw string. */
data: string | null;
createdAt: string;
}
+12 -45
View File
@@ -7,10 +7,8 @@ import type {
Scenario,
ScenarioRunDetail,
ScenarioRunStep,
ScenarioRunLog,
ScenarioRunStatus,
RunStepStatus,
LogLevel,
FileMetadata,
} from '../../api';
import { scenarios } from '../../api';
@@ -27,7 +25,7 @@ import {
Card,
CodeBlock,
DescriptionList,
Pagination,
LogOutput,
Search,
Table,
Timestamp,
@@ -70,12 +68,6 @@ const STEP_STATUS_VARIANT: Record<RunStepStatus, BadgeVariant> = {
cancelled: 'warning',
};
const LOG_LEVEL_VARIANT: Record<LogLevel, BadgeVariant> = {
log: 'neutral',
warn: 'warning',
error: 'error',
};
const FINAL: ScenarioRunStatus[] = ['pass', 'fail'];
export function RunDetailPage() {
@@ -97,7 +89,6 @@ function RunDetail() {
const [error, setError] = useState<string | null>(null);
const [polling, setPolling] = useState(false);
const [pulseKey, setPulseKey] = useState(0);
const [logPage, setLogPage] = useState(1);
const [logSearch, setLogSearch] = useState('');
const [debouncedSearch, setDebouncedSearch] = useState('');
const [expandAll, setExpandAll] = useState(false);
@@ -112,7 +103,6 @@ function RunDetail() {
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);
const stopPolling = () => {
@@ -221,7 +211,6 @@ function RunDetail() {
const t = setTimeout(() => {
setDebouncedSearch(logSearch);
logSearchRef.current = logSearch;
setLogPage(1);
}, 300);
return () => clearTimeout(t);
}, [logSearch]);
@@ -400,26 +389,6 @@ function RunDetail() {
},
];
const logColumns: TableColumn<ScenarioRunLog>[] = [
{
key: 'level',
header: t('runs.log_level'),
width: 70,
render: (l) => <Badge variant={LOG_LEVEL_VARIANT[l.level]}>{l.level}</Badge>,
},
{
key: 'message',
header: t('runs.log_message'),
render: (l) => <code style={{ wordBreak: 'break-word' }}>{l.message}</code>,
},
{
key: 'time',
header: t('runs.log_time'),
width: 160,
render: (l) => <Timestamp value={l.createdAt} />,
},
];
return (
<div>
<div className={styles.pageToolbar}>
@@ -560,21 +529,19 @@ function RunDetail() {
</Button>
</div>
</div>
<Table
columns={logColumns}
data={run.logs.slice((logPage - 1) * LOG_PAGE_SIZE, logPage * LOG_PAGE_SIZE)}
rowKey={(l) => l.id}
loading={false}
<LogOutput
startTime={run.createdAt}
maxHeight={480}
entries={run.logs.map((l) => ({
id: l.id,
level: l.level,
context: l.context,
message: l.message,
data: l.data,
createdAt: l.createdAt,
}))}
emptyMessage=""
/>
{run.logs.length > LOG_PAGE_SIZE && (
<Pagination
page={logPage}
pageSize={LOG_PAGE_SIZE}
total={run.logs.length}
onPageChange={setLogPage}
/>
)}
</div>
)}
@@ -0,0 +1,122 @@
.root {
display: flex;
flex-direction: column;
background: var(--color-bg-subtle);
border: var(--border-width) solid var(--color-border);
border-radius: var(--radius-md);
overflow: auto;
font-family: var(--font-mono, ui-monospace, 'Cascadia Code', 'Fira Code', monospace);
font-size: var(--font-size-xs);
}
.empty {
padding: var(--space-4);
color: var(--color-text-muted);
font-size: var(--font-size-sm);
}
.line {
display: flex;
flex-direction: column;
gap: var(--space-1);
padding: var(--space-1) var(--space-3);
border-left: 3px solid transparent;
border-bottom: 1px solid var(--color-border);
}
.line:last-child {
border-bottom: none;
}
.lineHeader {
display: flex;
align-items: baseline;
flex-wrap: wrap;
gap: var(--space-2);
}
.data {
margin: 0 0 var(--space-1) calc(7em + var(--space-2));
border: var(--border-width) solid var(--color-border);
border-radius: var(--radius-sm);
background: var(--color-bg);
max-width: 100%;
overflow-x: auto;
}
.data > * {
margin: 0;
padding: var(--space-2) var(--space-3);
}
.timestamp {
flex: 0 0 auto;
min-width: 7em;
text-align: right;
color: var(--color-text-muted);
white-space: nowrap;
}
.level {
flex: 0 0 auto;
width: 4.5em;
text-transform: uppercase;
font-weight: 700;
}
.context {
flex: 0 0 auto;
color: var(--color-secondary-fg);
}
.context::before {
content: '[';
}
.context::after {
content: ']';
}
.message {
flex: 1 1 320px;
color: var(--color-text);
white-space: pre-wrap;
word-break: break-word;
}
.debug {
border-left-color: var(--color-border);
}
.debug .level,
.debug .message,
.debug .timestamp,
.debug .context {
color: var(--color-text-faint);
}
.log {
border-left-color: var(--color-border);
}
.log .level {
color: var(--color-text-muted);
}
.warn {
border-left-color: var(--color-warning-fg);
background: var(--color-warning-bg);
}
.warn .level {
color: var(--color-warning-fg);
}
.error {
border-left-color: var(--color-error-fg);
background: var(--color-error-bg);
}
.error .level {
color: var(--color-error-fg);
}
+85
View File
@@ -0,0 +1,85 @@
import { CodeBlock } from '../CodeBlock/CodeBlock';
import styles from './LogOutput.module.css';
export type LogOutputLevel = 'debug' | 'log' | 'warn' | 'error';
export interface LogOutputEntry {
id: string | number;
level: LogOutputLevel;
/** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */
context?: string | null;
message: string;
/** JSON payload attached to the message, as a raw string; rendered pretty-printed, separately from the message. */
data?: string | null;
createdAt: string | number | Date;
}
export interface LogOutputProps {
entries: LogOutputEntry[];
/** Reference point for the elapsed-time column; defaults to the first entry's timestamp. */
startTime?: string | number | Date;
/** Caps the block's height (e.g. "400px"), scrolling once entries overflow it. */
maxHeight?: string | number;
emptyMessage?: string;
className?: string;
}
/** "+12.345s" (or "-" if before startTime, e.g. clock skew). */
function formatElapsed(deltaMs: number): string {
const sign = deltaMs < 0 ? '-' : '+';
return `${sign}${(Math.abs(deltaMs) / 1000).toFixed(3)}s`;
}
/** Pretty-prints `data` if it's valid JSON, otherwise returns it as-is. */
function prettyData(data: string): string {
try {
return JSON.stringify(JSON.parse(data), null, 2);
} catch {
return data;
}
}
/** Terminal-style log viewer: one line per entry, color-coded by level. */
export function LogOutput({
entries,
startTime,
maxHeight,
emptyMessage = 'No logs.',
className,
}: LogOutputProps) {
if (entries.length === 0) {
return <div className={styles.empty}>{emptyMessage}</div>;
}
const startMs = new Date(startTime ?? entries[0].createdAt).getTime();
return (
<div
className={[styles.root, className].filter(Boolean).join(' ')}
style={maxHeight !== undefined ? { maxHeight } : undefined}
role="log"
tabIndex={0}
>
{entries.map((entry) => {
const entryMs = new Date(entry.createdAt).getTime();
return (
<div key={entry.id} className={[styles.line, styles[entry.level]].join(' ')}>
<div className={styles.lineHeader}>
<span className={styles.timestamp} title={new Date(entryMs).toISOString()}>
{formatElapsed(entryMs - startMs)}
</span>
<span className={styles.level}>{entry.level}</span>
{entry.context && <span className={styles.context}>{entry.context}</span>}
<span className={styles.message}>{entry.message}</span>
</div>
{entry.data && (
<div className={styles.data}>
<CodeBlock code={prettyData(entry.data)} language="json" />
</div>
)}
</div>
);
})}
</div>
);
}
+3
View File
@@ -69,3 +69,6 @@ export type { TableProps, TableColumn, SortDirection } from './Table/Table';
export { UuidBadge } from './UuidBadge/UuidBadge';
export type { UuidBadgeProps } from './UuidBadge/UuidBadge';
export { LogOutput } from './LogOutput/LogOutput';
export type { LogOutputProps, LogOutputEntry, LogOutputLevel } from './LogOutput/LogOutput';
+2
View File
@@ -46,6 +46,7 @@
--color-text: #2D3339;
--color-text-muted: #634DBD; /* Kimberly, a touch darker for 4.5:1 on bg-subtle */
--color-text-faint: #5C6063; /* desaturated grey, ~4.5:1 on bg-subtle - as washed out as WCAG AA allows */
--color-border: #AFBFB9; /* Powder Ash */
--color-bg: #E6EDEA; /* Powder Ash tint */
--color-bg-subtle: #D8E2DE; /* Powder Ash tint, 1 step darker */
@@ -158,6 +159,7 @@ a:hover {
--color-text: #E2EAE6;
--color-text-muted: #A9A3C9; /* lightened Kimberly */
--color-text-faint: #9A9A9A; /* desaturated grey, ~4.5:1 on dark bg-subtle - as washed out as WCAG AA allows */
--color-border: #3D4850;
--color-bg: #1A1F24; /* deeper than Outer Space */
--color-bg-subtle: #2D3339; /* Outer Space */
+14 -3
View File
@@ -34,7 +34,7 @@ Available on `context`:
- `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.log(...args)`, `context.warn(...args)`, `context.error(...args)`, `context.debug(...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)`: 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
@@ -42,12 +42,23 @@ Available on `context`:
Also in scope:
- `console`: proxied to run logs (`log`, `warn`, `error`, etc.)
- `console`: proxied to run logs (`log`/`info` → `log`, `warn`, `error`, `debug`)
- `expect`: Playwright's `expect` (e.g. `await expect(context.page.locator("h1")).toHaveText("Done")`)
Code is syntax-checked before it runs, and executes in the server process: it's trusted code, not sandboxed.
Step run logs also include automatic "Step N started / passed / failed" entries.
## Run logs
Each run log entry (`level`, `message`, `createdAt`) also carries a `context`: where the message was generated. Contexts:
- `step:<N>`: the step a log entry belongs to, including the automatic "Step N started / passed / failed" entries
- `snippet:<alias>`: entries logged while `context.runSnippet(alias, ...)` is running, including its own automatic "started"/"finished"/"failed" entries. A snippet calling another snippet tags entries with the innermost running snippet's alias
- `assertion:pass` / `assertion:fail`: `context.assert()`'s own log entry
- `run`: run-level entries not tied to a step (e.g. a scenario timeout, or the Playwright trace upload notice)
`context.log`/`warn`/`error` (and `console.log`/etc.) split their arguments: strings/numbers/booleans are joined into `message`, while object/array arguments are kept out of it and stored as `data` (JSON), so `message` stays a stable string regardless of an object's shape. With one object argument `data` is that object; with more than one, `data` is an array of them. E.g. `context.log("Saved credential as run file:", fileMeta)` logs `message: "Saved credential as run file:"` with `fileMeta` as `data`, shown pretty-printed and separately from the message in the UI.
`GET /scenarios/:id/runs/:runId?q=...` and the exported CSV/Markdown logs (`GET /scenarios/:id/runs/:runId/export`) match `q` against both `message` and `context`.
## Versioning
@@ -19,8 +19,12 @@ export interface ExecResult {
}
export type ScriptLogger = (
level: "log" | "warn" | "error",
level: "debug" | "log" | "warn" | "error",
message: string,
/** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */
context?: string | null,
/** Structured payload attached to the message (e.g. a created file's metadata). */
data?: unknown,
) => void;
/** The `context` object available inside user scripts as the sole argument. */
@@ -42,6 +46,7 @@ export interface ScriptContext {
log: (...args: unknown[]) => void;
warn: (...args: unknown[]) => void;
error: (...args: unknown[]) => void;
debug: (...args: unknown[]) => void;
/**
* Runs `fn` and requires it to return `true`. Logs the outcome under
* `description`, then throws if `fn` returned anything other than `true`
@@ -70,6 +75,8 @@ export interface ExecContext {
browser: BrowserContext;
code: string;
log?: ScriptLogger;
/** Base log context for this execution, e.g. "step:2". */
logContext?: string | null;
getStepOutput?: (order: number) => Promise<unknown>;
credentials?: Record<string, unknown>;
environment?: EnvironmentData | null;
@@ -110,6 +117,7 @@ export class CodeExecutorService {
browser,
code,
log,
logContext,
getStepOutput,
credentials,
environment,
@@ -120,10 +128,44 @@ export class CodeExecutorService {
} = ctx;
const scriptLog: ScriptLogger =
log ?? ((level, msg) => this.logger[level](msg));
const toStr = (args: unknown[]) =>
args
.map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a)))
.join(" ");
// Splits console/log args into a plain-text message (strings, numbers, ...)
// and a structured data payload (objects/arrays), logged separately so the
// message stays a stable string regardless of the object's shape.
const splitLogArgs = (
args: unknown[],
): { message: string; data: unknown } => {
const strings: string[] = [];
const objects: unknown[] = [];
for (const a of args) {
if (a !== null && typeof a === "object") {
objects.push(a);
} else {
strings.push(String(a));
}
}
return {
message: strings.join(" "),
data: objects.length === 0 ? null : objects.length === 1 ? objects[0] : objects,
};
};
// Innermost running snippet (if any) tags log entries; falls back to
// logContext (e.g. the current step) outside of any runSnippet call.
const contextStack: string[] = logContext ? [logContext] : [];
const currentContext = (): string | null =>
contextStack.length > 0 ? contextStack[contextStack.length - 1] : null;
const emit = (
level: "debug" | "log" | "warn" | "error",
message: string,
contextOverride?: string,
data?: unknown,
): void =>
scriptLog(level, message, contextOverride ?? currentContext(), data);
const emitArgs = (level: "debug" | "log" | "warn" | "error", args: unknown[]): void => {
const { message, data } = splitLogArgs(args);
emit(level, message, undefined, data);
};
const credMap: Record<string, unknown> = credentials ?? {};
const envData: EnvironmentData = environment ?? {};
@@ -133,11 +175,11 @@ export class CodeExecutorService {
let scriptContext: ScriptContext;
const fakeConsole = {
log: (...args: unknown[]) => scriptLog("log", toStr(args)),
warn: (...args: unknown[]) => scriptLog("warn", toStr(args)),
error: (...args: unknown[]) => scriptLog("error", toStr(args)),
info: (...args: unknown[]) => scriptLog("log", toStr(args)),
debug: (...args: unknown[]) => scriptLog("log", toStr(args)),
log: (...args: unknown[]) => emitArgs("log", args),
warn: (...args: unknown[]) => emitArgs("warn", args),
error: (...args: unknown[]) => emitArgs("error", args),
info: (...args: unknown[]) => emitArgs("log", args),
debug: (...args: unknown[]) => emitArgs("debug", args),
};
try {
@@ -145,9 +187,10 @@ export class CodeExecutorService {
page,
browser,
dumpDom: (selector?: string) => dumpDom(page, selector),
log: (...args: unknown[]) => scriptLog("log", toStr(args)),
warn: (...args: unknown[]) => scriptLog("warn", toStr(args)),
error: (...args: unknown[]) => scriptLog("error", toStr(args)),
log: (...args: unknown[]) => emitArgs("log", args),
warn: (...args: unknown[]) => emitArgs("warn", args),
error: (...args: unknown[]) => emitArgs("error", args),
debug: (...args: unknown[]) => emitArgs("debug", args),
assert: async (
fn: () => boolean | Promise<boolean>,
description: string,
@@ -161,13 +204,13 @@ export class CodeExecutorService {
failureReason = (err as Error).message;
}
if (passed) {
scriptLog("log", `Assertion passed: ${description}`);
emit("log", description, "assertion:pass");
return;
}
const message = failureReason
? `Assertion failed: ${description} (${failureReason})`
: `Assertion failed: ${description}`;
scriptLog("error", message);
emit("error", message, "assertion:fail");
throw new Error(message);
},
getStepOutput: getStepOutput ?? (() => Promise.resolve(null)),
@@ -197,7 +240,9 @@ export class CodeExecutorService {
if (snippetCode == null) {
throw new Error(`Snippet alias "${alias}" not found`);
}
scriptLog("log", `Snippet "${alias}" started`);
const snippetContext = `snippet:${alias}`;
contextStack.push(snippetContext);
emit("log", `Snippet "${alias}" started`, snippetContext);
const snippetFn = new Function(
"context",
"console",
@@ -212,14 +257,17 @@ export class CodeExecutorService {
args,
playwrightExpect,
);
scriptLog("log", `Snippet "${alias}" finished`);
emit("log", `Snippet "${alias}" finished`, snippetContext);
return snippetResult;
} catch (err) {
scriptLog(
emit(
"error",
`Snippet "${alias}" failed: ${(err as Error).message}`,
snippetContext,
);
throw err;
} finally {
contextStack.pop();
}
},
getScenarioFiles: async (opts?: { limit?: number; offset?: number }) => {
@@ -26,6 +26,11 @@ export class ExecContextBuilder {
return this;
}
logContext(logContext: string): this {
this.ctx.logContext = logContext;
return this;
}
getStepOutput(fn: (order: number) => Promise<unknown>): this {
this.ctx.getStepOutput = fn;
return this;
@@ -0,0 +1,31 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddRunLogContext1790848194828 implements MigrationInterface {
name = "AddRunLogContext1790848194828";
public async up(queryRunner: QueryRunner): Promise<void> {
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, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, 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", "createdAt", "seq") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq" 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"`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
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, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), 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 "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq" FROM "temporary_scenario_run_logs"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`);
}
}
@@ -0,0 +1,31 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddRunLogData1790850734763 implements MigrationInterface {
name = "AddRunLogData1790850734763";
public async up(queryRunner: QueryRunner): Promise<void> {
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, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, "data" text, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "temporary_scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context" 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"`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
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, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`,
);
await queryRunner.query(
`INSERT INTO "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context" FROM "temporary_scenario_run_logs"`,
);
await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`);
}
}
+7 -1
View File
@@ -1,7 +1,13 @@
import { Initial1790793447827 } from "./1790793447827-Initial";
import { AddRunLogContext1790848194828 } from "./1790848194828-AddRunLogContext";
import { AddRunLogData1790850734763 } from "./1790850734763-AddRunLogData";
/**
* 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];
export const MIGRATIONS = [
Initial1790793447827,
AddRunLogContext1790848194828,
AddRunLogData1790850734763,
];
+10 -1
View File
@@ -9,7 +9,7 @@ import {
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario-run.entity";
export type LogLevel = "log" | "warn" | "error";
export type LogLevel = "debug" | "log" | "warn" | "error";
@Entity("scenario_run_logs")
export class ScenarioRunLogEntity {
@@ -36,9 +36,18 @@ export class ScenarioRunLogEntity {
@Column({ type: "text", default: "log" })
level: LogLevel;
// Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass"
@Column({ type: "text", nullable: true })
context: string | null;
@Column({ type: "text" })
message: string;
// JSON payload attached to the message (e.g. a created file's metadata),
// kept separate so message stays a stable, human-readable string.
@Column({ type: "text", nullable: true })
data: string | null;
// 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 })
@@ -143,26 +143,36 @@ export class ScenarioSchedulerService implements OnModuleInit {
statuses: ["pending", "waiting"],
})
.execute();
this.persistLog(runId, null, "error", reason);
this.persistLog(runId, null, "error", reason, "run");
}
private persistLog(
runId: string,
stepRunId: string | null,
level: "log" | "warn" | "error",
level: "debug" | "log" | "warn" | "error",
message: string,
context: string | null = null,
data: unknown = null,
): 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, seq }),
this.runLogRepo.create({
runId,
stepRunId,
level,
message,
context,
data: data != null ? JSON.stringify(data) : null,
seq,
}),
);
}
private stepLogger(stepRunId: string, runId: string): ScriptLogger {
return (level, msg) => {
return (level, msg, context, data) => {
this.logger[level](`StepRun #${stepRunId} script: ${msg}`);
this.persistLog(runId, stepRunId, level, msg);
this.persistLog(runId, stepRunId, level, msg, context ?? null, data ?? null);
};
}
@@ -384,6 +394,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.id,
"log",
`Step ${step.order} started${step.title ? `: ${step.title}` : ""}`,
`step:${step.order}`,
);
try {
@@ -402,6 +413,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
.browser(context)
.code(step.execCode)
.log(this.stepLogger(stepRun.id, stepRun.runId))
.logContext(`step:${step.order}`)
.getStepOutput(getStepOutput)
.credentials(creds)
.environment(env)
@@ -527,6 +539,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
null,
"log",
"Playwright trace uploaded to run files (open with `npx playwright show-trace trace.zip` or https://trace.playwright.dev)",
"run",
);
} catch (err) {
this.logger.warn(
@@ -557,6 +570,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.id,
"log",
`Step ${stepRun.order} passed`,
`step:${stepRun.order}`,
);
// Find the next waiting step in this run (next by order)
@@ -599,6 +613,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.id,
"error",
`Step ${stepRun.order} failed: ${description}`,
`step:${stepRun.order}`,
);
await this.dumpPageOnFailure(stepRun);
@@ -666,6 +681,7 @@ export class ScenarioSchedulerService implements OnModuleInit {
stepRun.id,
"log",
`Step ${stepRun.order} failed: page dump uploaded to run files`,
`step:${stepRun.order}`,
);
}
}
+15 -6
View File
@@ -578,7 +578,12 @@ export class ScenarioService implements OnModuleInit {
`Run ${runId} not found in scenario ${scenarioId}`,
);
const logs = await this.runLogRepo.find({
where: q?.trim() ? { runId, message: Like(`%${q.trim()}%`) } : { runId },
where: q?.trim()
? [
{ runId, message: Like(`%${q.trim()}%`) },
{ runId, context: Like(`%${q.trim()}%`) },
]
: { runId },
order: { seq: "ASC", createdAt: "ASC" },
});
return Object.assign(run, { logs });
@@ -606,9 +611,9 @@ export class ScenarioService implements OnModuleInit {
const escapeCsv = (value: string): string =>
/[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value;
const rows = [
["Timestamp", "Level", "Message"].map(escapeCsv).join(","),
["Timestamp", "Level", "Context", "Message", "Data"].map(escapeCsv).join(","),
...logs.map((l) =>
[l.createdAt.toISOString(), l.level, l.message]
[l.createdAt.toISOString(), l.level, l.context ?? "", l.message, l.data ?? ""]
.map((v) => escapeCsv(String(v)))
.join(","),
),
@@ -616,9 +621,13 @@ export class ScenarioService implements OnModuleInit {
return rows.join("\n");
}
const sections = logs.map(
(l) => `## [${l.level}] ${l.createdAt.toISOString()}\n\n${l.message}`,
);
const sections = logs.map((l) => {
const header = `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}`;
const data = l.data
? `\n\n\`\`\`json\n${JSON.stringify(JSON.parse(l.data), null, 2)}\n\`\`\``
: "";
return `${header}\n\n${l.message}${data}`;
});
return [`# Run ${runId} logs`, ...sections].join("\n\n");
}
+4 -2
View File
@@ -48,12 +48,14 @@ describe("Database migrations", () => {
}
});
it("can revert the initial migration", async () => {
it("can revert all migrations", async () => {
const dataSource = new DataSource(dataSourceOptions(":memory:"));
await dataSource.initialize();
try {
await dataSource.runMigrations();
await dataSource.undoLastMigration();
for (let i = 0; i < MIGRATIONS.length; i++) {
await dataSource.undoLastMigration();
}
const tables = await dataSource.query(
`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT IN ('migrations', 'sqlite_sequence')`,
);