108 lines
4.6 KiB
Go
108 lines
4.6 KiB
Go
package aiprompts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLocalizedDefaultTemplatesConsistentAndBraced(t *testing.T) {
|
|
languages := OfflinePromptLanguages()
|
|
if len(languages) != 25 {
|
|
t.Fatalf("languages=%d", len(languages))
|
|
}
|
|
for _, lang := range languages {
|
|
prompt, err := OfflineDefaultPrompt(lang)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
desc := DescriptionSectionInstructions(prompt)
|
|
if strings.Count(desc, "<h2>") != 2 || strings.Count(desc, "<p>") != 2 || strings.Count(desc, "<ul><li>{") != 1 || strings.Contains(desc, "<h1>") {
|
|
t.Errorf("%s invalid semantic HTML: %s", lang, desc)
|
|
}
|
|
if strings.Count(desc, "<h2>{") != 2 || strings.Count(desc, "<p>{") != 2 {
|
|
t.Errorf("%s missing localized instruction braces", lang)
|
|
}
|
|
if strings.Contains(desc, "100 words") || strings.Contains(desc, "100 besed") {
|
|
t.Errorf("%s obsolete quota", lang)
|
|
}
|
|
title, meta, err := DefaultSEOMetaInstructions(lang)
|
|
if err != nil || title == "" || meta == "" || strings.Contains(title, "meta_description:") || strings.Contains(meta, "meta_title:") {
|
|
t.Errorf("%s meta incorrectly parsed: %q %q %v", lang, title, meta, err)
|
|
}
|
|
body := ExtractEnhanceSectionBody(prompt, SectionMetaStart, SectionMetaEnd)
|
|
if strings.Count(body, "{meta_title:") != 1 || strings.Count(body, "{meta_description:") != 1 {
|
|
t.Errorf("%s meta must have independently braced instructions: %s", lang, body)
|
|
}
|
|
f := EffectiveDescriptionFormula(nil, prompt)
|
|
if len(f.Sections) != 5 || f.MetaTitle != title || f.MetaDescription != meta {
|
|
t.Errorf("%s formula parsing mismatch sections=%d title=%q meta=%q", lang, len(f.Sections), f.MetaTitle, f.MetaDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestA1MetaCleanupPreservesOtherSectionsAndCustomRules(t *testing.T) {
|
|
source := BuildCategoryEnhanceOverlay(LegacyEnhanceParts{
|
|
TitleRules: "{Custom A1 product title}", DescriptionRules: "<p>{Custom A1 description}</p>",
|
|
MetaRules: "Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki",
|
|
})
|
|
corrected, changed := NormalizeA1MetaRole(source, "sl")
|
|
if !changed {
|
|
t.Fatal("expected obsolete A1 meta update")
|
|
}
|
|
if !strings.Contains(corrected, "<p>{Custom A1 description}</p>") || !strings.Contains(corrected, "{Custom A1 product title}") {
|
|
t.Fatal("rewrote custom A1 rules")
|
|
}
|
|
if !strings.Contains(corrected, "{meta_title:") || !strings.Contains(corrected, "{meta_description:") {
|
|
t.Fatalf("incorrect normalized markup: %s", corrected)
|
|
}
|
|
again, changed := NormalizeA1MetaRole(corrected, "sl")
|
|
if changed || again != corrected {
|
|
t.Fatal("cleanup not idempotent")
|
|
}
|
|
custom := strings.Replace(source, "Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki", "Always mention our extended warranty", 1)
|
|
unchanged, changed := NormalizeA1MetaRole(custom, "sl")
|
|
if changed || unchanged != custom {
|
|
t.Fatal("custom A1 meta overwritten")
|
|
}
|
|
}
|
|
|
|
func TestA1MetaTemplateRepairsOnlyLegacy(t *testing.T) {
|
|
raw := `{"metaTitle":"Najprej napiši Novo ime izdelka in dodaj glavno prednost. Dolžina 50-60 znakov vključno s presledki.","metaDescription":"Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki","sections":[{"type":"p","instructions":"Existing custom"}]}`
|
|
updated, changed, err := NormalizeA1MetaTemplate(raw, "sl")
|
|
if err != nil || !changed {
|
|
t.Fatalf("failed to upgrade %v", err)
|
|
}
|
|
var obj map[string]any
|
|
if err := json.Unmarshal([]byte(updated), &obj); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if obj["sections"].([]any)[0].(map[string]any)["instructions"] != "Existing custom" {
|
|
t.Fatal("lost original A1 section")
|
|
}
|
|
if strings.Contains(updated, "140 znakov") {
|
|
t.Fatal("old meta still present")
|
|
}
|
|
second, changed, err := NormalizeA1MetaTemplate(updated, "sl")
|
|
if err != nil || changed || second != updated {
|
|
t.Fatalf("A1 meta repeat changed: %v", err)
|
|
}
|
|
custom, changed, err := NormalizeA1MetaTemplate(`{"metaTitle":"Our own rule","metaDescription":"Our own description"}`, "sl")
|
|
if err != nil || changed || !strings.Contains(custom, "Our own description") {
|
|
t.Fatal("custom meta overwritten")
|
|
}
|
|
}
|
|
|
|
func TestLegacyAndNewMetaWrapperCompatibility(t *testing.T) {
|
|
for _, raw := range []string{
|
|
"{meta_title: Short SEO title\nmeta_description: Concise summary}",
|
|
"{meta_title: Short SEO title}\n{meta_description: Concise summary}",
|
|
"meta_title: Short SEO title\nmeta_description: Concise summary",
|
|
} {
|
|
title, desc, ok := splitLocalizedMetaRules(raw)
|
|
if !ok || title != "Short SEO title" || desc != "Concise summary" {
|
|
t.Errorf("unparsed %q: %q %q", raw, title, desc)
|
|
}
|
|
}
|
|
}
|