The mail screens had no backend at all: they rendered a hardcoded array, and the storage service that ADR-001 assigned this to is still a health stub. The API lands here instead, because the folder, UID, MODSEQ, flag and blob logic already exists and is tested in this service for IMAP. Reimplementing it in TypeScript would mean two implementations of the same semantics and two chances to disagree about what a UID means. Reads use a projected read model rather than hydrated aggregates, which is the CQRS exception PLAN.md section 2 carves out for webmail queries. Every query is scoped to the mailbox resolved from the verified session, never to an identifier taken from the request, so one account cannot read another's mail by guessing a UID - covered by a test that tries exactly that and expects 404. Sending reuses ProcessOutboundEmailUseCase, so mail written in the browser gets the same treatment as mail submitted over 587: send limits, DKIM signing, and the durable queue. The From address comes from the session and never from the request body; letting the body choose it is how a webmail becomes a relay for spoofed mail. MIME is parsed server-side because this service already depends on a MIME library. A message that will not parse still returns its raw text - an unreadable message beats an inbox that refuses to open one. Marking a message read updates the flag and the folder counter in one transaction, and only on a real transition, so re-opening a message cannot drive unread_count negative. Verified against the running stack: a message delivered over port 25 appears in the list with its decoded subject and sender, opens, marks itself read, is found by search, and a message composed in the webmail reaches outbound_jobs with the session's address as the envelope sender. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package usecase
|
|
|
|
import "testing"
|
|
|
|
func TestRenderMessage_ExtractsHeadersAndText(t *testing.T) {
|
|
raw := "From: Alice <alice@remote.test>\r\n" +
|
|
"To: bob@example.test\r\n" +
|
|
"Subject: =?UTF-8?Q?Ol=C3=A1?=\r\n" +
|
|
"Date: Mon, 02 Jan 2006 15:04:05 -0700\r\n" +
|
|
"Content-Type: text/plain; charset=utf-8\r\n\r\nCorpo.\r\n"
|
|
|
|
got := RenderMessage([]byte(raw), 7)
|
|
if got.UID != 7 || got.Subject != "Olá" {
|
|
t.Errorf("UID/Subject = %d/%q", got.UID, got.Subject)
|
|
}
|
|
if got.Text != "Corpo.\r\n" {
|
|
t.Errorf("Text = %q", got.Text)
|
|
}
|
|
if got.Date == "" {
|
|
t.Error("Date was not parsed")
|
|
}
|
|
}
|
|
|
|
// A message with no Date header parses into the zero time without an error;
|
|
// rendering that verbatim showed the year 0001 in the reader.
|
|
func TestRenderMessage_LeavesDateEmptyWhenHeaderAbsent(t *testing.T) {
|
|
got := RenderMessage([]byte("From: a@b.test\r\nSubject: s\r\n\r\nbody\r\n"), 1)
|
|
if got.Date != "" {
|
|
t.Errorf("Date = %q, want empty so the UI falls back to received_at", got.Date)
|
|
}
|
|
}
|
|
|
|
func TestRenderMessage_PrefersPlainTextAndListsAttachments(t *testing.T) {
|
|
raw := "From: a@b.test\r\nSubject: s\r\n" +
|
|
"Content-Type: multipart/mixed; boundary=B\r\n\r\n" +
|
|
"--B\r\nContent-Type: text/plain\r\n\r\nplain part\r\n" +
|
|
"--B\r\nContent-Type: text/html\r\n\r\n<p>html part</p>\r\n" +
|
|
"--B\r\nContent-Type: application/pdf\r\n" +
|
|
"Content-Disposition: attachment; filename=\"report.pdf\"\r\n\r\nPDFDATA\r\n--B--\r\n"
|
|
|
|
got := RenderMessage([]byte(raw), 1)
|
|
if got.Text != "plain part" {
|
|
t.Errorf("Text = %q", got.Text)
|
|
}
|
|
if got.HTML != "<p>html part</p>" {
|
|
t.Errorf("HTML = %q", got.HTML)
|
|
}
|
|
if len(got.Attachments) != 1 || got.Attachments[0] != "report.pdf" {
|
|
t.Errorf("Attachments = %v", got.Attachments)
|
|
}
|
|
}
|
|
|
|
// Garbage must still open: an unreadable message beats an inbox that errors.
|
|
func TestRenderMessage_FallsBackToRawOnParseFailure(t *testing.T) {
|
|
got := RenderMessage([]byte("this is not a message"), 3)
|
|
if got.Text == "" {
|
|
t.Error("a malformed message produced no body at all")
|
|
}
|
|
}
|