146 lines
4.9 KiB
Go
146 lines
4.9 KiB
Go
package processing
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatTitleFormulaConstraint(t *testing.T) {
|
|
t.Parallel()
|
|
got := FormatTitleFormulaConstraint(map[string]any{
|
|
"separator": " ",
|
|
"elements": []any{
|
|
map[string]any{"type": "variable", "value": "brand"},
|
|
map[string]any{"type": "text", "value": "TV"},
|
|
map[string]any{"type": "variable", "value": "product_model"},
|
|
},
|
|
})
|
|
if !strings.Contains(got, `join with " "`) {
|
|
t.Fatalf("missing separator: %s", got)
|
|
}
|
|
if !strings.Contains(got, "1. attr [brand]") || !strings.Contains(got, `2. text "TV"`) || !strings.Contains(got, "3. attr [product_model]") {
|
|
t.Fatalf("missing ordered elements: %s", got)
|
|
}
|
|
if !strings.Contains(got, "{{language}}") {
|
|
t.Fatalf("missing language placeholder: %s", got)
|
|
}
|
|
if FormatTitleFormulaConstraint(nil) != "" || FormatTitleFormulaConstraint(map[string]any{}) != "" {
|
|
t.Fatal("empty templates should yield empty constraint")
|
|
}
|
|
}
|
|
|
|
func TestFormatDescriptionFormulaConstraint(t *testing.T) {
|
|
t.Parallel()
|
|
got := FormatDescriptionFormulaConstraint(map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "h1", "instructions": "Main product heading"},
|
|
map[string]any{"type": "p", "instructions": "Two factual sentences"},
|
|
map[string]any{"type": "ul", "instructions": "3 bullets of specs"},
|
|
},
|
|
})
|
|
if !strings.Contains(got, "REQUIRED") || !strings.Contains(got, "<hN>") {
|
|
t.Fatalf("missing required HTML guidance: %s", got)
|
|
}
|
|
if !strings.Contains(got, "- h1: Main product heading") {
|
|
t.Fatalf("missing h1: %s", got)
|
|
}
|
|
if !strings.Contains(got, "- p: Two factual sentences") || !strings.Contains(got, "- ul: 3 bullets of specs") {
|
|
t.Fatalf("missing sections: %s", got)
|
|
}
|
|
if !strings.Contains(got, "{{language}}") {
|
|
t.Fatalf("missing language placeholder: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestAppendDescriptionFormulaSystemOverride(t *testing.T) {
|
|
t.Parallel()
|
|
desc := map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "h1", "instructions": "Heading"},
|
|
map[string]any{"type": "p", "instructions": "Body"},
|
|
},
|
|
}
|
|
sys, user := resolveProductPromptTemplates(ProductInput{
|
|
EnhanceSystemTemplate: "Retail product copywriter.\n- description: 1-2 factual sentences",
|
|
EnhanceUserTemplate: "Name: {{name}}",
|
|
DescriptionTemplate: desc,
|
|
})
|
|
if !strings.Contains(user, "Description formula") {
|
|
t.Fatalf("missing user formula: %s", user)
|
|
}
|
|
if !strings.Contains(sys, "Description formula") || !strings.Contains(sys, "1-2 sentences") {
|
|
t.Fatalf("system should keep company text and append formula override: %s", sys)
|
|
}
|
|
if AppendDescriptionFormulaSystemOverride("plain", nil) != "plain" {
|
|
t.Fatal("empty formula must be no-op")
|
|
}
|
|
}
|
|
|
|
func TestAppendFormulaConstraints_injectedIntoResolve(t *testing.T) {
|
|
t.Parallel()
|
|
title := map[string]any{
|
|
"separator": "-",
|
|
"elements": []any{
|
|
map[string]any{"type": "variable", "value": "brand"},
|
|
map[string]any{"type": "variable", "value": "product_model"},
|
|
},
|
|
}
|
|
desc := map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "p", "instructions": "Factual summary"},
|
|
},
|
|
}
|
|
_, user := resolveProductPromptTemplates(ProductInput{
|
|
EnhanceUserTemplate: "Name: {{name}}\nAttrs: {{attrs}}",
|
|
TitleTemplate: title,
|
|
DescriptionTemplate: desc,
|
|
})
|
|
if !strings.Contains(user, "Name: {{name}}") {
|
|
t.Fatalf("lost base template: %s", user)
|
|
}
|
|
if !strings.Contains(user, "Title formula") || !strings.Contains(user, "attr [brand]") {
|
|
t.Fatalf("missing title formula: %s", user)
|
|
}
|
|
if !strings.Contains(user, "Description formula") || !strings.Contains(user, "- p: Factual summary") {
|
|
t.Fatalf("missing description formula: %s", user)
|
|
}
|
|
// Render substitutes {{language}} in formula blocks.
|
|
_, rendered := RenderProductEnhancePrompts(
|
|
"Write in {{language}}.", user,
|
|
"50", "Old Name", "", "", "", "sl",
|
|
map[string]any{"brand": "Vox", "product_model": "EHT6020"},
|
|
)
|
|
if !strings.Contains(rendered, "Slovenian") {
|
|
t.Fatalf("formula language not rendered: %s", rendered)
|
|
}
|
|
if strings.Contains(rendered, "{{language}}") {
|
|
t.Fatalf("unrendered language placeholder left: %s", rendered)
|
|
}
|
|
}
|
|
|
|
func TestCategoryFormulasFor_explicitWins(t *testing.T) {
|
|
t.Parallel()
|
|
in := ProductInput{
|
|
TitleTemplate: map[string]any{"elements": []any{map[string]any{"type": "text", "value": "X"}}},
|
|
CategoryFormulasByKey: map[string]CategoryFormulas{
|
|
"50": {TitleTemplate: map[string]any{"elements": []any{map[string]any{"type": "text", "value": "Y"}}}},
|
|
},
|
|
}
|
|
title, _ := categoryFormulasFor(in, "50")
|
|
m, _ := title.(map[string]any)
|
|
els, _ := m["elements"].([]any)
|
|
first, _ := els[0].(map[string]any)
|
|
if first["value"] != "X" {
|
|
t.Fatalf("explicit should win, got %#v", title)
|
|
}
|
|
in2 := ProductInput{
|
|
CategoryFormulasByKey: map[string]CategoryFormulas{
|
|
"50": {DescriptionTemplate: map[string]any{"sections": []any{map[string]any{"type": "p", "instructions": "Hi"}}}},
|
|
},
|
|
}
|
|
_, desc := categoryFormulasFor(in2, "50")
|
|
if FormatDescriptionFormulaConstraint(desc) == "" {
|
|
t.Fatal("expected cache formula")
|
|
}
|
|
}
|