import { describe, expect, test } from "bun:test";
import { projectDeliveryContinuation } from "./delivery-continuation";
import type { DeliveryDeploymentRecord, LandOperationRecord } from "./store";

const operation = (state: LandOperationRecord["state"]): LandOperationRecord => ({
  operationId: "land-1",
  receipt: {
    requestId: "request-1", taskId: "task-1", root: "/repo", worktree: "/repo/.worktrees/task-1",
    ref: "wt/task-1", commit: "a".repeat(40), artifact: "artifact-1", verification: "receipt-1",
    pendingOperation: "land", blocker: "", nextAction: "deploy", rollback: "revert",
    successorAccount: "account-1", successorModel: "model-1", queueDir: "/queue", ticketId: "ticket-1",
  },
  fence: 1,
  leaseExpiresAt: 1000,
  state,
  verdict: state === "waiting" ? null : { status: state === "succeeded" ? "landed" : "failed" },
  successorIntentId: state === "succeeded" ? "successor-1" : null,
});

const deployment: DeliveryDeploymentRecord = {
  deploymentId: "deploy-1", targetId: "local", deployedSha: "a".repeat(40), deployedTree: "b".repeat(40),
  artifactDigest: `sha256:${"c".repeat(64)}`, status: "ACTIVE", observedAt: "2026-08-13T00:00:00Z",
  evidenceReference: "receipt:deploy-1",
};

describe("projectDeliveryContinuation", () => {
  test("shows the next automatic action while landing is pending", () => {
    expect(projectDeliveryContinuation(operation("waiting"), null)).toEqual({
      operationId: "land-1", state: "landing", failure: null, nextAction: "wait for guarded landing",
      landedCommit: null, deploymentId: null, installedProof: "pending",
    });
  });

  test("shows deployment as next after a successful land", () => {
    expect(projectDeliveryContinuation(operation("succeeded"), null)).toMatchObject({
      state: "deployment-pending", nextAction: "deploy", landedCommit: "a".repeat(40), installedProof: "pending",
    });
  });

  test("keeps installed proof pending when deployment is only observed", () => {
    expect(projectDeliveryContinuation(operation("succeeded"), deployment)).toMatchObject({
      state: "deployment-observed", nextAction: "verify installed entrypoint", deploymentId: "deploy-1", installedProof: "pending",
    });
  });

  test("surfaces a rolled-back deployment", () => {
    expect(projectDeliveryContinuation(operation("succeeded"), { ...deployment, status: "ROLLED_BACK" })).toMatchObject({
      state: "failed", failure: "deployment rolled back", nextAction: "inspect deployment evidence", installedProof: "pending",
    });
  });

  test("surfaces a deployment identity mismatch", () => {
    expect(projectDeliveryContinuation(operation("succeeded"), { ...deployment, deployedSha: "d".repeat(40) })).toMatchObject({
      state: "failed", failure: "deployment identity mismatch", nextAction: "inspect deployment evidence", installedProof: "pending",
    });
  });

  test("surfaces the failed landing verdict", () => {
    expect(projectDeliveryContinuation(operation("failed"), null)).toMatchObject({
      state: "failed", failure: '{"status":"failed"}', nextAction: "inspect landing verdict", installedProof: "pending",
    });
  });
});
