package storeeventrunner import ( "regexp" "testing" "time" "queryorchestration/internal/database/repository" documentstore "queryorchestration/internal/document/store" "queryorchestration/internal/server/runner" "queryorchestration/internal/serviceconfig/objectstore" "queryorchestration/internal/serviceconfig/queue" "queryorchestration/internal/serviceconfig/queue/documentinit" "queryorchestration/internal/test" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) type DocInitConfig struct { runner.BaseConfig[S3EventNotification] documentinit.DocInitConfig queue.QueueConfig objectstore.ObjectStoreConfig } func TestStoreEventRunner(t *testing.T) { cfg := &DocInitConfig{} test.CreateDB(t, cfg) acfg := test.CreateAWSContainer(t, cfg) test.SetQueueClient(t, t.Context(), cfg, acfg.ExternalEndpoint) cfg.DocInitURL = test.CreateQueue(t, cfg, test.DocInitRunnerName) runner := New(&Services{ documentstore.New(cfg), }) clientId := "hi" bucketName := "bucketName" location := objectstore.BucketKey{ Location: objectstore.Import, ClientID: clientId, CreatedAt: time.Now().UTC(), EntityID: uuid.New(), } doc := S3EventNotification{ Records: []S3EventRecord{ { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: location.String(), ETag: "example_hash", }, }, }, }, } err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{ Clientid: clientId, Name: "name", }) require.NoError(t, err) assert.True(t, runner.Process(t.Context(), doc)) test.AssertMessageBody(t, cfg, cfg.GetDocInitURL(), regexp.MustCompile(`^\{"bucket":"bucketName","key":"hi\/import\/.+\/0\/.+","hash":"example_hash"\}$`)) } func TestStoreEventRunner_BatchFiltering(t *testing.T) { cfg := &DocInitConfig{} test.CreateDB(t, cfg) acfg := test.CreateAWSContainer(t, cfg) test.SetQueueClient(t, t.Context(), cfg, acfg.ExternalEndpoint) cfg.DocInitURL = test.CreateQueue(t, cfg, test.DocInitRunnerName) runner := New(&Services{ documentstore.New(cfg), }) bucketName := "bucketName" // Test that batch keys are filtered out batchDoc := S3EventNotification{ Records: []S3EventRecord{ { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: "batches/test_client/2025/10/06/20/batch-id/file.pdf", ETag: "example_hash", }, }, }, }, } // Should return true (success) but not process the batch message assert.True(t, runner.Process(t.Context(), batchDoc)) // Verify no message was sent to the queue (batch was filtered) // Try to receive a message with a short wait time - should get nothing result, err := cfg.ReceiveFromQueue(t.Context(), &queue.ReceiveParams{ QueueURL: cfg.GetDocInitURL(), VisibilityTimeout: 1, WaitTimeSeconds: 1, }) require.NoError(t, err) assert.Empty(t, result.Messages, "expected no messages in queue for batch key") } func TestStoreEventRunner_MixedMessages(t *testing.T) { cfg := &DocInitConfig{} test.CreateDB(t, cfg) acfg := test.CreateAWSContainer(t, cfg) test.SetQueueClient(t, t.Context(), cfg, acfg.ExternalEndpoint) cfg.DocInitURL = test.CreateQueue(t, cfg, test.DocInitRunnerName) runner := New(&Services{ documentstore.New(cfg), }) clientId := "hi" bucketName := "bucketName" location := objectstore.BucketKey{ Location: objectstore.Import, ClientID: clientId, CreatedAt: time.Now().UTC(), EntityID: uuid.New(), } err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{ Clientid: clientId, Name: "name", }) require.NoError(t, err) // Mix of batch and normal messages mixedDoc := S3EventNotification{ Records: []S3EventRecord{ { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: "batches/test_client/2025/10/06/20/batch-id/file.pdf", ETag: "batch_hash", }, }, }, { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: location.String(), ETag: "example_hash", }, }, }, }, } // Should return true and only process the non-batch message assert.True(t, runner.Process(t.Context(), mixedDoc)) // Verify only one message was sent (the non-batch one) test.AssertMessageBody(t, cfg, cfg.GetDocInitURL(), regexp.MustCompile(`^\{"bucket":"bucketName","key":"hi\/import\/.+\/0\/.+","hash":"example_hash"\}$`)) }