3ef0807456
- Updated the regex for placeholder matching to support more flexible key formats. - Refactored variable storage to allow both ID and key lookups in rendering. - Improved schema validation to check for duplicate keys and enforce non-empty keys. - Added new utility functions for variable value lookup and display name generation. - Enhanced tests to cover new key-based variable scenarios. - Refactored KolPrompt repository to simplify prompt storage and management, including the removal of obsolete revision handling. - Introduced new API endpoints for saving, activating, and archiving prompts. - Added new utility functions for handling Kol placeholders and platform options in the admin web. - Implemented database migrations to simplify prompt storage structure and ensure data integrity.
139 lines
3.4 KiB
Go
139 lines
3.4 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",
|
|
},
|
|
}
|
|
|
|
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")
|
|
}
|