import { afterEach, beforeEach } from "@std/testing/bdd"; import { ALL_SETTINGS_KEYS, settings } from "#db/settings.ts"; import { loadOrderAnswerLines } from "#shared/email/answers.ts"; import { buildTemplateData, renderEmailContent, type TemplateData, } from "#shared/email-renderer.ts"; import { resetLiquidEngines } from "#shared/liquid-engine.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { makeTestEntry as makeEntry } from "#test-utils/factories.ts"; import { useSetting } from "#test-utils/settings.ts"; export const TICKET_URL = "https://example.com/t/ABC"; export const describeEmailRenderer = (tests: () => void): void => { describeWithEnv("email-renderer", { db: true }, () => { useSetting({ currency: "GBP" }); beforeEach(resetLiquidEngines); afterEach(resetLiquidEngines); tests(); }); }; /** Template data as the registration email builds it: the answer lines load * from the database unless the test supplies its own. */ export const buildTestData = async ( entries: ReturnType[], options: Partial[3]> = {}, ): Promise => buildTemplateData(entries, "GBP", TICKET_URL, { answerLines: await loadOrderAnswerLines(entries), ...options, }); export const renderConfirmation = async (): Promise<{ data: TemplateData; result: Awaited>; }> => { settings.invalidateCache(); await settings.loadKeys(ALL_SETTINGS_KEYS); const data = await buildTestData([makeEntry()]); const result = await renderEmailContent("confirmation", data); return { data, result }; }; export const sampleData: TemplateData = { amount_owed: "0", attendee: { address: "123 St", answers: [], date: null, date_range_label: "", email: "jane@test.com", name: "Jane", phone: "555", price_paid: "2000", quantity: 2, special_instructions: "", }, currency: "GBP", entries: [ { attendee: { address: "123 St", answers: [], date: null, date_range_label: "", email: "jane@test.com", name: "Jane", phone: "555", price_paid: "2000", quantity: 2, special_instructions: "", }, listing: { is_paid: true, name: "Concert", slug: "concert" }, }, ], listing_names: "Concert", ticket_url: TICKET_URL, };