61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { createUser, db, get, insertDump, test } from "../helpers.ts";
|
|
import { assertEquals } from "@std/assert";
|
|
import type {
|
|
CommunityFeed,
|
|
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<CommunityStats>("/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<CommunityStats>("/api/community?period=monthly", {
|
|
auth: ann.auth,
|
|
});
|
|
assertEquals(month.data.totals.dumps, 1);
|
|
});
|
|
|
|
test("GET /api/community/feed: signed in only, the latest events and the week's figures", async () => {
|
|
const ann = await createUser();
|
|
insertDump({
|
|
userId: ann.id,
|
|
title: "Hit",
|
|
createdAt: new Date(Date.now() - 1000).toISOString(),
|
|
});
|
|
refreshCommunityStats();
|
|
|
|
assertEquals((await get("/api/community/feed")).status, 401);
|
|
const feed = await get<CommunityFeed>("/api/community/feed", {
|
|
auth: ann.auth,
|
|
});
|
|
assertEquals(feed.status, 200);
|
|
assertEquals(feed.data.events.map((e) => e.type), [
|
|
"user_joined",
|
|
"dump_posted",
|
|
]);
|
|
assertEquals(feed.data.week.totals.dumps, 1);
|
|
assertEquals(feed.data.week.top, undefined);
|
|
});
|