|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { i18nName, i18nDescription } from "./locales"; |
| 3 | +import type { SCMetadata } from "@App/app/repo/scripts"; |
| 4 | +import i18n from "i18next"; |
| 5 | + |
| 6 | +describe.concurrent("i18nName", () => { |
| 7 | + it("完全匹配语言时返回对应的国际化名称", () => { |
| 8 | + // 模拟当前语言为 zh-cn |
| 9 | + i18n.language = "zh-CN"; |
| 10 | + |
| 11 | + const script = { |
| 12 | + name: "Default Script Name", |
| 13 | + metadata: { |
| 14 | + "name:zh-cn": ["中文脚本名"], |
| 15 | + "name:en-us": ["English Script Name"], |
| 16 | + } as SCMetadata, |
| 17 | + }; |
| 18 | + |
| 19 | + const result = i18nName(script); |
| 20 | + expect(result).toBe("中文脚本名"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("前缀匹配语言时返回对应的国际化名称", () => { |
| 24 | + // 模拟当前语言为 zh-tw,但只有 zh 前缀的名称 |
| 25 | + i18n.language = "zh-TW"; |
| 26 | + |
| 27 | + const script = { |
| 28 | + name: "Default Script Name", |
| 29 | + metadata: { |
| 30 | + "name:zh": ["中文脚本名"], |
| 31 | + "name:en": ["English Script Name"], |
| 32 | + } as SCMetadata, |
| 33 | + }; |
| 34 | + |
| 35 | + const result = i18nName(script); |
| 36 | + expect(result).toBe("中文脚本名"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("没有匹配的国际化名称时返回默认名称", () => { |
| 40 | + i18n.language = "ja-JP"; |
| 41 | + |
| 42 | + const script = { |
| 43 | + name: "Default Script Name", |
| 44 | + metadata: { |
| 45 | + "name:zh-cn": ["中文脚本名"], |
| 46 | + "name:en-us": ["English Script Name"], |
| 47 | + } as SCMetadata, |
| 48 | + }; |
| 49 | + |
| 50 | + const result = i18nName(script); |
| 51 | + expect(result).toBe("Default Script Name"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("metadata 为空时返回默认名称", () => { |
| 55 | + i18n.language = "en-US"; |
| 56 | + |
| 57 | + const script = { |
| 58 | + name: "Default Script Name", |
| 59 | + metadata: {} as SCMetadata, |
| 60 | + }; |
| 61 | + |
| 62 | + const result = i18nName(script); |
| 63 | + expect(result).toBe("Default Script Name"); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe.concurrent("i18nDescription", () => { |
| 68 | + it("完全匹配语言时返回对应的国际化描述", () => { |
| 69 | + i18n.language = "zh-CN"; |
| 70 | + |
| 71 | + const script = { |
| 72 | + metadata: { |
| 73 | + description: ["Default description"], |
| 74 | + "description:zh-cn": ["中文描述"], |
| 75 | + "description:en-us": ["English description"], |
| 76 | + } as SCMetadata, |
| 77 | + }; |
| 78 | + |
| 79 | + const result = i18nDescription(script); |
| 80 | + expect(result).toBe("中文描述"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("前缀匹配语言时返回对应的国际化描述", () => { |
| 84 | + i18n.language = "zh-TW"; |
| 85 | + |
| 86 | + const script = { |
| 87 | + metadata: { |
| 88 | + description: ["Default description"], |
| 89 | + "description:zh": ["中文描述"], |
| 90 | + "description:en": ["English description"], |
| 91 | + } as SCMetadata, |
| 92 | + }; |
| 93 | + |
| 94 | + const result = i18nDescription(script); |
| 95 | + expect(result).toBe("中文描述"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("没有匹配的国际化描述时返回默认描述", () => { |
| 99 | + i18n.language = "ja-JP"; |
| 100 | + |
| 101 | + const script = { |
| 102 | + metadata: { |
| 103 | + description: ["Default description"], |
| 104 | + "description:zh-cn": ["中文描述"], |
| 105 | + "description:en-us": ["English description"], |
| 106 | + } as SCMetadata, |
| 107 | + }; |
| 108 | + |
| 109 | + const result = i18nDescription(script); |
| 110 | + expect(result).toBe("Default description"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("没有 description 字段时返回 undefined", () => { |
| 114 | + i18n.language = "en-US"; |
| 115 | + |
| 116 | + const script = { |
| 117 | + metadata: {} as SCMetadata, |
| 118 | + }; |
| 119 | + |
| 120 | + const result = i18nDescription(script); |
| 121 | + expect(result).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("description 字段为空数组时返回 undefined", () => { |
| 125 | + i18n.language = "en-US"; |
| 126 | + |
| 127 | + const script = { |
| 128 | + metadata: { |
| 129 | + description: [], |
| 130 | + } as SCMetadata, |
| 131 | + }; |
| 132 | + |
| 133 | + const result = i18nDescription(script); |
| 134 | + expect(result).toBeUndefined(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments