82 lines
3.8 KiB
Go
82 lines
3.8 KiB
Go
package saas
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"data-terminal-cloud/internal/audit"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func TestConsentWithdrawalCreatesReliableConversationStop(t *testing.T) {
|
|
databaseURL := os.Getenv("TEST_DATABASE_URL")
|
|
if databaseURL == "" {
|
|
t.Skip("TEST_DATABASE_URL is not set")
|
|
}
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, databaseURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer pool.Close()
|
|
tx, err := pool.BeginTx(ctx, pgx.TxOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback(context.Background())
|
|
|
|
consentSnapshotID := uuid.NewString()
|
|
contextSnapshotID := uuid.NewString()
|
|
conversationID := uuid.NewString()
|
|
conversationPublicID := uuid.NewString()
|
|
taskRef := "consent-stop-test-" + uuid.NewString()
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO consent_snapshot(id,tenant_id,subject_id,snapshot_version,effective_consents,source_consent_ids)
|
|
VALUES($1::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,1,'{"voice_chat":true,"story_extraction":true}'::jsonb,'[]'::jsonb)`, consentSnapshotID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO context_snapshot(id,tenant_id,subject_id,profile_version,consent_snapshot_id,prompt_bundle_version,engine_mode,content_digest)
|
|
VALUES($2::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,1,$1::uuid,'consent-stop-test','s2s_hybrid',repeat('8',64))`, consentSnapshotID, contextSnapshotID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO conversation(id,public_id,tenant_id,household_id,subject_id,device_id,consent_snapshot_id,context_snapshot_id,provider_task_ref,engine_mode,retention_mode,status,allocation_expires_at,started_at,lease_expires_at)
|
|
VALUES($3::uuid,$4::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'10000000-0000-0000-0000-000000000201'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,'10000000-0000-0000-0000-000000000502'::uuid,$1::uuid,$2::uuid,$5,'s2s_hybrid','transcript_only','ACTIVE',now()+interval '5 minutes',now(),now()+interval '30 seconds')`, consentSnapshotID, contextSnapshotID, conversationID, conversationPublicID, taskRef); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO rtc_task(conversation_id,provider,provider_task_ref,room_id,device_rtc_user_id,agent_rtc_user_id,status,lease_expires_at)
|
|
VALUES($1::uuid,'mock',$2,$3,'consent-stop-device','consent-stop-agent','ACTIVE',now()+interval '30 seconds')`,
|
|
conversationID, taskRef, "consent-stop-room-"+uuid.NewString()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
service := NewService(pool, audit.NewRepository(), nil, nil)
|
|
affected, err := service.stopConversationsAfterConsentWithdrawal(ctx, tx,
|
|
"10000000-0000-0000-0000-000000000001", "10000000-0000-0000-0000-000000000401", ConsentStoryExtraction)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(affected) != 1 || affected[0] != conversationPublicID {
|
|
t.Fatalf("affected=%v", affected)
|
|
}
|
|
var state, stopReason, rtcState string
|
|
var historyCount, outboxCount int
|
|
if err := tx.QueryRow(ctx, `
|
|
SELECT c.status,c.stop_reason,rt.status,
|
|
(SELECT count(*) FROM conversation_state_history h WHERE h.conversation_id=c.id AND h.to_status='STOPPING'),
|
|
(SELECT count(*) FROM outbox_event o WHERE o.aggregate_id=c.id AND o.event_type='conversation.agent_stop_requested')
|
|
FROM conversation c JOIN rtc_task rt ON rt.conversation_id=c.id WHERE c.id=$1::uuid`, conversationID).
|
|
Scan(&state, &stopReason, &rtcState, &historyCount, &outboxCount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if state != "STOPPING" || stopReason != "consent_withdrawn:story_extraction" || rtcState != "STOPPING" || historyCount != 1 || outboxCount != 1 {
|
|
t.Fatalf("state=%s reason=%s rtc=%s history=%d outbox=%d", state, stopReason, rtcState, historyCount, outboxCount)
|
|
}
|
|
}
|