import { Effect } from "effect";
import { describe, expect, test } from "vitest";

import {
  decodeWorkArtifactDraft,
  decodeWorkDeliveryDraft,
} from "./work-artifact";

describe("Work artifact contracts", () => {
  test("accepts traceable artifact and delivery metadata", async () => {
    await expect(
      Effect.runPromise(
        decodeWorkArtifactDraft({
          idempotencyKey: "attempt:1:test-report",
          kind: "test-report",
          metadataJson: "{}",
          producer: "fake-harness",
          provenanceJson: "{}",
          sourceRevision: "abc123",
          title: "Focused test report",
          verificationStatus: "verified",
        })
      )
    ).resolves.toMatchObject({ kind: "test-report" });

    await expect(
      Effect.runPromise(
        decodeWorkDeliveryDraft({
          externalId: "42",
          idempotencyKey: "delivery:pr:42",
          kind: "pull-request",
          metadataJson: "{}",
          provider: "gitea",
          sourceRevision: "abc123",
          status: "ready",
          target: "main",
          url: "https://git.example/pulls/42",
        })
      )
    ).resolves.toMatchObject({ kind: "pull-request" });
  });

  test("rejects unverifiable evidence metadata", async () => {
    await expect(
      Effect.runPromise(
        decodeWorkArtifactDraft({
          idempotencyKey: "attempt:1:test-report",
          kind: "test-report",
          metadataJson: "not-json",
          producer: "fake-harness",
          provenanceJson: "{}",
          title: "Focused test report",
          verificationStatus: "verified",
        })
      )
    ).rejects.toThrow();
  });
});
