import { count, createUser, get, insertDump, patch, 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, ); const theirs = (await get>("/api/notifications", { auth: other.auth, })).data .items[0].id; assertEquals( (await patch(`/api/notifications/${theirs}/read`, { auth: me.auth })) .status, 204, ); assertEquals( (await get>("/api/notifications", { auth: other.auth, })).data.items[0] .read, false, ); const first = list.data.items[0].id; await patch(`/api/notifications/${first}/read`, { auth: me.auth }); const after = (await get>("/api/notifications", { auth: me.auth, })).data.items; assertEquals(after.map((n) => n.read), [true, false]); 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); });