Files
watchman/web/apps/console/src/App.spec.ts
T
unknownandCursor 8fb16f4a4a feat(infra): 落地 M01 基础框架与本机开发环境
交付 FastAPI/Celery/Vue 薄壳、FORCE RLS、Compose 回环与 review 修复证据,关闭 M01 DONE_LOCAL。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 15:00:39 +08:00

124 lines
3.8 KiB
TypeScript

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<typeof import("@watchman/api")>("@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: "<div><slot /></div>" },
ElCard: {
template:
'<div class="el-card"><div class="el-card__header"><slot name="header" /></div><slot /></div>',
},
ElTag: {
props: ["type"],
template: '<span data-testid="health-tag"><slot /></span>',
},
ElAlert: {
props: ["title", "type"],
template:
'<div :data-testid="type === \'success\' ? \'health-success\' : \'health-failure\'">{{ title }}</div>',
},
},
},
});
}
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");
});
});