fix: harden attachment cleanup

This commit is contained in:
2026-10-09 13:27:58 +03:00
parent f8c31de7c0
commit 3fde1c324c
8 changed files with 134 additions and 24 deletions
+34 -1
View File
@@ -154,11 +154,44 @@ func (s *PostgresSuite) TestAttachmentAccessAndMessageLinking() {
referenced, err := s.store.AttachmentReferenced(context.Background(), created.ID)
s.Require().NoError(err)
s.True(referenced)
deleted, err := s.store.ClaimAttachmentForDeletion(context.Background(), created.ID, time.Now().Add(time.Hour))
deleted, err := s.store.ClaimAttachmentForDeletion(context.Background(), created, time.Now().Add(time.Hour))
s.Require().NoError(err)
s.False(deleted)
}
func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
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: "pending.png",
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("c", 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)
deleted, err := s.store.DeleteAttachmentIfDeleting(context.Background(), pending.ID)
s.Require().NoError(err)
s.True(deleted)
pending, err = s.store.CreatePendingAttachment(context.Background(), model.Attachment{
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "completed.png",
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("d", 64),
ObjectKey: "attachments/" + uuid.NewString() + "/original",
})
s.Require().NoError(err)
ready, err := s.store.MarkAttachmentReady(context.Background(), pending.ID, pending.ObjectKey)
s.Require().NoError(err)
claimed, err = s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour))
s.Require().NoError(err)
s.False(claimed)
visible, err := s.store.AttachmentForUser(context.Background(), ready.ID, owner.ID)
s.Require().NoError(err)
s.Equal(model.AttachmentReady, visible.Status)
}
func (s *PostgresSuite) TestMessageRejectsAttachmentFromAnotherChat() {
owner, firstChatID := createOwnerAndChat(s.T(), s.store)
secondChat, err := s.store.CreateChat(context.Background(), "Second", []string{owner.ID}, owner.ID)