import { describe, expect, it } from "vitest";

import {
  manualPerformanceInputSchema,
  performanceSnapshotSchema,
  publicationInputSchema,
} from "../validation";

describe("calibration validation", () => {
  it("accepts a publication URL and normalizes optional notes", () => {
    const parsed = publicationInputSchema.parse({
      platform: "official_site",
      url: "https://example.com/articles/geo",
      published_at: "2026-06-24T12:00:00.000Z",
      notes: " 官网首发 ",
    });

    expect(parsed.platform).toBe("official_site");
    expect(parsed.notes).toBe("官网首发");
  });

  it("keeps sparse manual metrics absent instead of turning them into zeroes", () => {
    const parsed = manualPerformanceInputSchema.parse({
      window_label: "T+7d",
      views: "1200",
      clicks: "",
      inquiries: undefined,
      comments: "3",
      feedback_summary: "用户追问案例依据",
    });

    expect(parsed.metrics).toEqual({
      views: 1200,
      comments: 3,
    });
    expect(parsed.feedback_summary).toBe("用户追问案例依据");
  });

  it("rejects unsafe raw adapter credentials in snapshots", () => {
    expect(() =>
      performanceSnapshotSchema.parse({
        id: "perf_test",
        publication_id: "pub_test",
        source: "manual",
        window_label: "T+3d",
        metrics: { views: 10 },
        feedback_summary: "",
        raw_reference: "cookie=sessionid=secret",
        snapshot_at: "2026-06-24T12:00:00.000Z",
      }),
    ).toThrow(/raw_reference/);
  });
});
