Files
descrybe/apps/api/internal/catalog/fix_catalog_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

87 lines
3.0 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 TestCategoryEnhanceTemplateHasAttrs(t *testing.T) {
t.Parallel()
tpl := aiprompts.CategoryEnhanceUserTemplate
if !strings.Contains(tpl, "{{attrs}}") {
t.Fatalf("template missing {{attrs}}: %q", tpl)
}
if !aiprompts.CategoryEnhanceHasRoleSections(tpl) {
t.Fatal("built-in template must include title/description/meta sections")
}
if !aiprompts.CategoryEnhancePromptNeedsRepair("legacy HTML prompt without attrs") {
t.Fatal("expected legacy prompt to need repair")
}
// The built-in template is render-time only; STORED copies of it are
// boilerplate pollution and must be repaired (cleared).
if !aiprompts.CategoryEnhancePromptNeedsRepair(tpl) {
t.Fatal("stored template text should need repair")
}
}
func TestPlatformDemoNameConstant(t *testing.T) {
t.Parallel()
if platformDemoCompanyName != "Platform Demo" {
t.Fatalf("got %q", platformDemoCompanyName)
}
}
func TestCategoryEnhancePromptMapOKAcceptsSplitOverlay(t *testing.T) {
t.Parallel()
legacy := `GPT predloga:
<name>{Napiši tip izdelka sentence case}</name>
<metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`
split := aiprompts.SplitLegacyCombinedEnhancePrompt(legacy)
m := company.LangPromptMap{"sl": split, company.LangPromptAny: split}
if !categoryEnhancePromptMapOK(m) {
t.Fatal("split overlay should count as OK")
}
}
func TestJsonbTemplatePresent(t *testing.T) {
t.Parallel()
if jsonbTemplatePresent(nil) || jsonbTemplatePresent([]byte("null")) || jsonbTemplatePresent([]byte("{}")) {
t.Fatal("empty templates must be absent")
}
if !jsonbTemplatePresent([]byte(`{"elements":[{"type":"variable","value":"brand"}]}`)) {
t.Fatal("title elements should count")
}
if !jsonbTemplatePresent([]byte(`{"sections":[{"type":"p","instructions":"x"}]}`)) {
t.Fatal("description sections should count")
}
}
func TestDeriveTitleTemplateJSON(t *testing.T) {
t.Parallel()
got := aiprompts.DeriveTitleTemplateJSON(`Napiši novo ime po formuli: "tip izdelka sentence case", "znamka s pravilno kapitalizacijo", "poln model izdelka"`)
if aiprompts.TitleTemplateNeedsRepair(got) {
t.Fatalf("derived formula still needs repair: %s", got)
}
if !strings.Contains(got, "product_type") || !strings.Contains(got, "brand") || !strings.Contains(got, "product_model") {
t.Fatalf("expected type+brand+model: %s", got)
}
fallback := aiprompts.DeriveTitleTemplateJSON("")
if !strings.Contains(fallback, "brand") || !strings.Contains(fallback, "product_model") {
t.Fatalf("fallback title template: %s", fallback)
}
if aiprompts.TitleTemplateIsBrandOnly([]byte(`{"elements":[{"type":"variable","value":"brand"}]}`)) != true {
t.Fatal("single brand stub must be brand-only")
}
}
func TestNormalizeCategoryPromptName(t *testing.T) {
t.Parallel()
if got := normalizeCategoryPromptName(" Avtosedeži in lupinice "); got != "avtosedezi in lupinice" {
t.Fatalf("got %q", got)
}
}