|
| 1 | +import "reflect-metadata"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { SendEmailRequestDataSchema } from "../src/helpers.js"; |
| 4 | +import { SmtpSendResultSchema } from "../src/data_types/smtpSendResult.js"; |
| 5 | +import { SmtpAttachmentSchema } from "../src/data_types/smtpAttachment.js"; |
| 6 | +import { CreateAttachmentFunction } from "../src/functions/utils/createAttachmentFunction.js"; |
| 7 | + |
| 8 | +describe("SendEmailRequestDataSchema", () => { |
| 9 | + it("requires To, Subject and Text", () => { |
| 10 | + expect(() => SendEmailRequestDataSchema.parse({ Subject: "hi", Text: "body" })).toThrow(); |
| 11 | + expect(() => SendEmailRequestDataSchema.parse({ To: "a@example.com", Text: "body" })).toThrow(); |
| 12 | + expect(() => SendEmailRequestDataSchema.parse({ To: "a@example.com", Subject: "hi" })).toThrow(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("accepts optional Html, From, Cc, Bcc, ReplyTo and Attachments", () => { |
| 16 | + const parsed = SendEmailRequestDataSchema.parse({ |
| 17 | + To: "a@example.com, b@example.com", |
| 18 | + Subject: "hi", |
| 19 | + Text: "body", |
| 20 | + Html: "<p>body</p>", |
| 21 | + From: "no-reply@example.com", |
| 22 | + Cc: "c@example.com", |
| 23 | + Bcc: "d@example.com", |
| 24 | + ReplyTo: "reply@example.com", |
| 25 | + Attachments: [{ filename: "note.txt", content: "hello" }], |
| 26 | + }); |
| 27 | + expect(parsed.To).toEqual("a@example.com, b@example.com"); |
| 28 | + expect(parsed.Attachments).toHaveLength(1); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("SmtpSendResultSchema", () => { |
| 33 | + it("parses a representative send result", () => { |
| 34 | + const result = { |
| 35 | + messageId: "<example-message-id@example.com>", |
| 36 | + accepted: ["a@example.com"], |
| 37 | + rejected: [], |
| 38 | + response: "250 2.0.0 OK", |
| 39 | + envelope: { from: "no-reply@example.com", to: ["a@example.com"] }, |
| 40 | + }; |
| 41 | + const parsed = SmtpSendResultSchema.parse(result); |
| 42 | + expect(parsed.messageId).toEqual(result.messageId); |
| 43 | + expect(parsed.accepted).toEqual(["a@example.com"]); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("CreateAttachmentFunction", () => { |
| 48 | + it("builds a valid SMTP_ATTACHMENT object", () => { |
| 49 | + const attachment = new CreateAttachmentFunction().run( |
| 50 | + undefined, |
| 51 | + "invoice.pdf", |
| 52 | + "JVBERi0=", |
| 53 | + "application/pdf", |
| 54 | + "base64" |
| 55 | + ); |
| 56 | + expect(() => SmtpAttachmentSchema.parse(attachment)).not.toThrow(); |
| 57 | + expect(attachment.filename).toEqual("invoice.pdf"); |
| 58 | + expect(attachment.encoding).toEqual("base64"); |
| 59 | + }); |
| 60 | +}); |
0 commit comments