From 4e1a74d9fff03b66ac30b0da9d4a6e9004a2b2a0 Mon Sep 17 00:00:00 2001 From: dizi Date: Fri, 30 Oct 2026 14:08:14 +0300 Subject: [PATCH] test: cover reconciliation dry runs --- internal/postgres/store_integration_test.go | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/internal/postgres/store_integration_test.go b/internal/postgres/store_integration_test.go index e478d9be..48675230 100644 --- a/internal/postgres/store_integration_test.go +++ b/internal/postgres/store_integration_test.go @@ -18,6 +18,7 @@ import ( "gitea.privcloud.tech/dizi/mtusi2026lab5/internal/database" "gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model" "gitea.privcloud.tech/dizi/mtusi2026lab5/internal/postgres" + "gitea.privcloud.tech/dizi/mtusi2026lab5/internal/reconcile" ) type PostgresSuite struct { @@ -412,6 +413,43 @@ func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() { s.Equal(model.AttachmentReady, visible.Status) } +func (s *PostgresSuite) TestReconciliationDryRunPreservesDeletingAttachment() { + owner, chatID := createOwnerAndChat(s.T(), s.store) + pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{ + ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "deleting.png", + ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("e", 64), + ObjectKey: "attachments/" + uuid.NewString() + "/original", + }) + s.Require().NoError(err) + claimed, err := s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour)) + s.Require().NoError(err) + s.True(claimed) + + reconciler, err := reconcile.New(s.store, emptyObjectStore{}, reconcile.Config{ + Prefix: "attachments/", PageSize: 1000, GraceTime: time.Minute, + UnreferencedRetention: time.Hour, + }) + s.Require().NoError(err) + report, err := reconciler.Run(context.Background()) + s.Require().NoError(err) + s.Zero(report.DeletedUnreferencedRows) + + var status string + err = s.pool.QueryRow(context.Background(), "SELECT status FROM attachments WHERE id = $1", pending.ID).Scan(&status) + s.Require().NoError(err) + s.Equal(model.AttachmentDeleting, status) +} + +type emptyObjectStore struct{} + +func (emptyObjectStore) List(context.Context, string, string, int) ([]model.StoredObject, string, error) { + return nil, "", nil +} + +func (emptyObjectStore) Delete(context.Context, string) error { + return errors.New("unexpected object deletion") +} + func (s *PostgresSuite) TestMessageRejectsAttachmentFromAnotherChat() { owner, firstChatID := createOwnerAndChat(s.T(), s.store) secondChat, err := s.store.CreateChat(context.Background(), "Second", []string{owner.ID}, owner.ID)