84 lines
3.3 KiB
Go
84 lines
3.3 KiB
Go
package elderagent
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"data-terminal-cloud/internal/vikingmemory"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestNativeMemoryFollowsElderUploadIdentityAndConsent(t *testing.T) {
|
||
|
|
s := Service{VikingCollectionName: "aiphone", VikingEventType: "event_v1"}
|
||
|
|
a, err := s.nativeMemoryConfiguration("tenant-one", "elder-one", true)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
b, _ := s.nativeMemoryConfiguration("tenant-two", "elder-one", true)
|
||
|
|
c, _ := s.nativeMemoryConfiguration("tenant-one", "elder-two", true)
|
||
|
|
user := func(m map[string]any) string {
|
||
|
|
return m["ProviderParams"].(map[string]any)["filter"].(map[string]any)["user_id"].([]string)[0]
|
||
|
|
}
|
||
|
|
if user(a) != vikingmemory.UserID("tenant-one", "elder-one") || user(a) == user(b) || user(a) == user(c) {
|
||
|
|
t.Fatal("wrong upload identity or cross-elder filter")
|
||
|
|
}
|
||
|
|
filter := a["ProviderParams"].(map[string]any)["filter"].(map[string]any)
|
||
|
|
if filter["assistant_id"].([]string)[0] != vikingmemory.AssistantID {
|
||
|
|
t.Fatal("wrong assistant")
|
||
|
|
}
|
||
|
|
off, err := (&Service{}).nativeMemoryConfiguration("", "", false)
|
||
|
|
if err != nil || off["Enable"] != false || len(off) != 1 {
|
||
|
|
t.Fatal("withdrawal retained filters", off, err)
|
||
|
|
}
|
||
|
|
if _, err := (&Service{}).nativeMemoryConfiguration("tenant", "elder", true); err == nil {
|
||
|
|
t.Fatal("authorized memory silently uses missing configuration")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyNativeMemoryRejectsWrongElderEvenWhenEnabled(t *testing.T) {
|
||
|
|
s := Service{VikingCollectionName: "aiphone", VikingEventType: "event_v1"}
|
||
|
|
want, _ := s.nativeMemoryConfiguration("tenant", "elder", true)
|
||
|
|
for _, wrong := range []bool{false, true} {
|
||
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
got, _ := s.nativeMemoryConfiguration("tenant", "elder", true)
|
||
|
|
if wrong {
|
||
|
|
got, _ = s.nativeMemoryConfiguration("tenant", "another-elder", true)
|
||
|
|
}
|
||
|
|
json.NewEncoder(w).Encode(map[string]any{"Result": map[string]any{"Bots": []any{map[string]any{"Id": "bot", "Config": map[string]any{"MemoryConfig": got}}}}})
|
||
|
|
}))
|
||
|
|
client := NewClient("test-key", "test-secret", "", server.URL, "cn-north-1")
|
||
|
|
err := client.VerifyNativeMemory(context.Background(), "bot", want)
|
||
|
|
server.Close()
|
||
|
|
if (err != nil) != wrong {
|
||
|
|
t.Fatalf("readback wrong=%v err=%v", wrong, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyNativeMemoryAcceptsOmittedFalseButRejectsEnabledOrMalformed(t *testing.T) {
|
||
|
|
for _, tc := range []struct {
|
||
|
|
name string
|
||
|
|
memory map[string]any
|
||
|
|
fail bool
|
||
|
|
}{
|
||
|
|
{"omitted-false", map[string]any{}, false},
|
||
|
|
{"explicit-false", map[string]any{"Enable": false}, false},
|
||
|
|
{"still-enabled", map[string]any{"Enable": true}, true},
|
||
|
|
{"malformed", map[string]any{"Enable": "false"}, true},
|
||
|
|
} {
|
||
|
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
json.NewEncoder(w).Encode(map[string]any{"Result": map[string]any{"Bots": []any{map[string]any{"Id": "bot", "Config": map[string]any{"MemoryConfig": tc.memory}}}}})
|
||
|
|
}))
|
||
|
|
defer server.Close()
|
||
|
|
client := NewClient("test-key", "test-secret", "", server.URL, "cn-north-1")
|
||
|
|
err := client.VerifyNativeMemory(context.Background(), "bot", map[string]any{"Enable": false})
|
||
|
|
if (err != nil) != tc.fail {
|
||
|
|
t.Fatalf("unexpected readback: %v", err)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|