import type { ActionRuntime } from "./automation/actions" import { createActionRuntime, getAutomationRuntimeState, setAutomationRuntimeStateForTest, type AutomationRunState, } from "./automation/runtime" interface ActionTestRuntimeOptions { completeAction?: AutomationRunState["completeAction"] completeActionAttempt?: AutomationRunState["completeActionAttempt"] googleAdsRequest?: AutomationRunState["googleAdsRequest"] resolveAccountInput?: AutomationRunState["resolveAccountInput"] runtime: ActionRuntime startActionAttempt?: AutomationRunState["startActionAttempt"] } /** * Installs one isolated pending action target for an SDK boundary test. * * @param options - Runtime capabilities and optional action hooks. */ export function installActionTestRuntime(options: ActionTestRuntimeOptions) { setAutomationRuntimeStateForTest({ actionDeferred: false, actionInvocations: [], compatibilityChecks: [], completeAction: options.completeAction ?? (() => Promise.resolve()), completeActionAttempt: options.completeActionAttempt ?? (async (outcome) => { if (!options.completeAction) return await options.completeAction( outcome.status === "retrying" || outcome.status === "indeterminate" ? { failure: outcome.failure, status: "failed" } : outcome, ) }), context: { actions: [], contextId: "context-1", contextParents: [], events: [], signalOutputs: [], }, coordinationEvaluations: [], googleAdsRequest: options.googleAdsRequest, initiatingSubscriptionSignals: [], nextHookSlot: 0, nonInitiatingSubscriptionOrigins: new Set(), phase: "run", resolveAccountInput: options.resolveAccountInput ?? (() => { throw new Error("Unexpected account input") }), runtime: options.runtime, signalEvaluations: [], signalInvocations: [], significanceEvaluations: [], startActionAttempt: options.startActionAttempt ?? (() => Promise.resolve({ attempt: 1, finalAttempt: false, status: "running", })), targetAction: { context: { actionOutputs: [], coordinationSelections: [], contextId: "context-1", contextParents: [], events: [], signalOutputs: [], }, id: "invocation-1", scopePath: [], slot: 0, status: "pending", }, }) } /** Removes the SDK test runtime installed for the current process. */ export function clearActionTestRuntime() { setAutomationRuntimeStateForTest(undefined) } /** Executes the single action registered by the installed test runtime. */ export async function executeTestAction() { const execution = getAutomationRuntimeState()?.actionExecution if (!execution) { throw new Error("No test action was registered for execution.") } await execution() } export { createActionRuntime }