Skip to content
Myr

mailer.test.ts

This file

Path

apps/platform/test/mailer.test.ts

90 lines · 2.5 KiB · TypeScript

Last changed by

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

History of this file

Take it

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 1–90why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Brand } from "@repo/brand";3import { Effect } from "effect";45import { Email } from "../src/auth/accounts.ts";6import { MailFailed, Mailer } from "../src/auth/mailer.ts";7import { MAIL_FROM, MailerBinding } from "../src/auth/mailer/binding.ts";8import { MailerMemory } from "../src/auth/mailer/memory.ts";910const mail = {11 subject: "123456 is your code",12 text: "Your code is 123456.",13 to: Email.make("alice@mail.example"),14};1516const sendWith = (binding: SendEmail) =>17 Mailer.use((mailer) => mailer.send(mail)).pipe(18 Effect.provide(MailerBinding(binding))19 );2021describe("the mailer over Email Service's binding", () => {22 it.effect(23 "sends plain text from the brand's no-reply address, to the one address",24 () =>25 Effect.gen(function* () {26 const sent: unknown[] = [];2728 const binding: SendEmail = {29 send: (message: EmailMessage | EmailMessageBuilder) => {30 sent.push(message);3132 return Promise.resolve({ messageId: "message-1" });33 },34 };3536 yield* sendWith(binding);3738 expect(MAIL_FROM).toStrictEqual({39 email: `no-reply@${Brand.domain}`,40 name: Brand.name,41 });42 expect(sent).toStrictEqual([43 {44 from: MAIL_FROM,45 subject: mail.subject,46 text: mail.text,47 to: mail.to,48 },49 ]);50 })51 );5253 it.effect("fails as MailFailed when the binding refuses", () =>54 Effect.gen(function* () {55 const binding: SendEmail = {56 send: () => Promise.reject(new Error("E_SENDER_NOT_VERIFIED")),57 };5859 expect(yield* Effect.flip(sendWith(binding))).toStrictEqual(60 new MailFailed()61 );62 })63 );64});6566describe("the mailer in memory", () => {67 it.effect(68 "keeps each mail in the order sent, or fails each when told to",69 () =>70 Effect.gen(function* () {71 const kept = yield* MailerMemory.make();7273 const failing = yield* MailerMemory.make({ failing: true });7475 yield* Mailer.use((mailer) => mailer.send(mail)).pipe(76 Effect.provide(kept.layer)77 );7879 expect(yield* kept.sent).toStrictEqual([mail]);80 expect(81 yield* Effect.flip(82 Mailer.use((mailer) => mailer.send(mail)).pipe(83 Effect.provide(failing.layer)84 )85 )86 ).toStrictEqual(new MailFailed());87 expect(yield* failing.sent).toStrictEqual([]);88 })89 );90});