79c65c1da7
- Add length/range limits to KOL variable definitions: max_length and default_value for input/textarea, min_value/max_value/default_number for number. Backend validates and normalizes; frontend renders limit inputs in KolVariableConfig and applies limits at runtime in KolDynamicForm. - Sort workspace cards by most recent prompt/package update, falling back to granted_at. Adds updated_at to KolWorkspaceCard. - Polish TemplatesView, WorkspaceView, and KOL package management UI; route create/update/publish/archive/delete toasts through i18n and expand package form copy and status labels. - Remove stale KnowledgeView.vue.patch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
215 lines
5.8 KiB
Go
215 lines
5.8 KiB
Go
package app
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestKolVariableRenderPrompt(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
content string
|
|
schema KolSchemaJSON
|
|
values map[string]any
|
|
want string
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "success",
|
|
content: "Hello {{var_a}} from {{var_b}}",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_a", Key: "brand", Type: KolVariableTypeInput, Required: true},
|
|
{ID: "var_b", Key: "industry", Type: KolVariableTypeSelect},
|
|
},
|
|
},
|
|
values: map[string]any{
|
|
"var_a": "宜家",
|
|
"var_b": "家具",
|
|
},
|
|
want: "Hello 宜家 from 家具",
|
|
},
|
|
{
|
|
name: "supports key placeholders and key values",
|
|
content: "围绕 {{目标词}} 写一篇文章,并突出 {{目标词}} 的优势",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_a", Key: "目标词", Label: "目标词", Type: KolVariableTypeInput, Required: true},
|
|
},
|
|
},
|
|
values: map[string]any{
|
|
"目标词": "北京装修公司",
|
|
},
|
|
want: "围绕 北京装修公司 写一篇文章,并突出 北京装修公司 的优势",
|
|
},
|
|
{
|
|
name: "supports key placeholders with id values",
|
|
content: "Hello {{brand}}",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_a", Key: "brand", Label: "Brand", Type: KolVariableTypeInput, Required: true},
|
|
},
|
|
},
|
|
values: map[string]any{
|
|
"var_a": "宜家",
|
|
},
|
|
want: "Hello 宜家",
|
|
},
|
|
{
|
|
name: "missing required",
|
|
content: "Hello {{var_required}}",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_required", Key: "required", Label: "Required", Type: KolVariableTypeInput, Required: true},
|
|
},
|
|
},
|
|
values: map[string]any{},
|
|
wantErr: "missing variables",
|
|
},
|
|
{
|
|
name: "uses default text value when missing",
|
|
content: "Hello {{city}}",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_city", Key: "city", Label: "城市", Type: KolVariableTypeInput, Required: true, DefaultValue: stringPtr("合肥")},
|
|
},
|
|
},
|
|
values: map[string]any{},
|
|
want: "Hello 合肥",
|
|
},
|
|
{
|
|
name: "rejects text exceeding max length",
|
|
content: "Topic: {{topic}}",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_topic", Key: "topic", Label: "主题", Type: KolVariableTypeInput, Required: true, MaxLength: intPtr(4)},
|
|
},
|
|
},
|
|
values: map[string]any{"topic": "超过四个字"},
|
|
wantErr: "exceeds max length",
|
|
},
|
|
{
|
|
name: "rejects number outside configured range",
|
|
content: "Top {{count}} results",
|
|
schema: KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_count", Key: "count", Label: "数量", Type: KolVariableTypeNumber, Required: true, MinValue: float64Ptr(1), MaxValue: float64Ptr(5)},
|
|
},
|
|
},
|
|
values: map[string]any{"count": 6},
|
|
wantErr: "must be between 1 and 5",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
rendered, err := RenderPrompt(tc.content, tc.schema, tc.values)
|
|
if tc.wantErr != "" {
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), tc.wantErr)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, rendered)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestKolVariableHashPromptStable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.Equal(t, HashPrompt("abc"), HashPrompt("abc"))
|
|
require.NotEqual(t, HashPrompt("abc"), HashPrompt("abd"))
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsDuplicateID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_a", Key: "first", Type: KolVariableTypeInput},
|
|
{ID: "var_a", Key: "second", Type: KolVariableTypeSelect},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "duplicate variable id")
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsInvalidID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "bad_id", Key: "bad", Type: KolVariableTypeInput},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "start with var_")
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsDuplicateKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_a", Key: "topic", Type: KolVariableTypeInput},
|
|
{ID: "var_b", Key: "topic", Type: KolVariableTypeSelect},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "duplicate variable key")
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsLongDefaultValue(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_topic", Key: "topic", Label: "主题", Type: KolVariableTypeInput, MaxLength: intPtr(2), DefaultValue: stringPtr("过长默认值")},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "default value exceeds max length")
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsInvalidNumberRange(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_count", Key: "count", Label: "数量", Type: KolVariableTypeNumber, MinValue: float64Ptr(10), MaxValue: float64Ptr(5)},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "max value must be greater than or equal to min value")
|
|
}
|
|
|
|
func TestKolVariableValidateSchemaRejectsDefaultNumberOutOfRange(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := ValidateSchema(KolSchemaJSON{
|
|
Variables: []KolVariableDefinition{
|
|
{ID: "var_count", Key: "count", Label: "数量", Type: KolVariableTypeNumber, MinValue: float64Ptr(1), MaxValue: float64Ptr(5), DefaultNumber: float64Ptr(6)},
|
|
},
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "default number must be between 1 and 5")
|
|
}
|
|
|
|
func stringPtr(value string) *string {
|
|
return &value
|
|
}
|