Files
gerbeur/api/test/comments.test.ts
khannurien de5abeceb2
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 49s
v3: a performance pass over both halves — indexed queries, streamed files, cached previews, and feeds that stop re-rendering on every tick — a review turned up whole-file reads, unindexed scans and render storms; each fix comes with a test. Database: migration 0016 adds the indexes the hot queries lacked — dumps by created_at, (user_id, created_at) and slug, votes by (user_id, created_at), playlists by slug, users by lower(username) for @mentions, and the child side of every cascading foreign key (comment replies, a user's comments, follows both ways, chat messages, invites, reset tokens); the feeds, dump and playlist pages, upvotes and follower fan-out all plan as index searches now, with no temp-table sort. SQLite runs in WAL with synchronous=NORMAL and a busy timeout, and a transaction() helper wraps the multi-row writes (a dump and its categories, category sets, backlinks, attachment links, playlist reorders, follower notifications) so they commit once instead of once a row. Lists no longer run a COUNT(*) per page: they fetch limit + 1 rows and hasMore comes from the extra one; total stays only on search, the one place it's shown. Files: /api/files read the whole upload (up to 50 MB) for every range request a player makes while seeking; api/lib/serve-file.ts streams the requested bytes from disk, with an ETag and 304s, and serves avatars, attachments, covers and thumbnails too. File and thumbnail URLs carry the file's version (size and last change), so the current one is cached for a year and anything else revalidates — a same-size replacement used to stay stale, and a video's still was cached immutable forever under a URL that never changed. Feeds showed image dumps at full size: /api/thumbnails now makes a 960px preview with ffmpeg (GIFs and unreadable images go out as they are), one run per output however many cards ask at once, and ffmpeg is probed once per process instead of spawned per request. Uploads stream to disk; replacing a file writes beside it and renames over, a hard link keeping the old one for rollback instead of holding both in memory; generated stills and previews go when the file is replaced or the dump deleted, and a custom thumbnail with its dump. Outbound: fetchWithTimeout's deadline now covers the body — a server that answered at once and then dripped could hold a create open indefinitely — and pages are read to 4 MB at most; metadata is cached five minutes so the create form's preview and the post that follows share one fetch (refresh-metadata still goes to the page); proxied images sit in a 32 MB LRU. Broadcasts serialize once rather than per socket, and index.html is kept in memory until the file changes. Frontend: currentTime lived in PlayerContext, so every card re-rendered — and re-parsed its Markdown — four times a second while anything played; the playhead has its own PlayerTimeContext now. The socket context is split three ways (feed, chat via useWSChat, presence via useWSPresence), so typing, presence and chat messages leave the feeds alone. DumpCard, JournalCard and Markdown are memoized; card images use the server preview and load lazily; the feed cache is written when the page is left rather than after every pause in scrolling; the theme context value is memoized.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 16:35:51 +00:00

269 lines
7.5 KiB
TypeScript

import {
count,
createUser,
db,
del,
get,
insertComment,
insertDump,
patch,
post,
test,
} from "./helpers.ts";
import { assertEquals, assertExists, assertFalse } from "@std/assert";
import {
castCommentLike,
removeCommentLike,
} from "../services/comment-like-service.ts";
import {
APIException,
type Comment,
type Dump,
type PaginatedData,
type PublicUser,
} from "../model/interfaces.ts";
test("comments: post, list in order, count on the dump", async () => {
const owner = await createUser();
const other = await createUser();
const { id } = insertDump({ userId: owner.id });
const first = await post<Comment>(`/api/dumps/${id}/comments`, {
auth: other.auth,
json: { body: " first " },
});
assertEquals(first.status, 201);
assertEquals(first.data.body, "first");
assertEquals(first.data.authorUsername, other.username);
const reply = await post<Comment>(`/api/dumps/${id}/comments`, {
auth: owner.auth,
json: { body: "reply", parentId: first.data.id },
});
assertEquals(reply.data.parentId, first.data.id);
const list = await get<Comment[]>(`/api/dumps/${id}/comments`);
assertEquals(list.data.map((c) => c.body), [
"first",
"reply",
]);
assertEquals((await get<Dump>(`/api/dumps/${id}`)).data.commentCount, 2);
});
test("comments: validation, auth and unknown dumps", async () => {
const user = await createUser();
const { id } = insertDump({ userId: user.id });
for (const body of ["", " ", "x".repeat(5001)]) {
assertEquals(
(await post(`/api/dumps/${id}/comments`, {
auth: user.auth,
json: { body },
}))
.status,
400,
);
}
assertEquals(
(await post(`/api/dumps/${id}/comments`, { json: { body: "hi" } })).status,
401,
);
assertEquals(
(await post(`/api/dumps/nope/comments`, {
auth: user.auth,
json: { body: "hi" },
}))
.status,
404,
);
});
test("comments on a private dump: owner only", async () => {
const owner = await createUser();
const other = await createUser();
const { id } = insertDump({ userId: owner.id, isPrivate: true });
assertEquals(
(await post(`/api/dumps/${id}/comments`, {
auth: other.auth,
json: { body: "x" },
}))
.status,
404,
);
assertEquals(
(await post(`/api/dumps/${id}/comments`, {
auth: owner.auth,
json: { body: "x" },
}))
.status,
201,
);
assertEquals(
(await get(`/api/dumps/${id}/comments`, { auth: other.auth })).status,
404,
);
});
test("comments: the dump's owner is notified, the commenter isn't", async () => {
const owner = await createUser();
const other = await createUser();
const { id } = insertDump({ userId: owner.id, title: "Mine" });
await post(`/api/dumps/${id}/comments`, {
auth: owner.auth,
json: { body: "self" },
});
assertEquals(count(`SELECT COUNT(*) as n FROM notifications;`), 0);
const c = await post<Comment>(`/api/dumps/${id}/comments`, {
auth: other.auth,
json: { body: "nice" },
});
const note = db.prepare(`SELECT user_id, type, data FROM notifications;`)
.get() as {
user_id: string;
type: string;
data: string;
};
assertEquals(note.user_id, owner.id);
assertEquals(note.type, "dump_commented");
assertEquals(JSON.parse(note.data).commentId, c.data.id);
assertEquals(JSON.parse(note.data).dumpTitle, "Mine");
});
test("PATCH /api/comments/:id: author and moderator edit, others can't", async () => {
const author = await createUser();
const other = await createUser();
const mod = await createUser({ role: "moderator" });
const { id: dumpId } = insertDump({ userId: author.id });
const commentId = insertComment({ dumpId, userId: author.id, body: "orig" });
assertEquals(
(await patch(`/api/comments/${commentId}`, {
auth: other.auth,
json: { body: "x" },
}))
.status,
401,
);
const edited = await patch<Comment>(`/api/comments/${commentId}`, {
auth: author.auth,
json: { body: " edited " },
});
assertEquals(edited.status, 200);
assertEquals(edited.data.body, "edited");
assertExists(edited.data.updatedAt);
assertEquals(
(await patch(`/api/comments/${commentId}`, {
auth: mod.auth,
json: { body: "m" },
}))
.status,
200,
);
assertEquals(
(await patch(`/api/comments/${commentId}`, {
auth: author.auth,
json: { body: "" },
}))
.status,
400,
);
assertEquals(
(await patch(`/api/comments/${crypto.randomUUID()}`, {
auth: author.auth,
json: { body: "x" },
})).status,
404,
);
});
test("DELETE /api/comments/:id soft-deletes, and a deleted comment can't be edited", async () => {
const author = await createUser();
const other = await createUser();
const { id: dumpId } = insertDump({ userId: author.id });
const commentId = insertComment({
dumpId,
userId: author.id,
body: "gone soon",
});
assertEquals(
(await del(`/api/comments/${commentId}`, { auth: other.auth })).status,
401,
);
assertEquals(
(await del(`/api/comments/${commentId}`, { auth: author.auth })).status,
200,
);
const [c] = (await get<Comment[]>(`/api/dumps/${dumpId}/comments`)).data;
assertEquals(c.deleted, true);
assertEquals(c.body, "");
assertEquals((await get<Dump>(`/api/dumps/${dumpId}`)).data.commentCount, 0);
assertEquals(
(await patch(`/api/comments/${commentId}`, {
auth: author.auth,
json: { body: "back" },
}))
.status,
400,
);
});
test("comment likes: count, conflicts, likers list and notifications", async () => {
const author = await createUser();
const liker = await createUser();
const { id: dumpId } = insertDump({ userId: author.id });
const commentId = insertComment({ dumpId, userId: author.id });
assertEquals(castCommentLike(commentId, liker.id).likeCount, 1);
assertEquals(castCommentLike(commentId, author.id).likeCount, 2);
try {
castCommentLike(commentId, liker.id);
throw new Error("expected a conflict");
} catch (err) {
assertEquals((err as APIException).status, 409);
}
// Only the other user's like notifies the author.
assertEquals(
count(
`SELECT COUNT(*) as n FROM notifications WHERE type = 'comment_liked';`,
),
1,
);
const likers = await get<PaginatedData<PublicUser>>(
`/api/comments/${commentId}/likers`,
);
assertEquals(likers.data.items.length, 2);
assertFalse("email" in likers.data.items[0]);
assertEquals(removeCommentLike(commentId, liker.id).likeCount, 1);
try {
removeCommentLike(commentId, liker.id);
throw new Error("expected a 404");
} catch (err) {
assertEquals((err as APIException).status, 404);
}
assertEquals(
(db.prepare(`SELECT like_count FROM comments WHERE id = ?;`).get(
commentId,
) as {
like_count: number;
}).like_count,
1,
);
});
test("comment likes on a private dump: owner only", async () => {
const owner = await createUser();
const other = await createUser();
const { id: dumpId } = insertDump({ userId: owner.id, isPrivate: true });
const commentId = insertComment({ dumpId, userId: owner.id });
try {
castCommentLike(commentId, other.id);
throw new Error("expected a 404");
} catch (err) {
assertEquals((err as APIException).status, 404);
}
assertEquals(castCommentLike(commentId, owner.id).isPrivate, true);
assertEquals((await get(`/api/comments/${commentId}/likers`)).status, 404);
assertEquals(
(await get(`/api/comments/${commentId}/likers`, { auth: owner.auth }))
.status,
200,
);
});