104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
package processing
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTruncateLogEnds(t *testing.T) {
|
|
t.Parallel()
|
|
short := truncateLogEnds("hello", 500, 500)
|
|
if !strings.Contains(short, `body="hello"`) || !strings.Contains(short, "len=5") {
|
|
t.Fatalf("short: %s", short)
|
|
}
|
|
long := strings.Repeat("a", 600) + "MID" + strings.Repeat("b", 600)
|
|
got := truncateLogEnds(long, 10, 10)
|
|
if !strings.Contains(got, "len=1203") || !strings.Contains(got, "head=") || !strings.Contains(got, "tail=") {
|
|
t.Fatalf("long: %s", got)
|
|
}
|
|
if strings.Contains(got, "MID") {
|
|
t.Fatalf("middle should be omitted: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestFormulaTemplateSummary(t *testing.T) {
|
|
t.Parallel()
|
|
if formulaTemplateSummary(nil, "description") != "none" {
|
|
t.Fatal("nil → none")
|
|
}
|
|
desc := map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "h1", "instructions": "Heading"},
|
|
map[string]any{"type": "p", "instructions": "Body"},
|
|
map[string]any{"type": "ul", "instructions": "Specs"},
|
|
},
|
|
}
|
|
sum := formulaTemplateSummary(desc, "description")
|
|
if !strings.Contains(sum, "sections=h1+p+ul") || !strings.Contains(sum, "len=") || !strings.Contains(sum, "hash=") {
|
|
t.Fatalf("desc summary: %s", sum)
|
|
}
|
|
title := map[string]any{
|
|
"separator": " ",
|
|
"elements": []any{
|
|
map[string]any{"type": "variable", "value": "brand"},
|
|
map[string]any{"type": "text", "value": "TV"},
|
|
},
|
|
}
|
|
ts := formulaTemplateSummary(title, "title")
|
|
if !strings.Contains(ts, "elements=variable+text") || !strings.Contains(ts, `sep=" "`) {
|
|
t.Fatalf("title summary: %s", ts)
|
|
}
|
|
}
|
|
|
|
// TestA1StyleDescriptionFormulaInResolvedPrompts asserts a realistic multi-section
|
|
// category description_template (A1-style) is injected into both system and user
|
|
// enhance prompts via resolveProductPromptTemplates / RenderProductEnhancePrompts.
|
|
func TestA1StyleDescriptionFormulaInResolvedPrompts(t *testing.T) {
|
|
t.Parallel()
|
|
// Representative A1 cooker/monitor style: h1 + p + ul sections.
|
|
descFormula := map[string]any{
|
|
"sections": []any{
|
|
map[string]any{"type": "h1", "instructions": "Naziv izdelka"},
|
|
map[string]any{"type": "h2", "instructions": "Podnaslov prednosti"},
|
|
map[string]any{"type": "p", "instructions": "Kratek opis v slovenščini"},
|
|
map[string]any{"type": "ul", "instructions": "Ključne specifikacije"},
|
|
},
|
|
}
|
|
titleFormula := map[string]any{
|
|
"separator": " ",
|
|
"elements": []any{
|
|
map[string]any{"type": "variable", "value": "brand"},
|
|
map[string]any{"type": "variable", "value": "product_model"},
|
|
},
|
|
}
|
|
sysTpl, userTpl := resolveProductPromptTemplates(ProductInput{
|
|
EnhanceSystemTemplate: "You write retail copy. description: 1-2 factual sentences.",
|
|
EnhanceUserTemplate: "Category: {{category}}\nName: {{name}}\nAttrs: {{attrs}}",
|
|
TitleTemplate: titleFormula,
|
|
DescriptionTemplate: descFormula,
|
|
})
|
|
if !strings.Contains(sysTpl, "Description formula") {
|
|
t.Fatalf("system missing formula override: %s", sysTpl)
|
|
}
|
|
if !strings.Contains(userTpl, "Description formula") || !strings.Contains(userTpl, "- h1: Naziv izdelka") || !strings.Contains(userTpl, "- h2: Podnaslov prednosti") {
|
|
t.Fatalf("user missing description formula: %s", userTpl)
|
|
}
|
|
if !strings.Contains(userTpl, "Title formula") || !strings.Contains(userTpl, "attr [brand]") {
|
|
t.Fatalf("user missing title formula: %s", userTpl)
|
|
}
|
|
system, user := RenderProductEnhancePrompts(
|
|
sysTpl, userTpl,
|
|
"Štedilniki", "VOX EHT6020", "", "", "", "sl",
|
|
map[string]any{"brand": "VOX", "product_model": "EHT6020"},
|
|
)
|
|
if !strings.Contains(user, "Description formula") || !strings.Contains(user, "- h1: Naziv izdelka") {
|
|
t.Fatalf("rendered user missing formula: %s", user)
|
|
}
|
|
if !strings.Contains(user, "Slovenian") && !strings.Contains(system, "Slovenian") {
|
|
t.Fatalf("language not rendered into prompts: sys=%s user=%s", system, user)
|
|
}
|
|
if !enhanceFormulaOverride(ProductInput{DescriptionTemplate: descFormula}) {
|
|
t.Fatal("formula_override should be true")
|
|
}
|
|
}
|