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
This commit is contained in:
15 files changed
+383
-70
No files matched your search
@@ -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<unknown>;
|
||||
credentials?: Record<string, unknown>;
|
||||
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<string, unknown> = credentials ?? {};
|
||||
const envData: EnvironmentData = environment ?? {};
|
||||
const snippetMap: Record<string, string> = 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<boolean>,
|
||||
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 }) => {
|
||||
|
||||
Reference in new issue
Block a user