Files
descrybe/apps/api/internal/company/lang_content_test.go
T

104 lines
2.5 KiB
Go
Raw Normal View History

package company
import (
"encoding/json"
"testing"
)
func TestSanitizeLangPromptMap(t *testing.T) {
t.Parallel()
_, err := SanitizeLangPromptMap(map[string]string{
"SL": " hello {{name}} ",
"xx": "bad",
}, 100)
if err == nil {
t.Fatal("expected error for unsupported language")
}
m, err := SanitizeLangPromptMap(map[string]string{
"SL": " hello {{name}} ",
"en": "",
2026-08-16 16:57:36 +02:00
"*": " shared ",
}, 100)
if err != nil {
t.Fatal(err)
}
if m["sl"] != "hello {{name}}" {
t.Fatalf("got %#v", m)
}
if _, ok := m["en"]; ok {
t.Fatalf("empty en should be dropped: %#v", m)
}
2026-08-16 16:57:36 +02:00
if m[LangPromptAny] != "shared" {
t.Fatalf("wildcard missing: %#v", m)
}
}
func TestPromptForLanguage(t *testing.T) {
t.Parallel()
m := LangPromptMap{"sl": "slo", "en": "eng"}
2026-08-16 16:57:36 +02:00
if got := PromptForLanguage(m, "SL", ""); got != "slo" {
t.Fatalf("got %q", got)
}
2026-08-16 16:57:36 +02:00
if got := PromptForLanguage(m, "de", ""); got != "" {
t.Fatalf("expected empty without primary, got %q", got)
}
// sl-only map: exact hit for sl
slOnly := LangPromptMap{"sl": "slo-only"}
if got := PromptForLanguage(slOnly, "sl", "sl"); got != "slo-only" {
t.Fatalf("sl exact: got %q", got)
}
// sl-only map: en request falls back to primary=sl
if got := PromptForLanguage(slOnly, "en", "sl"); got != "slo-only" {
t.Fatalf("en→primary sl: got %q", got)
}
// wildcard before primary
anyMap := LangPromptMap{"*": "shared", "sl": "slo"}
if got := PromptForLanguage(anyMap, "de", "sl"); got != "shared" {
t.Fatalf("wildcard before primary: got %q", got)
}
// explicit lang beats wildcard
if got := PromptForLanguage(anyMap, "sl", "en"); got != "slo" {
t.Fatalf("explicit beats *: got %q", got)
}
}
func TestParseContentLanguagesPrimaryFirst(t *testing.T) {
t.Parallel()
got, err := ParseContentLanguages([]string{"en", "de", "sl"}, "sl")
if err != nil {
t.Fatal(err)
}
want := []string{"sl", "en", "de"}
if len(got) != len(want) {
t.Fatalf("got %#v", got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("got %#v want %#v", got, want)
}
}
}
func TestLocalizedContentRoundTrip(t *testing.T) {
t.Parallel()
c := LocalizedContent{
"sl": {ProcessedName: "Naslov", ProcessedDescription: "Opis"},
}
b, err := EncodeLocalizedContent(c)
if err != nil {
t.Fatal(err)
}
var raw any
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}
decoded, err := DecodeLocalizedContent(raw)
if err != nil {
t.Fatal(err)
}
f := FieldsForLanguage(decoded, "sl")
if f.ProcessedName != "Naslov" || f.ProcessedDescription != "Opis" {
t.Fatalf("got %#v", f)
}
}