Files
descrybe/apps/api/internal/aiprompts/roles_test.go
T

97 lines
3.5 KiB
Go
Raw Normal View History

2026-08-16 23:42:00 +02:00
package aiprompts
import (
"strings"
"testing"
)
func TestCategoryEnhanceHasRoleSections(t *testing.T) {
t.Parallel()
if !CategoryEnhanceHasRoleSections(CategoryEnhanceUserTemplate) {
t.Fatal("canonical CategoryEnhanceUserTemplate must have title/description/meta sections")
2026-08-16 23:42:00 +02:00
}
if CategoryEnhanceHasRoleSections("legacy HTML prompt") {
t.Fatal("unstructured prompt must not report role sections")
}
if CategoryEnhanceHasRoleSections("--- Title ---\nonly title") {
t.Fatal("partial sections must not pass")
}
// Old stored prompts with an attributes section still count as sectioned
// (repair rewrites them via CategoryEnhancePromptNeedsRepair).
old := "--- Title ---\nx\n--- Description ---\ny\n--- Meta ---\nz\n--- Attributes ---\na\n--- End Attributes ---"
if !CategoryEnhanceHasRoleSections(old) {
t.Fatal("old attributes-bearing prompt must still report role sections")
}
if !CategoryEnhancePromptNeedsRepair(old) {
t.Fatal("old attributes-bearing prompt must need repair (attributes section retired)")
}
// The built-in template is render-time only; a STORED copy is boilerplate
// pollution and must be repaired (cleared to company default).
if !CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("stored canonical template text must need repair")
}
2026-08-16 23:42:00 +02:00
}
func TestCategoryPromptRolesOrder(t *testing.T) {
t.Parallel()
want := []string{RoleCategorize, RoleTitle, RoleDescription, RoleMeta, RoleAttributes}
if len(CategoryPromptRoles) != len(want) {
t.Fatalf("roles len=%d want %d", len(CategoryPromptRoles), len(want))
}
for i, w := range want {
if CategoryPromptRoles[i] != w {
t.Fatalf("roles[%d]=%q want %q", i, CategoryPromptRoles[i], w)
}
}
}
func TestCategoryEnhanceUserTemplateRoleMarkers(t *testing.T) {
t.Parallel()
tpl := CategoryEnhanceUserTemplate
for _, marker := range []string{
SectionTitleStart, SectionTitleEnd,
SectionDescriptionStart, SectionDescriptionEnd,
SectionMetaStart, SectionMetaEnd,
} {
if !strings.Contains(tpl, marker) {
t.Fatalf("template missing %q", marker)
}
}
// Attribute prompting is retired from category prompts (pipeline-level only).
if strings.Contains(tpl, SectionAttributesStart) || strings.Contains(strings.ToLower(tpl), "role: attributes") {
t.Fatal("template must not carry an attributes role section")
}
2026-08-16 23:42:00 +02:00
lower := strings.ToLower(tpl)
for _, role := range []string{"role: title", "role: description", "role: meta"} {
2026-08-16 23:42:00 +02:00
if !strings.Contains(lower, role) {
t.Fatalf("template missing %q", role)
}
}
// HeuristicCompleter / labeledPromptValue depend on these product context labels.
for _, label := range []string{"Name: {{name}}", "Description: {{description}}", "Category: {{category}}", "Attrs: {{attrs}}"} {
if !strings.Contains(tpl, label) {
t.Fatalf("template missing product label %q", label)
}
}
if !strings.Contains(tpl, "meta_title") || !strings.Contains(tpl, "meta_description") {
t.Fatal("template must keep enhance meta_* schema (parallel title/meta work)")
}
if !strings.Contains(tpl, `"attrs":{}`) {
t.Fatal("template must keep enhance attrs schema (parallel attributes work)")
}
}
func TestSEOMetaUserTemplateHasMetaSection(t *testing.T) {
t.Parallel()
def, ok := DefaultFor(KeySEOMeta)
if !ok {
t.Fatal("missing seo_meta default")
}
if !strings.Contains(def.UserTemplate, SectionMetaStart) {
t.Fatal("seo_meta user template should use --- Meta --- section")
}
if !strings.Contains(strings.ToLower(def.UserTemplate), "role: meta") {
t.Fatal("seo_meta user template should declare Role: meta")
}
}