feat(scenario): split log args into message/data and add debug level
CI / changes (pull_request) Successful in 12s
CI / version (pull_request) Skipped
CI / server (pull_request) Successful in 1m4s
CI / client (pull_request) Successful in 1m33s
CI / image (pull_request) Skipped

- context.log/warn/error and console.* split arguments: strings join
  into message, object/array args are kept out of it and stored as
  the log entry's new data column (JSON), so message stays a stable
  string regardless of an object's shape
- added a debug log level alongside log/warn/error, including
  context.debug() and console.debug() (previously folded into log)
- CSV/Markdown run log export include the data column/block
- LogOutput renders data pretty-printed in a separate block below
  the message, and dims debug entries with a dedicated grey token
  calibrated to the WCAG AA contrast floor
This commit is contained in:
ars9 committed 2026-10-01 14:09:45 +03:00
1 parent 6cba785e2f
commit 1273491eee
13 files changed
+204 -45

No files matched your search

@@ -19,10 +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. */
@@ -44,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`
@@ -125,10 +128,27 @@ 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.
@@ -136,10 +156,16 @@ export class CodeExecutorService {
const currentContext = (): string | null =>
contextStack.length > 0 ? contextStack[contextStack.length - 1] : null;
const emit = (
level: "log" | "warn" | "error",
level: "debug" | "log" | "warn" | "error",
message: string,
contextOverride?: string,
): void => scriptLog(level, message, contextOverride ?? currentContext());
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 ?? {};
@@ -149,11 +175,11 @@ export class CodeExecutorService {
let scriptContext: ScriptContext;
const fakeConsole = {
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)),
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 {
@@ -161,9 +187,10 @@ export class CodeExecutorService {
page,
browser,
dumpDom: (selector?: string) => dumpDom(page, selector),
log: (...args: unknown[]) => emit("log", toStr(args)),
warn: (...args: unknown[]) => emit("warn", toStr(args)),
error: (...args: unknown[]) => emit("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,
@@ -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"`);
}
}
+6 -1
View File
@@ -1,8 +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, AddRunLogContext1790848194828];
export const MIGRATIONS = [
Initial1790793447827,
AddRunLogContext1790848194828,
AddRunLogData1790850734763,
];
@@ -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 {
@@ -43,6 +43,11 @@ export class ScenarioRunLogEntity {
@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 })
@@ -149,21 +149,30 @@ export class ScenarioSchedulerService implements OnModuleInit {
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, context, 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, context) => {
return (level, msg, context, data) => {
this.logger[level](`StepRun #${stepRunId} script: ${msg}`);
this.persistLog(runId, stepRunId, level, msg, context ?? null);
this.persistLog(runId, stepRunId, level, msg, context ?? null, data ?? null);
};
}
+9 -6
View File
@@ -611,9 +611,9 @@ export class ScenarioService implements OnModuleInit {
const escapeCsv = (value: string): string =>
/[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value;
const rows = [
["Timestamp", "Level", "Context", "Message"].map(escapeCsv).join(","),
["Timestamp", "Level", "Context", "Message", "Data"].map(escapeCsv).join(","),
...logs.map((l) =>
[l.createdAt.toISOString(), l.level, l.context ?? "", l.message]
[l.createdAt.toISOString(), l.level, l.context ?? "", l.message, l.data ?? ""]
.map((v) => escapeCsv(String(v)))
.join(","),
),
@@ -621,10 +621,13 @@ export class ScenarioService implements OnModuleInit {
return rows.join("\n");
}
const sections = logs.map(
(l) =>
`## [${l.level}]${l.context ? ` (${l.context})` : ""} ${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");
}