Files
descrybe/apps/api/internal/aiprompts/description_formula_precedence_test.go
2026-08-23 20:49:40 +02:00

56 lines
2.5 KiB
Go

package aiprompts
import "testing"
// Regression: A1 categories carry both a legacy HTML formula in categories.prompt
// and a stale description_template written by an old migration (seven English
// boilerplate sections). Preferring whichever side had MORE sections handed the
// stale template to the validation gate while the model was instructed from the
// prompt — every reply then failed as "formula mismatch" and enhance fell back to
// synth or supplier copy. The tenant's own prompt formula must win.
func TestEffectiveDescriptionFormula_promptFormulaBeatsStaleStoredSections(t *testing.T) {
t.Parallel()
prompt := SectionDescriptionStart + "\n" +
`<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>` +
`<p>{Napiši odstavek, ki je dolg 100 besed.}</p>` +
`<H2>{Izpostavi en benefit}</H2>` +
`<p>{Napiši odstavek, ki je dolg 100 besed.}</p>` +
`<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij}</li></ul>` + "\n" +
SectionDescriptionEnd
stale := map[string]any{"sections": []any{
map[string]any{"type": "h1", "instructions": "Write a compelling main title."},
map[string]any{"type": "p", "instructions": "Write a detailed paragraph about specific features."},
map[string]any{"type": "h2", "instructions": "Create a section heading."},
map[string]any{"type": "ul", "instructions": "List key features or specifications."},
map[string]any{"type": "p", "instructions": "Izpostavi eno prednost izdelka."},
map[string]any{"type": "p", "instructions": "Izpostavi eno prednost izdelka."},
map[string]any{"type": "p", "instructions": "Izpostavi eno prednost izdelka."},
}}
got := EffectiveDescriptionFormula(stale, prompt)
want := []string{"h2", "p", "h2", "p", "ul"}
if len(got.Sections) != len(want) {
t.Fatalf("got %d sections, want the %d from the prompt formula: %+v",
len(got.Sections), len(want), got.Sections)
}
for i, typ := range want {
if got.Sections[i].Type != typ {
t.Fatalf("section %d type=%q want %q (%+v)", i, got.Sections[i].Type, typ, got.Sections)
}
}
}
// Without a legacy formula in the prompt, the stored template still applies.
func TestEffectiveDescriptionFormula_keepsStoredWhenPromptHasNoFormula(t *testing.T) {
t.Parallel()
stored := map[string]any{"sections": []any{
map[string]any{"type": "h2", "instructions": "Heading"},
map[string]any{"type": "p", "instructions": "Body"},
}}
got := EffectiveDescriptionFormula(stored, "")
if len(got.Sections) != 2 || got.Sections[0].Type != "h2" {
t.Fatalf("stored template must survive an empty prompt: %+v", got.Sections)
}
}