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>
This commit is contained in:
146
internal/store/drafts_test.go
Normal file
146
internal/store/drafts_test.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDraftsRoundTrip(t *testing.T) {
|
||||
s, ctx := testStore(t)
|
||||
id := seedMessage(t, s, ctx, "1", "question", "hello@where-woof.com")
|
||||
|
||||
d, err := s.SaveDraft(ctx, id, "Thanks, got it.", "quick-ack")
|
||||
if err != nil {
|
||||
t.Fatalf("SaveDraft() error = %v", err)
|
||||
}
|
||||
if d.ID == 0 || d.Status != DraftStatusDraft || d.TemplateID != "quick-ack" {
|
||||
t.Fatalf("SaveDraft() = %+v", d)
|
||||
}
|
||||
if d.MessageID != id {
|
||||
t.Errorf("MessageID = %d, want %d", d.MessageID, id)
|
||||
}
|
||||
|
||||
if err := s.UpdateDraftBody(ctx, d.ID, "Edited body."); err != nil {
|
||||
t.Fatalf("UpdateDraftBody() error = %v", err)
|
||||
}
|
||||
latest, ok, err := s.LatestDraft(ctx, id)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("LatestDraft() ok=%v err=%v", ok, err)
|
||||
}
|
||||
if latest.Body != "Edited body." || latest.Status != DraftStatusDraft {
|
||||
t.Errorf("latest = %+v", latest)
|
||||
}
|
||||
|
||||
if err := s.MarkDraftSent(ctx, latest.ID); err != nil {
|
||||
t.Fatalf("MarkDraftSent() error = %v", err)
|
||||
}
|
||||
sent, _, err := s.LatestDraft(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatalf("LatestDraft() after send error = %v", err)
|
||||
}
|
||||
if sent.Status != DraftStatusSent || sent.SentAt.IsZero() {
|
||||
t.Errorf("sent draft = %+v, want status=sent and a sent_at", sent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLatestDraftMissing(t *testing.T) {
|
||||
s, ctx := testStore(t)
|
||||
id := seedMessage(t, s, ctx, "1", "x", "a@b.test")
|
||||
if _, ok, err := s.LatestDraft(ctx, id); err != nil || ok {
|
||||
t.Fatalf("LatestDraft() ok=%v err=%v, want ok=false", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageByIDIncludesReplyFields(t *testing.T) {
|
||||
s, ctx := testStore(t)
|
||||
id, err := s.UpsertMessage(ctx, &Message{
|
||||
GmailID: "99",
|
||||
FromAddr: "Customer <cust@example.test>",
|
||||
Subject: "Booking",
|
||||
BodyText: "Saturday?",
|
||||
SourceAddress: "hello@where-woof.com",
|
||||
MessageIDHeader: "<orig-1@mail.example>",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertMessage() error = %v", err)
|
||||
}
|
||||
msg, err := s.MessageByID(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatalf("MessageByID() error = %v", err)
|
||||
}
|
||||
if msg.MessageIDHeader != "<orig-1@mail.example>" {
|
||||
t.Errorf("MessageIDHeader = %q", msg.MessageIDHeader)
|
||||
}
|
||||
if msg.BodyText != "Saturday?" || msg.SourceAddress != "hello@where-woof.com" {
|
||||
t.Errorf("message = %+v", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationV5AddsReplyColumns(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "old.db")
|
||||
|
||||
// Simulate a v4 database: messages has no message_id_header and drafts has
|
||||
// no sent_at. Opening it must apply migration v5.
|
||||
db, err := sql.Open("sqlite", "file:"+path)
|
||||
if err != nil {
|
||||
t.Fatalf("open old db: %v", err)
|
||||
}
|
||||
stmts := []string{
|
||||
`CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
gmail_id TEXT NOT NULL UNIQUE,
|
||||
thread_id TEXT, from_addr TEXT, to_addr TEXT, subject TEXT,
|
||||
body_text TEXT, source_address TEXT, date_sent TEXT,
|
||||
imap_uid INTEGER, imap_uidvalidity INTEGER,
|
||||
folder TEXT NOT NULL DEFAULT 'inbox',
|
||||
mailbox TEXT NOT NULL DEFAULT 'INBOX',
|
||||
received_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')))`,
|
||||
`CREATE TABLE drafts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
template_id TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'draft',
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')))`,
|
||||
`CREATE TABLE schema_migrations (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (datetime('now')))`,
|
||||
`INSERT INTO schema_migrations (version) VALUES (4)`,
|
||||
}
|
||||
for _, q := range stmts {
|
||||
if _, err := db.ExecContext(ctx, q); err != nil {
|
||||
t.Fatalf("seed old db: %v", err)
|
||||
}
|
||||
}
|
||||
_ = db.Close()
|
||||
|
||||
s, err := Open(ctx, path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open() on v4 database error = %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
v, err := s.SchemaVersion(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("SchemaVersion() error = %v", err)
|
||||
}
|
||||
if v < 5 {
|
||||
t.Errorf("SchemaVersion() = %d, want >= 5", v)
|
||||
}
|
||||
for _, tc := range []struct{ table, col string }{
|
||||
{"messages", "message_id_header"},
|
||||
{"drafts", "sent_at"},
|
||||
} {
|
||||
var n int
|
||||
q := `SELECT COUNT(*) FROM pragma_table_info('` + tc.table + `') WHERE name = ?`
|
||||
if err := s.DB().QueryRowContext(ctx, q, tc.col).Scan(&n); err != nil {
|
||||
t.Fatalf("count %s.%s: %v", tc.table, tc.col, err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Errorf("%s.%s missing after migration v5", tc.table, tc.col)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user