Files
outline/server/routes/api/comments/comments.test.ts
T
Tom MoorandClaude Opus 5 e737968365 fix: Allow inline comments on documents created via the API (#13289)
* fix: Allow inline comments on documents without collaborative state

Documents created through the API have no collaborative state until they
are opened in an editor, which made anchored comments fail. Derive the
state from the document content when it is missing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Read comment marks from content instead of decoding state

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Read node-anchored comment mark from content

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 18:26:01 -04:00

1423 lines
40 KiB
TypeScript

import { Node } from "prosemirror-model";
import { prosemirrorToYDoc } from "y-prosemirror";
import * as Y from "yjs";
import type { ProsemirrorData, ReactionSummary } from "@shared/types";
import { CommentStatusFilter } from "@shared/types";
import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
import documentCollaborativeUpdater from "@server/commands/documentCollaborativeUpdater";
import { schema } from "@server/editor";
import { Comment, Document, Reaction } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import {
buildAdmin,
buildCollection,
buildComment,
buildCommentMark,
buildDocument,
buildEmoji,
buildResolvedComment,
buildTeam,
buildUser,
buildViewer,
} from "@server/test/factories";
import { getTestServer } from "@server/test/support";
const server = getTestServer();
describe("#comments.info", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.info");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should return comment info", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user2.id,
documentId: document.id,
});
const res = await server.post("/api/comments.info", user, {
body: {
id: comment.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(comment.id);
expect(body.data.data).toEqual(comment.data);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toEqual(false);
expect(body.policies[0].abilities.delete).toEqual(false);
});
it("should return comment info for admin", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user2.id,
documentId: document.id,
});
const res = await server.post("/api/comments.info", user, {
body: {
id: comment.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(comment.id);
expect(body.data.data).toEqual(comment.data);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toBeTruthy();
expect(body.policies[0].abilities.delete).toBeTruthy();
});
it("should return anchor text for an anchored comment", async () => {
const anchorText = "anchor text";
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const content = {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "text",
text: anchorText,
marks: [buildCommentMark({ id: comment.id, userId: user.id })],
},
],
},
],
} as ProsemirrorData;
await document.update({ content });
const res = await server.post("/api/comments.info", user, {
body: {
id: comment.id,
includeAnchorText: true,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(comment.id);
expect(body.data.anchorText).toEqual(anchorText);
});
it("should not return anchor text for a non-anchored comment", async () => {
const anchorText = "anchor text";
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const content = {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "text",
text: anchorText,
marks: [buildCommentMark({ userId: user.id })],
},
],
},
],
} as ProsemirrorData;
await document.update({ content });
const res = await server.post("/api/comments.info", user, {
body: {
id: comment.id,
includeAnchorText: true,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(comment.id);
expect(body.data.anchorText).toBeUndefined();
});
});
describe("#comments.list", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.list");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should return all comments for a document", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
createdAt: new Date(Date.now() - 1000),
});
await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
createdAt: new Date(),
});
const res = await server.post("/api/comments.list", user, {
body: {
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(2);
expect(body.data[1].id).toEqual(comment.id);
expect(body.policies.length).toEqual(2);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[1].abilities.read).toBeTruthy();
expect(body.pagination.total).toEqual(2);
});
it("should return anchor texts for comments in a document", async () => {
const anchorText = "anchor text";
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const commentOne = await buildComment({
userId: user.id,
documentId: document.id,
});
const commentTwo = await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
});
const content = {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "text",
text: anchorText,
marks: [buildCommentMark({ id: commentOne.id, userId: user.id })],
},
],
},
],
} as ProsemirrorData;
await document.update({ content });
const res = await server.post("/api/comments.list", user, {
body: {
documentId: document.id,
includeAnchorText: true,
sort: "createdAt",
direction: "ASC",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(2);
expect(body.data[0].id).toEqual(commentOne.id);
expect(body.data[1].id).toEqual(commentTwo.id);
expect(body.data[0].anchorText).toEqual(anchorText);
expect(body.data[1].anchorText).toBeUndefined();
});
it("should return unresolved comments for a collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection.id,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.list", user, {
body: {
collectionId: collection.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(comment.id);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.pagination.total).toEqual(1);
});
it("should return unresolved comments for a parentCommentId", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const childComment = await buildComment({
userId: user.id,
documentId: document.id,
parentCommentId: comment.id,
});
const res = await server.post("/api/comments.list", user, {
body: {
parentCommentId: comment.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(childComment.id);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.pagination.total).toEqual(1);
});
it("should return resolved comments for a statusFilter", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
await buildComment({
userId: user.id,
documentId: document.id,
});
const resolved = await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.list", user, {
body: {
documentId: document.id,
statusFilter: [CommentStatusFilter.Resolved],
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(resolved.id);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.unresolve).toBeTruthy();
expect(body.policies[0].abilities.resolve).toEqual(false);
expect(body.pagination.total).toEqual(1);
});
it("should return all unresolved comments", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection1 = await buildCollection({
userId: user.id,
teamId: team.id,
});
const collection2 = await buildCollection({
userId: user.id,
teamId: team.id,
});
const document1 = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection1.id,
});
const document2 = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection2.id,
});
const comment1 = await buildComment({
userId: user.id,
documentId: document1.id,
});
const comment2 = await buildComment({
userId: user.id,
documentId: document2.id,
});
const res = await server.post("/api/comments.list", user);
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(2);
expect(
[body.data[0].id, body.data[1].id].sort((a, b) => a.localeCompare(b))
).toEqual([comment1.id, comment2.id].sort((a, b) => a.localeCompare(b)));
expect(body.policies.length).toEqual(2);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[1].abilities.read).toBeTruthy();
expect(body.pagination.total).toEqual(2);
});
it("should return reactions for a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const reactions: ReactionSummary[] = [
{ emoji: "😄", userIds: [user.id] },
{ emoji: "🙃", userIds: [user.id] },
];
const comment = await buildComment({
userId: user.id,
documentId: document.id,
reactions,
});
const res = await server.post("/api/comments.list", user, {
body: {
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(comment.id);
expect(body.data[0].reactions).toEqual(reactions);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.pagination.total).toEqual(1);
});
});
describe("#comments.create", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.create");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should create a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: comment.data,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.data).toEqual(comment.data);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toBeTruthy();
expect(body.policies[0].abilities.delete).toBeTruthy();
});
it("should create a comment from markdown text", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const text = "test\n\n- list item 1\n- list item 2";
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.data.content[0].type).toEqual("paragraph");
expect(body.data.data.content[0].content[0].text).toEqual("test");
expect(body.data.data.content[1].type).toEqual("bullet_list");
});
it("should not allow empty comment data", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: null,
},
});
const anotherRes = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [{ type: "paragraph" }],
},
},
});
expect(res.status).toEqual(400);
expect(anotherRes.status).toEqual(400);
});
it("should not allow comments containing only whitespaces", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: " \n\r\n" }],
},
],
},
},
});
expect(res.status).toEqual(400);
});
it("should allow adding images to comments", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "image",
attrs: {
src: "https://example.com/image.png",
alt: "Example image",
},
},
],
},
],
},
},
});
expect(res.status).toEqual(200);
});
it("should allow adding images from internal sources", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "image",
attrs: {
src: "/api/attachments.redirect?id=1401323b-c4e2-40de-b172-e1668ec89111",
alt: null,
},
},
],
},
],
},
},
});
expect(res.status).toEqual(200);
});
it("should not allow invalid comment data", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "nonsense",
},
},
});
expect(res.status).toEqual(400);
});
it("should not allow rich formatting nodes", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "heading",
attrs: { level: 1 },
content: [{ type: "text", text: "Heading" }],
},
],
},
},
});
expect(res.status).toEqual(400);
});
it("should allow list nodes", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "bullet_list",
content: [
{
type: "list_item",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "Item 1" }],
},
],
},
],
},
],
},
},
});
expect(res.status).toEqual(200);
});
it("should allow mention nodes", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const mentionedUser = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
data: {
type: "doc",
content: [
{
type: "paragraph",
content: [
{ type: "text", text: "Hello " },
{
type: "mention",
attrs: {
type: "user",
label: mentionedUser.name,
modelId: mentionedUser.id,
id: mentionedUser.id,
},
},
],
},
],
},
},
});
expect(res.status).toEqual(200);
await expect(res.json()).resolves.toMatchObject({
data: {
data: {
content: [
{
type: "paragraph",
content: expect.arrayContaining([
expect.objectContaining({ type: "mention" }),
]),
},
],
},
},
});
});
describe("anchoring", () => {
const imageAttrs = { src: "https://example.com/a.png" };
const documentContent = {
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "The quick brown fox" }],
},
{
type: "paragraph",
content: [{ type: "image", attrs: imageAttrs }],
},
],
} as ProsemirrorData;
const buildAnchorableDocument = async (overrides: {
userId: string;
teamId: string;
}) => {
const doc = Node.fromJSON(schema, documentContent);
const ydoc = prosemirrorToYDoc(doc, "default");
return buildDocument({
...overrides,
content: documentContent,
state: Buffer.from(Y.encodeStateAsUpdate(ydoc)),
});
};
const imageNodeId = () =>
SharedProsemirrorHelper.getNodeHash(
Node.fromJSON(schema, documentContent).child(1).child(0)
);
it("should create a comment anchored to a node", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment on image",
anchorNodeId: imageNodeId(),
},
});
const body = await res.json();
expect(res.status).toEqual(200);
const updated = await Document.findByPk(document.id, {
userId: user.id,
});
const image = DocumentHelper.toProsemirror(updated!).child(1).child(0);
expect(image.attrs.marks).toEqual([
{
type: "comment",
attrs: {
id: body.data.id,
userId: user.id,
draft: false,
resolved: false,
},
},
]);
});
it("should allow a viewer to create an anchored comment", async () => {
const team = await buildTeam();
const author = await buildUser({ teamId: team.id });
const viewer = await buildViewer({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: author.id,
teamId: team.id,
});
const res = await server.post("/api/comments.create", viewer, {
body: {
documentId: document.id,
text: "viewer comment",
anchorText: "brown fox",
},
});
expect(res.status).toEqual(200);
});
it("should not update the document updatedAt timestamp when anchoring", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: user.id,
teamId: user.teamId,
});
const updatedAt = document.updatedAt;
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorText: "brown fox",
},
});
expect(res.status).toEqual(200);
await document.reload();
expect(document.updatedAt).toEqual(updatedAt);
});
it("should not register an edit when the collaboration server persists after anchoring", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorText: "brown fox",
},
});
expect(res.status).toEqual(200);
// Simulate the collaboration server persisting the session after it has
// merged the server-applied comment mark — no further edits were made,
// so the document must not be marked as updated.
const updated = await Document.findByPk(document.id, {
userId: user.id,
includeState: true,
});
const updatedAt = updated!.updatedAt;
const ydoc = new Y.Doc();
Y.applyUpdate(ydoc, updated!.state!);
await documentCollaborativeUpdater({
documentId: document.id,
ydoc,
sessionCollaboratorIds: [user.id],
isLastConnection: true,
clientVersion: null,
});
await document.reload();
expect(document.updatedAt).toEqual(updatedAt);
});
it("should not allow both anchorText and anchorNodeId", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorText: "brown fox",
anchorNodeId: imageNodeId(),
},
});
expect(res.status).toEqual(400);
});
it("should error when no node matches the anchorNodeId", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildAnchorableDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorNodeId: "unknown-hash",
},
});
// The "was not found" message is transformed to a 404 by the API error
// handler, matching the behavior of a missing anchorText.
expect(res.status).toEqual(404);
});
it("should create state when the document has no collaborative state", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
content: documentContent,
});
expect(document.state).toBeFalsy();
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorText: "brown fox",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
const updated = await Document.findByPk(document.id, {
userId: user.id,
includeState: true,
});
expect(updated!.state).toBeTruthy();
expect(
SharedProsemirrorHelper.getComments(
DocumentHelper.toProsemirror(updated!)
)
).toMatchObject([
{ id: body.data.id, userId: user.id, text: "brown fox" },
]);
});
it("should error when the anchor node is not in a document without state", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/comments.create", user, {
body: {
documentId: document.id,
text: "comment",
anchorNodeId: imageNodeId(),
},
});
expect(res.status).toEqual(404);
});
});
});
describe("#comments.update", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.update");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should update an existing comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.update", user, {
body: {
id: comment.id,
data: comment.data,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.data).toEqual(comment.data);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toBeTruthy();
expect(body.policies[0].abilities.delete).toBeTruthy();
});
it("should not allow rich formatting nodes in update", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.update", user, {
body: {
id: comment.id,
data: {
type: "doc",
content: [
{
type: "heading",
attrs: { level: 1 },
content: [{ type: "text", text: "Heading" }],
},
],
},
},
});
expect(res.status).toEqual(400);
});
});
describe("#comments.resolve", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.resolve");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should allow resolving a comment thread", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.resolve", user, {
body: {
id: comment.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.updatedAt).toEqual(comment.updatedAt.toISOString());
expect(body.data.resolvedAt).toBeTruthy();
expect(body.data.resolvedById).toEqual(user.id);
expect(body.data.resolvedBy.id).toEqual(user.id);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toBeTruthy();
expect(body.policies[0].abilities.delete).toBeTruthy();
expect(body.policies[0].abilities.unresolve).toBeTruthy();
expect(body.policies[0].abilities.resolve).toEqual(false);
});
it("should not allow resolving a child comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const parentComment = await buildComment({
userId: user.id,
documentId: document.id,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
parentCommentId: parentComment.id,
});
const res = await server.post("/api/comments.resolve", user, {
body: {
id: comment.id,
},
});
expect(res.status).toEqual(403);
});
});
describe("#comments.unresolve", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.unresolve");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should allow unresolving a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildResolvedComment(user, {
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.unresolve", user, {
body: {
id: comment.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.updatedAt).toEqual(comment.updatedAt.toISOString());
expect(body.data.resolvedAt).toEqual(null);
expect(body.data.resolvedBy).toEqual(null);
expect(body.data.resolvedById).toEqual(null);
expect(body.policies.length).toEqual(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.update).toBeTruthy();
expect(body.policies[0].abilities.delete).toBeTruthy();
expect(body.policies[0].abilities.resolve).toBeTruthy();
expect(body.policies[0].abilities.unresolve).toEqual(false);
});
});
describe("#comments.add_reaction", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.add_reaction");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should add a reaction to a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/comments.add_reaction", user, {
body: {
id: comment.id,
emoji: "😄",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
const updatedComment = await Comment.findByPk(comment.id);
const addedReaction = await Reaction.findOne({
where: { commentId: comment.id, emoji: "😄", userId: user.id },
});
expect(updatedComment?.reactions).toEqual([
{ emoji: "😄", userIds: [user.id] },
]);
expect(addedReaction).toBeTruthy();
});
it("should add a reaction to a comment with existing reactions", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
reactions: [{ emoji: "😄", userIds: ["test-user"] }],
});
const res = await server.post("/api/comments.add_reaction", user, {
body: {
id: comment.id,
emoji: "😄",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
const updatedComment = await Comment.findByPk(comment.id);
const addedReaction = await Reaction.findOne({
where: { commentId: comment.id, emoji: "😄", userId: user.id },
});
expect(updatedComment?.reactions).toEqual([
{ emoji: "😄", userIds: ["test-user", user.id] },
]);
expect(addedReaction).toBeTruthy();
});
it("should add a custom emoji reaction to a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const emoji = await buildEmoji({
teamId: team.id,
createdById: user.id,
});
const res = await server.post("/api/comments.add_reaction", user, {
body: {
id: comment.id,
emoji: emoji.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
const updatedComment = await Comment.findByPk(comment.id);
const addedReaction = await Reaction.findOne({
where: { commentId: comment.id, emoji: emoji.id, userId: user.id },
});
expect(updatedComment?.reactions).toEqual([
{ emoji: emoji.id, userIds: [user.id] },
]);
expect(addedReaction).toBeTruthy();
});
it("should fail with custom emoji from different team", async () => {
const team = await buildTeam();
const otherTeam = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
const emoji = await buildEmoji({
teamId: otherTeam.id,
});
const res = await server.post("/api/comments.add_reaction", user, {
body: {
id: comment.id,
emoji: emoji.id,
},
});
expect(res.status).toEqual(403);
});
});
describe("#comments.remove_reaction", () => {
it("should require authentication", async () => {
const res = await server.post("/api/comments.remove_reaction");
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
it("should remove a reaction from a comment", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
});
await Reaction.create(
{
emoji: "😄",
commentId: comment.id,
userId: user.id,
},
{ hooks: false }
);
const res = await server.post("/api/comments.remove_reaction", user, {
body: {
id: comment.id,
emoji: "😄",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
const updatedComment = await Comment.findByPk(comment.id);
const removedReaction = await Reaction.findOne({
where: { commentId: comment.id, emoji: "😄", userId: user.id },
});
expect(updatedComment?.reactions).toEqual([]);
expect(removedReaction).toBeNull();
});
it("should remove a reaction from a comment with existing reactions", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const comment = await buildComment({
userId: user.id,
documentId: document.id,
reactions: [{ emoji: "😄", userIds: ["test-user"] }],
});
await Reaction.create(
{
emoji: "😄",
commentId: comment.id,
userId: user.id,
},
{ hooks: false }
);
const res = await server.post("/api/comments.remove_reaction", user, {
body: {
id: comment.id,
emoji: "😄",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
const updatedComment = await Comment.findByPk(comment.id);
const removedReaction = await Reaction.findOne({
where: { commentId: comment.id, emoji: "😄", userId: user.id },
});
expect(updatedComment?.reactions).toEqual([
{ emoji: "😄", userIds: ["test-user"] },
]);
expect(removedReaction).toBeNull();
});
});