Files
agent-desk-ng/internal/pkg/i18nx/context_test.go
mono 2f7a491666 feat: harden support, localize knowledge and add operator availability [skip ci]
Source-only publication of reviewed commit 64e96a7f423673dc29ad56485be9fc79bce8af1c.

Passed in isolation: focused frontend/backend tests, race checks, TypeScript typecheck, lint, Go compilation, Flowgram build, and deterministic SDK build/tests.

Not accepted as a production release: Next.js export/native assembly remains blocked by environment memory limits; browser interaction and MySQL staging acceptance remain unverified. No production server, database/schema, runtime configuration, deployment, release tag, or workflow execution was performed.

Expected source tree: 0a4a7528e9.
2026-10-16 15:21:42 +00:00

46 lines
1.5 KiB
Go

package i18nx
import (
"context"
"errors"
"testing"
"time"
)
func TestWithLocalePreservesContext(t *testing.T) {
type testKey struct{}
deadline := time.Now().Add(time.Minute)
parent, cancel := context.WithDeadline(context.WithValue(context.Background(), testKey{}, "request-value"), deadline)
defer cancel()
localized := WithLocale(parent, "ru")
if got := LocaleFromContext(localized); got != LocaleRuRU {
t.Fatalf("locale = %q", got)
}
if got := localized.Value(testKey{}); got != "request-value" {
t.Fatalf("parent value lost: %v", got)
}
if got, ok := localized.Deadline(); !ok || !got.Equal(deadline) {
t.Fatalf("deadline lost: %v, %v", got, ok)
}
cancel()
if !errors.Is(localized.Err(), context.Canceled) {
t.Fatalf("cancellation lost: %v", localized.Err())
}
}
func TestLocaleFromContextDefaultsAndIsolation(t *testing.T) {
for _, ctx := range []context.Context{nil, context.Background(), context.WithValue(context.Background(), "i18nx.locale", LocaleZhCN)} {
if got := LocaleFromContext(ctx); got != DefaultLocale {
t.Fatalf("missing explicit locale = %q, want default %q", got, DefaultLocale)
}
}
english := WithLocale(context.Background(), LocaleEnUS)
russian := WithLocale(english, LocaleRuRU)
if LocaleFromContext(english) != LocaleEnUS || LocaleFromContext(russian) != LocaleRuRU {
t.Fatal("child locale changed parent locale")
}
if got := LocaleFromContext(WithLocale(nil, "unsupported")); got != DefaultLocale {
t.Fatalf("invalid locale = %q", got)
}
}