// Pure unit tests for the run-level action helpers (R11–R14): the pending human // decision extracted from a waiting step (with the pinned-graph fallback), the // option/target wording, the cancel/retry eligibility, the deliberate-confirm // draft rule and the verbatim Android copy. No server, no DOM. import { test } from "node:test"; import assert from "node:assert/strict"; import { ACTION_FAILED_OK, ACTION_FAILED_TITLE, CANCEL_CONFIRM, CANCEL_DIALOG_MESSAGE, DECISION_ACTION, DECISION_CANCEL, DECISION_CARD_TITLE, DECISION_CONFIRM, DECISION_SENDING, DISMISS_KEEP, DecisionDraft, FEEDBACK_LABEL, RESTART_CONFIRM, RESTART_DIALOG_MESSAGE, RETRY_CONFIRM, RETRY_DIALOG_MESSAGE, canCancel, canRetry, cancelDialogTitle, decisionCardMeta, decisionSheetMeta, decisionSheetTitle, decisionTargetLabel, humanOptions, lastWaitingStep, optionTargetText, pendingDecision, retryDialogTitle, retryModes, retryRequestKey, restartDialogTitle, waitingOptions, waitingPrompt, } from "../src/actions.js"; /** A run with the fields pendingDecision reads; overridable per test. */ function run(overrides = {}) { return { id: 42, title: "", name: "Pipe", status: "waiting", current_stage: "review", executing: false, steps: [], ...overrides, }; } test("lastWaitingStep finds the last waiting step, null when none is waiting", () => { const steps = [ { stage: "plan", status: "completed" }, { stage: "review", status: "waiting" }, { stage: "review", status: "waiting" }, ]; assert.equal(lastWaitingStep(steps)?.stage, "review"); assert.equal(lastWaitingStep(steps), steps[2]); assert.equal(lastWaitingStep([{ stage: "plan", status: "completed" }]), null); assert.equal(lastWaitingStep([]), null); assert.equal(lastWaitingStep(undefined), null); }); test("waitingPrompt returns the recorded instructions, else empty", () => { assert.equal(waitingPrompt({ prompt: "Choose a path." }), "Choose a path."); assert.equal(waitingPrompt({ prompt: 7 }), ""); assert.equal(waitingPrompt({}), ""); assert.equal(waitingPrompt(null), ""); }); test("waitingOptions keeps string entries in recorded order, dropping malformed and blank", () => { assert.deepEqual(waitingOptions({ options: ["approve", "reject"] }), ["approve", "reject"]); assert.deepEqual(waitingOptions({ options: ["a", 3, null, " ", "b"] }), ["a", "b"]); assert.deepEqual(waitingOptions({ options: "approve" }), []); assert.deepEqual(waitingOptions({}), []); }); test("humanOptions are a stage's non-error edges, sorted", () => { assert.deepEqual(humanOptions({ publish: "#success", error: "#failure", revise: "review" }), [ "publish", "revise", ]); assert.deepEqual(humanOptions({}), []); assert.deepEqual(humanOptions(undefined), []); }); test("decisionTargetLabel resolves declared edges and is blank otherwise", () => { assert.equal(decisionTargetLabel("publish", "publish"), "publish"); assert.equal(decisionTargetLabel("publish", "#"), "SUCCESS"); assert.equal(decisionTargetLabel("error", "#"), "FAILURE"); assert.equal(decisionTargetLabel("approve", ""), ""); assert.equal(decisionTargetLabel("approve", null), ""); assert.equal(decisionTargetLabel("approve", undefined), ""); assert.equal(optionTargetText("publish"), "→ publish"); assert.equal(optionTargetText(""), ""); assert.equal(optionTargetText(null), ""); }); test("pendingDecision returns null unless the run is waiting", () => { assert.equal(pendingDecision(null), null); assert.equal(pendingDecision(run({ status: "failed" })), null); assert.equal(pendingDecision(run({ status: "running", executing: true })), null); assert.equal(pendingDecision(run({ status: "succeeded" })), null); }); test("pendingDecision yields the recorded options in author order and the verbatim prompt", () => { const decision = pendingDecision( run({ title: "Review the plan", current_stage: "review", steps: [ { stage: "plan", status: "completed" }, { stage: "review", status: "waiting", output: { prompt: "Ship it or revise it?", options: ["revise", "approve"] }, }, ], graph: { stages: [ { key: "review", prompt: "graph prompt", edges: { approve: "publish", revise: "review", error: "#failure" }, }, ], }, }), ); assert.deepEqual(decision.options.map((o) => o.key), ["revise", "approve"]); assert.equal(decision.instructions, "Ship it or revise it?"); assert.equal(decision.stageKey, "review"); assert.equal(decision.runId, 42); assert.equal(decision.runLabel, "Review the plan"); assert.deepEqual(decision.options, [ { key: "revise", target: "review" }, { key: "approve", target: "publish" }, ]); }); test("pendingDecision falls back to the pinned graph's sorted non-error edges", () => { const decision = pendingDecision( run({ current_stage: "review", steps: [{ stage: "", status: "waiting", output: {} }], graph: { stages: [ { key: "review", prompt: "Graph instructions.", edges: { publish: "#success", error: "#failure", revise: "review" } }, ], }, }), ); assert.deepEqual(decision.options.map((o) => o.key), ["publish", "revise"]); assert.equal(decision.instructions, "Graph instructions."); assert.equal(decision.stageKey, "review"); }); test("pendingDecision keeps a recorded option whose edge is not declared, with a blank target", () => { const decision = pendingDecision( run({ steps: [{ stage: "review", status: "waiting", output: { options: ["approve", "legacy"] } }], graph: { stages: [{ key: "review", edges: { approve: "publish" } }] }, }), ); assert.deepEqual(decision.options, [ { key: "approve", target: "publish" }, { key: "legacy", target: "" }, ]); }); test("pendingDecision is null when no option can be derived", () => { assert.equal( pendingDecision(run({ steps: [{ stage: "review", status: "waiting", output: {} }], graph: { stages: [] } })), null, ); }); test("DecisionDraft: a blank selection cannot submit and selecting never sends", () => { const blank = new DecisionDraft(); assert.equal(blank.canSubmit, false); const selected = blank.select("approve"); assert.equal(selected.canSubmit, true); assert.equal(selected.selected, "approve"); // Selecting is pure: the original draft is untouched and no side effect fires. assert.equal(blank.selected, null); assert.equal(blank.canSubmit, false); // Feedback is carried verbatim and never changes the submit rule. const withFeedback = selected.withFeedback(" looks good "); assert.equal(withFeedback.feedback, " looks good "); assert.equal(withFeedback.canSubmit, true); assert.equal(new DecisionDraft(" ").canSubmit, false); }); test("canCancel is true only while executing; canRetry only for a failed, idle run", () => { assert.equal(canCancel(run({ executing: true })), true); assert.equal(canCancel(run({ executing: false })), false); assert.equal(canCancel(null), false); assert.equal(canRetry(run({ status: "failed", executing: false })), true); assert.equal(canRetry(run({ status: "failed", executing: true })), false); assert.equal(canRetry(run({ status: "succeeded", executing: false })), false); assert.equal(canRetry(run({ status: "waiting", executing: false })), false); assert.equal(canRetry(null), false); assert.equal(canRetry(undefined), false); }); test("retryModes offers resume and restart for a retryable run, nothing otherwise", () => { assert.deepEqual(retryModes(run({ status: "failed" })), ["resume", "restart"]); assert.deepEqual(retryModes(run({ status: "succeeded" })), []); assert.deepEqual(retryModes(run({ status: "failed", executing: true })), []); }); test("retryRequestKey is unique, prefixed and within the server's 128-character bound", () => { const key = retryRequestKey(440, 1_700_000_000_000, () => 0.5); assert.match(key, /^web-retry-440-1700000000000-/); assert.ok(key.length <= 128, `key too long: ${key.length}`); assert.notEqual( retryRequestKey(440, 1, () => 0.1), retryRequestKey(440, 1, () => 0.2), ); }); test("the decision/action copy matches the Android sources verbatim", () => { assert.equal(DECISION_CARD_TITLE, "Waiting for your decision"); assert.equal(DECISION_ACTION, "Decide"); assert.equal(FEEDBACK_LABEL, "Feedback (optional)"); assert.equal(DECISION_CANCEL, "Cancel"); assert.equal(DECISION_CONFIRM, "Confirm decision"); assert.equal(DECISION_SENDING, "Sending…"); assert.equal(cancelDialogTitle(440), "Cancel run 440?"); assert.equal(CANCEL_DIALOG_MESSAGE, "This asks the server to stop the active run."); assert.equal(CANCEL_CONFIRM, "Cancel run"); assert.equal(retryDialogTitle(440), "Retry run 440?"); assert.equal( RETRY_DIALOG_MESSAGE, "Only failed runs can be retried. The server starts a new run from the same input.", ); assert.equal(RETRY_CONFIRM, "Retry"); assert.equal(DISMISS_KEEP, "Keep"); assert.equal(RESTART_CONFIRM, "Restart from start"); assert.ok(RESTART_DIALOG_MESSAGE.includes("replays the whole pipeline")); assert.ok(RESTART_DIALOG_MESSAGE.includes("instead of resuming the failed stage")); assert.equal(restartDialogTitle(440), "Restart run 440?"); assert.equal(ACTION_FAILED_TITLE, "Action failed"); assert.equal(ACTION_FAILED_OK, "OK"); }); test("the decision card and sheet meta/title render the app's exact shapes", () => { const decision = { runId: 12, runLabel: "Ship it", stageKey: "review" }; assert.equal(decisionCardMeta(decision), "review · run 12 · Ship it"); assert.equal(decisionSheetTitle(decision.stageKey), "Decide review"); assert.equal(decisionSheetMeta(decision), "run 12 · Ship it"); });