import { expect, test } from "vitest";

import { errorMessageKey } from "./error-message";
import { HttpError, InUseError } from "./queries";

test("maps HTTP statuses to specific keys", () => {
  expect(errorMessageKey(new HttpError(403))).toEqual({ key: "errors.forbidden" });
  expect(errorMessageKey(new HttpError(404))).toEqual({ key: "errors.notFound" });
  expect(errorMessageKey(new HttpError(409))).toEqual({ key: "errors.conflict" });
  expect(errorMessageKey(new HttpError(422))).toEqual({ key: "errors.validation" });
  expect(errorMessageKey(new HttpError(500))).toEqual({ key: "errors.server" });
  expect(errorMessageKey(new HttpError(502))).toEqual({ key: "errors.server" });
});

test("an unmapped status falls back to the generic key", () => {
  expect(errorMessageKey(new HttpError(418))).toEqual({ key: "toast.error" });
});

test("InUseError carries the count", () => {
  expect(errorMessageKey(new InUseError(3))).toEqual({ key: "actions.inUse", opts: { count: 3 } });
});

test("a bare Error or unknown maps to the generic key", () => {
  expect(errorMessageKey(new Error("boom"))).toEqual({ key: "toast.error" });
  expect(errorMessageKey(null)).toEqual({ key: "toast.error" });
});
