返回 CodeWhale
fallback.test.ts
根目录 / web / lib / i18n / fallback.test.ts
1 import { readdirSync } from "node:fs";
2 import { fileURLToPath } from "node:url";
3 import { describe, expect, it } from "vitest";
4 import {
5 bodyDateLocale,
6 bodyFallsBackToEnglish,
7 docsChromeFallsBackToEnglish,
8 docsEnglishTopics,
9 DOCS_TOPICS,
10 fallbackAttrs,
11 } from "./fallback";
12
13 describe("fallback helper", () => {
14 it("treats English and Chinese bodies as translated, and a partial locale as English", () => {
15 expect(bodyFallsBackToEnglish("/changelog", "en")).toBe(false);
16 expect(bodyFallsBackToEnglish("/changelog", "zh")).toBe(false);
17 expect(bodyFallsBackToEnglish("/changelog", "ar")).toBe(true);
18 expect(bodyFallsBackToEnglish("/models", "ja")).toBe(true);
19 expect(fallbackAttrs("/models", "ar")).toEqual({ lang: "en", dir: "ltr" });
20 expect(fallbackAttrs("/models", "zh")).toBeUndefined();
21 expect(fallbackAttrs("/models", "en")).toBeUndefined();
22 });
23
24 it("follows the page's own dictionary where one exists", () => {
25 expect(bodyFallsBackToEnglish("/computer-use", "ar")).toBe(false);
26 expect(bodyFallsBackToEnglish("/computer-use", "ja")).toBe(false);
27 expect(bodyFallsBackToEnglish("/models", "ar")).toBe(true);
28 });
29
30 it("formats dates in English inside an English body only", () => {
31 expect(bodyDateLocale("/models", "ar")).toBe(bodyDateLocale("/models", "en"));
32 expect(bodyDateLocale("/models", "zh")).not.toBe(bodyDateLocale("/models", "en"));
33 });
34
35 it("labels the docs chrome English wherever the docs shell dictionary is missing", () => {
36 expect(docsChromeFallsBackToEnglish("en")).toBe(false);
37 expect(docsChromeFallsBackToEnglish("zh")).toBe(false);
38 expect(docsChromeFallsBackToEnglish("ar")).toBe(true);
39 });
40
41 it("lists English docs topics for Arabic and none for English or Chinese", () => {
42 const ar = docsEnglishTopics("ar");
43 expect(ar).toContain("modes");
44 expect(ar).toContain("");
45 expect(ar).not.toContain("guide");
46 expect(docsEnglishTopics("en")).toEqual([]);
47 expect(docsEnglishTopics("zh")).not.toContain("modes");
48 expect(docsEnglishTopics("zh")).toContain("runtime-reference");
49 });
50
51 it("names every docs topic directory", () => {
52 const dir = fileURLToPath(new URL("../../app/[locale]/docs", import.meta.url));
53 const topics = readdirSync(dir, { withFileTypes: true })
54 .filter((entry) => entry.isDirectory() && !entry.name.startsWith("_"))
55 .map((entry) => entry.name)
56 .sort();
57 expect([...DOCS_TOPICS].sort()).toEqual(topics);
58 });
59 });
60
60 lines TYPESCRIPT