84 lines · 3.2 KB
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildCommandConfig } from "../src/core/config.ts";
import { handleSlashCommand } from "../src/adapters/slack/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 { slackPayload, testDb } from "./helpers.ts";

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, command: string, text: string = "") =>
  handleSlashCommand(slackPayload(userId, command, text), db, createFn, config);

let seq = 0;
const freshUser = () => `U${100000 + seq++}`;

// The address a reply embeds, same "address: <addr>" shape Telegram's plain
// text replies use, since both hand back a bare string rather than a
// wrapped object.
const replyAddress = (reply: string): string => (reply.match(/address: (\S+)/) ?? [])[1] ?? "";

test("strips the /cm- prefix so the same command names as Discord/Telegram apply", async () => {
  const { db } = testDb();
  const reply = await run(db, freshUser(), "/cm-new");
  assert.match(reply, /Your new disposable address/);
});

test("an unprefixed or unknown command is rejected, not silently handled", async () => {
  const { db } = testDb();
  const reply = await run(db, freshUser(), "/cm-bogus");
  assert.equal(reply, "Unknown command.");
});

test("/cm-new then /cm-list shows the created address", async () => {
  const { db } = testDb();
  const user = freshUser();
  const created = await run(db, user, "/cm-new", "7 test note");