Files
Reims-Go/pkg/pluginsdk/sdk_test.go
T
Nwflower ceeb483cfb fix(i18n,plugin,ui): harden PR #10 redaction, trust boundaries, ICU limits, and UI contrast
Follow-up hardening from PR #10 review:
- Redact quoted/space-containing SMC OSKs, hyphenated MACs, and extended token keys without corrupting benign 'serial ...' messages; make RawDetail truncation UTF-8-safe and register reims.RedactText in CLI
- Enforce trust boundaries so unprivileged community language packs cannot hijack built-in zh-CN/en-US catalogs or overwrite target-owned plugin metadata/messages
- Avoid triggering inUseChecker/Sidecar wake-ups inside Registry.OnChange (ResolvedLocalePacks) and Broker.AllowRate; bound Sidecar NotifyIfRunning with a 500ms timeout; add l10nCache generation counter and syncLocales broadcast
- Cap ICU nesting depth (16) and template size (16 KiB) in Go & TS; reserve built-in anchors in 8-hop FallbackChain; fix C.UTF-8 detection, zero-var fault isolation, and PseudoLocalize brace escaping
- Fix pluginsdk Server.T apostrophe/contraction handling, defensive cloning, and sequential plugin/localeChanged dispatch
- Fix WCAG 2.1 AA contrast for --accent-soft-text, prevent PluginPanelFrame RPC hang on locale switch, guard setPluginLocales re-renders, and fix <=768px drawer & RTL toast swipe-out CSS
2026-11-30 00:26:14 -07:00

116 lines
3.2 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} 项)",
"contraction": "Can't open {name}'s disk",
},
},
}); 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("contraction", map[string]any{"name": "Sonoma"}); got != "Can't open Sonoma's disk" {
t.Fatalf("T(contraction) = %q, want Can't open Sonoma's disk", 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)
}
}