2026-08-16 16:57:36 +02:00
package catalog
import (
"strings"
"testing"
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
)
2026-08-18 00:53:26 +02:00
func TestCategoryEnhancePromptMapRepairStates ( t * testing . T ) {
2026-08-16 16:57:36 +02:00
t . Parallel ()
2026-08-18 00:53:26 +02:00
// 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" )
2026-08-16 16:57:36 +02:00
}
2026-08-18 00:53:26 +02:00
if ! categoryEnhancePromptMapOK ( company . LangPromptMap { "sl" : split , company . LangPromptAny : split }) {
t . Fatal ( "split overlay map should be OK" )
2026-08-16 16:57:36 +02:00
}
2026-08-18 00:53:26 +02:00
if ! categoryEnhancePromptMapOK ( company . LangPromptMap { "sl" : split }) {
t . Fatal ( "sl-only split map should be OK (idempotent)" )
2026-08-16 16:57:36 +02:00
}
2026-08-18 00:53:26 +02:00
if ! categoryEnhancePromptMapOK ( company . LangPromptMap { company . LangPromptAny : split }) {
t . Fatal ( "*-only split map should be OK (idempotent)" )
2026-08-16 16:57:36 +02:00
}
legacy := company . LangPromptMap { "sl" : "<H2>legacy HTML marketing</H2>" }
2026-08-17 00:39:25 +02:00
if categoryEnhancePromptMapOK ( legacy ) {
2026-08-16 16:57:36 +02:00
t . Fatal ( "legacy HTML must need repair" )
}
2026-08-18 00:53:26 +02:00
// 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" )
2026-08-16 16:57:36 +02:00
}
}
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 )
}
}
2026-08-17 00:39:25 +02:00
if ! strings . Contains ( strings . ToLower ( tpl ), "never brand-only" ) {
t . Fatal ( "Title section must forbid brand-only names" )
}
2026-08-16 16:57:36 +02:00
if a1CompanyID == "" || platformDemoCompanyName == "" {
t . Fatal ( "missing company target constants" )
}
}
2026-08-17 00:39:25 +02:00
2026-08-18 00:53:26 +02:00
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 )
}
}
2026-08-17 00:39:25 +02:00
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 )
}
}