import { describe, it, expect } from "vitest";
import { RecordingAuditLog } from "./recording-audit-log";

// Inline type to avoid importing from @repo/core-shared (boundary: tooling cannot depend on core)
type SampleEntry = Parameters<RecordingAuditLog["record"]>[0];

const sample: SampleEntry = {
  actorId: "user_1",
  actorType: "user",
  actorRoles: [],
  action: "CREATE",
  resource: { type: "articles" },
  at: new Date(),
  scope: { feature: "blog", environment: "test", tenant: "default" },
  from: { ipTruncated: "10.0.0.0", userAgent: "test" },
  containsPii: false,
  outcome: "success",
};

describe("RecordingAuditLog", () => {
  it("record() pushes to recorded[]", async () => {
    const log = new RecordingAuditLog();
    await log.record(sample);
    expect(log.recorded).toHaveLength(1);
    expect(log.recorded[0]!.actorId).toBe("user_1");
  });

  it("eraseSubject(pseudonymize) tracks erasure + rewrites actorId in recorded", async () => {
    const log = new RecordingAuditLog();
    await log.record(sample);
    await log.eraseSubject("user_1", "pseudonymize");
    expect(log.erasures).toEqual([{ actorId: "user_1", mode: "pseudonymize" }]);
    expect(log.recorded[0]!.actorId).toBe("erased-user_1");  // sentinel rewrite
  });

  it("eraseSubject(delete) removes matching entries from recorded", async () => {
    const log = new RecordingAuditLog();
    await log.record(sample);
    await log.record({ ...sample, actorId: "user_2" });
    await log.eraseSubject("user_1", "delete");
    expect(log.recorded.map((r) => r.actorId)).toEqual(["user_2"]);
    expect(log.erasures).toEqual([{ actorId: "user_1", mode: "delete" }]);
  });

  it("reset() clears recorded + erasures", async () => {
    const log = new RecordingAuditLog();
    await log.record(sample);
    await log.eraseSubject("user_1", "pseudonymize");
    log.reset();
    expect(log.recorded).toEqual([]);
    expect(log.erasures).toEqual([]);
  });
});
