import { describe, expect, it, vi } from "vitest";
import { HttpError } from "../../../shared/api/errors";
import { createDictionaryApi } from "../dictionaryApi";
import { dictionaryFixture } from "./fixtures";

describe("dictionary API", () => {
  it("只请求业务相对路径，不重复拼接/api/v1", async () => {
    const get = vi.fn().mockResolvedValue({
      data: dictionaryFixture(),
      requestId: "dict-1",
      httpStatus: 200,
    });
    await createDictionaryApi({ get }).getUiDictionaries();
    expect(get).toHaveBeenCalledTimes(1);
    expect(get).toHaveBeenCalledWith("config/ui-dictionaries", { signal: undefined });
  });

  it("响应不符合契约时保留requestId并拒绝", async () => {
    const get = vi.fn().mockResolvedValue({
      data: { version: "bad" },
      requestId: "dict-invalid",
      httpStatus: 200,
    });
    await expect(createDictionaryApi({ get }).getUiDictionaries())
      .rejects.toMatchObject({
        code: "INTERNAL_ERROR",
        requestId: "dict-invalid",
      } satisfies Partial<HttpError>);
  });
});
