134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
package company
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"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": "",
|
|
"*": " 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)
|
|
}
|
|
if m[LangPromptAny] != "shared" {
|
|
t.Fatalf("wildcard missing: %#v", m)
|
|
}
|
|
}
|
|
|
|
func TestPromptForLanguage(t *testing.T) {
|
|
t.Parallel()
|
|
m := LangPromptMap{"sl": "slo", "en": "eng"}
|
|
if got := PromptForLanguage(m, "SL", ""); got != "slo" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestLocalizedContentPublicOmitsEnhanceHash(t *testing.T) {
|
|
t.Parallel()
|
|
c := LocalizedContent{
|
|
"sl": {
|
|
ProcessedName: "Naslov",
|
|
ProcessedDescription: "Opis",
|
|
MetaTitle: "Meta",
|
|
EnhanceInputHash: "deadbeef",
|
|
},
|
|
}
|
|
pub := c.Public()
|
|
if pub["sl"].EnhanceInputHash != "" {
|
|
t.Fatalf("public hash leaked: %#v", pub["sl"])
|
|
}
|
|
if pub["sl"].ProcessedName != "Naslov" || pub["sl"].MetaTitle != "Meta" {
|
|
t.Fatalf("public stripped too much: %#v", pub["sl"])
|
|
}
|
|
if c["sl"].EnhanceInputHash != "deadbeef" {
|
|
t.Fatalf("Public must not mutate storage copy: %#v", c["sl"])
|
|
}
|
|
b, err := json.Marshal(pub)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(b), "enhance_input_hash") || strings.Contains(string(b), "deadbeef") {
|
|
t.Fatalf("hash in JSON: %s", b)
|
|
}
|
|
}
|