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

import { checkKeyedExpectations, resolveKeyedExpectations } from "../../../../../src/server/checker/expect/keyed";

describe("KeyedExpectations", () => {
  test("resolve 将 Raw Record 转为保持顺序的有序数组", () => {
    expect(resolveKeyedExpectations({ Count: { gte: 1 }, Name: "alice" })).toEqual([
      { key: "Count", matcher: { gte: 1 } },
      { key: "Name", matcher: { equals: "alice" } },
    ]);
  });

  test("failure.path 包含基础路径和原始 key", () => {
    const result = checkKeyedExpectations(
      { "content-type": "text/plain" },
      resolveKeyedExpectations({ "Content-Type": { contains: "json" } }),
      { normalizeKey: (key) => key.toLowerCase(), path: "headers", phase: "headers" },
    );

    expect(result.matched).toBe(false);
    expect(result.failure?.path).toBe("headers.Content-Type");
  });

  test("DB rows 默认保持大小写敏感匹配", () => {
    const expectations = resolveKeyedExpectations({ Name: "alice", name: "bob" });

    expect(
      checkKeyedExpectations({ Name: "alice", name: "bob" }, expectations, { path: "rows[0]", phase: "rows" }).matched,
    ).toBe(true);
    expect(
      checkKeyedExpectations({ Name: "bob", name: "alice" }, expectations, { path: "rows[0]", phase: "rows" }).matched,
    ).toBe(false);
  });
});
