This commit is contained in:
2026-08-16 23:42:00 +02:00
parent 6e77154228
commit 92f046b542
32 changed files with 1443 additions and 105 deletions
@@ -76,6 +76,118 @@ func TestAppendDescriptionFormulaSystemOverride(t *testing.T) {
}
}
func TestAppendTitleFormulaSystemOverride(t *testing.T) {
t.Parallel()
title := map[string]any{
"separator": " ",
"elements": []any{
map[string]any{"type": "variable", "value": "brand"},
map[string]any{"type": "text", "value": "Pro"},
},
}
sys, user := resolveProductPromptTemplates(ProductInput{
EnhanceSystemTemplate: "Retail product copywriter.\n- name: short retail title",
EnhanceUserTemplate: "Name: {{name}}",
TitleTemplate: title,
})
if !strings.Contains(user, "Title formula") || !strings.Contains(user, "attr [brand]") {
t.Fatalf("missing user title formula: %s", user)
}
if !strings.Contains(sys, `When the user message includes a "Title formula"`) || !strings.Contains(sys, "short retail title") {
t.Fatalf("system should keep company text and append title override: %s", sys)
}
if AppendTitleFormulaSystemOverride("plain", nil) != "plain" {
t.Fatal("empty title formula must be no-op")
}
dup := AppendTitleFormulaSystemOverride(sys, title)
if strings.Count(dup, `When the user message includes a "Title formula"`) != 1 {
t.Fatalf("title override must be idempotent: %s", dup)
}
}
func TestAppendCategoryEnhanceSystemOverlay(t *testing.T) {
t.Parallel()
if AppendCategoryEnhanceSystemOverlay("plain", "") != "plain" {
t.Fatal("empty category prompt must be no-op")
}
got := AppendCategoryEnhanceSystemOverlay("Retail system.", "Focus on panel tech.")
if !strings.Contains(got, "Retail system.") || !strings.Contains(got, `apply it to "name", "description", and "attrs"`) {
t.Fatalf("expected category system overlay: %s", got)
}
if AppendCategoryEnhanceSystemOverlay(got, "again") != got {
t.Fatal("category overlay must be idempotent")
}
legacy := AppendCategoryEnhanceSystemOverlay("Retail.", "x")
// Force-inject legacy marker then upgrade.
legacy = "Retail.\nWhen the user message includes category guidance, apply it to BOTH \"name\" and \"description\" (tone)."
upgraded := AppendCategoryEnhanceSystemOverlay(legacy, "Focus")
if !strings.Contains(upgraded, `apply it to "name", "description", and "attrs"`) {
t.Fatalf("legacy overlay should upgrade to attrs: %s", upgraded)
}
}
func TestFormatAttributeAllowlistConstraint(t *testing.T) {
t.Parallel()
allowed := map[string]struct{}{
"diagonala_zaslona": {},
"vrsta_panela": {},
}
title := map[string]any{
"separator": " ",
"elements": []any{
map[string]any{"type": "variable", "value": "brand"},
map[string]any{"type": "variable", "value": "product_model"},
},
}
got := FormatAttributeAllowlistConstraint(allowed, title)
if !strings.Contains(got, "Allowed attribute keys") {
t.Fatalf("missing header: %s", got)
}
if !strings.Contains(got, "diagonala_zaslona") || !strings.Contains(got, "vrsta_panela") {
t.Fatalf("missing category keys: %s", got)
}
if !strings.Contains(got, "brand") || !strings.Contains(got, "product_model") {
t.Fatalf("missing formula keys: %s", got)
}
if FormatAttributeAllowlistConstraint(nil, nil) != "" {
t.Fatal("empty allowlist+formula must be no-op")
}
appended := AppendAttributeConstraints("Attrs: {{attrs}}", allowed, title)
if !strings.Contains(appended, "Allowed attribute keys") || !strings.HasPrefix(appended, "Attrs:") {
t.Fatalf("append failed: %s", appended)
}
if AppendAttributeConstraints(appended, allowed, title) != appended {
t.Fatal("attribute constraints must be idempotent")
}
}
func TestResolveProductPromptTemplates_includesAttributeAllowlist(t *testing.T) {
t.Parallel()
_, user := resolveProductPromptTemplates(ProductInput{
EnhanceUserTemplate: "Name: {{name}}",
CategoryUniqueID: "50",
CategoryAttrKeys: map[string]map[string]struct{}{
"50": {"diagonala_zaslona": {}, "vrsta_panela": {}},
},
TitleTemplate: map[string]any{
"separator": " ",
"elements": []any{
map[string]any{"type": "variable", "value": "brand"},
},
},
CategoryEnhancePrompt: "Focus on panel tech for monitors.",
})
if !strings.Contains(user, "Allowed attribute keys") {
t.Fatalf("missing attribute allowlist in user tpl: %s", user)
}
if !strings.Contains(user, "diagonala_zaslona") {
t.Fatalf("missing category attr key: %s", user)
}
if !strings.Contains(user, "Focus on panel tech") {
t.Fatalf("missing category overlay: %s", user)
}
}
func TestDescriptionSatisfiesFormula(t *testing.T) {
t.Parallel()
tpl := map[string]any{
@@ -119,8 +231,10 @@ func TestAppendFormulaConstraints_injectedIntoResolve(t *testing.T) {
"sections": []any{
map[string]any{"type": "p", "instructions": "Factual summary"},
},
"metaTitle": "Include product name and main benefit (50-60 chars)",
"metaDescription": "Write a short Meta Description spotlighting key specs (120-155 chars)",
}
_, user := resolveProductPromptTemplates(ProductInput{
sys, user := resolveProductPromptTemplates(ProductInput{
EnhanceUserTemplate: "Name: {{name}}\nAttrs: {{attrs}}",
TitleTemplate: title,
DescriptionTemplate: desc,
@@ -134,6 +248,12 @@ func TestAppendFormulaConstraints_injectedIntoResolve(t *testing.T) {
if !strings.Contains(user, "Description formula") || !strings.Contains(user, "- p: Factual summary") {
t.Fatalf("missing description formula: %s", user)
}
if !strings.Contains(user, "SEO meta formula") || !strings.Contains(user, "meta_title (50-60 chars)") {
t.Fatalf("missing SEO meta formula: %s", user)
}
if !strings.Contains(sys, "SEO meta formula") {
t.Fatalf("system missing meta override: %s", sys)
}
// Render substitutes {{language}} in formula blocks.
_, rendered := RenderProductEnhancePrompts(
"Write in {{language}}.", user,
@@ -148,6 +268,29 @@ func TestAppendFormulaConstraints_injectedIntoResolve(t *testing.T) {
}
}
func TestFormatMetaFormulaConstraint(t *testing.T) {
t.Parallel()
got := FormatMetaFormulaConstraint(map[string]any{
"metaTitle": "Create a concise Meta Title with product name",
"metaDescription": "Write a short Meta Description for search results",
"sections": []any{
map[string]any{"type": "p", "instructions": "Body"},
},
})
if !strings.Contains(got, "SEO meta formula") {
t.Fatalf("missing header: %s", got)
}
if !strings.Contains(got, "meta_title (50-60 chars): Create a concise Meta Title") {
t.Fatalf("missing meta_title: %s", got)
}
if !strings.Contains(got, "meta_description (120-155 chars): Write a short Meta Description") {
t.Fatalf("missing meta_description: %s", got)
}
if FormatMetaFormulaConstraint(map[string]any{"sections": []any{}}) != "" {
t.Fatal("sections-only template must not invent SEO meta formula")
}
}
func TestCategoryFormulasFor_explicitWins(t *testing.T) {
t.Parallel()
in := ProductInput{