|
| 1 | +import { screen, waitFor, within } from "@testing-library/react"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { I18nProvider } from "@/i18n"; |
| 4 | +import { PoolOverview } from "@/components/PoolOverview"; |
| 5 | +import type { Credential } from "@/api/types"; |
| 6 | +import { makeCredential, renderPage } from "@/test-utils"; |
| 7 | +import { AlertsPage } from "../AlertsPage"; |
| 8 | + |
| 9 | +const CJK = /[\u4e00-\u9fff]/; |
| 10 | + |
| 11 | +/** 英文态渲染页面(Layout 提供会话与导航,Provider 决定语言)。 */ |
| 12 | +function renderEn(ui: React.ReactNode) { |
| 13 | + return renderPage(<I18nProvider initial="en">{ui}</I18nProvider>, { |
| 14 | + username: "root", |
| 15 | + is_admin: true, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function stubFetch(body: unknown) { |
| 20 | + const spy = vi.fn( |
| 21 | + async () => |
| 22 | + new Response(JSON.stringify(body), { |
| 23 | + status: 200, |
| 24 | + headers: { "Content-Type": "application/json" }, |
| 25 | + }), |
| 26 | + ); |
| 27 | + vi.stubGlobal("fetch", spy); |
| 28 | + return spy; |
| 29 | +} |
| 30 | + |
| 31 | +describe("告警页 / 上游概况:英文态插值文案不再漏中文", () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.unstubAllGlobals(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("告警页标题与记录 aria-label 均为英文(含条数插值)", async () => { |
| 37 | + stubFetch({ |
| 38 | + total: 1, |
| 39 | + alerts: [ |
| 40 | + { |
| 41 | + id: "alert_1", |
| 42 | + ts: 1_700_000_000, |
| 43 | + rule: "pool_empty", |
| 44 | + severity: "critical", |
| 45 | + scope: "pool", |
| 46 | + message: "No credentials available", |
| 47 | + detail: "{}", |
| 48 | + delivered: 1, |
| 49 | + delivery_error: null, |
| 50 | + resolved_at: null, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }); |
| 54 | + renderEn(<AlertsPage />); |
| 55 | + const main = await screen.findByRole("main"); |
| 56 | + await waitFor( |
| 57 | + () => expect(within(main).getByTestId("alerts-table")).toBeInTheDocument(), |
| 58 | + { timeout: 3000 }, |
| 59 | + ); |
| 60 | + |
| 61 | + const text = main.textContent ?? ""; |
| 62 | + expect(text).toContain("Recent alerts"); |
| 63 | + expect(text).toContain("(1)"); |
| 64 | + expect(text).not.toContain("最近告警"); |
| 65 | + |
| 66 | + const labels = [...main.querySelectorAll("[aria-label]")].map( |
| 67 | + (el) => el.getAttribute("aria-label") ?? "", |
| 68 | + ); |
| 69 | + expect(labels.length).toBeGreaterThan(0); |
| 70 | + for (const label of labels) expect(label).not.toMatch(CJK); |
| 71 | + expect(labels.join(" | ")).toContain("Alerts"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("上游概况健康度分布的 aria-label 为英文(含四态插值)", () => { |
| 75 | + const credentials = [makeCredential({}) as Credential]; |
| 76 | + renderEn(<PoolOverview credentials={credentials} now={Date.now() / 1000} />); |
| 77 | + |
| 78 | + const label = screen.getByTestId("pool-overview").querySelector("[aria-label]"); |
| 79 | + const text = label?.getAttribute("aria-label") ?? ""; |
| 80 | + expect(text).toMatch(/Health distribution/); |
| 81 | + expect(text).not.toMatch(CJK); |
| 82 | + }); |
| 83 | +}); |
0 commit comments