Files
heidi/lib/domain/operator/operator.test.ts

88 lines
4.7 KiB
TypeScript

import { after, before, describe, test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { PGlite } from "@electric-sql/pglite";
import type { Run } from "../../db/run.ts";
import { prepared } from "../admin/actions.ts";
import { operatorWords } from "../../i18n/dictionaries/operator.ts";
import { replyDraft } from "./drafts.ts";
import { heidiProviders } from "./provide.ts";
import { weekTrends } from "./trends.ts";
import { recentOrgRequests, recordReply } from "../orgs/request.ts";
test("the reply is in the language the request came in, names them, and promises no price", () => {
const base = { name: "Anna Muster", organisation: "Haus Seeblick", people: "40", email: "anna@example.org" };
const de = replyDraft({ ...base, locale: "de" }, "Cato");
const en = replyDraft({ ...base, locale: "en" }, "Cato");
assert.match(de.body, /^Guten Tag Anna Muster/);
assert.match(en.body, /^Dear Anna Muster/);
for (const d of [de, en]) {
assert.equal(d.to, "anna@example.org");
assert.match(d.subject!, /Haus Seeblick/);
assert.match(d.body, /40/);
assert.match(d.body, /Cato$/);
assert.doesNotMatch(d.body, /CHF|Fr\.|€|\bfree\b|kostenlos|gratis/i, "a price or a promise nobody made");
}
});
test("both catalogues have the same shape", () => {
const shape = (o: unknown): unknown => (o && typeof o === "object" ? Object.fromEntries(Object.entries(o).map(([k, v]) => [k, shape(v)])) : typeof o);
assert.deepEqual(shape(operatorWords("en")), shape(operatorWords("de")));
});
describe("what Heidi prepares", () => {
let db: PGlite;
const run: Run = async (text, params) => (await db.query<Record<string, unknown>>(text, [...params])).rows;
const DIR = new URL("../../../drizzle/", import.meta.url);
const w = operatorWords("en");
const providers = () => heidiProviders({ run, words: w, from: "Cato", invitation: "Please check a few lines: https://example.org", mailWorks: true });
before(async () => {
db = new PGlite();
for (const file of readdirSync(DIR).filter((f) => /^\d{4}_.*\.sql$/.test(f)).sort()) {
for (const statement of readFileSync(new URL(file, DIR), "utf8").split("--> statement-breakpoint")) {
if (statement.trim()) await db.exec(statement);
}
}
await run(`insert into users (id, name, email, email_verified, created_at, updated_at, role) values ('boss','Boss','boss@example.org',true,now(),now(),'admin')`, []);
await run(`insert into org_requests (sector, locale, name, organisation, email, people) values ('care','de','Anna Muster','Haus Seeblick','anna@example.org','40')`, []);
});
after(async () => db.close());
test("an open request leads, with its reply written; every action has something to press", async () => {
const actions = await prepared(providers());
assert.equal(actions[0]!.area, "customers");
assert.match(actions[0]!.title, /Haus Seeblick/);
assert.match(actions[0]!.draft!.body, /Guten Tag Anna Muster/);
assert.match(actions[0]!.act.href, /^inbox#r-/);
for (const a of actions) assert.ok(a.act.label && a.act.href, a.id);
// A fresh product: nobody has checked the lines, the test never ran, one admin only.
assert.deepEqual(actions.map((a) => a.id.replace(/:[0-9a-f-]{36}$/, "")).sort(), ["people:alone", "quality:no-readers", "quality:no-test", "request"]);
assert.match(actions.find((a) => a.id === "quality:no-readers")!.draft!.body, /example\.org/);
});
test("answering closes it: the action is gone and the reply is kept", async () => {
const [request] = await recentOrgRequests(run);
await recordReply(run, request!.id, "Guten Tag", new Date());
assert.ok(!(await prepared(providers())).some((a) => a.area === "customers"));
const [after] = await recentOrgRequests(run);
assert.equal(after!.replyText, "Guten Tag");
assert.ok(after!.handledAt);
});
test("mail that does not work is said, urgently", async () => {
const actions = await prepared(heidiProviders({ run, words: w, from: "Cato", invitation: null, mailWorks: false }));
assert.equal(actions[0]!.id, "health:mail");
});
test("the four numbers compare the last seven days with the seven before", async () => {
const now = new Date();
await run(`insert into users (id, name, email, email_verified, created_at, updated_at) values ('old','o','o@example.org',true, now() - interval '10 days', now())`, []);
const trends = await weekTrends(run, now);
assert.deepEqual(trends.map((t) => t.key), ["visitors", "chatted", "accounts", "requests"]);
const accounts = trends.find((t) => t.key === "accounts")!;
assert.deepEqual([accounts.now, accounts.before], [1, 1]);
assert.equal(trends.find((t) => t.key === "requests")!.now, 1);
});
});