import { describe, test, expect } from "bun:test";
import { formatDatadogLogLines, isThinkingLine } from "@/datadog/format.js";

describe("isThinkingLine", () => {
  test("matches [cursor:tool prefix", () => {
    expect(isThinkingLine("[cursor:tool_call_started] some data")).toBe(true);
    expect(isThinkingLine("[cursor:tool_call_completed] done")).toBe(true);
  });

  test("matches [cursor:user prefix", () => {
    expect(isThinkingLine("[cursor:user_prompt] prompt text")).toBe(true);
  });

  test("matches [cursor:system prefix", () => {
    expect(isThinkingLine("[cursor:system_init] init")).toBe(true);
  });

  test("matches [cursor:result prefix", () => {
    expect(isThinkingLine("[cursor:result] final")).toBe(true);
  });

  test('matches "thinkingMessage" in JSON payload', () => {
    expect(isThinkingLine('{"thinkingMessage":"some deep thought"}')).toBe(true);
  });

  test("returns false for normal log messages", () => {
    expect(isThinkingLine("Build succeeded in 42s")).toBe(false);
    expect(isThinkingLine("PR #123 merged")).toBe(false);
  });

  test("returns false for empty string", () => {
    expect(isThinkingLine("")).toBe(false);
  });
});

describe("formatDatadogLogLines", () => {
  test("empty array returns empty array", () => {
    expect(formatDatadogLogLines([])).toEqual([]);
  });

  test("single log line formats as 'timestamp message'", () => {
    const result = formatDatadogLogLines([
      { timestamp: "2025-01-15T10:30:00Z", message: "hello world" },
    ]);
    expect(result).toEqual(["2025-01-15T10:30:00Z hello world"]);
  });

  test("multiple logs are sorted newest-first (descending by timestamp)", () => {
    const result = formatDatadogLogLines([
      { timestamp: "2025-01-15T10:00:00Z", message: "first" },
      { timestamp: "2025-01-15T12:00:00Z", message: "third" },
      { timestamp: "2025-01-15T11:00:00Z", message: "second" },
    ]);
    expect(result).toEqual([
      "2025-01-15T12:00:00Z third",
      "2025-01-15T11:00:00Z second",
      "2025-01-15T10:00:00Z first",
    ]);
  });

  test("null timestamp produces message-only line", () => {
    const result = formatDatadogLogLines([{ timestamp: null, message: "no ts" }]);
    expect(result).toEqual(["no ts"]);
  });

  test("null message is treated as empty and skipped", () => {
    const result = formatDatadogLogLines([{ timestamp: "2025-01-15T10:00:00Z", message: null }]);
    expect(result).toEqual([]);
  });

  test("empty message string is skipped after sanitization", () => {
    const result = formatDatadogLogLines([{ timestamp: "2025-01-15T10:00:00Z", message: "" }]);
    expect(result).toEqual([]);
  });

  test("ANSI sequences are stripped from messages", () => {
    const result = formatDatadogLogLines([
      { timestamp: "2025-01-15T10:00:00Z", message: "\u001b[31mred error\u001b[0m" },
    ]);
    expect(result).toEqual(["2025-01-15T10:00:00Z red error"]);
  });

  test("Pino JSON message extracts msg and context fields", () => {
    const pinoJson = JSON.stringify({
      msg: "Pipeline started",
      issueKey: "PROJ-42",
      jobKey: "fix-42",
      repo: "my-repo",
      branch: "fix/bug",
    });
    const result = formatDatadogLogLines([
      { timestamp: "2025-01-15T10:00:00Z", message: pinoJson },
    ]);
    expect(result).toEqual([
      "2025-01-15T10:00:00Z PROJ-42 · fix-42 · my-repo · fix/bug — Pipeline started",
    ]);
  });

  test("hideThinking filters thinking lines", () => {
    const result = formatDatadogLogLines(
      [
        { timestamp: "2025-01-15T10:00:00Z", message: "[cursor:tool_call_started] big json" },
        { timestamp: "2025-01-15T10:01:00Z", message: "normal log" },
      ],
      { hideThinking: true },
    );
    expect(result).toEqual(["2025-01-15T10:01:00Z normal log"]);
  });

  test("hideThinking also filters sibling lines sharing the same timestamp as a thinking line", () => {
    const result = formatDatadogLogLines(
      [
        { timestamp: "2025-01-15T10:00:00Z", message: "[cursor:tool_call_started] payload" },
        { timestamp: "2025-01-15T10:00:00Z", message: "continuation fragment of same entry" },
        { timestamp: "2025-01-15T10:01:00Z", message: "unrelated normal log" },
      ],
      { hideThinking: true },
    );
    expect(result).toEqual(["2025-01-15T10:01:00Z unrelated normal log"]);
  });

  test("hideThinking false (default) keeps thinking lines", () => {
    const result = formatDatadogLogLines([
      { timestamp: "2025-01-15T10:00:00Z", message: "[cursor:tool_call_started] data" },
    ]);
    expect(result).toEqual(["2025-01-15T10:00:00Z [cursor:tool_call_started] data"]);
  });
});
