// SPDX-Licence-Identifier: EUPL-1.2 package html import ( "testing" i18n "dappco.re/go/core/i18n" "github.com/stretchr/testify/require" ) type localeTranslator struct { language string } func (t *localeTranslator) T(key string, args ...any) string { if key == "prompt.yes" && t.language == "fr" { return "o" } if key == "prompt.yes" && t.language == "en" { return "y" } return key } func (t *localeTranslator) SetLanguage(language string) error { t.language = language return nil } func TestContext_NewContextWithService_AppliesLocale(t *testing.T) { svc := &localeTranslator{} ctx := NewContextWithService(svc, "fr-FR") if svc.language != "fr" { t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("NewContextWithService locale translation = %q, want %q", got, "o") } } func TestContext_NewContext_AppliesLocaleToDefaultService(t *testing.T) { ctx := NewContext("fr-FR") if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("NewContext(locale) translation = %q, want %q", got, "o") } } func TestContext_NewContextWithService_UsesLocale(t *testing.T) { svc := &localeTranslator{} ctx := NewContextWithService(svc, "en-GB") if svc.language != "en" { t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "y" { t.Fatalf("NewContextWithService translation = %q, want %q", got, "y") } } func TestContext_SetLocale_ReappliesToTranslator(t *testing.T) { svc := &localeTranslator{} ctx := NewContextWithService(svc, "en-GB") ctx.SetLocale("fr-FR") if ctx.Locale != "fr-FR" { t.Fatalf("SetLocale should update context locale, got %q", ctx.Locale) } if svc.language != "fr" { t.Fatalf("SetLocale should reapply locale to translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("SetLocale translation = %q, want %q", got, "o") } } func TestContext_SetService_ReappliesCurrentLocale(t *testing.T) { ctx := NewContext("fr-FR") svc := &localeTranslator{} ctx.SetService(svc) if ctx.service != svc { t.Fatalf("SetService should replace the active translator") } if svc.language != "fr" { t.Fatalf("SetService should apply the existing locale to the new translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("SetService translation = %q, want %q", got, "o") } } func TestContext_SetIdentity_UpdatesIdentity(t *testing.T) { ctx := NewContext() if got := ctx.SetIdentity("user-123"); got != ctx { t.Fatalf("SetIdentity should return the same context") } if ctx.Identity != "user-123" { t.Fatalf("SetIdentity should update context identity, got %q", ctx.Identity) } if ctx.Data == nil { t.Fatal("SetIdentity should preserve initialised Data map") } } func TestContext_Setters_NilReceiver(t *testing.T) { var ctx *Context if got := ctx.SetIdentity("user-123"); got != nil { t.Fatalf("nil Context.SetIdentity should return nil, got %v", got) } if got := ctx.SetLocale("en-GB"); got != nil { t.Fatalf("nil Context.SetLocale should return nil, got %v", got) } if got := ctx.SetService(&localeTranslator{}); got != nil { t.Fatalf("nil Context.SetService should return nil, got %v", got) } } func TestText_RenderFallsBackToDefaultTranslator(t *testing.T) { svc, _ := i18n.New() i18n.SetDefault(svc) require.NoError(t, svc.SetLanguage("fr")) ctx := &Context{} if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("Text() fallback translation = %q, want %q", got, "o") } }