Files
archie/internal/channels/email/email_test.go
T
sam 6fe1a7d037 fix: lint cleanup across repo (errcheck, staticcheck, ineffassign, unused)
- daemon.go: real bug — maybeRetryParked resolved fg := d.forgeFor(task)
  (matching markDead's pattern) but called d.Forge.SetStateLabel instead
  of fg.SetStateLabel, silently using the wrong forge client for
  multi-identity/multi-forge task routing. Fixed to use fg throughout.
- telegram.go: updateThreadID existed but was never called anywhere;
  wired it into updateLoggingMiddleware so debug logs include thread_id.
- Two SA9003 empty-branch tests (nell adapter_test.go, tools
  toolentry_test.go) were dead assertions with no body; implemented the
  actual check instead of deleting them.
- memory manager_test.go: mockProvider.lastConfigSchema/lastBackupPaths
  were declared but never recorded; wired GetConfigSchema/BackupPaths to
  populate them and strengthened the aggregation tests to assert on
  per-provider content, not just counts.
- Removed genuinely dead gateway messageevent_test.go helpers (intPtr/
  i64Ptr, marked //go:fix inline, already fully superseded by the new()
  builtin used throughout the file).
- Remaining errcheck findings (defer Close/Flush/Write, os.Setenv, etc.
  in test files) fixed with the existing `_ = ` convention already used
  elsewhere in the codebase.

go build ./..., go test ./... (all packages ok), and
golangci-lint run ./... all clean.
2026-07-27 03:49:33 +10:00

153 lines
3.9 KiB
Go

package email
import (
"context"
"fmt"
"log/slog"
"net"
"net/smtp"
"os"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/gateway"
)
func TestName(t *testing.T) {
g := New(":2525", "", slog.Default())
if g.Name() != "email" {
t.Errorf("Name() = %q", g.Name())
}
}
func TestExtractAddr(t *testing.T) {
tests := []struct {
input string
want string
}{
{"MAIL FROM:<user@example.com>", "user@example.com"},
{"RCPT TO:<test@foo.bar>", "test@foo.bar"},
{"MAIL FROM:<user@example.com> SIZE=1234", "user@example.com"},
{"garbage", "garbage"},
}
for _, tt := range tests {
if got := extractAddr(tt.input); got != tt.want {
t.Errorf("extractAddr(%q) = %q, want %q", tt.input, got, tt.want)
}
}
}
func TestExtractBodyPlain(t *testing.T) {
raw := "From: test@example.com\r\nSubject: Hello\r\n\r\nThis is the body text.\r\nMore text."
if got := extractBody(raw); !strings.Contains(got, "This is the body") {
t.Errorf("extractBody = %q", got)
}
}
func TestExtractBodyMultipart(t *testing.T) {
raw := "From: test@example.com\r\nContent-Type: multipart/alternative; boundary=abc123\r\n\r\n--abc123\r\nContent-Type: text/plain\r\n\r\nplain text here\r\n--abc123\r\nContent-Type: text/html\r\n\r\n<html></html>\r\n--abc123--"
if got := extractBody(raw); got != "plain text here" {
t.Errorf("extractBody = %q, want 'plain text here'", got)
}
}
func TestExtractBoundary(t *testing.T) {
if got := extractBoundary(`boundary="abc123"`); got != "abc123" {
t.Errorf("got %q", got)
}
if got := extractBoundary(`boundary=xyz`); got != "xyz" {
t.Errorf("got %q", got)
}
}
func TestSMTPReceiveAndRoute(t *testing.T) {
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
g := New(":0", "", log)
router := gateway.NewRouter(nil, func(ctx context.Context, msg gateway.Message) (string, error) {
if msg.From != "sender@test.com" {
return "", fmt.Errorf("unexpected sender: %s", msg.From)
}
return "got it", nil
}, "email")
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Start the server to get the assigned port, then connect.
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := ln.Addr().String()
_ = ln.Close()
g.ListenAddr = addr
go func() { _ = g.Start(ctx, router) }()
time.Sleep(20 * time.Millisecond)
defer func() { _ = g.Stop(context.Background()) }()
// Connect via SMTP and send a message.
conn, err := net.DialTimeout("tcp", addr, 1*time.Second)
if err != nil {
t.Fatal(err)
}
defer func() { _ = conn.Close() }()
// Read greeting.
buf := make([]byte, 1024)
n, _ := conn.Read(buf)
if !strings.HasPrefix(string(buf[:n]), "220") {
t.Fatalf("unexpected greeting: %q", string(buf[:n]))
}
// Send a minimal SMTP transaction.
send := func(s string) {
_, _ = conn.Write([]byte(s + "\r\n"))
time.Sleep(5 * time.Millisecond)
}
send("EHLO test")
send("MAIL FROM:<sender@test.com>")
send("RCPT TO:<archie@local>")
send("DATA")
send("Hello from email test.")
send(".")
send("QUIT")
// Drain response.
time.Sleep(50 * time.Millisecond)
}
func TestStopUnstarted(t *testing.T) {
g := New(":2525", "", slog.Default())
if err := g.Stop(context.Background()); err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestSendReplyNoRelay(t *testing.T) {
g := New(":2525", "", slog.Default())
// Should not panic when relay is empty.
if err := g.sendReply("to@x.com", "from@x.com", "test"); err == nil {
t.Log("expected error with empty relay")
}
}
func TestTruncateSubject(t *testing.T) {
if got := truncateSubject("hello"); got != "hello" {
t.Errorf("short: %q", got)
}
long := strings.Repeat("x", 100)
if got := truncateSubject(long); len(got) > 60 {
t.Errorf("long: %d chars", len(got))
}
}
// Compile-time guard.
var (
_ gateway.Gateway = (*Gateway)(nil)
_ = smtp.PlainAuth
)