184 lines · 7.6 KB
import { Database } from "bun:sqlite";
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { readFileSync } from "node:fs";

mock.module("cloudflare:workers", () => ({ env: { PUBLIC_URL: "https://forge.example.com" } }));

const sent: Array<{ to: string; subject: string; url: string }> = [];
mock.module("./email", () => ({
  emailTemplates: {
    waitlistInvite: (url: string, _holdFor: string) => ({
      subject: "You're off the waitlist",
      text: url,
      html: url,
      url,
    }),
  },
  queueEmail: async (to: string, content: { subject: string; url: string }) => {
    sent.push({ to, subject: content.subject, url: content.url });
  },
}));
mock.module("./events/emit", () => ({ publicUrl: (path: string) => `https://forge.example.com${path}` }));

let db = new Database(":memory:");
type Statement = { sql: string; params: unknown[] };
const exec = ({ sql, params }: Statement) => {
  const changes = db.query(sql).run(...(params as never[])).changes;
  return { meta: { changes } };
};
mock.module("./db", () => ({
  stmt: (sql: string, ...params: unknown[]): Statement => ({ sql, params }),
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async (sql: string, ...params: unknown[]) => exec({ sql, params }),
  batch: async (statements: Statement[]) => db.transaction(() => statements.map(exec))(),
}));

const waitlist = await import("./waitlist");
const {
  advanceWaitlist,
  approveWaitlistEntry,