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>
96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package catalog
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
|
)
|
|
|
|
func TestCategoryEnhancePromptMapRepairStates(t *testing.T) {
|
|
t.Parallel()
|
|
// Clean split overlay (pure legacy text) is OK under sl and *.
|
|
split := aiprompts.SplitLegacyCombinedEnhancePrompt(`GPT predloga:
|
|
<name>{Napiši tip izdelka}</name>
|
|
<metaDescription>{140 znakov}</metaDescription>
|
|
<H2>{benefit}</H2>`)
|
|
if split == "" {
|
|
t.Fatal("expected split overlay")
|
|
}
|
|
if !categoryEnhancePromptMapOK(company.LangPromptMap{"sl": split, company.LangPromptAny: split}) {
|
|
t.Fatal("split overlay map should be OK")
|
|
}
|
|
if !categoryEnhancePromptMapOK(company.LangPromptMap{"sl": split}) {
|
|
t.Fatal("sl-only split map should be OK (idempotent)")
|
|
}
|
|
if !categoryEnhancePromptMapOK(company.LangPromptMap{company.LangPromptAny: split}) {
|
|
t.Fatal("*-only split map should be OK (idempotent)")
|
|
}
|
|
legacy := company.LangPromptMap{"sl": "<H2>legacy HTML marketing</H2>"}
|
|
if categoryEnhancePromptMapOK(legacy) {
|
|
t.Fatal("legacy HTML must need repair")
|
|
}
|
|
// Stored template boilerplate (old seed versions) must need repair → cleared.
|
|
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
|
|
if categoryEnhancePromptMapOK(company.LangPromptMap{"sl": tpl, company.LangPromptAny: tpl}) {
|
|
t.Fatal("stored template boilerplate must need repair")
|
|
}
|
|
}
|
|
|
|
func TestRepairTargetsUseSharedTemplate(t *testing.T) {
|
|
t.Parallel()
|
|
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
|
|
for _, v := range []string{"name", "description", "attrs", "category", "language"} {
|
|
if !strings.Contains(tpl, "{{"+v+"}}") {
|
|
t.Fatalf("shared template missing {{%s}}", v)
|
|
}
|
|
}
|
|
if !strings.Contains(strings.ToLower(tpl), "never brand-only") {
|
|
t.Fatal("Title section must forbid brand-only names")
|
|
}
|
|
if a1CompanyID == "" || platformDemoCompanyName == "" {
|
|
t.Fatal("missing company target constants")
|
|
}
|
|
}
|
|
|
|
func TestComputeRepairedEnhancePromptClears(t *testing.T) {
|
|
t.Parallel()
|
|
// No seed + no legacy content → clear (fallback), never template boilerplate.
|
|
prompt, fromSeed, fromLegacy, fallback := computeRepairedEnhancePrompt(
|
|
company.LangPromptMap{"sl": strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)}, "", false)
|
|
if prompt != "" || fromSeed || fromLegacy || !fallback {
|
|
t.Fatalf("want clear fallback, got prompt=%q seed=%v legacy=%v fallback=%v", prompt, fromSeed, fromLegacy, fallback)
|
|
}
|
|
// Seed legacy → split overlay with the exact Slovenian text.
|
|
seed := `GPT predloga:
|
|
<name>{Napiši tip izdelka}</name>
|
|
<metaDescription>{140 znakov}</metaDescription>
|
|
<H2>{benefit}</H2>`
|
|
prompt, fromSeed, fromLegacy, fallback = computeRepairedEnhancePrompt(nil, seed, true)
|
|
if prompt == "" || !fromSeed || !fromLegacy || fallback {
|
|
t.Fatalf("want seed split, got prompt=%q seed=%v legacy=%v fallback=%v", prompt, fromSeed, fromLegacy, fallback)
|
|
}
|
|
if !strings.Contains(prompt, "140 znakov") || strings.Contains(strings.ToLower(prompt), "build json") {
|
|
t.Fatalf("split must keep legacy text without boilerplate: %q", prompt)
|
|
}
|
|
}
|
|
|
|
func TestLegacyTitleRulesFromSeedShape(t *testing.T) {
|
|
t.Parallel()
|
|
seed := `Ustvari nov opis
|
|
|
|
GPT predloga:
|
|
<name>{Napiši novo ime izdelka po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka". Ne uporabljaj vejic.}</name>
|
|
<metaDescription>{140 znakov}</metaDescription>
|
|
<H2>{benefit}</H2>`
|
|
rules := legacyTitleRules(nil, seed)
|
|
if !strings.Contains(strings.ToLower(rules), "znamka") {
|
|
t.Fatalf("expected title rules from seed, got %q", rules)
|
|
}
|
|
raw := aiprompts.DeriveTitleTemplateJSON(rules)
|
|
if aiprompts.TitleTemplateNeedsRepair(raw) {
|
|
t.Fatalf("derived title_template still needs repair: %s", raw)
|
|
}
|
|
}
|