Files
descrybe/apps/api/cmd/seed-category-prompt-translations/main_test.go
T

70 lines
3.3 KiB
Go
Raw Normal View History

2026-10-14 18:18:54 +02:00
package main
import (
"strings"
"testing"
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
"github.com/descrybe/descrybe-v2/apps/api/internal/catalog"
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
)
2026-10-14 20:02:42 +02:00
func TestDefaultMigrationRecognizesMixedLegacyEnglishWithoutTouchingCustom(t *testing.T) {
current := aiprompts.DefaultCategoryFormula("", "High chairs")
old := aiprompts.PriorDefaultCategoryFormula("", "High chairs")
seo := aiprompts.PreviousSEODefaultCategoryFormula("", "High chairs")
variants := []aiprompts.CategoryFormulaDefaults{current, seo, old, aiprompts.LegacyDefaultCategoryFormula("", "High chairs")}
mixed := strings.Replace(current.Overlay,
aiprompts.DescriptionSectionInstructions(current.Overlay),
aiprompts.DescriptionSectionInstructions(old.Overlay), 1)
if !isMixedBuiltInEnglishPrompt(mixed, variants) {
t.Fatal("old English five-section mixed default was not recognized")
2026-10-14 18:18:54 +02:00
}
2026-10-14 20:02:42 +02:00
cache := map[string]*defaultTranslation{
catalog.DefaultPromptHash(current.Overlay): {Source: current.Overlay, Values: company.LangPromptMap{"en": current.Overlay}},
catalog.DefaultPromptHash(old.Overlay): {Source: old.Overlay, Values: company.LangPromptMap{"en": old.Overlay}},
2026-10-14 18:18:54 +02:00
}
2026-10-14 20:28:38 +02:00
if !isKnownBuiltInVariantSet(company.LangPromptMap{"en": mixed}, variants, cache, nil) {
2026-10-14 20:02:42 +02:00
t.Fatal("mixed, unedited platform default was flagged custom")
2026-10-14 18:18:54 +02:00
}
2026-10-14 20:02:42 +02:00
bad := strings.Replace(mixed, "--- Meta ---", "--- Meta ---\nUse my shop's custom brand voice", 1)
2026-10-14 20:28:38 +02:00
if isKnownBuiltInVariantSet(company.LangPromptMap{"en": bad}, variants, cache, nil) {
2026-10-14 20:02:42 +02:00
t.Fatal("custom tenant meta text must remain protected")
2026-10-14 18:18:54 +02:00
}
2026-10-14 20:02:42 +02:00
a1 := company.LangPromptMap{"sl": "A1 custom Slovenian naming, meta, and description", "en": mixed}
2026-10-14 20:28:38 +02:00
changed := replaceKnownBuiltInLocales(a1, variants, cache, nil)
2026-10-14 20:02:42 +02:00
if changed != 1 || a1["sl"] != "A1 custom Slovenian naming, meta, and description" || a1["en"] != current.Overlay {
t.Fatalf("A1 locale migration touched custom data: changed=%d, en=%q, sl=%q", changed, a1["en"], a1["sl"])
2026-10-14 18:18:54 +02:00
}
}
2026-10-14 20:28:38 +02:00
func TestV6SharedCacheMigrationUsesHistoricalExactStrings(t *testing.T) {
canonical := aiprompts.DefaultCategoryFormula("", "Cables")
previous := aiprompts.PreviousCanonicalV6CategoryFormula("", "Cables")
if previous.Overlay == canonical.Overlay {
t.Fatal("v6/v7 should be distinct snapshots")
}
oldCroatian, err := aiprompts.PreviousOfflineDefaultPromptV6("hr")
if err != nil {
t.Fatal(err)
}
newCroatian, err := aiprompts.OfflineDefaultPrompt("hr")
if err != nil {
t.Fatal(err)
}
current := map[string]*defaultTranslation{
catalog.DefaultPromptHash(canonical.Overlay): {Source: canonical.Overlay, Values: company.LangPromptMap{"en": canonical.Overlay, "hr": newCroatian}},
}
history := map[string]*defaultTranslation{
catalog.DefaultPromptHash(previous.Overlay): {Source: previous.Overlay, Values: company.LangPromptMap{"en": previous.Overlay, "hr": oldCroatian}},
}
versions := []aiprompts.CategoryFormulaDefaults{canonical, previous}
legacy := company.LangPromptMap{"*": previous.Overlay, "hr": oldCroatian}
if !isKnownBuiltInVariantSet(legacy, versions, current, history) {
t.Fatal("v6 stock translations not upgradeable")
}
legacy["hr"] = "This company wrote its own Croatian instructions"
if isKnownBuiltInVariantSet(legacy, versions, current, history) {
t.Fatal("custom company locale overwritten")
}
}