#3: close 3 known F1 gaps - folder/mailbox counters, quota-at-RCPT, alias fan-out

Per feature/3-f0-f1-validation's own gap analysis (commit 097fa37):

- InboundMessageRepository.Persist now updates folders.unread_count,
  folders.total_count, and mailboxes.used_bytes in the same transaction
  as the message insert (PLAN.md section 6.1 "++counters" was a no-op
  before this).
- MailboxRecord carries QuotaBytes/UsedBytes; ResolveRecipient rejects
  RCPT with 452 4.2.2 when a target mailbox has already reached its
  quota (used_bytes >= quota_bytes), before DATA - never silently
  accepting mail into a full mailbox.
- MailboxRepository.ResolveDeliveryTargets tries a direct mailbox match
  first, then falls back to the aliases table (exact source_address
  match takes precedence over a domain catch-all). One RCPT TO can now
  fan out to multiple mailboxes; alias destinations that don't resolve
  to an internal active mailbox are silently skipped, since outbound/
  external forwarding doesn't exist yet. postmaster@/abuse@/dmarc@-style
  aliases now actually deliver.

The E2E test's counters_updated subtest flips from a documented KNOWN
GAP (t.Skipf) to a hard assertion now that it passes, and gains two new
subtests (quota_rejected_when_mailbox_full, alias_resolves_to_destination_mailbox)
covering the other two fixes end-to-end. New unit and Postgres
integration test coverage added for all three fixes at every layer.

Verified: full suite green on Windows (unit + E2E via embedded
Postgres, no Docker needed) and via WSL Docker against the real
docker-compose Postgres (all infrastructure/postgres and diskstorage
integration tests, not just E2E). lang-lint clean.
This commit is contained in:
Lucas Rayan committed 2026-07-23 11:36:03 -03:00
1 parent 097fa373e1
commit 2f7389841d
10 files changed
+472 -31

No files matched your search

@@ -175,9 +175,7 @@ func TestInboundSMTPEndToEnd(t *testing.T) {
t.Errorf("max uid = %d, want 2 (sequential per folder)", maxUID)
}
// --- 8. PLAN gaps this test documents deliberately --------------------
// The following FAIL today and are expected to: they mark the F1 scope
// that is not implemented yet. Remove t.Skip as each lands.
// --- 8. Folder/mailbox counters kept in sync with persisted messages --
t.Run("counters_updated", func(t *testing.T) {
var unread, total int
var used int64
@@ -190,7 +188,75 @@ func TestInboundSMTPEndToEnd(t *testing.T) {
t.Fatalf("counters query: %v", err)
}
if unread != 2 || total != 2 || used != sizeBytes*2 {
t.Skipf("KNOWN GAP (PLAN 6.1 '++counters'): unread=%d total=%d used=%d — repository does not update folder counters or mailboxes.used_bytes yet", unread, total, used)
t.Errorf("unread=%d total=%d used=%d, want unread=2 total=2 used=%d (PLAN section 6.1 '++counters')", unread, total, used, sizeBytes*2)
}
})
// --- 9. A full mailbox rejects RCPT with 452 4.2.2, in-session --------
t.Run("quota_rejected_when_mailbox_full", func(t *testing.T) {
domainID := "00000000-0000-0000-0000-000000000001"
fullMailboxID := "10000000-0000-0000-0000-000000000099"
fullAddress := "full@example.test"
if _, err := pool.Exec(ctx, `
INSERT INTO mailboxes (id, domain_id, local_part, email_address, password_hash, quota_bytes, used_bytes)
VALUES ($1, $2, 'full', $3, '$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG', 1000, 1000)
`, fullMailboxID, domainID, fullAddress); err != nil {
t.Fatalf("seed full mailbox: %v", err)
}
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("dial: %v", err)
}
defer c.Close()
if err := c.Mail("sender@remote.example"); err != nil {
t.Fatalf("MAIL FROM: %v", err)
}
err = c.Rcpt(fullAddress)
if err == nil {
t.Fatal("RCPT to a full mailbox succeeded; want 452 4.2.2")
}
if !strings.Contains(err.Error(), "452") {
t.Errorf("full-mailbox RCPT error = %q, want SMTP 452", err)
}
})
// --- 10. An alias fans out to its destination mailbox -----------------
t.Run("alias_resolves_to_destination_mailbox", func(t *testing.T) {
domainID := "00000000-0000-0000-0000-000000000001"
aliasDestMailboxID := "10000000-0000-0000-0000-000000000098"
aliasDestAddress := "alias-dest@example.test"
aliasAddress := "hello@example.test"
if _, err := pool.Exec(ctx, `
INSERT INTO mailboxes (id, domain_id, local_part, email_address, password_hash)
VALUES ($1, $2, 'alias-dest', $3, '$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG')
`, aliasDestMailboxID, domainID, aliasDestAddress); err != nil {
t.Fatalf("seed alias destination mailbox: %v", err)
}
if _, err := pool.Exec(ctx, `
INSERT INTO folders (id, mailbox_id, name, special_use) VALUES ($1, $2, 'INBOX', 'inbox')
`, "10000000-0000-0000-0000-000000000097", aliasDestMailboxID); err != nil {
t.Fatalf("seed alias destination inbox folder: %v", err)
}
if _, err := pool.Exec(ctx, `
INSERT INTO aliases (id, domain_id, source_address, destination_addresses)
VALUES ($1, $2, $3, $4)
`, "10000000-0000-0000-0000-000000000096", domainID, aliasAddress, []string{aliasDestAddress}); err != nil {
t.Fatalf("seed alias: %v", err)
}
if err := smtp.SendMail(addr, nil, "sender@remote.example",
[]string{aliasAddress}, []byte(testMessage)); err != nil {
t.Fatalf("SendMail to alias: %v", err)
}
var count int
if err := pool.QueryRow(ctx,
`SELECT count(*) FROM email_messages WHERE mailbox_id = $1`, aliasDestMailboxID).Scan(&count); err != nil {
t.Fatalf("query alias destination messages: %v", err)
}
if count != 1 {
t.Errorf("messages persisted for alias destination mailbox = %d, want 1", count)
}
})