import { describe, expect, test } from "bun:test";

import { CommandChecker } from "../../../../src/server/checker/runner/cmd/execute";
import { DbChecker } from "../../../../src/server/checker/runner/db/execute";
import { HttpChecker } from "../../../../src/server/checker/runner/http/execute";
import { IcmpChecker } from "../../../../src/server/checker/runner/icmp/execute";
import { LlmChecker } from "../../../../src/server/checker/runner/llm/execute";
import { TcpChecker } from "../../../../src/server/checker/runner/tcp/execute";
import { UdpChecker } from "../../../../src/server/checker/runner/udp/execute";

describe("Checker buildDetail", () => {
  test("HTTP detail", () => {
    expect(new HttpChecker().buildDetail({ statusCode: 200 })).toBe("HTTP 200");
  });

  test("TCP detail", () => {
    const detail = new TcpChecker().buildDetail({
      banner: "220 smtp.example.com ESMTP",
      connected: true,
      connectTimeMs: 12,
    });
    expect(detail).toContain("connected in 12ms");
    expect(detail).toContain("banner:");
  });

  test("UDP detail", () => {
    const checker = new UdpChecker();
    expect(checker.buildDetail({ durationMs: 12, responded: true, responsePreview: "PONG", responseSize: 4 })).toBe(
      "responded in 12ms, 4 bytes, response: PONG",
    );
    expect(checker.buildDetail({ durationMs: 200, responded: false })).toBe("no response in 200ms");
  });

  test("Ping detail", () => {
    const checker = new IcmpChecker();
    expect(checker.buildDetail({ alive: true, avgLatencyMs: 12, packetLoss: 0, received: 3, transmitted: 3 })).toBe(
      "alive, avg 12ms, loss 0% (3/3)",
    );
    expect(checker.buildDetail({ alive: false, received: 0, transmitted: 3 })).toBe("unreachable (0/3 received)");
  });

  test("DB detail", () => {
    const checker = new DbChecker();
    expect(checker.buildDetail({ connected: true, rowCount: 3 })).toBe("3 rows");
    expect(checker.buildDetail({ connected: true, rowCount: null })).toBe("connected");
  });

  test("CMD detail", () => {
    expect(new CommandChecker().buildDetail({ exitCode: 0 })).toBe("exitCode=0");
  });

  test("LLM detail", () => {
    const detail = new LlmChecker().buildDetail({
      finishReason: "stop",
      http: { status: 200 },
      mode: "http",
      outputLength: 2,
      provider: "openai",
      usage: { inputTokens: 12, outputTokens: 2, totalTokens: 14 },
    });
    expect(detail).toContain("LLM openai http");
    expect(detail).toContain("200");
    expect(detail).toContain("finish=stop");
    expect(detail).toContain("output=2 chars");
    expect(detail).toContain("usage=12/2 tokens");
  });
});
