fixes
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package processing
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
)
|
||||
|
||||
@@ -16,8 +18,24 @@ func TestResolveProductPromptTemplates_categoryOverridesUser(t *testing.T) {
|
||||
if sys != "sys {{brand_voice}}" {
|
||||
t.Fatalf("system=%q", sys)
|
||||
}
|
||||
if user != "category user {{description}}" {
|
||||
t.Fatalf("user=%q want category override", user)
|
||||
if !strings.HasPrefix(user, "category user {{description}}") {
|
||||
t.Fatalf("user=%q want category override prefix", user)
|
||||
}
|
||||
if !strings.Contains(user, "{{attrs}}") {
|
||||
t.Fatalf("user=%q want injected {{attrs}}", user)
|
||||
}
|
||||
if !strings.Contains(user, "{{name}}") || !strings.Contains(user, "{{category}}") {
|
||||
t.Fatalf("user=%q want name/category placeholders", user)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveProductPromptTemplates_categoryWithAttrsUnchanged(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, user := resolveProductPromptTemplates(ProductInput{
|
||||
CategoryEnhancePrompt: "Write copy.\nAttrs: {{attrs}}\nName: {{name}}",
|
||||
})
|
||||
if user != "Write copy.\nAttrs: {{attrs}}\nName: {{name}}" {
|
||||
t.Fatalf("user=%q want unchanged when attrs present", user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,22 +49,105 @@ func TestResolveProductPromptTemplates_fallsBackToCompany(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderProductEnhancePrompts_categoryOverrideGetsAttrs(t *testing.T) {
|
||||
t.Parallel()
|
||||
sysTpl, userTpl := resolveProductPromptTemplates(ProductInput{
|
||||
CategoryEnhancePrompt: "Write a monitor listing. Focus on panel tech.",
|
||||
})
|
||||
_, user := RenderProductEnhancePrompts(
|
||||
sysTpl, userTpl,
|
||||
"Monitors", "UltraView 27", "", "", "", "en",
|
||||
map[string]any{"brand": "Acme", "size": "27 inch"},
|
||||
)
|
||||
if !strings.Contains(user, "Attrs:") {
|
||||
t.Fatalf("user missing Attrs block:\n%s", user)
|
||||
}
|
||||
if !strings.Contains(user, "Acme") || !strings.Contains(user, "27") {
|
||||
t.Fatalf("user missing attr values:\n%s", user)
|
||||
}
|
||||
if !strings.Contains(user, thinEnhanceUserInstruction) {
|
||||
t.Fatalf("thin input should prepend instruction:\n%s", user)
|
||||
}
|
||||
if !strings.Contains(user, "UltraView 27") {
|
||||
t.Fatalf("user missing name:\n%s", user)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCategoryEnhancePromptFor(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := map[string]company.LangPromptMap{
|
||||
"monitorji": {"sl": "prompt-a", "en": "prompt-a-en"},
|
||||
"televizorji": {"sl": "prompt-b"},
|
||||
}
|
||||
if got := categoryEnhancePromptFor(m, " Monitorji ", "sl"); got != "prompt-a" {
|
||||
if got := categoryEnhancePromptFor(m, " Monitorji ", "sl", "sl"); got != "prompt-a" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(m, " Monitorji ", "en"); got != "prompt-a-en" {
|
||||
if got := categoryEnhancePromptFor(m, " Monitorji ", "en", "sl"); got != "prompt-a-en" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(m, "missing", "sl"); got != "" {
|
||||
if got := categoryEnhancePromptFor(m, "missing", "sl", "sl"); got != "" {
|
||||
t.Fatalf("expected empty, got %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(m, "televizorji", "en"); got != "" {
|
||||
t.Fatalf("expected empty fallback, got %q", got)
|
||||
// sl-only category: en falls back to primary=sl
|
||||
if got := categoryEnhancePromptFor(m, "televizorji", "en", "sl"); got != "prompt-b" {
|
||||
t.Fatalf("en→primary sl: got %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(m, "televizorji", "en", "en"); got != "" {
|
||||
t.Fatalf("en with primary=en should stay empty, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCategoryEnhancePromptFor_uniqueIDAndNameKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
overlay := company.LangPromptMap{"*": aiprompts.CategoryEnhanceUserTemplate}
|
||||
byName := map[string]company.LangPromptMap{
|
||||
"štedilniki": overlay,
|
||||
}
|
||||
byUID := map[string]company.LangPromptMap{
|
||||
"50": overlay,
|
||||
}
|
||||
if got := categoryEnhancePromptFor(byName, "50", "sl", "sl"); got != "" {
|
||||
t.Fatalf("name-only map must miss unique_id lookup, got %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(byUID, "50", "sl", "sl"); !strings.Contains(got, "{{attrs}}") {
|
||||
t.Fatalf("unique_id key should resolve overlay: %q", got)
|
||||
}
|
||||
// Dual-index map (as loadCategoryEnhanceOverlays builds): unique_id OR name works.
|
||||
dual := map[string]company.LangPromptMap{}
|
||||
for _, k := range categoryOverlayKeys("50", "Štedilniki") {
|
||||
dual[k] = overlay
|
||||
}
|
||||
if got := categoryEnhancePromptFor(dual, "50", "en", "sl"); !strings.Contains(got, "{{category}}") {
|
||||
t.Fatalf("dual unique_id lookup: %q", got)
|
||||
}
|
||||
if got := categoryEnhancePromptFor(dual, "Štedilniki", "en", "sl"); !strings.Contains(got, "{{attrs}}") {
|
||||
t.Fatalf("dual name lookup: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCategoryOverlayKeys_indexesUIDAndName(t *testing.T) {
|
||||
t.Parallel()
|
||||
keys := categoryOverlayKeys("50", "Štedilniki")
|
||||
want := map[string]bool{"50": true, "štedilniki": true}
|
||||
if len(keys) != 2 {
|
||||
t.Fatalf("keys=%v want 2", keys)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if !want[k] {
|
||||
t.Fatalf("unexpected key %q in %v", k, keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderProductEnhancePrompts_languageLabelsSLEN(t *testing.T) {
|
||||
t.Parallel()
|
||||
sysTpl, userTpl := resolveProductPromptTemplates(ProductInput{})
|
||||
sysSL, _ := RenderProductEnhancePrompts(sysTpl, userTpl, "Cat", "Name", "Desc", "", "", "sl", nil)
|
||||
if !strings.Contains(sysSL, "Slovenian") {
|
||||
t.Fatalf("sl system missing Slovenian label:\n%s", sysSL)
|
||||
}
|
||||
sysEN, _ := RenderProductEnhancePrompts(sysTpl, userTpl, "Cat", "Name", "Desc", "", "", "en", nil)
|
||||
if !strings.Contains(sysEN, "English") {
|
||||
t.Fatalf("en system missing English label:\n%s", sysEN)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user