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

import { validateDbConfig } from "../../../../../src/server/checker/runner/db/validate";

describe("validateDbConfig", () => {
  test("空配置无问题", () => {
    const result = validateDbConfig({ defaults: {}, targets: [] });
    expect(result).toHaveLength(0);
  });

  test("缺少 db.url 返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ id: "test", name: "test", type: "db" }],
    });
    expect(result.length).toBeGreaterThan(0);
    const dbError = result.find((e) => e.path.includes("db"));
    expect(dbError).toBeDefined();
    expect(dbError!.code).toBe("required");
  });

  test("db.url 为空字符串返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ db: { url: "" }, id: "test", name: "test", type: "db" }],
    });
    const urlError = result.find((e) => e.path.includes("db.url"));
    expect(urlError).toBeDefined();
    expect(urlError!.code).toBe("required");
  });

  test("db.query 为空字符串返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ db: { query: "", url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
    });
    const queryError = result.find((e) => e.path.includes("db.query"));
    expect(queryError).toBeDefined();
    expect(queryError!.code).toBe("invalid-value");
  });

  test("db 分组未知字段返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ db: { timeout: 5, url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
    });
    const unknownError = result.find((e) => e.path.includes("db.timeout"));
    expect(unknownError).toBeDefined();
    expect(unknownError!.code).toBe("unknown-field");
  });

  test("expect.durationMs 数组简写返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        {
          db: { url: "sqlite://:memory:" },
          expect: { durationMs: [1, 2] },
          id: "test",
          name: "test",
          type: "db",
        },
      ],
    });
    const durationError = result.find((e) => e.path.includes("expect.durationMs"));
    expect(durationError).toBeDefined();
    expect(durationError!.code).toBe("invalid-type");
  });

  test("expect.rowCount 非法 operator 返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        { db: { url: "sqlite://:memory:" }, expect: { rowCount: { foo: 1 } }, id: "test", name: "test", type: "db" },
      ],
    });
    const rowCountError = result.find((e) => e.path.includes("expect.rowCount"));
    expect(rowCountError).toBeDefined();
    expect(rowCountError!.code).toBe("unknown-matcher");
  });

  test("expect.rows 不是数组返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        { db: { url: "sqlite://:memory:" }, expect: { rows: "not-array" }, id: "test", name: "test", type: "db" },
      ],
    });
    const rowsError = result.find((e) => e.path.includes("expect.rows"));
    expect(rowsError).toBeDefined();
    expect(rowsError!.code).toBe("invalid-type");
  });

  test("expect.rows 元素不是对象返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        { db: { url: "sqlite://:memory:" }, expect: { rows: ["not-object"] }, id: "test", name: "test", type: "db" },
      ],
    });
    const rowError = result.find((e) => e.path.includes("expect.rows[0]"));
    expect(rowError).toBeDefined();
    expect(rowError!.code).toBe("invalid-type");
  });

  test("expect.rows 中 regex 正则非法返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        {
          db: { url: "sqlite://:memory:" },
          expect: { rows: [{ name: { regex: "[invalid" } }] },
          id: "test",
          name: "test",
          type: "db",
        },
      ],
    });
    const matchError = result.find((e) => e.path.includes("expect.rows[0].name"));
    expect(matchError).toBeDefined();
    expect(matchError!.code).toBe("invalid-regex");
  });

  test("expect 未知字段返回错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ db: { url: "sqlite://:memory:" }, expect: { status: [200] }, id: "test", name: "test", type: "db" }],
    });
    const unknownError = result.find((e) => e.path.includes("expect.status"));
    expect(unknownError).toBeDefined();
    expect(unknownError!.code).toBe("unknown-field");
  });

  test("有效配置无错误", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        {
          db: { query: "SELECT 1", url: "sqlite://:memory:" },
          expect: { durationMs: { lte: 5000 }, rowCount: { gte: 1 }, rows: [{ cnt: { gte: 1 } }] },
          id: "test",
          name: "test",
          type: "db",
        },
      ],
    });
    expect(result).toHaveLength(0);
  });

  test("忽略非 db 类型 target", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [{ id: "test", name: "test", type: "http" }],
    });
    expect(result).toHaveLength(0);
  });

  test("多个 db target 分别校验", () => {
    const result = validateDbConfig({
      defaults: {},
      targets: [
        { db: { url: "sqlite://:memory:" }, id: "db1", name: "db1", type: "db" },
        { db: { url: "" }, id: "db2", name: "db2", type: "db" },
      ],
    });
    expect(result.length).toBeGreaterThan(0);
    const db2Error = result.find((e) => e.targetName === "db2");
    expect(db2Error).toBeDefined();
  });
});
