141 lines
4.8 KiB
Go
141 lines
4.8 KiB
Go
package aiprompts
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDeriveDescriptionFormulaFromLegacyParts(t *testing.T) {
|
|
t.Parallel()
|
|
legacy := `Ustvari nov opis
|
|
|
|
GPT predloga:
|
|
<name>{Napiši novo ime izdelka po formuli: "znamka", "tip izdelka lowercase". Ne uporabljaj vejic.}</name>
|
|
<metaDescription>{Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov}</metaDescription>
|
|
|
|
<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>
|
|
<p>{Napiši odstavek, ki je dolg 100 besed.}</p>
|
|
<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`
|
|
|
|
parts := ParseLegacyCombinedEnhancePrompt(legacy)
|
|
if !parts.WasLegacy {
|
|
t.Fatal("expected legacy parse")
|
|
}
|
|
f := DeriveDescriptionFormulaFromLegacyParts(parts)
|
|
if strings.TrimSpace(f.MetaDescription) == "" || !strings.Contains(f.MetaDescription, "140") {
|
|
t.Fatalf("metaDescription=%q", f.MetaDescription)
|
|
}
|
|
if len(f.Sections) < 3 {
|
|
t.Fatalf("want >=3 sections, got %#v", f.Sections)
|
|
}
|
|
if f.Sections[0].Type != "h2" {
|
|
t.Fatalf("first section should be h2 (document order), got %#v", f.Sections[0])
|
|
}
|
|
last := f.Sections[len(f.Sections)-1]
|
|
if last.Type != "ul" {
|
|
t.Fatalf("last section should be ul specs, got %#v", last)
|
|
}
|
|
var sawH2, sawP, sawUL bool
|
|
for _, s := range f.Sections {
|
|
switch s.Type {
|
|
case "h2":
|
|
sawH2 = true
|
|
case "p":
|
|
sawP = true
|
|
case "ul":
|
|
sawUL = true
|
|
}
|
|
}
|
|
if !sawH2 || !sawP || !sawUL {
|
|
t.Fatalf("missing section types: %#v", f.Sections)
|
|
}
|
|
raw := DeriveDescriptionTemplateJSON(parts)
|
|
if raw == "" || DescriptionTemplateNeedsRepair([]byte(raw)) {
|
|
t.Fatalf("derived template should be OK: %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestEffectiveDescriptionFormula_fromPromptDescriptionSection(t *testing.T) {
|
|
t.Parallel()
|
|
prompt := strings.Join([]string{
|
|
SectionTitleStart,
|
|
`Napiši novo ime izdelka po formuli: "tip izdelka sentence case", "znamka". Ne uporabljaj vejic.`,
|
|
SectionTitleEnd,
|
|
"",
|
|
SectionDescriptionStart,
|
|
`<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>`,
|
|
`<p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p>`,
|
|
`<H2>{Izpostavi en benefit, in NE napiši Novo ime izdelka}</H2>`,
|
|
`<p>{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi tip izdelka lowercase}</p>`,
|
|
`<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`,
|
|
SectionDescriptionEnd,
|
|
}, "\n")
|
|
f := EffectiveDescriptionFormula(nil, prompt)
|
|
if len(f.Sections) < 4 {
|
|
t.Fatalf("want HTML sections from Description role, got %#v", f.Sections)
|
|
}
|
|
if f.Sections[0].Type != "h2" || f.Sections[1].Type != "p" {
|
|
t.Fatalf("unexpected order: %#v", f.Sections)
|
|
}
|
|
last := f.Sections[len(f.Sections)-1]
|
|
if last.Type != "ul" {
|
|
t.Fatalf("last should be ul specs, got %#v", last)
|
|
}
|
|
anyTpl := EffectiveDescriptionTemplateAny(nil, prompt)
|
|
if anyTpl == nil {
|
|
t.Fatal("expected non-nil template any")
|
|
}
|
|
if !CategoryTitlePromptRequiresRewrite(prompt) {
|
|
t.Fatal("Title section should require rewrite")
|
|
}
|
|
if CategoryTitlePromptRequiresRewrite("--- Title ---\nKeep supplier name.\n--- End Title ---") {
|
|
t.Fatal("keep-only Title must not require rewrite")
|
|
}
|
|
}
|
|
|
|
func TestEffectiveDescriptionFormula_prefersRicherPromptHTML(t *testing.T) {
|
|
t.Parallel()
|
|
stored := map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "p", "instructions": "Short"},
|
|
},
|
|
"metaTitle": "Stored meta title rule",
|
|
}
|
|
prompt := SectionDescriptionStart + "\n" +
|
|
`<H2>{One}</H2><p>{Two}</p><H2>{Three}</H2><p>{Four}</p>` +
|
|
`<b>{Specs}</b><ul><li>{Bullets}</li></ul>` + "\n" +
|
|
SectionDescriptionEnd + "\n" +
|
|
SectionMetaStart + "\nIgnored because stored meta exists\n" + SectionMetaEnd
|
|
f := EffectiveDescriptionFormula(stored, prompt)
|
|
if len(f.Sections) < 4 {
|
|
t.Fatalf("want prompt HTML sections to win over thin stored, got %#v", f.Sections)
|
|
}
|
|
if f.MetaTitle != "Stored meta title rule" {
|
|
t.Fatalf("stored metaTitle should win, got %q", f.MetaTitle)
|
|
}
|
|
}
|
|
|
|
func TestEffectiveDescriptionFormula_metaSectionWhenStoredEmpty(t *testing.T) {
|
|
t.Parallel()
|
|
prompt := SectionMetaStart + "\nWrite meta_description to 140 chars.\n" + SectionMetaEnd
|
|
f := EffectiveDescriptionFormula(nil, prompt)
|
|
if !strings.Contains(f.MetaDescription, "140") {
|
|
t.Fatalf("metaDescription=%q", f.MetaDescription)
|
|
}
|
|
if strings.TrimSpace(f.MetaTitle) == "" {
|
|
t.Fatal("expected default metaTitle when Meta section present")
|
|
}
|
|
}
|
|
|
|
func TestExtractEnhanceSectionBody(t *testing.T) {
|
|
t.Parallel()
|
|
prompt := SectionTitleStart + "\nHello\n" + SectionTitleEnd + "\n" +
|
|
SectionDescriptionStart + "\nBody here\n" + SectionDescriptionEnd
|
|
if got := ExtractEnhanceSectionBody(prompt, SectionTitleStart, SectionTitleEnd); got != "Hello" {
|
|
t.Fatalf("title body=%q", got)
|
|
}
|
|
if got := ExtractEnhanceSectionBody(prompt, SectionDescriptionStart, SectionDescriptionEnd); got != "Body here" {
|
|
t.Fatalf("desc body=%q", got)
|
|
}
|
|
}
|