212 lines
7.5 KiB
Go
212 lines
7.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
)
|
|
|
|
func TestOfficialReferenceLanguagePacks(t *testing.T) {
|
|
for _, rel := range []string{
|
|
filepath.Join("testdata", "official.lang.zh-tw"),
|
|
filepath.Join("testdata", "official.lang.ja-jp"),
|
|
} {
|
|
reports, findings, err := CheckLocalePack(rel)
|
|
if err != nil {
|
|
t.Fatalf("CheckLocalePack(%s): %v", rel, err)
|
|
}
|
|
if len(reports) != 1 {
|
|
t.Fatalf("CheckLocalePack(%s): expected 1 report, got %d", rel, len(reports))
|
|
}
|
|
for _, f := range findings {
|
|
if f.Severity == "error" || f.Severity == "warn" {
|
|
t.Fatalf("CheckLocalePack(%s) unexpected %s (%s): %s", rel, f.Severity, f.Check, f.Message)
|
|
}
|
|
}
|
|
}
|
|
|
|
// End-to-end Manager test: link and enable both official reference packs.
|
|
dataRoot := t.TempDir()
|
|
mgr, err := NewManager(ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.8"})
|
|
if err != nil {
|
|
t.Fatalf("NewManager: %v", err)
|
|
}
|
|
defer mgr.Close()
|
|
|
|
twAbs, err := filepath.Abs(filepath.Join("testdata", "official.lang.zh-tw"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jaAbs, err := filepath.Abs(filepath.Join("testdata", "official.lang.ja-jp"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := mgr.LinkDev(context.Background(), twAbs); err != nil {
|
|
t.Fatalf("LinkDev zh-tw: %v", err)
|
|
}
|
|
if err := mgr.Enable(context.Background(), "official.lang.zh-tw", false); err != nil {
|
|
t.Fatalf("Enable zh-tw: %v", err)
|
|
}
|
|
if _, err := mgr.LinkDev(context.Background(), jaAbs); err != nil {
|
|
t.Fatalf("LinkDev ja-jp: %v", err)
|
|
}
|
|
if err := mgr.Enable(context.Background(), "official.lang.ja-jp", false); err != nil {
|
|
t.Fatalf("Enable ja-jp: %v", err)
|
|
}
|
|
|
|
uiPacks, backendPacks := mgr.ResolvedLocalePacks()
|
|
i18n.SetPluginCatalogs(backendPacks)
|
|
defer i18n.SetPluginCatalogs(nil)
|
|
|
|
if len(uiPacks) != 2 {
|
|
t.Fatalf("expected 2 UI locale packs, got %d", len(uiPacks))
|
|
}
|
|
|
|
// 1. Verify zh-HK -> zh-TW -> zh-CN -> en-US multi-hop fallback!
|
|
if got := i18n.T(i18n.Locale("zh-HK"), "validate.invalid_memory"); got != "分配記憶體不得小於 2G(例如 8G、16G)" {
|
|
t.Fatalf("zh-HK -> zh-TW hop failed: got %q", got)
|
|
}
|
|
// Key not overridden in zh-TW should fall back to zh-CN (NOT en-US!).
|
|
if got := i18n.T(i18n.Locale("zh-HK"), "doctor.os"); got != "操作系统" {
|
|
t.Fatalf("zh-HK -> zh-TW -> zh-CN hop failed: got %q, want 操作系统", got)
|
|
}
|
|
|
|
// 2. Verify ja-JP -> en-US fallback & FormatGuestTerm dual terminology!
|
|
if got := i18n.T(i18n.Locale("ja-JP"), "guide.select_disk.btn.erase"); got != "ディスクユーティリティで消去する" {
|
|
t.Fatalf("ja-JP native translation failed: got %q", got)
|
|
}
|
|
if got := i18n.T(i18n.Locale("ja-JP"), "doctor.os"); got != "Operating System" {
|
|
t.Fatalf("ja-JP -> en-US fallback failed: got %q, want Operating System", got)
|
|
}
|
|
if dual := i18n.FormatGuestTerm(i18n.Locale("zh-TW"), "en-US:0", "macos.diskUtility", true); dual != "「磁碟工具程式 (Disk Utility)」" {
|
|
t.Fatalf("FormatGuestTerm(zh-TW, en-US:0) = %q", dual)
|
|
}
|
|
if dual := i18n.FormatGuestTerm(i18n.Locale("ja-JP"), "en-US:0", "macos.diskUtility", true); dual != "「ディスクユーティリティ (Disk Utility)」" {
|
|
t.Fatalf("FormatGuestTerm(ja-JP, en-US:0) = %q", dual)
|
|
}
|
|
if single := i18n.FormatGuestTerm(i18n.Locale("ja-JP"), "ja-JP:0", "macos.diskUtility", true); single != "「ディスクユーティリティ」" {
|
|
t.Fatalf("FormatGuestTerm(ja-JP, ja-JP:0) = %q", single)
|
|
}
|
|
}
|
|
|
|
func TestInitAndCheckLocalePackCLIWorkflow(t *testing.T) {
|
|
// 1. Scaffold full zh-TW host language pack -> should validate at 100% coverage with 0 errors.
|
|
outTW := filepath.Join(t.TempDir(), "lang-zh-tw")
|
|
if err := InitLocalePack(LocaleInitOptions{
|
|
Locale: "zh-TW",
|
|
OutDir: outTW,
|
|
}); err != nil {
|
|
t.Fatalf("InitLocalePack(zh-TW): %v", err)
|
|
}
|
|
reps, _, err := CheckLocalePack(outTW)
|
|
if err != nil {
|
|
t.Fatalf("CheckLocalePack(scaffolded zh-TW): %v", err)
|
|
}
|
|
if len(reps) != 1 || reps[0].Errors != 0 || reps[0].TotalPercent != 100.0 {
|
|
t.Fatalf("expected 100%% coverage and 0 errors on freshly scaffolded pack, got %+v", reps)
|
|
}
|
|
|
|
// Note: scaffolding zh-TW copies zh-CN source text as a starting template, so regional
|
|
// lint should proactively flag Mainland terms like 默认/内存/磁盘 for the translator!
|
|
if reps[0].Warnings == 0 {
|
|
t.Fatal("expected regional terminology warnings on untranslated zh-CN -> zh-TW template")
|
|
}
|
|
|
|
// 2. Scaffold cross-plugin override pack (--for acme.backup).
|
|
outFor := filepath.Join(t.TempDir(), "lang-for-plugin")
|
|
if err := InitLocalePack(LocaleInitOptions{
|
|
Locale: "ko-KR",
|
|
OutDir: outFor,
|
|
ForPlugin: "acme.backup",
|
|
}); err != nil {
|
|
t.Fatalf("InitLocalePack(--for): %v", err)
|
|
}
|
|
repsFor, _, err := CheckLocalePack(outFor)
|
|
if err != nil || len(repsFor) != 1 || repsFor[0].Errors != 0 {
|
|
t.Fatalf("CheckLocalePack(--for) failed: err=%v reps=%+v", err, repsFor)
|
|
}
|
|
|
|
// 3. Inject an ICU placeholder mismatch and a syntax error -> CheckLocalePack must report errors.
|
|
badDir := filepath.Join(t.TempDir(), "bad-pack")
|
|
if err := os.MkdirAll(filepath.Join(badDir, "locales"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(badDir, "locales", "core.json"), []byte(`{
|
|
"footerDiskValue": "{wrongVar} GB free",
|
|
"staleKeyNotInHost": "hello"
|
|
}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(badDir, "plugin.json"), []byte(`{
|
|
"schema": 1,
|
|
"id": "test.lang.de",
|
|
"version": "0.1.0",
|
|
"name": "Deutsch",
|
|
"contributes": {
|
|
"locales": [{"locale": "de-DE", "files": {"core": "locales/core.json"}}]
|
|
}
|
|
}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
badReps, badFindings, err := CheckLocalePack(badDir)
|
|
if err != nil {
|
|
t.Fatalf("CheckLocalePack(badDir): %v", err)
|
|
}
|
|
if len(badReps) != 1 || badReps[0].Errors == 0 || len(badReps[0].UnknownKeys) != 1 {
|
|
t.Fatalf("expected placeholder mismatch error and 1 unknown key, got %+v", badReps)
|
|
}
|
|
hasParamErr := false
|
|
for _, f := range badFindings {
|
|
if f.Check == "icu-params" && strings.Contains(f.Message, "footerDiskValue") {
|
|
hasParamErr = true
|
|
}
|
|
}
|
|
if !hasParamErr {
|
|
t.Fatalf("expected icu-params finding for footerDiskValue, got %+v", badFindings)
|
|
}
|
|
|
|
// Missing a newly added host placeholder (strict subset of expected vars)
|
|
// should warn rather than hard-block previously published language packs.
|
|
subsetDir := filepath.Join(t.TempDir(), "subset-pack")
|
|
if err := os.MkdirAll(filepath.Join(subsetDir, "locales"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(subsetDir, "locales", "native.json"), []byte(`{
|
|
"err.commit_memory": "Need {need} (avail {avail}, rec {rec})"
|
|
}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(subsetDir, "plugin.json"), []byte(`{
|
|
"schema": 1,
|
|
"id": "test.lang.fr",
|
|
"version": "1.0.0",
|
|
"name": "Français",
|
|
"contributes": {
|
|
"locales": [{"locale": "fr-FR", "files": {"native": "locales/native.json"}}]
|
|
}
|
|
}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subReps, subFindings, err := CheckLocalePack(subsetDir)
|
|
if err != nil {
|
|
t.Fatalf("CheckLocalePack(subsetDir): %v", err)
|
|
}
|
|
if len(subReps) != 1 || subReps[0].Errors != 0 {
|
|
t.Fatalf("expected 0 errors for subset placeholder pack, got %+v (findings=%+v)", subReps, subFindings)
|
|
}
|
|
hasParamWarn := false
|
|
for _, f := range subFindings {
|
|
if f.Check == "icu-params" && f.Severity == "warn" && strings.Contains(f.Message, "err.commit_memory") {
|
|
hasParamWarn = true
|
|
}
|
|
}
|
|
if !hasParamWarn {
|
|
t.Fatalf("expected warn icu-params finding for err.commit_memory, got %+v", subFindings)
|
|
}
|
|
}
|