239 lines · 10.6 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, getAddress } from "../src/core/db.ts";
import type { SqlExecutor } from "../src/core/storage.ts";
import type { OwnerRef } from "../src/core/types.ts";
import { command, owner, replyAddress, replyText, testDb } from "./helpers.ts";

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

const run = (db: SqlExecutor, userId: string, name: string, opts: { name: string; value: string | number }[] = []) =>
  handleInteraction(command(userId, name, opts), db, createFn, config);

// /new is rate limited to one call per 30s per owner, so anything creating
// more than one address has to use a fresh owner id each time.
let seq = 0;
const freshUser = () => `u${seq++}`;

test("/new expiry", async (t) => {
  await t.test("is permanent when expiry is omitted", async () => {
    const { db } = testDb();
    const reply = await run(db, freshUser(), "new");
    assert.match(replyText(reply), /Permanent/);
    assert.equal((await getAddress(db, replyAddress(reply)))?.permanent, 1);
  });

  await t.test("expiry: 0 is also permanent", async () => {
    const { db } = testDb();
    const reply = await run(db, freshUser(), "new", [{ name: "expiry", value: 0 }]);
    assert.equal((await getAddress(db, replyAddress(reply)))?.permanent, 1);
  });

  await t.test("expiry in days sets a real expiry", async () => {
    const { db } = testDb();
    const reply = await run(db, freshUser(), "new", [{ name: "expiry", value: 3 }]);
    assert.match(replyText(reply), /Expires in 3 days/);