package aiprompts
import (
"strings"
"testing"
)
func TestDedupeA1DescriptionBlocks(t *testing.T) {
input := `--- Title ---
Napiši ime v slovenščini
--- End Title ---
--- Description ---
{Benefit one}
{No title}
{Benefit two}
{Include title}
{Benefit one}
{No title}
{Benefit two}
{Exclude title}
{Benefit two}
{Include title}
{Tehnične specifikacije}
--- End Description ---
--- Meta ---
140 znakov
--- End Meta ---`
got, n := DedupeA1DescriptionBlocks(input)
if n != 2 {
t.Fatalf("removed=%d want 2", n)
}
if strings.Count(got, "") != 3 {
t.Fatalf("H2 count changed incorrectly: %s", got)
}
for _, want := range []string{"Napiši ime", "140 znakov", "Tehnične specifikacije", "Exclude title"} {
if !strings.Contains(got, want) {
t.Fatalf("lost %q", want)
}
}
again, n := DedupeA1DescriptionBlocks(got)
if n != 0 || again != got {
t.Fatal("cleanup not idempotent")
}
}
func TestDedupeA1RealRepeatingTemplate(t *testing.T) {
headingA := `{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
`
headingB := `{Izpostavi en benefit, in NE napiši Novo ime izdelka}
`
paragraphA := `
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
`
paragraphB := `{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}
`
original := headingA + "\n" + paragraphA + "\n" + headingB + "\n" + paragraphB + "\n" +
headingA + "\n" + paragraphA + "\n" + headingB + "\n" + paragraphB + "\n" +
headingB + "\n" + paragraphA + `{Tehnične specifikacije}- {Napiši 3-10 tehničnih specifikacij v alinejah}
`
cleaned, removed := DedupeA1DescriptionBlocks(original)
if removed != 2 {
t.Fatalf("expected 2 duplicate pairs, got %d", removed)
}
if strings.Count(cleaned, "") != 3 {
t.Fatalf("expected 3 distinct pairs: %s", cleaned)
}
if !strings.Contains(cleaned, "Tehnične specifikacije") {
t.Fatal("lost specs")
}
twice, removedAgain := DedupeA1DescriptionBlocks(cleaned)
if removedAgain != 0 || twice != cleaned {
t.Fatal("A1 cleanup must be idempotent")
}
}
func TestDedupeStructuredA1Descriptions(t *testing.T) {
raw := `{"sections":[{"type":"h2","instructions":"A"},{"type":"p","instructions":"X"},{"type":"h2","instructions":"B"},{"type":"p","instructions":"Y"},{"type":"h2","instructions":"A"},{"type":"p","instructions":"X"},{"type":"ul","instructions":"Specifications"}],"metaDescription":"140 znakov"}`
cleaned, removed, err := DedupeA1DescriptionTemplate(raw)
if err != nil || removed != 1 {
t.Fatalf("removed=%d err=%v", removed, err)
}
if strings.Count(cleaned, `"type":"h2"`) != 2 || !strings.Contains(cleaned, `140 znakov`) || !strings.Contains(cleaned, `Specifications`) {
t.Fatalf("invalid result: %s", cleaned)
}
again, n, err := DedupeA1DescriptionTemplate(cleaned)
if n != 0 || err != nil || again != cleaned {
t.Fatal("structured cleanup not idempotent")
}
}
func TestNormalizeA1LegacySEOHTML(t *testing.T) {
source := `--- Title ---
Napiši Novo ime izdelka
--- End Title ---
--- Description ---
{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
{Izpostavi en benefit, in NE napiši Novo ime izdelka}
{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}
{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
{Izpostavi en benefit, in NE napiši Novo ime izdelka}
{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}
{Izpostavi en benefit, in NE napiši Novo ime izdelka}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
{Tehnične specifikacije}- {Napiši 3-10 tehničnih specifikacij v alinejah}
--- End Description ---
--- Meta ---
Najprej Novo ime izdelka
--- End Meta ---`
result, changed := NormalizeA1RepeatedBenefits(source)
if changed != 5 {
t.Fatalf("expected five legacy boilerplate pairs, got %d", changed)
}
for _, want := range []string{"--- Title ---", "Napiši Novo ime izdelka", "--- Meta ---", "Najprej Novo ime izdelka", "{", "
{", "- {"} {
if !strings.Contains(result, want) {
t.Fatalf("missing %q: %s", want, result)
}
}
if strings.Count(result, "
") != 2 || strings.Contains(result, "100 besed") {
t.Fatalf("still repetitive: %s", result)
}
again, n := NormalizeA1RepeatedBenefits(result)
if n != 0 || again != result {
t.Fatal("SEO upgrade must be idempotent")
}
}
func TestNormalizeA1StructuredKeepsSections(t *testing.T) {
source := `{"sections":[{"type":"h2","instructions":"Benefit A"},{"type":"p","instructions":"NE vsebuje Novo ime izdelka"},{"type":"h2","instructions":"Benefit B"},{"type":"p","instructions":"VKLJUČI Novo ime izdelka"},{"type":"h2","instructions":"Benefit A"},{"type":"p","instructions":"NE vsebuje Novo ime izdelka"}],"metaDescription":"140 znakov"}`
result, changed, err := NormalizeA1DescriptionTemplate(source)
if err != nil || changed != 1 {
t.Fatalf("changed=%d err=%v", changed, err)
}
if strings.Count(result, `"type":"h2"`) != 3 || !strings.Contains(result, "3. drugačno") || !strings.Contains(result, "140 znakov") {
t.Fatalf("invalid: %s", result)
}
again, n, err := NormalizeA1DescriptionTemplate(result)
if n != 0 || err != nil || again != result {
t.Fatal("not idempotent")
}
}
func TestNormalizeA1LegacyStructuredSEO(t *testing.T) {
raw := `{"sections":[{"type":"h2","instructions":"Napiši Novo ime izdelka brez barve in izpostavi en benefit"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka"},{"type":"h2","instructions":"Izpostavi en benefit"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed in VKLJUČI Novo ime izdelka"},{"type":"h2","instructions":"Napiši Novo ime izdelka"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed"},{"type":"h2","instructions":"Benefit"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed"},{"type":"ul","instructions":"Tehnične specifikacije"}],"metaDescription":"140 znakov"}`
clean, n, err := NormalizeA1DescriptionTemplate(raw)
if err != nil || n != 4 {
t.Fatalf("changed=%d err=%v", n, err)
}
if !strings.Contains(clean, `"type":"ul"`) || !strings.Contains(clean, `140 znakov`) || strings.Contains(clean, "100 besed") {
t.Fatal(clean)
}
twice, n, err := NormalizeA1DescriptionTemplate(clean)
if err != nil || n != 0 || twice != clean {
t.Fatal("not repeatable")
}
}
func TestNormalizeA1MissingDescriptionJSON(t *testing.T) {
for _, source := range []string{"", " ", "null", "{}", `{"metaDescription":"140 znakov"}`, `{"sections":null,"metaTitle":"Custom"}`} {
result, changed, err := NormalizeA1DescriptionTemplate(source)
if err != nil || changed != 0 || result != source {
t.Fatalf("source=%q changed=%d err=%v result=%q", source, changed, err, result)
}
}
}
func TestNormalizeA1MixedOldAndNewInstructions(t *testing.T) {
source := `{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
{Izpostavi en benefit, in NE napiši Novo ime izdelka}
{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}
{Izpostavi 3. drugačno in preverjeno prednost izdelka. Ne ponavljaj prejšnjih prednosti ali celotnega imena izdelka.}
{Napiši približno 100 besed o 3. samostojni, z dejanskimi podatki podprti prednosti. Ne ponavljaj informacij iz prejšnjih odstavkov. Ne uporabi celotnega novega imena izdelka. Če prednosti ni mogoče potrditi z vhodnimi podatki, je ne izmišljaj.}
{Tehnične specifikacije}- {Napiši 3-10 tehničnih specifikacij v alinejah}
`
cleaned, replaced := NormalizeA1RepeatedBenefits(source)
if replaced != 3 {
t.Fatalf("expected three old/mixed pairs replaced, got %d: %s", replaced, cleaned)
}
if strings.Contains(cleaned, "100 besed") || strings.Contains(cleaned, "Izpostavi en benefit") || strings.Contains(cleaned, "3. drugačno") {
t.Fatalf("mixed legacy boilerplate survived: %s", cleaned)
}
if strings.Count(cleaned, "") != 2 || strings.Count(cleaned, "- ") != 1 || !strings.HasPrefix(cleaned, "
{") {
t.Fatalf("expected modern SEO description with 1 intro, 2 sections and one specs list: %s", cleaned)
}
again, changed := NormalizeA1RepeatedBenefits(cleaned)
if changed != 0 || again != cleaned {
t.Fatal("mixed A1 cleanup not idempotent")
}
}
func TestNormalizeA1ShortLegacyAndPreservesOtherRoles(t *testing.T) {
source := `--- Title ---
{Ohranite naslov in pravila iz kategorije}
--- End Title ---
--- Description ---
{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
{Izpostavi en benefit, in NE napiši Novo ime izdelka}
{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}
{Tehnične specifikacije}- {Napiši 3-10 tehničnih specifikacij v alinejah}
--- End Description ---
--- Meta ---
{Ohranite lasten meta prompt}
--- End Meta ---`
cleaned, n := NormalizeA1RepeatedBenefits(source)
if n != 2 || strings.Count(cleaned, "") != 2 {
t.Fatalf("expected short template upgraded, changed=%d: %s", n, cleaned)
}
for _, retained := range []string{"{Ohranite naslov in pravila iz kategorije}", "{Ohranite lasten meta prompt}", "--- End Description ---"} {
if !strings.Contains(cleaned, retained) {
t.Fatalf("custom content lost: %s", cleaned)
}
}
if strings.Contains(cleaned, "100 besed") || strings.Count(cleaned, "- ") != 1 {
t.Fatalf("duplicated spec or old paragraph remains: %s", cleaned)
}
}
func TestNormalizeA1MixedStructuredTemplatePreservesMeta(t *testing.T) {
source := `{"sections":[{"type":"h2","instructions":"Napiši Novo ime izdelka brez barve in izpostavi en benefit"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka"},{"type":"h2","instructions":"Izpostavi en benefit, in NE napiši Novo ime izdelka"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed in VKLJUČI Novo ime izdelka"},{"type":"h2","instructions":"Izpostavi 3. drugačno in preverjeno prednost izdelka. Ne ponavljaj prejšnjih prednosti ali celotnega imena izdelka"},{"type":"p","instructions":"Napiši približno 100 besed o 3. samostojni prednosti"},{"type":"ul","instructions":"Napiši 3-10 tehničnih specifikacij v alinejah"}],"metaTitle":"A1 custom title","metaDescription":"A1 custom meta","otherField":true}`
cleaned, count, err := NormalizeA1DescriptionTemplate(source)
if err != nil || count != 3 {
t.Fatalf("mixed structured template count=%d err=%v result=%s", count, err, cleaned)
}
for _, retained := range []string{`"metaTitle":"A1 custom title"`, `"metaDescription":"A1 custom meta"`, `"otherField":true`} {
if !strings.Contains(cleaned, retained) {
t.Fatalf("lost metadata %s in %s", retained, cleaned)
}
}
if strings.Contains(cleaned, "100 besed") || strings.Count(cleaned, `"type":"h2"`) != 2 || strings.Count(cleaned, `"type":"ul"`) != 1 {
t.Fatalf("did not upgrade description to SEO structure: %s", cleaned)
}
twice, n, err := NormalizeA1DescriptionTemplate(cleaned)
if err != nil || n != 0 || twice != cleaned {
t.Fatalf("structured upgrade not idempotent: n=%d err=%v", n, err)
}
}
func TestNormalizeA1UnrelatedCustomPromptsUntouched(t *testing.T) {
source := `
{Izbira pravega izdelka}
{Napiši opis glede na navedena dejstva, brez ugibanja.}
`
actual, changed := NormalizeA1RepeatedBenefits(source)
if actual != source || changed != 0 {
t.Fatalf("unexpected change of custom text: %s", actual)
}
}
func TestNormalizeA1LegacyEnDashSpecs(t *testing.T) {
source := `{Napiši Novo ime izdelka brez barve in izpostavi en benefit}
{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}
Tehnične specifikacije
- {Napiši 3–10 tehničnih specifikacij v alinejah}
`
got, count := NormalizeA1RepeatedBenefits(source)
if count != 1 || strings.Count(got, "