|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { setTrLang } from "@/i18n"; |
| 3 | +import { |
| 4 | + ALERT_RULE_LABEL, |
| 5 | + CREDIT_SOURCE_LABEL, |
| 6 | + METRIC_AXIS_UNIT, |
| 7 | + STATE_LABEL, |
| 8 | + TASK_REPORT_LABEL, |
| 9 | + UPSTREAM_LEVEL_LABEL, |
| 10 | + USAGE_ERROR_LABEL, |
| 11 | + alertRuleLabel, |
| 12 | + creditSourceLabel, |
| 13 | + credentialStateLabel, |
| 14 | + metricAxisUnit, |
| 15 | + taskReportLabel, |
| 16 | + upstreamLevelLabel, |
| 17 | + usageErrorLabel, |
| 18 | +} from "../display"; |
| 19 | +import { TIMELINE_EVENT_LABEL, timelineEventLabel } from "../credentialTimeline"; |
| 20 | +import type { CredentialState } from "../display"; |
| 21 | +import type { UpstreamLevel } from "../types"; |
| 22 | + |
| 23 | +/** |
| 24 | + * 展示用常量表的英文护栏(COD-44 返工)。 |
| 25 | + * |
| 26 | + * 表值必须是**中文源串(msgid)**,翻译留到渲染时由 `xxxLabel()` 过 `tr`: |
| 27 | + * 模块级 `tr(...)` 会把加载瞬间的语言冻进常量,切英文后表里仍是中文 |
| 28 | + * (「尝试 3 · 失败 1」就是这么漏出来的)。这里对每张表逐个取值断言 |
| 29 | + * 英文下无中日韩字符、中文下与表值完全一致。 |
| 30 | + */ |
| 31 | +const CJK = /[\u4e00-\u9fff]/; |
| 32 | + |
| 33 | +afterEach(() => setTrLang("zh")); |
| 34 | + |
| 35 | +describe("展示表在英文下不留中文", () => { |
| 36 | + it("STATE_LABEL → credentialStateLabel", () => { |
| 37 | + const states = Object.keys(STATE_LABEL) as CredentialState[]; |
| 38 | + setTrLang("en"); |
| 39 | + const en = states.map((state) => credentialStateLabel(state)); |
| 40 | + expect(en.filter((label) => CJK.test(label))).toEqual([]); |
| 41 | + setTrLang("zh"); |
| 42 | + expect(states.map((state) => credentialStateLabel(state))) |
| 43 | + .toEqual(states.map((state) => STATE_LABEL[state])); |
| 44 | + }); |
| 45 | + |
| 46 | + it("TASK_REPORT_LABEL → taskReportLabel(含拼接后的整行)", () => { |
| 47 | + const keys = Object.keys(TASK_REPORT_LABEL); |
| 48 | + const report = Object.fromEntries(keys.map((key) => [key, 3])); |
| 49 | + setTrLang("en"); |
| 50 | + expect(CJK.test(taskReportLabel(report))).toBe(false); |
| 51 | + expect(taskReportLabel({ unknown_field: 1 })).toBe("unknown_field 1"); |
| 52 | + // 表里的每个 key 都出现在整行里(不是被 key 兜底吞掉) |
| 53 | + expect(taskReportLabel(report)).not.toContain("succeeded 3"); |
| 54 | + setTrLang("zh"); |
| 55 | + expect(taskReportLabel({ attempted: 3, failed: 1 })).toBe("尝试 3 · 失败 1"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("CREDIT_SOURCE_LABEL → creditSourceLabel", () => { |
| 59 | + const sources = Object.keys(CREDIT_SOURCE_LABEL); |
| 60 | + setTrLang("en"); |
| 61 | + expect(sources.map(creditSourceLabel).filter((label) => CJK.test(label))).toEqual([]); |
| 62 | + expect(creditSourceLabel("nope")).toBe("nope"); |
| 63 | + setTrLang("zh"); |
| 64 | + expect(sources.map(creditSourceLabel)).toEqual(sources.map((s) => CREDIT_SOURCE_LABEL[s])); |
| 65 | + }); |
| 66 | + |
| 67 | + it("METRIC_AXIS_UNIT → metricAxisUnit(无单位指标返回 undefined)", () => { |
| 68 | + const metrics = Object.keys(METRIC_AXIS_UNIT); |
| 69 | + setTrLang("en"); |
| 70 | + expect(metrics.map(metricAxisUnit).filter((unit) => CJK.test(String(unit)))).toEqual([]); |
| 71 | + expect(metricAxisUnit("latency")).toBeUndefined(); |
| 72 | + expect(metricAxisUnit("ttfb")).toBeUndefined(); |
| 73 | + setTrLang("zh"); |
| 74 | + expect(metrics.map(metricAxisUnit)).toEqual(metrics.map((m) => METRIC_AXIS_UNIT[m])); |
| 75 | + }); |
| 76 | + |
| 77 | + it("USAGE_ERROR_LABEL → usageErrorLabel(未知类型兜底也不回中文)", () => { |
| 78 | + const types = Object.keys(USAGE_ERROR_LABEL); |
| 79 | + setTrLang("en"); |
| 80 | + expect(types.map(usageErrorLabel).filter((label) => CJK.test(label))).toEqual([]); |
| 81 | + expect(CJK.test(usageErrorLabel(null))).toBe(false); |
| 82 | + expect(CJK.test(usageErrorLabel("brand_new"))).toBe(false); |
| 83 | + setTrLang("zh"); |
| 84 | + expect(types.map(usageErrorLabel)).toEqual(types.map((t) => USAGE_ERROR_LABEL[t as keyof typeof USAGE_ERROR_LABEL])); |
| 85 | + }); |
| 86 | + |
| 87 | + it("UPSTREAM_LEVEL_LABEL → upstreamLevelLabel(未知档位兜底)", () => { |
| 88 | + const levels = Object.keys(UPSTREAM_LEVEL_LABEL) as UpstreamLevel[]; |
| 89 | + setTrLang("en"); |
| 90 | + expect(levels.map(upstreamLevelLabel).filter((label) => CJK.test(label))).toEqual([]); |
| 91 | + expect(CJK.test(upstreamLevelLabel("brand_new"))).toBe(false); |
| 92 | + setTrLang("zh"); |
| 93 | + expect(levels.map(upstreamLevelLabel)).toEqual(levels.map((l) => UPSTREAM_LEVEL_LABEL[l])); |
| 94 | + }); |
| 95 | + |
| 96 | + it("TIMELINE_EVENT_LABEL → timelineEventLabel(未知事件原样透出)", () => { |
| 97 | + const events = Object.keys(TIMELINE_EVENT_LABEL); |
| 98 | + setTrLang("en"); |
| 99 | + expect(events.map(timelineEventLabel).filter((label) => CJK.test(label))).toEqual([]); |
| 100 | + expect(timelineEventLabel("brand_new")).toBe("brand_new"); |
| 101 | + setTrLang("zh"); |
| 102 | + expect(events.map(timelineEventLabel)).toEqual(events.map((e) => TIMELINE_EVENT_LABEL[e])); |
| 103 | + }); |
| 104 | + |
| 105 | + it("ALERT_RULE_LABEL → alertRuleLabel", () => { |
| 106 | + const rules = Object.keys(ALERT_RULE_LABEL); |
| 107 | + setTrLang("en"); |
| 108 | + expect(rules.map(alertRuleLabel).filter((label) => CJK.test(label))).toEqual([]); |
| 109 | + setTrLang("zh"); |
| 110 | + expect(rules.map(alertRuleLabel)).toEqual(rules.map((r) => ALERT_RULE_LABEL[r])); |
| 111 | + }); |
| 112 | +}); |
0 commit comments