import { describe, expect, it } from "vitest";
import { sanitizeMemoryFact } from "../services/memory-sanitizer.js";

describe("sanitizeMemoryFact", () => {
  describe("OpenAI API key (sk-) scrubbing", () => {
    it("scrubs sk- key embedded in a sentence", () => {
      expect(sanitizeMemoryFact("My API key is sk-abc123def456ghijklmnopqrst")).toBe(
        "My API key is [REDACTED]",
      );
    });

    it("scrubs a standalone sk- key", () => {
      expect(sanitizeMemoryFact("sk-abc123def456ghijklmnopqrstuvwxyz1234")).toBe("[REDACTED]");
    });
  });

  describe("GitHub PAT (ghp_) scrubbing", () => {
    it("scrubs a full GitHub PAT", () => {
      expect(sanitizeMemoryFact("ghp_aBcDeFgHiJkLmNoPqRsTuVwXyZaBcDeFgHiJk")).toBe("[REDACTED]");
    });
  });

  describe("Google API key (AIza) scrubbing", () => {
    it("scrubs a Google API key", () => {
      expect(sanitizeMemoryFact("AIzaSyA1234567890abcdefghijklmnopqrstuv")).toBe("[REDACTED]");
    });
  });

  describe("key=value credential patterns", () => {
    it("scrubs token=value pattern", () => {
      expect(sanitizeMemoryFact("token=abc123longvalue")).toBe("[REDACTED]");
    });

    it("scrubs api_key: value pattern", () => {
      expect(sanitizeMemoryFact("api_key: sk-something")).toBe("[REDACTED]");
    });

    it("scrubs password = value pattern", () => {
      expect(sanitizeMemoryFact("password = hunter2")).toBe("[REDACTED]");
    });
  });

  describe("JWT scrubbing", () => {
    it("scrubs a JWT token", () => {
      expect(
        sanitizeMemoryFact(
          "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
        ),
      ).toBe("[REDACTED]");
    });
  });

  describe("safe facts (no scrubbing)", () => {
    it("leaves a safe preference unchanged", () => {
      expect(sanitizeMemoryFact("I prefer TypeScript over JavaScript")).toBe(
        "I prefer TypeScript over JavaScript",
      );
    });

    it("leaves a config note unchanged", () => {
      expect(sanitizeMemoryFact("Use port 3000 for the server")).toBe(
        "Use port 3000 for the server",
      );
    });
  });
});
