package vikingmemory import ( "context" "data-terminal-cloud/internal/callmemory" "encoding/json" "fmt" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgxpool" "net/http" "os" "strings" "testing" ) func TestCuratedProjectionUnknownCreateNeverDuplicatesOrProvesDeletion(t *testing.T) { url := os.Getenv("TEST_DATABASE_URL") if url == "" { t.Skip("TEST_DATABASE_URL required") } ctx := context.Background() pool, e := pgxpool.New(ctx, url) if e != nil { t.Fatal(e) } defer pool.Close() id := "test-" + uuid.NewString() defer pool.Exec(ctx, `DELETE FROM call_provider_profile WHERE id=$1`, id) _, e = pool.Exec(ctx, `INSERT INTO call_provider_profile(id,protocol_version,sdk_version,bot_id,model_version,transport,audio_profile,vendor_template,field_mapping,capabilities,evidence,projection_schema,published) VALUES($1,'CALL-MEMORY-1.1','test','test','test','RTC','{}','{}','{}','{"H08":true}','{"H08":"synthetic test only"}','{"event_type":"curated","memory_id_field":"memory_id","record_version_field":"record_version","operation_key_field":"operation_key","event_content_field":"content"}',true)`, id) if e != nil { t.Fatal(e) } adds := 0 var stored map[string]any visible := false c := testClient(t, func(w http.ResponseWriter, r *http.Request) { var in map[string]any json.NewDecoder(r.Body).Decode(&in) switch { case strings.HasSuffix(r.URL.Path, "/search"): result := []any{} if visible && stored != nil { result = append(result, map[string]any{"id": "event-1", "user_id": []string{UserID("t", "s")}, "assistant_id": []string{ProjectionAssistantID}, "memory_info": stored}) } json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"result_list": result}}) case strings.HasSuffix(r.URL.Path, "/add"): adds++ stored = in["memory_info"].(map[string]any) fmt.Fprint(w, `{"code":0,"data":{}}`) default: t.Error("unexpected external operation", r.URL.Path) http.Error(w, "unexpected", 400) } }) a := NewProjectionAdapter(pool, c) record := callmemory.ProjectionRecord{Tenant: "t", Subject: "s", ID: uuid.NewString(), Kind: "EVENT", Content: "喜欢喝茶", Policy: "ALLOWED", Status: "ACTIVE", Version: 1} _, e = a.Project(ctx, record, "", "", false) if e == nil || adds != 1 { t.Fatal("missing add identity not treated as unknown", e, adds) } _, e = a.Project(ctx, record, "", "", true) if e == nil || adds != 1 { t.Fatal("uncertain create duplicated", e, adds) } record.Status = "PENDING_DELETE" _, e = a.Project(ctx, record, "", "", true) if e == nil { t.Fatal("empty search falsely proved deletion of uncertain write") } visible = true record.Status = "ACTIVE" result, e := a.Project(ctx, record, "", "", true) if e != nil || !result.Visible || adds != 1 { t.Fatal("failed to reconcile original event", result, e, adds) } }