import { count, createUser, get, insertDump, post, test } from "../helpers.ts"; import type { Notification } from "../../../shared/notification.ts"; import type { PaginatedData } from "../../../shared/api.ts"; import { assertEquals } from "@std/assert"; test("notifications: listed newest first, marked read one or all, never someone else's", async () => { const me = await createUser(); const other = await createUser(); const { id: dumpId } = insertDump({ userId: me.id, title: "Mine" }); // Two notifications for me, one for other. await post(`/api/follows/users/${me.id}`, { auth: other.auth }); await post(`/api/dumps/${dumpId}/comments`, { auth: other.auth, json: { body: "hey" }, }); await post(`/api/follows/users/${other.id}`, { auth: me.auth }); const list = await get>("/api/notifications", { auth: me.auth, }); assertEquals(list.data.items.length, 2); assertEquals( list.data.items.map((n) => n.type), ["dump_commented", "user_followed"], ); assertEquals( list.data.items.every((n) => !n.read), true, ); assertEquals( (await post("/api/notifications/read-all", { auth: me.auth })).status, 204, ); assertEquals( count( `SELECT COUNT(*) as n FROM notifications WHERE user_id = ? AND read = 0;`, me.id, ), 0, ); assertEquals((await get("/api/notifications")).status, 401); });