import { afterEach, describe, expect, it, vi } from "vitest"; import { commentRequest, COMMENT_ERRORS } from "./client"; afterEach(() => vi.unstubAllGlobals()); describe("comment error language", () => { it("supplies Thai messages for every error code", () => { for (const message of Object.values(COMMENT_ERRORS)) expect(message).toMatch(/[\u0e00-\u0e7f]/); }); it("shows Thai server errors and preserves uncached requests", async () => { const fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: "comment-edited-reload" }), { status: 409 })); vi.stubGlobal("fetch", fetch); await expect(commentRequest("/api/comments/test")).rejects.toThrow("ข้อความร่างของคุณยังอยู่"); expect(fetch).toHaveBeenCalledWith("/api/comments/test", { cache: "no-store" }); }); it("uses a Thai fallback when an error response is not JSON", async () => { vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response("Unavailable", { status: 503 }))); await expect(commentRequest("/api/comments")).rejects.toThrow("ไม่สามารถทำรายการได้ กรุณาลองใหม่"); }); });