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 { DnsChecker } from "../../../../src/server/checker/runner/dns/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");
  });

  const dnsChecker = new DnsChecker();

  test("DnsChecker system mode: successful resolution shows addresses and duration", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 15,
      error: null,
      family: "any",
      name: "example.com",
      resolver: "system",
      valueCount: 2,
      values: ["93.184.216.34", "93.184.216.35"],
    });
    expect(detail).toContain("93.184.216.34");
    expect(detail).toContain("15ms");
  });

  test("DnsChecker system mode: failed resolution shows error", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 100,
      error: "getaddrinfo ENOTFOUND",
      family: "any",
      name: "example.com",
      resolver: "system",
      valueCount: 0,
      values: [],
    });
    expect(detail).toContain("解析失败");
    expect(detail).toContain("getaddrinfo ENOTFOUND");
  });

  test("DnsChecker system mode: no results shows '解析成功但无结果'", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 10,
      error: null,
      family: "any",
      name: "example.com",
      resolver: "system",
      valueCount: 0,
      values: [],
    });
    expect(detail).toContain("解析成功但无结果");
  });

  test("DnsChecker server mode: successful response shows rcode, values, protocol, duration", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 25,
      error: null,
      name: "example.com",
      port: 53,
      protocol: "udp",
      protocolUsed: "udp",
      rcode: "NOERROR",
      recordType: "A",
      resolver: "server",
      responded: true,
      server: "8.8.8.8",
      valueCount: 2,
      values: ["1.1.1.1", "2.2.2.2"],
    });
    expect(detail).toContain("NOERROR");
    expect(detail).toContain("1.1.1.1");
    expect(detail).toContain("UDP");
    expect(detail).toContain("25ms");
  });

  test("DnsChecker server mode: NXDOMAIN shows rcode", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 30,
      error: null,
      name: "example.com",
      port: 53,
      protocol: "udp",
      protocolUsed: "udp",
      rcode: "NXDOMAIN",
      recordType: "A",
      resolver: "server",
      responded: true,
      server: "8.8.8.8",
      valueCount: 0,
      values: [],
    });
    expect(detail).toContain("NXDOMAIN");
  });

  test("DnsChecker server mode: no response shows error", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 500,
      error: "探测超时",
      name: "example.com",
      port: 53,
      protocol: "udp",
      resolver: "server",
      responded: false,
      server: "8.8.8.8",
    });
    expect(detail).toContain("查询失败");
    expect(detail).toContain("探测超时");
  });

  test("DnsChecker server mode: CNAME chain shows CNAME chain", () => {
    const detail = dnsChecker.buildDetail({
      cnameChain: ["cdn.example.com", "cdn-edge.example.net"],
      durationMs: 40,
      error: null,
      name: "example.com",
      port: 53,
      protocol: "udp",
      protocolUsed: "udp",
      rcode: "NOERROR",
      recordType: "A",
      resolver: "server",
      responded: true,
      server: "8.8.8.8",
      valueCount: 1,
      values: ["93.184.216.34"],
    });
    expect(detail).toContain("CNAME: cdn.example.com → cdn-edge.example.net");
  });

  test("DnsChecker server mode: TCP fallback shows TCP in output", () => {
    const detail = dnsChecker.buildDetail({
      durationMs: 50,
      error: null,
      name: "example.com",
      port: 53,
      protocol: "udp",
      protocolUsed: "tcp",
      rcode: "NOERROR",
      recordType: "A",
      resolver: "server",
      responded: true,
      server: "8.8.8.8",
      valueCount: 1,
      values: ["1.2.3.4"],
    });
    expect(detail).toContain("TCP");
  });
});
