Files
email_agent/internal/mailops/smtp_test.go
Paperclip Builder 7ca888f0c6 Phase 5: reply drafts — template/LLM draft, edit, SMTP send (AUD-71)
Implements Phase 5 of AGENTS.md §11 (FR-8, FR-15):

- Message detail pane (GET /partials/message) with body, recommended
  action, reply identity, template picker, editable draft, and history.
- internal/reply: OmniRoute (OpenAI-compatible) chat client and a
  template-based draft generator. All non-JEV models go through
  OmniRoute (§10).
- internal/mailops: SMTP send over smtp.gmail.com:587 with STARTTLS and
  the app password, forcing IPv4 (§9); FR-15 reply identity (send as the
  arrived-at X-Pm-Original-To address, else "Sam Rolfe"); threading via
  In-Reply-To/References; -check-smtp safe verification; and a
  [Gmail]/Sent Mail thread check.
- Migration v5 stores messages.message_id_header and drafts.sent_at;
  mailfetch parses Message-ID.
- Send is user-triggered only: CSRF + UI confirm + confirm=yes guard,
  no auto-send, duplicate-send refused. A sent reply files the original
  under Replied-to/<address> and logs every step (FR-13).
- Tests for reply, mailops SMTP/identity/thread check, store drafts and
  migration v5, and the server draft/edit/send guards.
- docs/phase5-replies.md, README, .env.example, docker-compose updated.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-10-11 22:05:35 +11:00

141 lines
4.4 KiB
Go

package mailops
import (
"context"
"net"
"strings"
"testing"
"time"
"gitea.lab.audasmedia.com.au/sam/email_agent/internal/config"
"gitea.lab.audasmedia.com.au/sam/email_agent/internal/store"
)
func TestReplyIdentityUsesArrivedAtAddress(t *testing.T) {
msg := store.Message{SourceAddress: "Hello@where-woof.com"}
if got := ReplyIdentity(msg, "samuelrolfe@gmail.com"); got != `"Sam Rolfe" <Hello@where-woof.com>` {
t.Errorf("ReplyIdentity() = %q", got)
}
fallback := ReplyIdentity(store.Message{}, "samuelrolfe@gmail.com")
if fallback != `"Sam Rolfe" <samuelrolfe@gmail.com>` {
t.Errorf("fallback = %q", fallback)
}
if got := ReplyIdentity(store.Message{}, ""); got != "Sam Rolfe" {
t.Errorf("bare identity = %q", got)
}
}
func TestReplyToParsesSender(t *testing.T) {
if got := ReplyTo(store.Message{FromAddr: "Customer Name <cust@example.test>"}); got != "cust@example.test" {
t.Errorf("ReplyTo() = %q", got)
}
if got := ReplyTo(store.Message{FromAddr: "plain@example.test"}); got != "plain@example.test" {
t.Errorf("ReplyTo() = %q", got)
}
if got := ReplyTo(store.Message{}); got != "" {
t.Errorf("ReplyTo() empty = %q", got)
}
}
func TestReplySubjectPrefixesOnce(t *testing.T) {
if got := ReplySubject("Booking"); got != "Re: Booking" {
t.Errorf("ReplySubject() = %q", got)
}
if got := ReplySubject("RE: Booking"); got != "RE: Booking" {
t.Errorf("ReplySubject() should not double-prefix: %q", got)
}
}
func TestBuildRFC822ThreadsReply(t *testing.T) {
raw := string(BuildRFC822(OutgoingMessage{
To: "cust@example.test",
From: `"Sam Rolfe" <hello@where-woof.com>`,
Subject: "Re: Booking",
InReplyTo: "<orig-123@mail.example>",
References: "orig-123@mail.example",
Body: "Saturday works.\n\nSam",
}, "samuelrolfe@gmail.com", time.Unix(0, 0).UTC()))
for _, want := range []string{
"From: \"Sam Rolfe\" <hello@where-woof.com>\r\n",
"To: cust@example.test\r\n",
"Subject: Re: Booking\r\n",
"In-Reply-To: <orig-123@mail.example>\r\n",
"References: <orig-123@mail.example>\r\n",
"Content-Type: text/plain; charset=UTF-8\r\n",
"\r\nSaturday works.\r\n\r\nSam\r\n",
} {
if !strings.Contains(raw, want) {
t.Errorf("message missing %q\n%s", want, raw)
}
}
if strings.Contains(strings.ReplaceAll(raw, "\r\n", ""), "\n") {
t.Errorf("message contains a bare LF; SMTP needs CRLF line endings")
}
if !strings.Contains(raw, "Message-ID: <") {
t.Errorf("message has no generated Message-ID:\n%s", raw)
}
}
func TestSendWithoutCredentials(t *testing.T) {
c := New(&config.Config{Env: "test"}, nil)
out := OutgoingMessage{To: "a@b.test", From: "Sam Rolfe <x@y.test>", Body: "hi"}
if err := c.Send(context.Background(), out); err != ErrNotConfigured {
t.Fatalf("Send() error = %v, want ErrNotConfigured", err)
}
}
func TestDialSMTP4ConnectsOverIPv4(t *testing.T) {
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
// "localhost" may resolve to ::1 first; the tcp4 network must still reach the
// IPv4 listener.
conn, err := dialSMTP4(context.Background(), "localhost", port, 2*time.Second)
if err != nil {
t.Fatalf("dialSMTP4() error = %v", err)
}
_ = conn.Close()
}
func TestVerifyThreadUpdatedSearchesSentMailbox(t *testing.T) {
fc := &fakeConn{uidValidity: 3, searchUIDs: []uint32{11}}
c := newFakeClient(fc)
ok, err := c.VerifyThreadUpdated(context.Background(), "987654321")
if err != nil {
t.Fatalf("VerifyThreadUpdated() error = %v", err)
}
if !ok {
t.Error("VerifyThreadUpdated() = false, want true when the thread has a sent message")
}
if fc.selected != LabelSent {
t.Errorf("selected = %q, want %q", fc.selected, LabelSent)
}
if len(fc.searched) != 1 || fc.searched[0] != "thrid:987654321" {
t.Errorf("searched = %v, want [thrid:987654321]", fc.searched)
}
}
func TestVerifyThreadUpdatedEmptyThread(t *testing.T) {
c := newFakeClient(&fakeConn{searchUIDs: nil})
if _, err := c.VerifyThreadUpdated(context.Background(), ""); err == nil {
t.Fatal("VerifyThreadUpdated() error = nil, want an error for a missing thread id")
}
}
func TestVerifyThreadUpdatedNoSentMatch(t *testing.T) {
c := newFakeClient(&fakeConn{searchUIDs: nil})
ok, err := c.VerifyThreadUpdated(context.Background(), "42")
if err != nil {
t.Fatalf("VerifyThreadUpdated() error = %v", err)
}
if ok {
t.Error("VerifyThreadUpdated() = true, want false when no sent message matches")
}
}