import { describe, it, expect } from "vitest";
import { isPdfExportServiceRequest } from "@/lib/pdf-export-auth";

const TOKEN = "a".repeat(64);

describe("isPdfExportServiceRequest", () => {
  it("allows the export route when the svc token matches", () => {
    expect(isPdfExportServiceRequest("/api/po/cmqrug123/export", TOKEN, TOKEN)).toBe(true);
    expect(isPdfExportServiceRequest("/api/po/cmqrug123/export/", TOKEN, TOKEN)).toBe(true); // trailing slash
  });

  it("denies when the token is missing, empty, or wrong", () => {
    expect(isPdfExportServiceRequest("/api/po/x/export", TOKEN, undefined)).toBe(false); // service not configured
    expect(isPdfExportServiceRequest("/api/po/x/export", null, TOKEN)).toBe(false); // no svc on request
    expect(isPdfExportServiceRequest("/api/po/x/export", "", TOKEN)).toBe(false);
    expect(isPdfExportServiceRequest("/api/po/x/export", "wrong", TOKEN)).toBe(false);
  });

  it("only matches the PO export route, not other paths", () => {
    expect(isPdfExportServiceRequest("/api/po/x/export/extra", TOKEN, TOKEN)).toBe(false);
    expect(isPdfExportServiceRequest("/api/po/x", TOKEN, TOKEN)).toBe(false);
    expect(isPdfExportServiceRequest("/dashboard", TOKEN, TOKEN)).toBe(false);
    expect(isPdfExportServiceRequest("/api/reports/spend", TOKEN, TOKEN)).toBe(false);
    expect(isPdfExportServiceRequest("/api/po//export", TOKEN, TOKEN)).toBe(false); // empty id
  });
});
