/* eslint-disable no-non-null-assertion -- test assertions on defined events */
import { describe, expect, it } from "vitest";

import { makeOrbEvent, normalizeSessionEvent, redactSecrets } from "./events";

describe("redactSecrets", () => {
  it("redacts api_key patterns", () => {
    const input = "Using api_key=sk-abc123def456ghi789jkl012mno345";
    const result = redactSecrets(input);
    expect(result).not.toContain("sk-abc123");
    expect(result).toContain("[REDACTED]");
  });

  it("redacts Bearer tokens", () => {
    const input = "Authorization: Bearer eyJhbGciOiJIUzI1";
    const result = redactSecrets(input);
    expect(result).toContain("Bearer [REDACTED]");
    expect(result).not.toContain("eyJhbGciOiJIUzI1");
  });

  it("redacts token= patterns", () => {
    const input = 'token: "my-secret-token-value"';
    const result = redactSecrets(input);
    expect(result).not.toContain("my-secret-token-value");
  });

  it("preserves non-secret text", () => {
    expect(redactSecrets("just regular text")).toBe("just regular text");
  });
});

describe("makeOrbEvent", () => {
  it("creates an event with sequence and timestamp", () => {
    const event = makeOrbEvent(5, "session_opened", { text: "opened" });
    expect(event.sequence).toBe(5);
    expect(event.type).toBe("session_opened");
    expect(event.text).toBe("opened");
    expect(event.timestamp).toBeDefined();
  });

  it("creates an event with command and exitCode", () => {
    const event = makeOrbEvent(3, "command_executed", {
      command: "bun test",
      exitCode: 0,
    });
    expect(event.command).toBe("bun test");
    expect(event.exitCode).toBe(0);
  });
});

describe("normalizeSessionEvent", () => {
  it("normalizes agent_message_chunk", () => {
    const event = normalizeSessionEvent({
      rawText: "Working on the fix",
      sequence: 5,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:00Z",
      type: "agent_message_chunk",
    });
    expect(event).toBeDefined();
    expect(event!.type).toBe("agent_message_chunk");
    expect(event!.text).toBe("Working on the fix");
    expect(event!.sequence).toBe(5);
  });

  it("normalizes agent_message to agent_message_completed", () => {
    const event = normalizeSessionEvent({
      content: [{ text: "Done", type: "text" }],
      sequence: 10,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:05Z",
      type: "agent_message",
    });
    expect(event).toBeDefined();
    expect(event!.type).toBe("agent_message_completed");
    expect(event!.text).toBe("Done");
  });

  it("normalizes tool_call", () => {
    const event = normalizeSessionEvent({
      sequence: 3,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:01Z",
      title: "bash",
      toolCallId: "tc-1",
      type: "tool_call",
    });
    expect(event).toBeDefined();
    expect(event!.type).toBe("tool_call_started");
    expect(event!.toolName).toBe("bash");
    expect(event!.toolCallId).toBe("tc-1");
  });

  it("normalizes permission_request", () => {
    const event = normalizeSessionEvent({
      sequence: 7,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:03Z",
      type: "permission_request",
    });
    expect(event).toBeDefined();
    expect(event!.type).toBe("permission_requested");
  });

  it("returns undefined for unmapped types", () => {
    const event = normalizeSessionEvent({
      sequence: 1,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:00Z",
      type: "session_config",
    });
    expect(event).toBeUndefined();
  });

  it("returns undefined when type is missing", () => {
    const event = normalizeSessionEvent({
      sequence: 1,
      sessionId: "sess-1",
    });
    expect(event).toBeUndefined();
  });

  it("redacts secrets in persisted event copies", () => {
    const event = normalizeSessionEvent({
      rawText: "Using api_key=sk-secret123456789012345678",
      sequence: 2,
      sessionId: "sess-1",
      timestamp: "2026-07-24T12:00:00Z",
      type: "agent_message_chunk",
    });
    expect(event).toBeDefined();
    expect(event!.text).not.toContain("sk-secret");
  });
});
