Files
descrybe/apps/api/internal/aiprompts/legacy_split_test.go
T
greeneclipseandClaude Fable 5 9a839d6d13 Store category prompts as EXACT split legacy text, no boilerplate
The per-category prompt editor showed English machine boilerplate
("Role: description. Build JSON …", schema intro, {{var}} context lines)
because seed/sync baked the render framing into categories.prompt. Now the
stored overlay is exactly the legacy wp_product_categories.sql content,
split into the three sections a user would type themselves:

  --- Title ---        Slovenian naming formula
  --- Description ---  legacy <H2>/<p>/<ul> body structure
  --- Meta ---         legacy metaDescription instruction

Schema, role framing, and Name/Description/Category/Attrs product context
stay render-time only (system template + ensureCategoryEnhanceUserContext),
where they already existed.

- SplitLegacyCombinedEnhancePrompt: non-legacy input now returns "" —
  categories without seed/legacy content get their override CLEARED
  (company default) instead of being stuffed with the canonical template
  (e.g. parent categories like "Bela tehnika" absent from the SQL).
- CategoryEnhancePromptNeedsRepair flags stored boilerplate ("parsed as
  JSON", "Build JSON", retired Attributes section) so Sync rewrites old
  data to the clean shape; repair supports clearing (prompt = '{}').
- seed-a1 apply-category-prompts skips instead of writing template text.
- Web editor compose stores only the user's section text: empty sections
  keep bare markers, default bodies and the default preamble are never
  persisted (DEFAULT_SECTION_BODIES removed).
- Verified locally: apply rewrote 238 A1+Demo categories (216 exact
  splits, 22 cleared), zero boilerplate matches in DB, idempotent re-run
  (would_update=0).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-18 00:53:26 +02:00

78 lines
3.0 KiB
Go

package aiprompts
import (
"strings"
"testing"
)
func TestSplitLegacyCombinedEnhancePrompt(t *testing.T) {
t.Parallel()
legacy := `Ustvari nov opis izdelka v Slovenščini z naslednjimi spremenljivkami:
Star_opis_izdelka: {""OPIS IZDELKA""};
Staro_ime_izdelka: {""STARO IME IZDELKA""};
Uporabi spodnjo GPT predlogo.
Sledi tej GPT predlogi stavek po stavek in sestavi nov opis izdelka:
GPT predloga:
<name>{Napiši novo ime izdelka po formuli: ""tip izdelka sentence case"", ""znamka s pravilno kapitalizacijo"". Ne uporabljaj vejic.}</name>
<metaDescription>{Najprej napiši Novo_ime_izdelka in potem nadaljuj dokler nisi zapisal 140 znakov vključno s presledki}</metaDescription>
<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><b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`
got := SplitLegacyCombinedEnhancePrompt(legacy)
if !CategoryEnhanceHasRoleSections(got) {
t.Fatal("split output must be role-sectioned")
}
if CategoryEnhancePromptNeedsRepair(got) {
t.Fatal("split output should not need further repair")
}
if reLegacyNameTag.MatchString(got) || reLegacyMetaTag.MatchString(got) {
t.Fatal("split must not keep legacy <name>/<metaDescription> wrapper tags")
}
if !strings.Contains(got, "tip izdelka sentence case") {
t.Fatal("title section must preserve Slovenian naming formula intent")
}
if !strings.Contains(got, "140 znakov") {
t.Fatal("meta section must preserve Slovenian SEO intent")
}
if !strings.Contains(got, "Tehnične specifikacije") && !strings.Contains(got, "tehničnih specifikacij") {
t.Fatal("description section must preserve HTML body intent")
}
// The stored overlay is EXACTLY the split legacy text — no schema/role
// boilerplate, no {{var}} placeholders (product context is render-time).
lower := strings.ToLower(got)
for _, junk := range []string{"your reply is parsed as json", "role: title", "role: description", "role: meta", "{{attrs}}", "{{name}}", "{{language}}", "product context"} {
if strings.Contains(lower, junk) {
t.Fatalf("overlay must not carry boilerplate %q:\n%s", junk, got)
}
}
if strings.Contains(got, "OPIS IZDELKA") || strings.Contains(got, "STARO IME IZDELKA") {
t.Fatal("legacy placeholders must be modernized")
}
}
func TestSplitLegacyNonLegacyReturnsEmpty(t *testing.T) {
t.Parallel()
// Non-legacy input has no per-category content to preserve — callers must
// leave the category on the company default instead of storing boilerplate.
if got := SplitLegacyCombinedEnhancePrompt("short retail overlay"); got != "" {
t.Fatalf("non-legacy should return empty, got %q", got)
}
}
func TestIsLegacyCombinedEnhancePrompt(t *testing.T) {
t.Parallel()
if IsLegacyCombinedEnhancePrompt(CategoryEnhanceUserTemplate) {
t.Fatal("canonical template is not legacy combined")
}
if !IsLegacyCombinedEnhancePrompt("GPT predloga:\n<name>{x}</name><metaDescription>{y}</metaDescription>") {
t.Fatal("expected legacy detection")
}
}