import { describe, it, expect } from "vitest";
import { getNonce } from "@/security/tanstack/get-nonce";

describe("getNonce", () => {
  it("reads x-nonce from node request headers", () => {
    expect(getNonce({ headers: { "x-nonce": "test-nonce-value" } })).toBe(
      "test-nonce-value",
    );
  });

  it("returns the first value when x-nonce is an array", () => {
    expect(
      getNonce({ headers: { "x-nonce": ["first-nonce", "second"] } }),
    ).toBe("first-nonce");
  });

  it("returns empty string when x-nonce is absent", () => {
    expect(getNonce({ headers: {} })).toBe("");
  });

  it("returns empty string when x-nonce is undefined", () => {
    expect(getNonce({ headers: { "x-nonce": undefined } })).toBe("");
  });

  it("returns empty string when x-nonce is an empty array", () => {
    expect(getNonce({ headers: { "x-nonce": [] } })).toBe("");
  });
});
