import { flushPromises, mount } from "@vue/test-utils"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { createI18n } from "vue-i18n"; import { BusinessError } from "@watchman/api"; import App from "./App.vue"; import zhCN from "../locales/zh-CN/common.json"; import en from "../locales/en/common.json"; vi.mock("@watchman/api", async () => { const actual = await vi.importActual("@watchman/api"); return { ...actual, requestJson: vi.fn(), }; }); import { requestJson } from "@watchman/api"; const mockedRequestJson = vi.mocked(requestJson); function mountApp(locale = "zh-CN") { const i18n = createI18n({ legacy: false, locale, fallbackLocale: "zh-CN", messages: { "zh-CN": zhCN, en, // Intentionally omit th to prove fallbackLocale=zh-CN. }, }); return mount(App, { global: { plugins: [i18n], stubs: { ElConfigProvider: { template: "
" }, ElCard: { template: '
', }, ElTag: { props: ["type"], template: '', }, ElAlert: { props: ["title", "type"], template: '
{{ title }}
', }, }, }, }); } describe("App health three states", () => { beforeEach(() => { mockedRequestJson.mockReset(); }); it("shows loading before the request settles", async () => { let resolveRequest: (value: unknown) => void = () => undefined; mockedRequestJson.mockImplementation( () => new Promise((resolve) => { resolveRequest = resolve; }), ); const wrapper = mountApp(); expect(wrapper.get("[data-testid=health-loading]").text()).toContain("健康检查加载中"); resolveRequest({ status: "ok", app: "watchman", version: "0.1.0" }); await flushPromises(); }); it("shows success after a healthy response", async () => { mockedRequestJson.mockResolvedValue({ status: "ok", app: "watchman", version: "0.1.0", }); const wrapper = mountApp(); await flushPromises(); expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功"); expect(wrapper.get("[data-testid=health-success]").text()).toContain("watchman 0.1.0"); }); it("shows failure after a BusinessError", async () => { mockedRequestJson.mockRejectedValue( new BusinessError({ code: 50000, message: "Internal server error", status: 500, requestId: "req-fail", }), ); const wrapper = mountApp(); await flushPromises(); expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查失败"); expect(wrapper.get("[data-testid=health-failure]").text()).toContain("50000"); }); it("falls back to zh-CN when locale messages are missing", async () => { mockedRequestJson.mockResolvedValue({ status: "ok", app: "watchman", version: "0.1.0", }); const wrapper = mountApp("th"); await flushPromises(); expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功"); }); it("shows english health copy when locale is en", async () => { mockedRequestJson.mockResolvedValue({ status: "ok", app: "watchman", version: "0.1.0", }); const wrapper = mountApp("en"); await flushPromises(); expect(wrapper.get("[data-testid=health-tag]").text()).toContain("Health check succeeded"); expect(wrapper.get("[data-testid=health-success]").text()).toContain("Service available"); }); });