141 lines · 5.9 KB
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildCommandConfig } from "../src/core/config.ts";
import { handleUpdate } from "../src/adapters/telegram/commands.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 { telegramOwner, telegramReplyAddress, telegramReplyText, telegramUpdate, 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, text: string) =>
  handleUpdate(telegramUpdate(userId, text), db, createFn, config);

let seq = 0;
const freshUser = () => `${100000 + 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(telegramReplyText(reply), /Permanent/);
    assert.equal((await getAddress(db, telegramReplyAddress(reply)))?.permanent, 1);
  });

  await t.test("a leading integer is the expiry", async () => {
    const { db } = testDb();
    const reply = await run(db, freshUser(), "/new 3");
    assert.match(telegramReplyText(reply), /Expires in 3 days/);
    const row = await getAddress(db, telegramReplyAddress(reply));
    assert.equal(row?.permanent, 0);
  });

  await t.test("expiry 0 is also permanent", async () => {
    const { db } = testDb();
    const reply = await run(db, freshUser(), "/new 0");
    assert.equal((await getAddress(db, telegramReplyAddress(reply)))?.permanent, 1);