From 6cba785e2fa55b74af0dfe81eb1c966aeb74db11 Mon Sep 17 00:00:00 2001 From: Andrii Arsenin Date: Thu, 1 Oct 2026 13:26:07 +0300 Subject: [PATCH] feat(scenario): tag run log entries with their context - scenario_run_logs gains a context column (step:N, snippet:alias, assertion:pass/fail, run), tracked through a call stack in CodeExecutorService so nested runSnippet calls tag their own and inner log lines - automatic step started/passed/failed entries and the Playwright trace upload notice are tagged accordingly - run log export (CSV/Markdown) and the logs search match context as well as message - add a LogOutput UI component: a terminal-style log viewer with level color coding, elapsed-time-since-start timestamps and a context tag, replacing the generic Table on the run detail page - show the full log list instead of paginating it, capped to a scrollable block - fix migrations.spec.ts's revert test, which only undid the last migration instead of all of them --- .../.storybook/stories/LogOutput.stories.tsx | 99 +++++++++++++++++++ client/src/api/types.ts | 2 + client/src/pages/run/RunDetailPage.tsx | 56 +++-------- client/src/ui/LogOutput/LogOutput.module.css | 91 +++++++++++++++++ client/src/ui/LogOutput/LogOutput.tsx | 66 +++++++++++++ client/src/ui/index.ts | 3 + docs/scenario.md | 11 ++- .../code-executor/code-executor.service.ts | 47 ++++++--- .../src/code-executor/exec-context.builder.ts | 5 + .../1790848194828-AddRunLogContext.ts | 31 ++++++ server/src/database/migrations/index.ts | 3 +- .../src/scenario/scenario-run-log.entity.ts | 4 + .../scenario/scenario-scheduler.service.ts | 15 ++- server/src/scenario/scenario.service.ts | 14 ++- server/test/migrations.spec.ts | 6 +- 15 files changed, 383 insertions(+), 70 deletions(-) create mode 100644 client/.storybook/stories/LogOutput.stories.tsx create mode 100644 client/src/ui/LogOutput/LogOutput.module.css create mode 100644 client/src/ui/LogOutput/LogOutput.tsx create mode 100644 server/src/database/migrations/1790848194828-AddRunLogContext.ts diff --git a/client/.storybook/stories/LogOutput.stories.tsx b/client/.storybook/stories/LogOutput.stories.tsx new file mode 100644 index 0000000..067da07 --- /dev/null +++ b/client/.storybook/stories/LogOutput.stories.tsx @@ -0,0 +1,99 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; +import { LogOutput } from '../../src/ui/LogOutput/LogOutput'; + +const meta: Meta = { + title: 'UI/LogOutput', + component: LogOutput, + tags: ['autodocs'], + parameters: { layout: 'padded' }, +}; +export default meta; + +type Story = StoryObj; + +const secondsAgo = (n: number) => new Date(Date.now() - n * 1000).toISOString(); + +export const Default: Story = { + args: { + startTime: secondsAgo(60), + entries: [ + { + 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: 'Navigating to ID login: https://id.example.com', + createdAt: secondsAgo(57), + }, + { + id: 4, + level: 'log', + context: 'assertion:pass', + message: 'Assertion passed: page must have redirected to cabinet URL', + createdAt: secondsAgo(40), + }, + { + id: 5, + level: 'warn', + context: 'snippet:loginWithPkcs', + message: 'Retrying file upload after a transient error', + createdAt: secondsAgo(39), + }, + { + id: 6, + level: 'log', + context: 'step:1', + message: 'Step 1 passed', + createdAt: secondsAgo(30), + }, + { + id: 7, + level: 'error', + context: 'assertion:fail', + message: 'Assertion failed: Document validation must pass (got 422)', + createdAt: secondsAgo(10), + }, + { + id: 8, + 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.', + }, +}; diff --git a/client/src/api/types.ts b/client/src/api/types.ts index 80677ae..1a61646 100644 --- a/client/src/api/types.ts +++ b/client/src/api/types.ts @@ -189,6 +189,8 @@ 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; createdAt: string; } diff --git a/client/src/pages/run/RunDetailPage.tsx b/client/src/pages/run/RunDetailPage.tsx index 22e02b8..1f7cb28 100644 --- a/client/src/pages/run/RunDetailPage.tsx +++ b/client/src/pages/run/RunDetailPage.tsx @@ -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 = { cancelled: 'warning', }; -const LOG_LEVEL_VARIANT: Record = { - log: 'neutral', - warn: 'warning', - error: 'error', -}; - const FINAL: ScenarioRunStatus[] = ['pass', 'fail']; export function RunDetailPage() { @@ -97,7 +89,6 @@ function RunDetail() { const [error, setError] = useState(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 | 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[] = [ - { - key: 'level', - header: t('runs.log_level'), - width: 70, - render: (l) => {l.level}, - }, - { - key: 'message', - header: t('runs.log_message'), - render: (l) => {l.message}, - }, - { - key: 'time', - header: t('runs.log_time'), - width: 160, - render: (l) => , - }, - ]; - return (
@@ -560,21 +529,18 @@ function RunDetail() {
- l.id} - loading={false} + ({ + id: l.id, + level: l.level, + context: l.context, + message: l.message, + createdAt: l.createdAt, + }))} emptyMessage="" /> - {run.logs.length > LOG_PAGE_SIZE && ( - - )} )} diff --git a/client/src/ui/LogOutput/LogOutput.module.css b/client/src/ui/LogOutput/LogOutput.module.css new file mode 100644 index 0000000..eb3a93d --- /dev/null +++ b/client/src/ui/LogOutput/LogOutput.module.css @@ -0,0 +1,91 @@ +.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; + align-items: baseline; + flex-wrap: wrap; + gap: var(--space-2); + 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; +} + +.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; +} + +.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); +} diff --git a/client/src/ui/LogOutput/LogOutput.tsx b/client/src/ui/LogOutput/LogOutput.tsx new file mode 100644 index 0000000..acdca32 --- /dev/null +++ b/client/src/ui/LogOutput/LogOutput.tsx @@ -0,0 +1,66 @@ +import styles from './LogOutput.module.css'; + +export type LogOutputLevel = '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; + 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`; +} + +/** 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
{emptyMessage}
; + } + + const startMs = new Date(startTime ?? entries[0].createdAt).getTime(); + + return ( +
+ {entries.map((entry) => { + const entryMs = new Date(entry.createdAt).getTime(); + return ( +
+ + {formatElapsed(entryMs - startMs)} + + {entry.level} + {entry.context && {entry.context}} + {entry.message} +
+ ); + })} +
+ ); +} diff --git a/client/src/ui/index.ts b/client/src/ui/index.ts index b57f21d..7e98da9 100644 --- a/client/src/ui/index.ts +++ b/client/src/ui/index.ts @@ -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'; diff --git a/docs/scenario.md b/docs/scenario.md index 91ce5cf..e861e2a 100644 --- a/docs/scenario.md +++ b/docs/scenario.md @@ -47,7 +47,16 @@ Also in scope: 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:`: the step a log entry belongs to, including the automatic "Step N started / passed / failed" entries +- `snippet:`: 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) + +`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 diff --git a/server/src/code-executor/code-executor.service.ts b/server/src/code-executor/code-executor.service.ts index 7c56cde..80c5cd9 100644 --- a/server/src/code-executor/code-executor.service.ts +++ b/server/src/code-executor/code-executor.service.ts @@ -21,6 +21,8 @@ export interface ExecResult { export type ScriptLogger = ( level: "log" | "warn" | "error", message: string, + /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ + context?: string | null, ) => void; /** The `context` object available inside user scripts as the sole argument. */ @@ -70,6 +72,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; credentials?: Record; environment?: EnvironmentData | null; @@ -110,6 +114,7 @@ export class CodeExecutorService { browser, code, log, + logContext, getStepOutput, credentials, environment, @@ -125,6 +130,17 @@ export class CodeExecutorService { .map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))) .join(" "); + // 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: "log" | "warn" | "error", + message: string, + contextOverride?: string, + ): void => scriptLog(level, message, contextOverride ?? currentContext()); + const credMap: Record = credentials ?? {}; const envData: EnvironmentData = environment ?? {}; const snippetMap: Record = snippets ?? {}; @@ -133,11 +149,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[]) => emit("log", toStr(args)), + warn: (...args: unknown[]) => emit("warn", toStr(args)), + error: (...args: unknown[]) => emit("error", toStr(args)), + info: (...args: unknown[]) => emit("log", toStr(args)), + debug: (...args: unknown[]) => emit("log", toStr(args)), }; try { @@ -145,9 +161,9 @@ 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[]) => emit("log", toStr(args)), + warn: (...args: unknown[]) => emit("warn", toStr(args)), + error: (...args: unknown[]) => emit("error", toStr(args)), assert: async ( fn: () => boolean | Promise, description: string, @@ -161,13 +177,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 +213,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 +230,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 }) => { diff --git a/server/src/code-executor/exec-context.builder.ts b/server/src/code-executor/exec-context.builder.ts index bcb540a..56d4322 100644 --- a/server/src/code-executor/exec-context.builder.ts +++ b/server/src/code-executor/exec-context.builder.ts @@ -26,6 +26,11 @@ export class ExecContextBuilder { return this; } + logContext(logContext: string): this { + this.ctx.logContext = logContext; + return this; + } + getStepOutput(fn: (order: number) => Promise): this { this.ctx.getStepOutput = fn; return this; diff --git a/server/src/database/migrations/1790848194828-AddRunLogContext.ts b/server/src/database/migrations/1790848194828-AddRunLogContext.ts new file mode 100644 index 0000000..ba07440 --- /dev/null +++ b/server/src/database/migrations/1790848194828-AddRunLogContext.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddRunLogContext1790848194828 implements MigrationInterface { + name = "AddRunLogContext1790848194828"; + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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"`); + } +} diff --git a/server/src/database/migrations/index.ts b/server/src/database/migrations/index.ts index 1337591..e096d6e 100644 --- a/server/src/database/migrations/index.ts +++ b/server/src/database/migrations/index.ts @@ -1,7 +1,8 @@ import { Initial1790793447827 } from "./1790793447827-Initial"; +import { AddRunLogContext1790848194828 } from "./1790848194828-AddRunLogContext"; /** * 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]; diff --git a/server/src/scenario/scenario-run-log.entity.ts b/server/src/scenario/scenario-run-log.entity.ts index 75d6e13..a2b4458 100644 --- a/server/src/scenario/scenario-run-log.entity.ts +++ b/server/src/scenario/scenario-run-log.entity.ts @@ -36,6 +36,10 @@ 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; diff --git a/server/src/scenario/scenario-scheduler.service.ts b/server/src/scenario/scenario-scheduler.service.ts index 0994ca7..56a5c7c 100644 --- a/server/src/scenario/scenario-scheduler.service.ts +++ b/server/src/scenario/scenario-scheduler.service.ts @@ -143,7 +143,7 @@ export class ScenarioSchedulerService implements OnModuleInit { statuses: ["pending", "waiting"], }) .execute(); - this.persistLog(runId, null, "error", reason); + this.persistLog(runId, null, "error", reason, "run"); } private persistLog( @@ -151,18 +151,19 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRunId: string | null, level: "log" | "warn" | "error", message: string, + context: string | null = 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, seq }), ); } private stepLogger(stepRunId: string, runId: string): ScriptLogger { - return (level, msg) => { + return (level, msg, context) => { this.logger[level](`StepRun #${stepRunId} script: ${msg}`); - this.persistLog(runId, stepRunId, level, msg); + this.persistLog(runId, stepRunId, level, msg, context ?? null); }; } @@ -384,6 +385,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "log", `Step ${step.order} started${step.title ? `: ${step.title}` : ""}`, + `step:${step.order}`, ); try { @@ -402,6 +404,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 +530,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 +561,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 +604,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "error", `Step ${stepRun.order} failed: ${description}`, + `step:${stepRun.order}`, ); await this.dumpPageOnFailure(stepRun); @@ -666,6 +672,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "log", `Step ${stepRun.order} failed: page dump uploaded to run files`, + `step:${stepRun.order}`, ); } } diff --git a/server/src/scenario/scenario.service.ts b/server/src/scenario/scenario.service.ts index b4e7fd6..2387ded 100644 --- a/server/src/scenario/scenario.service.ts +++ b/server/src/scenario/scenario.service.ts @@ -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"].map(escapeCsv).join(","), ...logs.map((l) => - [l.createdAt.toISOString(), l.level, l.message] + [l.createdAt.toISOString(), l.level, l.context ?? "", l.message] .map((v) => escapeCsv(String(v))) .join(","), ), @@ -617,7 +622,8 @@ export class ScenarioService implements OnModuleInit { } const sections = logs.map( - (l) => `## [${l.level}] ${l.createdAt.toISOString()}\n\n${l.message}`, + (l) => + `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}\n\n${l.message}`, ); return [`# Run ${runId} logs`, ...sections].join("\n\n"); } diff --git a/server/test/migrations.spec.ts b/server/test/migrations.spec.ts index 369c759..6f5a634 100644 --- a/server/test/migrations.spec.ts +++ b/server/test/migrations.spec.ts @@ -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')`, );