package memoria import ( "context" "errors" "log/slog" "net" "net/http" "os" "path/filepath" "strings" "testing" "amadan.net/rastrillo/rastrillo/mail" ) // The boots CARLOS platform mail submission needs (carlos plan Task 9): // on submission with the agent's socket, on SMTP alone, and with // neither, which refuses. // mailEnv is every variable the boot's mail choice reads, so each case // starts from none of them whatever the test process inherited. var mailEnv = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST", "CARLOS_SMTP_PORT", "CARLOS_SMTP_FROM", "CARLOS_SMTP_USER", "CARLOS_SMTP_PASS"} // boot is main's mail path: LoadConfig, then Mailer. LoadConfig reads // the map; mail.FromCarlos reads the process environment, so the map // is set there too. func boot(t *testing.T, env map[string]string) (mail.Sender, error) { t.Helper() for _, key := range mailEnv { t.Setenv(key, "") } for key, value := range env { t.Setenv(key, value) } config, err := LoadConfig(func(key string) string { return env[key] }) if err != nil { return nil, err } return Mailer(config, slog.New(slog.DiscardHandler)) } // public is a deployed instance: no logged mail, a public origin. func public(env map[string]string) map[string]string { full := baseEnv() delete(full, "MEMORIA_ALLOW_LOGGED_MAIL") full["MEMORIA_ORIGIN"] = "https://memoria.oficina.works" for key, value := range env { full[key] = value } return full } // mailVerb is the agent's control socket, answering POST /v1/mail. func mailVerb(t *testing.T) (string, chan string) { t.Helper() // A short directory: a unix socket path has a 104-byte limit. dir, err := os.MkdirTemp("/tmp", "memoria-mail") if err != nil { t.Fatal(err) } t.Cleanup(func() { os.RemoveAll(dir) }) socket := filepath.Join(dir, "control.sock") listener, err := net.Listen("unix", socket) if err != nil { t.Fatal(err) } paths := make(chan string, 1) server := &http.Server{Handler: http.HandlerFunc( func(writer http.ResponseWriter, request *http.Request) { paths <- request.Method + " " + request.URL.Path writer.Write([]byte(`{"message_id":"m-1"}`)) })} go server.Serve(listener) t.Cleanup(func() { server.Close() }) return socket, paths } func TestBootsOnMailSubmission(t *testing.T) { socket, paths := mailVerb(t) sender, err := boot(t, public(map[string]string{ "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", "CARLOS_MAIL_FROM": "noreply@memoria.oficina.works"})) if err != nil { t.Fatal(err) } err = sender.Send(context.Background(), "ana@example.com", "Sign in", "link") if err != nil { t.Fatal(err) } if got := <-paths; got != "POST /v1/mail" { t.Fatalf("the sign-in went to %s, not the mail verb", got) } } func TestBootsOnSMTPAlone(t *testing.T) { sender, err := boot(t, public(map[string]string{ "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", "CARLOS_SMTP_FROM": "noreply@memoria.oficina.works"})) if err != nil { t.Fatal(err) } if !mail.IsConfigured(sender) { t.Fatal("SMTP is configured but the sender only logs") } } func TestRefusesWithNeitherChannel(t *testing.T) { _, err := boot(t, public(nil)) if !errors.Is(err, ErrMissing) { t.Fatalf("booted with no mail: %v", err) } if !strings.Contains(err.Error(), "CARLOS_MAIL=submit") { t.Errorf("refusal %q does not name submission", err) } } // A submission missing anything the agent or platform delivers is a // boot refusal, never SMTP or logged mail in its place: not even with // MEMORIA_ALLOW_LOGGED_MAIL set. func TestRefusesHalfASubmission(t *testing.T) { socket, _ := mailVerb(t) for _, missing := range []string{"CARLOS_CONTROL_SOCKET", "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} { t.Run(missing, func(t *testing.T) { env := public(map[string]string{"CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", "CARLOS_MAIL_FROM": "noreply@memoria.oficina.works", "MEMORIA_ALLOW_LOGGED_MAIL": "1"}) delete(env, missing) _, err := boot(t, env) if err == nil || !strings.Contains(err.Error(), missing) { t.Fatalf("booted on submission with no %s: %v", missing, err) } }) } } // Logged mail stays what it was: only with MEMORIA_ALLOW_LOGGED_MAIL // and no channel at all. func TestLoggedMailOnlyWhenAskedAndNoChannel(t *testing.T) { sender, err := boot(t, baseEnv()) if err != nil { t.Fatal(err) } if mail.IsConfigured(sender) { t.Fatal("logged mail with no channel built a real sender") } }