refactor(mail): one send path for Sent, a fixed lock order, and concurrency tests

- the webmail's Send no longer files its own Sent copy: it asks Submit to
  (FileSentCopy), the same path SMTP clients use, so the rule exists once
- every operation now locks the folders first (lowest id first) and the message
  after. Opposite moves, and a move racing a flag change, deadlocked (SQLSTATE
  40P01) because they took those locks in different orders; the new test
  reproduces it (10-19 s of retries, errors) and passes in ~1 s with the order
- TestOneMailboxEverywhere drives SMTP, IMAP and the webmail on one mailbox,
  mixing them, and checks after each step that every view agrees with the others
  and with the rows. It fails on the previous code and passes now.
This commit is contained in:
Lucas Rayan committed 2026-10-16 13:30:59 -03:00
1 parent 67bbe7a50a
commit 183191d18d
6 files changed
+612 -48

No files matched your search

@@ -137,3 +137,51 @@ func TestSubmit_WorksWithoutASentCopyRepository(t *testing.T) {
t.Fatalf("Submit without a Sent repository: %v", err)
}
}
// One rule for every path: a message sent from the webmail and the same message
// sent by an SMTP client leave the same thing behind in Sent. They used to differ
// completely - the webmail filed a copy, SMTP filed nothing - because the rule
// lived in the webmail alone.
func TestSentCopyIsTheSameWhicheverPathSent(t *testing.T) {
mailboxID := uuid.New()
messages := &recordingMessageRepository{}
submission := NewProcessOutboundEmailUseCase(nil, &countingOutboundRepo{}, &capturingBlobStorage{}, nil)
submission.SetSentCopies(messages)
account := &port.MailboxAuth{ID: mailboxID, EmailAddress: "me@example.test", DomainName: "example.test", MaxRecipientsPerHour: 100}
web := &WebmailUseCase{
repo: &stubWebmailRepo{mailboxID: mailboxID.String()},
auth: &stubAuthRepo{account: account},
submission: submission,
localHost: "mail.example.test",
}
if err := web.Send(context.Background(), ComposeInput{From: "me@example.test", To: []string{"you@remote.test"}, Subject: "Aviso", Text: "hi"}); err != nil {
t.Fatalf("webmail send: %v", err)
}
smtp := ProcessOutboundEmailInput{
MailboxID: mailboxID, SenderAddr: "me@example.test", DomainName: "example.test",
Recipients: []string{"you@remote.test"}, FileSentCopy: true,
Body: bytes.NewBufferString("From: me@example.test\r\nTo: you@remote.test\r\nSubject: Aviso\r\n\r\nhi"),
}
if err := submission.Submit(context.Background(), smtp); err != nil {
t.Fatalf("smtp submit: %v", err)
}
if len(messages.persisted) != 2 {
t.Fatalf("want one copy per path (2), got %d: the webmail must not file its own copy as well", len(messages.persisted))
}
fromWeb, fromSMTP := messages.persisted[0], messages.persisted[1]
for _, got := range []port.PersistInboundMessageInput{fromWeb, fromSMTP} {
if got.TargetFolderName != "Sent" || !got.AlreadySeen || got.MailboxID != mailboxID {
t.Errorf("a Sent copy is not what the rule says: %+v", got)
}
if got.SenderAddress != "me@example.test" || got.RecipientAddress != "me@example.test" {
t.Errorf("a Sent copy belongs to its sender: %+v", got)
}
if got.Subject != "Aviso" {
t.Errorf("subject = %q, want Aviso", got.Subject)
}
}
}