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

import { normalizeAuthoringConfig } from "../../../../../src/server/checker/normalizer";
import { checkerRegistry } from "../../../../../src/server/checker/runner";
import { validateProbeConfigContract } from "../../../../../src/server/checker/schema/validate";

describe("DNS normalize", () => {
  test("ValueMatcher 简写被展开", () => {
    const result = normalizeAuthoringConfig(
      {
        targets: [
          {
            dns: { name: "example.com", resolver: "server", server: "8.8.8.8" },
            expect: { durationMs: 1000, valueCount: { gte: 1 } },
            id: "dns-test",
            type: "dns",
          },
        ],
      },
      checkerRegistry,
    );
    expect(result.issues).toHaveLength(0);
    const target = (result.config as { targets: Array<{ expect: Record<string, unknown> }> }).targets[0]!;
    expect(target.expect["durationMs"]).toEqual({ equals: 1000 });
    expect(target.expect["valueCount"]).toEqual({ gte: 1 });
  });

  test("ContentExpectations 简写被展开", () => {
    const result = normalizeAuthoringConfig(
      {
        targets: [
          {
            dns: { name: "example.com", resolver: "server", server: "8.8.8.8" },
            expect: { result: [{ contains: "NOERROR" }] },
            id: "dns-test",
            type: "dns",
          },
        ],
      },
      checkerRegistry,
    );
    expect(result.issues).toHaveLength(0);
    const target = (result.config as { targets: Array<{ expect: Record<string, unknown> }> }).targets[0]!;
    const resultExpect = target.expect["result"] as Array<Record<string, unknown>>;
    expect(resultExpect[0]!["kind"]).toBe("value");
    expect((resultExpect[0]!["matcher"] as Record<string, unknown>)["contains"]).toBe("NOERROR");
  });

  test("DNS authoring 简写经 normalize 后通过 normalized contract 校验", () => {
    const result = normalizeAuthoringConfig(
      {
        targets: [
          {
            dns: { name: "example.com", resolver: "server", server: "8.8.8.8" },
            expect: { durationMs: 1000, result: [{ contains: "NOERROR" }], valueCount: { gte: 1 } },
            id: "dns-test",
            type: "dns",
          },
        ],
      },
      checkerRegistry,
    );
    expect(result.issues).toHaveLength(0);
    const contract = validateProbeConfigContract(result.config, checkerRegistry);
    expect(contract.config).not.toBeNull();
    expect(contract.issues).toHaveLength(0);
  });

  test("DNS system 模式 ValueMatcher 简写被展开", () => {
    const result = normalizeAuthoringConfig(
      {
        targets: [
          {
            dns: { name: "example.com", resolver: "system" },
            expect: { durationMs: 500, valueCount: { gte: 1 } },
            id: "dns-system-test",
            type: "dns",
          },
        ],
      },
      checkerRegistry,
    );
    expect(result.issues).toHaveLength(0);
    const target = (result.config as { targets: Array<{ expect: Record<string, unknown> }> }).targets[0]!;
    expect(target.expect["durationMs"]).toEqual({ equals: 500 });
  });
});
