Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions apps/web/__tests__/unit/delete-comment-notifications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Comment, Video } from "@cap/web-domain";
import { beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
query: vi.fn(),
getCurrentUser: vi.fn(),
revalidatePath: vi.fn(),
}));

vi.mock("@cap/database", async () => {
const { drizzle } = await import("drizzle-orm/mysql-proxy");
const client = drizzle(mocks.query);
return {
db: () => ({
transaction: <T>(callback: (tx: typeof client) => Promise<T>) =>
callback(client),
}),
};
});
vi.mock("@cap/database/auth/session", () => ({
getCurrentUser: mocks.getCurrentUser,
}));
vi.mock("@cap/web-backend", () => ({ Storage: {}, VideosRepo: {} }));
vi.mock("@/lib/server", () => ({}));
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));

import { deleteComment } from "@/actions/videos/delete-comment";

const commentId = Comment.CommentId.make("reaction");
const videoId = Video.VideoId.make("video");

beforeEach(() => {
mocks.getCurrentUser.mockResolvedValue({ id: "viewer" });
mocks.query.mockReset();
mocks.query.mockResolvedValue({ rows: [{ affectedRows: 1, insertId: 0 }] });
mocks.query.mockResolvedValueOnce({
rows: [[commentId, videoId, null, null]],
});
});

describe("deleted comment notifications", () => {
it.each([null, Comment.CommentId.make("")])(
"removes the reaction notification for a root with parent %j",
async (parentId) => {
await expect(
deleteComment({ commentId, videoId, parentId }),
).resolves.toEqual({ success: true });

expect(mocks.query).toHaveBeenCalledWith(
expect.stringContaining("delete from `comments`"),
[commentId, "viewer"],
"execute",
);
expect(mocks.query).toHaveBeenCalledWith(
expect.stringContaining(
"where (`notifications`.`type` in (?, ?) and JSON_EXTRACT(`notifications`.`data`, '$.comment.id') = ?)",
),
["comment", "reaction", commentId],
"execute",
);
expect(mocks.revalidatePath).toHaveBeenCalledWith(`/s/${videoId}`);
},
);

it("removes reply notifications for replies to a deleted root", async () => {
await deleteComment({ commentId, videoId, parentId: null });

expect(mocks.query).toHaveBeenCalledWith(
expect.stringContaining(
"where (`notifications`.`type` = ? and JSON_EXTRACT(`notifications`.`data`, '$.parentCommentId') = ?)",
),
["reply", commentId],
"execute",
);
});

it("still removes only the reply notification when deleting a reply", async () => {
await deleteComment({
commentId,
videoId,
parentId: Comment.CommentId.make("parent"),
});

const deletes = mocks.query.mock.calls.filter(([query]) =>
query.startsWith("delete from `notifications`"),
);
expect(deletes).toEqual([
[
expect.stringContaining("'$.comment.id'"),
["reply", commentId],
"execute",
],
]);
});

it("does not delete notifications for a missing or unowned comment", async () => {
mocks.query.mockReset();
mocks.query.mockResolvedValue({ rows: [] });
vi.spyOn(console, "error").mockImplementation(() => {});

await expect(
deleteComment({ commentId, videoId, parentId: null }),
).rejects.toThrow("Comment not found or you don't have permission");

expect(mocks.query).toHaveBeenCalledExactlyOnceWith(
expect.stringContaining("`comments`.`authorId` = ?"),
[commentId, "viewer", 1],
"all",
);
expect(mocks.revalidatePath).not.toHaveBeenCalled();
});
});
7 changes: 7 additions & 0 deletions apps/web/__tests__/unit/reply-notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ describe("reply notifications", () => {
expect(inserts[0]?.[1]).toEqual(
expect.arrayContaining(["comment-author", "reply-org", "reply"]),
);
const data = inserts[0]?.[1].find(
(param: unknown) => typeof param === "string" && param.startsWith("{"),
);
expect(JSON.parse(data)).toMatchObject({
comment: { id: "reply" },
parentCommentId: "parent",
});
expect(mocks.sendEmail).not.toHaveBeenCalled();
},
);
Expand Down
6 changes: 3 additions & 3 deletions apps/web/actions/videos/delete-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getCurrentUser } from "@cap/database/auth/session";
import { comments, notifications } from "@cap/database/schema";
import { Storage, VideosRepo } from "@cap/web-backend";
import type { Comment, Video } from "@cap/web-domain";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, inArray, sql } from "drizzle-orm";
import { Effect, Option } from "effect";
import { revalidatePath } from "next/cache";
import * as EffectRuntime from "@/lib/server";
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function deleteComment({
.delete(notifications)
.where(
and(
eq(notifications.type, "comment"),
inArray(notifications.type, ["comment", "reaction"]),
sql`JSON_EXTRACT(${notifications.data}, '$.comment.id') = ${commentId}`,
),
);
Expand All @@ -94,7 +94,7 @@ export async function deleteComment({
.where(
and(
eq(notifications.type, "reply"),
sql`JSON_EXTRACT(${notifications.data}, '$.comment.parentCommentId') = ${commentId}`,
sql`JSON_EXTRACT(${notifications.data}, '$.parentCommentId') = ${commentId}`,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/mobile/[...route]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3778,7 +3778,7 @@ const ApiLive = HttpApiBuilder.api(Mobile.MobileApiContract).pipe(
.where(
and(
eq(Db.notifications.type, "reply"),
sql`JSON_EXTRACT(${Db.notifications.data}, '$.comment.parentCommentId') = ${path.id}`,
sql`JSON_EXTRACT(${Db.notifications.data}, '$.parentCommentId') = ${path.id}`,
),
);
}
Expand Down
Loading