Files
Reims-Go/internal/plugin/locale_test.go
T
Nwflower f0b69c252c feat(update,plugin,ui): add in-app self-update, plugin locale packs, and About page
- Add shared resumable + SHA256SUMS-verified downloader (internal/fetch) and Windows executable swap/relaunch flow (internal/selfupdate, internal/app/appupdate_flow.go, --wait-pid in main.go/cli).
- Add dedicated About page and sidebar update capsule with background release checks, download progress, and restart confirmation when VMs are running.
- Implement plugin-contributed UI and backend/CLI locale packs (contributes.locales, LocalizedText extra tags, validation, runtime overlay, and PopupSelect language picker).
- Move kernel-irqchip and Windows processor scheduling controls into VM Compute > Performance settings, hide Plugins settings gear for VM-only schemas, and align locked system plugin switches.
2026-11-29 10:32:11 -07:00

135 lines
4.5 KiB
Go

package plugin
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestLocalizedTextExtraLocales(t *testing.T) {
var lt LocalizedText
raw := `{"zh-CN":"日语包","en-US":"Japanese Pack","ja-jp":"日本語パック"}`
if err := json.Unmarshal([]byte(raw), &lt); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if !lt.HasAny() {
t.Fatal("expected HasAny=true")
}
if got := lt.Get("ja-JP"); got != "日本語パック" {
t.Fatalf("Get(ja-JP) = %q, want 日本語パック", got)
}
if got := lt.Get("zh-CN"); got != "日语包" {
t.Fatalf("Get(zh-CN) = %q", got)
}
if got := lt.Get("fr-FR"); got != "Japanese Pack" {
t.Fatalf("Get(fr-FR) fallback = %q, want Japanese Pack", got)
}
}
func TestValidateLocaleContribution(t *testing.T) {
dir := t.TempDir()
locDir := filepath.Join(dir, "locales")
if err := os.MkdirAll(locDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(locDir, "ja-JP.json"), []byte(`{"sidebarSettings":"設定"}`), 0o644); err != nil {
t.Fatal(err)
}
okContrib := LocaleContribution{Locale: "ja-JP", Label: "日本語", File: "locales/ja-JP.json"}
if err := ValidateLocaleContribution(okContrib, dir); err != nil {
t.Fatalf("expected valid locale contribution, got: %v", err)
}
if err := ValidateLocaleContribution(LocaleContribution{Locale: "ja-JP"}, dir); err == nil {
t.Fatal("expected error for empty locale contribution")
}
if err := ValidateLocaleContribution(LocaleContribution{Locale: "bad/tag", Messages: map[string]string{"a": "b"}}, dir); err == nil {
t.Fatal("expected error for invalid locale tag")
}
if err := ValidateLocaleContribution(LocaleContribution{Locale: "ja-JP", File: "../secret.json"}, dir); err == nil {
t.Fatal("expected error for path traversal")
}
if err := ValidateLocaleContribution(LocaleContribution{Locale: "ja-JP", File: "locales/ja-JP.txt"}, dir); err == nil {
t.Fatal("expected error for non-.json locale file")
}
}
func TestManagerResolvedLocalePacks(t *testing.T) {
dataRoot := t.TempDir()
devDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(devDir, "locales"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(devDir, "locales", "ja-JP.json"), []byte(`{"sidebarSettings":"設定","language":"言語"}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(devDir, "locales", "ja-JP.backend.json"), []byte(`{"doctor.os":"OS (JP)"}`), 0o644); err != nil {
t.Fatal(err)
}
manifest := `{
"schema": 1,
"id": "community.locale.ja",
"version": "1.0.0",
"name": {"zh-CN": "日语语言包", "en-US": "Japanese Pack", "ja-JP": "日本語パック"},
"description": {"zh-CN": "日语翻译", "en-US": "Japanese translations"},
"contributes": {
"locales": [
{
"locale": "ja_jp",
"label": "日本語",
"fallback": "en-US",
"file": "locales/ja-JP.json",
"backendFile": "locales/ja-JP.backend.json",
"messages": {"themeDark": "ダーク"}
}
]
}
}`
if err := os.WriteFile(filepath.Join(devDir, "plugin.json"), []byte(manifest), 0o644); err != nil {
t.Fatal(err)
}
mgr, err := NewManager(ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.6"})
if err != nil {
t.Fatalf("NewManager: %v", err)
}
defer mgr.Close()
if _, err := mgr.LinkDev(context.Background(), devDir); err != nil {
t.Fatalf("LinkDev: %v", err)
}
if err := mgr.Enable(context.Background(), "community.locale.ja", false); err != nil {
t.Fatalf("Enable: %v", err)
}
uiPacks, backendPacks := mgr.ResolvedLocalePacks()
if len(uiPacks) != 1 {
t.Fatalf("expected 1 UI locale pack, got %d", len(uiPacks))
}
p := uiPacks[0]
if p.Locale != "ja-JP" || p.Label != "日本語" || p.Fallback != "en-US" {
t.Fatalf("unexpected pack metadata: %+v", p)
}
if p.Messages["sidebarSettings"] != "設定" || p.Messages["themeDark"] != "ダーク" {
t.Fatalf("unexpected merged UI messages: %+v", p.Messages)
}
bp, ok := backendPacks[i18n.Locale("ja-JP")]
if !ok || bp.Messages["doctor.os"] != "OS (JP)" {
t.Fatalf("unexpected backend catalog: %+v", backendPacks)
}
// Disabling the plugin removes the locale pack cleanly.
if err := mgr.Disable(context.Background(), "community.locale.ja"); err != nil {
t.Fatalf("Disable: %v", err)
}
uiPacks, backendPacks = mgr.ResolvedLocalePacks()
if len(uiPacks) != 0 || len(backendPacks) != 0 {
t.Fatalf("expected empty locale packs after disable, got ui=%d backend=%d", len(uiPacks), len(backendPacks))
}
}