import { createUser, db, get, insertDump, test } from "../helpers.ts"; import { assertEquals } from "@std/assert"; import type { CommunityStats } from "../../../shared/community.ts"; import { refreshCommunityStats } from "../../services/community-service.ts"; function vote(dumpId: string, n: number) { db.prepare(`UPDATE dumps SET vote_count = ? WHERE id = ?;`).run(n, dumpId); } test("GET /api/community: signed in only, for a week or a month", async () => { const ann = await createUser(); vote( insertDump({ userId: ann.id, title: "Hit", createdAt: new Date(Date.now() - 1000).toISOString(), }).id, 1, ); refreshCommunityStats(); assertEquals((await get("/api/community")).status, 401); assertEquals( (await get("/api/community?period=daily", { auth: ann.auth })).status, 400, ); const week = await get("/api/community", { auth: ann.auth }); assertEquals(week.status, 200); assertEquals(week.data.top.map((d) => d.title), ["Hit"]); assertEquals(week.data.members.map((m) => m.username), [ann.username]); const month = await get("/api/community?period=monthly", { auth: ann.auth, }); assertEquals(month.data.totals.dumps, 1); });