import { describe, expect, it } from "vitest";
import { ALL_TRUTH_LABELS, TRUTH_LABELS, resolveTruthLabels } from "../src/truth-labels";
import { DEFAULT_V3_FLAGS } from "../src/version-flags";
import {
  BASE_V2_ADDRESSES,
  type WebsiteAddressEntry,
} from "../generated/website-addresses.generated";

const activeV2 = BASE_V2_ADDRESSES[0];
const deprecatedV2: WebsiteAddressEntry = { ...activeV2, status: "deprecated" };
const v3Deployment: WebsiteAddressEntry = {
  ...activeV2,
  name: "VaultfireBIPVaultV3",
  protocolVersion: "v3",
  baselineTruthLabel: TRUTH_LABELS.V3_TEST_DEPLOYMENT,
};

describe("truth label vocabulary", () => {
  it("exposes exactly the five required labels", () => {
    expect(ALL_TRUTH_LABELS).toEqual([
      "Legacy V2",
      "V3 test deployment",
      "V3 audited pilot",
      "Deprecated",
      "Read-only",
    ]);
  });

  it("labels every generated V2 entry as Legacy V2 in the baseline", () => {
    for (const entry of BASE_V2_ADDRESSES) {
      expect(entry.baselineTruthLabel).toBe(TRUTH_LABELS.LEGACY_V2);
    }
  });
});

describe("resolveTruthLabels", () => {
  it("marks an active V2 surface Legacy V2 and Read-only when writes are unavailable", () => {
    expect(resolveTruthLabels({ protocolVersion: "v2", deployment: activeV2 })).toEqual([
      "Legacy V2",
      "Read-only",
    ]);
  });

  it("drops Read-only when the V2 surface can write", () => {
    expect(
      resolveTruthLabels({ protocolVersion: "v2", deployment: activeV2, writesAvailable: true }),
    ).toEqual(["Legacy V2"]);
  });

  it("marks deprecated deployments Deprecated instead of Legacy V2", () => {
    expect(
      resolveTruthLabels({ protocolVersion: "v2", deployment: deprecatedV2, writesAvailable: true }),
    ).toEqual(["Deprecated"]);
  });

  it("labels V3 as a test deployment by default", () => {
    expect(
      resolveTruthLabels(
        { protocolVersion: "v3", deployment: v3Deployment, writesAvailable: true },
        DEFAULT_V3_FLAGS,
      ),
    ).toEqual(["V3 test deployment"]);
  });

  it("labels V3 an audited pilot only with the flag on and a verified deployment", () => {
    const flags = { ...DEFAULT_V3_FLAGS, v3AuditedPilotEnabled: true };
    expect(
      resolveTruthLabels(
        { protocolVersion: "v3", deployment: v3Deployment, writesAvailable: true },
        flags,
      ),
    ).toEqual(["V3 audited pilot"]);
    // No verified address -> cannot claim audited pilot, and no writes available.
    expect(
      resolveTruthLabels({ protocolVersion: "v3", deployment: null }, flags),
    ).toEqual(["Read-only"]);
  });
});
