67 lines
2.6 KiB
Go
67 lines
2.6 KiB
Go
package elderagent
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestPrepareEventTimeGuidanceIsCallScoped(t *testing.T) {
|
||
|
|
s := Service{CallbackURL: "https://example.com/callback", CallbackSecret: []byte(strings.Repeat("k", 32))}
|
||
|
|
raw := []byte(`{"S2SConfig":{"ProviderParams":{"app":{"appid":"test-app","token":"test-token"},"dialog":{"system_role":"现有人设","extra":{"model":"O"}}}},"MemoryConfig":{"Enable":false}}`)
|
||
|
|
agentRaw := []byte(`{"WelcomeMessage":"原开场","EnableConversationStateCallback":false}`)
|
||
|
|
baseBytes, agentBytes := string(raw), string(agentRaw)
|
||
|
|
welcome := "咱们聊会儿。"
|
||
|
|
guidance := strings.TrimSpace(eventTimeGuidance)
|
||
|
|
for _, override := range []*string{nil, &welcome} {
|
||
|
|
first, _, err := s.prepareConfiguration(raw, agentRaw, "first", "第一通的事实", override)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
role := eventTimeTestField(first, []string{"S2SConfig", "ProviderParams", "dialog", "system_role"})
|
||
|
|
text := role.(string)
|
||
|
|
if !strings.HasPrefix(text, "现有人设\n"+guidance) || strings.Count(text, guidance) != 1 || strings.Contains(text, "第一通的事实") {
|
||
|
|
t.Fatal("guidance missing or platform facts injected")
|
||
|
|
}
|
||
|
|
second, agent, err := s.prepareConfiguration(raw, agentRaw, "second", "第二通的事实", nil)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
role = eventTimeTestField(second, []string{"S2SConfig", "ProviderParams", "dialog", "system_role"})
|
||
|
|
if strings.Contains(role.(string), "第一通的事实") || strings.Count(role.(string), guidance) != 1 || agent["WelcomeMessage"] != "原开场" {
|
||
|
|
t.Fatal("prior call facts or opening leaked into next preparation")
|
||
|
|
}
|
||
|
|
var original map[string]any
|
||
|
|
if err := json.Unmarshal(raw, &original); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, path := range [][]string{{"S2SConfig", "ProviderParams", "app"}, {"S2SConfig", "ProviderParams", "dialog", "extra"}, {"MemoryConfig"}} {
|
||
|
|
want := eventTimeTestField(original, path)
|
||
|
|
got := eventTimeTestField(second, path)
|
||
|
|
if !reflect.DeepEqual(got, want) {
|
||
|
|
t.Fatal("unrelated provider configuration changed", path)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if string(raw) != baseBytes || string(agentRaw) != agentBytes {
|
||
|
|
t.Fatal("saved persona configuration was mutated")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
role := withEventTimeGuidance("现有人设")
|
||
|
|
if withEventTimeGuidance(role) != role {
|
||
|
|
t.Fatal("preparing an already guided role duplicates instructions")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func eventTimeTestField(root map[string]any, path []string) any {
|
||
|
|
var value any = root
|
||
|
|
for _, key := range path {
|
||
|
|
object, ok := value.(map[string]any)
|
||
|
|
if !ok {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
value = object[key]
|
||
|
|
}
|
||
|
|
return value
|
||
|
|
}
|