Files
Reims-Go/pkg/pluginsdk/sdk_test.go
T
Dnyo666 ceeec602be feat(i18n,plugin): implement end-to-end i18n & L10n v2 architecture and language pack protocol
- Build single-source-of-truth 7-domain locale catalog (`locales/{zh-CN,en-US}/*.json`, 1,100+ keys) and `cmd/i18ngen` generator emitting Go catalogs and eager/lazy-split TypeScript domain modules (`core`/`vm` eager; `wizard`/`settings`/`plugins` lazy)
- Add zero-dependency ICU MessageFormat subset (`{var}`, `{n, number}`, CLDR `plural`, `select`), 8-hop cycle-free regional fallback (`zh-HK/zh-MO -> zh-Hant -> zh-TW -> zh-CN`), `auto` OS display-language preference, and `en-XA` pseudo-localization across Go and TypeScript
- Introduce `CodedError` supportability with `[E_CODE]` badges, collapsible `[detail]` diagnostics, and automatic SMC OSK / SMBIOS serial / MAC / token redaction
- Add Host × Guest macOS dual-terminology annotation (`FormatGuestTerm`) across 6 official Apple guest UI locales and localize Win32 system tray, file pickers, and DPI-adaptive `installguide` overlays with CJK font fallback probing
- Upgrade L1–L4 plugin localization to dual-track v2: Track A full language packs (`files`, WOFF2 `fonts`, cross-plugin `pluginOverrides`), Track B plugin-scoped `l10n` bundles (`HasUIMessages` isolation), live `plugin/localeChanged` notifications, `< 2ms` static CLI pack loading, and `reims-go plugin locale init|check` translator toolchain with `zh-TW` and `ja-JP` reference packs
- Harden security, WCAG 2.1 AA token contrast, `< 768px` responsive drawer & form layouts, and RTL logical properties; add backend security/locale regression tests and frontend `node --test` static/runtime guard suite in CI

# Conflicts:
#	frontend/src/i18n.ts
#	internal/cli/cli.go
#	internal/i18n/i18n.go
2026-11-30 14:56:19 +08:00

112 lines
3.0 KiB
Go

package pluginsdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"sync/atomic"
"testing"
"time"
)
func writeRPCFrame(w io.Writer, v any) error {
raw, err := json.Marshal(v)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "Content-Length: %d\r\n\r\n%s", len(raw), raw)
return err
}
func TestServerLocaleHandshakeAndHotUpdate(t *testing.T) {
inR, inW := io.Pipe()
var outBuf bytes.Buffer
srv := NewWithIO(inR, &outBuf)
var changedCount atomic.Int32
var lastChangedLocale atomic.Value
srv.OnLocaleChanged(func(ev LocaleChangedEvent) {
changedCount.Add(1)
lastChangedLocale.Store(ev.Locale)
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
done := make(chan error, 1)
go func() {
done <- srv.Serve(ctx)
}()
if err := writeRPCFrame(inW, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "plugin/initialize",
"params": map[string]any{
"pluginId": "acme.backup",
"pluginApi": "1.0.0",
"dataDir": t.TempDir(),
"locale": "zh-CN",
"fallbackChain": []string{"zh-CN", "en-US"},
"l10nBundle": map[string]string{
"backup.done": "已备份 {name}(共 {count} 项)",
},
},
}); err != nil {
t.Fatalf("write initialize: %v", err)
}
deadline := time.Now().Add(2 * time.Second)
for srv.InitInfo().PluginID == "" && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if got := srv.Locale(); got != "zh-CN" {
t.Fatalf("Locale() = %q, want zh-CN", got)
}
if got := srv.T("backup.done", map[string]any{"name": "Sonoma", "count": 3}); got != "已备份 Sonoma(共 3 项)" {
t.Fatalf("T(backup.done) = %q", got)
}
if got := srv.T("missing.key", nil); got != "missing.key" {
t.Fatalf("T(missing.key) = %q, want missing.key", got)
}
// Broadcast live locale change (notification without id).
if err := writeRPCFrame(inW, map[string]any{
"jsonrpc": "2.0",
"method": "plugin/localeChanged",
"params": map[string]any{
"locale": "ja-JP",
"fallbackChain": []string{"ja-JP", "en-US", "zh-CN"},
"l10nBundle": map[string]string{
"backup.done": "{name} をバックアップしました({count} 件)",
},
},
}); err != nil {
t.Fatalf("write localeChanged: %v", err)
}
deadline = time.Now().Add(2 * time.Second)
for changedCount.Load() == 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if changedCount.Load() != 1 || lastChangedLocale.Load() != "ja-JP" {
t.Fatalf("expected OnLocaleChanged(ja-JP), count=%d val=%v", changedCount.Load(), lastChangedLocale.Load())
}
if got := srv.Locale(); got != "ja-JP" {
t.Fatalf("Locale() after update = %q, want ja-JP", got)
}
if got := srv.T("backup.done", map[string]any{"name": "Sonoma", "count": 3}); got != "Sonoma をバックアップしました(3 件)" {
t.Fatalf("T(backup.done) after ja-JP switch = %q", got)
}
_ = writeRPCFrame(inW, map[string]any{
"jsonrpc": "2.0",
"method": "plugin/shutdown",
})
_ = inW.Close()
if err := <-done; err != nil {
t.Fatalf("Serve returned error: %v", err)
}
}