262 lines · 10.2 KB
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildCommandConfig } from "../src/core/config.ts";
import { handleInteraction } from "../src/adapters/discord/interactions.ts";
import { createAddress, revokeAddress, setExpiryReminderPreference } from "../src/core/db.ts";
import { createDispatcher } from "../src/core/dispatch.ts";
import { sendExpiryWarnings } from "../src/core/expiry-warning.ts";
import type { SqlExecutor } from "../src/core/storage.ts";
import type { MailAdapter, OwnerRef } from "../src/core/types.ts";
import { command, owner, replyText, testDb } from "./helpers.ts";

const HOUR = 3600;
const config = buildCommandConfig({});
const createFn = (db: SqlExecutor, o: OwnerRef, ttl: number, permanent: boolean, note: string | null) =>
  createAddress(db, o, "ex.com", ttl, permanent, note);

// Records what was sent rather than asserting on it inline, so tests can
// check both how many DMs went out and what was in them.
function recordingAdapter(fail = false) {
  const sent: { ownerId: string; message: string }[] = [];
  const adapter: MailAdapter = {
    name: "discord",
    deliver: async () => ({ success: true }),
    notify: async (o, message) => {
      sent.push({ ownerId: o.id, message });
      return fail ? { success: false, error: "DMs closed" } : { success: true };
    },
  };
  return { dispatcher: createDispatcher([adapter]), sent };
}

async function optedInDb() {
  const { db } = testDb();
  await setExpiryReminderPreference(db, owner("u1"), true);
  return db;
}

test("expiry reminder window", async (t) => {
  await t.test("warns an address expiring inside the window", async () => {
    const db = await optedInDb();