chore: update SMTP module and tests; remove unused assets

This commit is contained in:
Esdras Renan 2025-10-07 16:15:46 -03:00
parent 81fd572e48
commit 037970d52b
4 changed files with 38 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
// Mock tls to simulate an SMTP server over implicit TLS
let lastWrites: string[] = []
vi.mock("tls", () => {
class MockSocket {
listeners: Record<string, Function[]> = {}
@ -58,6 +59,7 @@ vi.mock("tls", () => {
function connect(_port: number, _host: string, _opts: unknown, cb?: Function) {
const socket = new MockSocket()
lastWrites = socket.writes
// initial server greeting
setTimeout(() => {
cb?.()
@ -66,7 +68,7 @@ vi.mock("tls", () => {
return socket as unknown as NodeJS.WritableStream & { on: MockSocket["on"] }
}
return { default: { connect }, connect }
return { default: { connect }, connect, __getLastWrites: () => lastWrites }
})
describe("sendSmtpMail", () => {
@ -87,5 +89,23 @@ describe("sendSmtpMail", () => {
)
).resolves.toBeUndefined()
})
})
it("extracts envelope address from parentheses or raw email", async () => {
const { sendSmtpMail } = await import("@/server/email-smtp")
const tlsMock = await import("tls" as any)
await sendSmtpMail(
{
host: "smtp.mock",
port: 465,
username: "user@example.com",
password: "secret",
from: "Chatwoot chat@esdrasrenan.com.br (chat@esdrasrenan.com.br)",
},
"rcpt@example.com",
"Subject",
"<p>Hi</p>"
)
const writes = (tlsMock as any).__getLastWrites() as string[]
expect(writes.some((w) => /MAIL FROM:<chat@esdrasrenan.com.br>\r\n/.test(w))).toBe(true)
})
})