Files
descrybe/apps/api/internal/aiprompts/title_formula_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

113 lines
3.8 KiB
Go

package aiprompts
import (
"strings"
"testing"
)
func TestDeriveTitleFormulaFromLegacyNameRules_Agregati(t *testing.T) {
t.Parallel()
rules := `Napiši novo ime izdelka po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka, če lahko z besedo in ID uppercase". Ne uporabljaj vejic.`
f := DeriveTitleFormulaFromLegacyNameRules(rules)
if len(f.Elements) < 3 {
t.Fatalf("want >=3 elements, got %#v", f.Elements)
}
vals := make([]string, 0, len(f.Elements))
for _, el := range f.Elements {
if el.Type == "variable" {
vals = append(vals, el.Value)
}
}
wantOrder := []string{"product_type", "brand", "product_model"}
for i, w := range wantOrder {
if i >= len(vals) || vals[i] != w {
t.Fatalf("vars=%v want prefix %v", vals, wantOrder)
}
}
if TitleTemplateIsBrandOnly(f) {
t.Fatal("derived formula must not be brand-only")
}
}
func TestDeriveTitleFormulaFromLegacyNameRules_CarSeatOrder(t *testing.T) {
t.Parallel()
rules := `Napiši novo ime izdelka po formuli: "znamka s pravilno kapitalizacijo", "tip izdelka lowercase", "poln model izdelka, če lahko z besedo in ID uppercase", "skupina po teži [kg]". Ne uporabljaj vejic.`
f := DeriveTitleFormulaFromLegacyNameRules(rules)
vars := []string{}
for _, el := range f.Elements {
if el.Type == "variable" {
vars = append(vars, el.Value)
}
}
if len(vars) < 3 || vars[0] != "brand" || vars[1] != "product_type" || vars[2] != "product_model" {
t.Fatalf("unexpected order: %v", vars)
}
foundWeight := false
for _, v := range vars {
if v == "weight_group" {
foundWeight = true
}
}
if !foundWeight {
t.Fatalf("missing weight_group in %v", vars)
}
}
func TestTitleTemplateIsBrandOnly(t *testing.T) {
t.Parallel()
brandOnly := map[string]any{
"separator": " ",
"elements": []any{
map[string]any{"type": "variable", "value": "brand", "label": "Znamka"},
},
}
if !TitleTemplateIsBrandOnly(brandOnly) {
t.Fatal("single brand variable should be brand-only")
}
if !TitleTemplateNeedsRepair(brandOnly) {
t.Fatal("brand-only needs repair")
}
if TitleTemplateIsBrandOnly(DefaultRetailTitleFormula()) {
t.Fatal("default retail must not be brand-only")
}
if TitleTemplateNeedsRepair(DefaultRetailTitleFormula()) {
t.Fatal("default retail must not need repair")
}
if !TitleTemplateNeedsRepair(nil) {
t.Fatal("nil needs repair")
}
}
func TestTitleRoleInstructionNeverBrandOnly(t *testing.T) {
t.Parallel()
if !strings.Contains(strings.ToLower(TitleRoleInstruction), "never brand-only") {
t.Fatal("TitleRoleInstruction must forbid brand-only names")
}
if !strings.Contains(CategoryEnhanceUserTemplate, TitleRoleInstruction) {
t.Fatal("CategoryEnhanceUserTemplate must embed TitleRoleInstruction")
}
// Split overlays carry ONLY the legacy per-category text; the never-brand-only
// rule lives in the render-time role framing, not the stored prompt.
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
<name>{Napiši tip izdelka, znamka, poln model}</name>
<metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`)
if strings.Contains(split, "never brand-only") {
t.Fatalf("split overlay must not embed role boilerplate: %s", split)
}
if !strings.Contains(split, "Napiši tip izdelka, znamka, poln model") {
t.Fatalf("split Title section must keep the legacy naming text: %s", split)
}
}
func TestDeriveTitleTemplateJSON_roundTrip(t *testing.T) {
t.Parallel()
raw := DeriveTitleTemplateJSON(`formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka"`)
if TitleTemplateNeedsRepair(raw) {
t.Fatalf("derived JSON should be OK: %s", raw)
}
if !strings.Contains(raw, `"product_type"`) || !strings.Contains(raw, `"brand"`) || !strings.Contains(raw, `"product_model"`) {
t.Fatalf("missing core vars: %s", raw)
}
}