import type { BrowserContext, Page } from "playwright"; import type { EnvironmentData } from "../environment/environment.entity"; import type { FileStorageService } from "../file/file-storage.service"; import type { ExecContext, ScriptLogger } from "./code-executor.service"; export class ExecContextBuilder { private readonly ctx: Partial = {}; page(page: Page): this { this.ctx.page = page; return this; } browser(browser: BrowserContext): this { this.ctx.browser = browser; return this; } code(code: string): this { this.ctx.code = code; return this; } log(log: ScriptLogger): this { this.ctx.log = log; return this; } logContext(logContext: string): this { this.ctx.logContext = logContext; return this; } getStepOutput(fn: (order: number) => Promise): this { this.ctx.getStepOutput = fn; return this; } credentials(credentials: Record): this { this.ctx.credentials = credentials; return this; } environment(environment: EnvironmentData | null): this { this.ctx.environment = environment; return this; } snippets(snippets: Record | null): this { this.ctx.snippets = snippets; return this; } scenarioId(scenarioId: string): this { this.ctx.scenarioId = scenarioId; return this; } runId(runId: string): this { this.ctx.runId = runId; return this; } fileService(fileService: FileStorageService): this { this.ctx.fileService = fileService; return this; } build(): ExecContext { if (!this.ctx.page) throw new Error("ExecContextBuilder: page is required"); if (!this.ctx.browser) throw new Error("ExecContextBuilder: browser is required"); if (this.ctx.code === undefined) throw new Error("ExecContextBuilder: code is required"); return this.ctx as ExecContext; } }