mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* fix: Reject document updates that contain no updatable fields A documents.update request carrying a valid id but no recognized body field (e.g. text omitted, or an unmapped key stripped by validation) previously returned 200 OK while writing nothing, so API and MCP callers could believe content was persisted when nothing changed. documentUpdater now throws a ValidationError when none of the updatable properties are provided, making the no-op fail loud with a 400 for both the REST route and the MCP update_document tool. Requests that only signal an editing session (done) or publish a draft continue to work. Closes #12875 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCYPC1guJ85BezXLNw8Zjo * fix: Error on MCP document update with no fields provided Scope the no-op guard to the MCP surface only, and derive it from the provided input rather than a hardcoded list of properties: any key other than id counts, since the tool schema already strips unrecognized fields. Closes #12875 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCYPC1guJ85BezXLNw8Zjo * fix: Base MCP no-op update detection on whether the document changed Compare the document's revisionCount before and after documentUpdater runs instead of inspecting the request input. Every save increments revisionCount, so an unchanged count means nothing was persisted — this catches both requests with no recognized fields and requests whose values are identical to the current document. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCYPC1guJ85BezXLNw8Zjo * fix: Extend no-op update detection to all MCP update tools Apply the same fail-loud behavior to update_collection and update_comment: check model.changed() before saving and return an error when the request would persist nothing. Mark all three update tools as non-idempotent since retrying an already-applied update now returns an error rather than a second success. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCYPC1guJ85BezXLNw8Zjo * chore: Fix punctuation in no-op update error messages Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCYPC1guJ85BezXLNw8Zjo --------- Co-authored-by: Claude <noreply@anthropic.com>
382 lines
11 KiB
TypeScript
382 lines
11 KiB
TypeScript
import { Scope } from "@shared/types";
|
|
import type { ProsemirrorData } from "@shared/types";
|
|
import {
|
|
buildCollection,
|
|
buildComment,
|
|
buildCommentMark,
|
|
buildDocument,
|
|
buildOAuthAuthentication,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
import { getTestServer } from "@server/test/support";
|
|
import { buildOAuthUser, callMcpTool } from "@server/test/McpHelper";
|
|
|
|
const server = getTestServer();
|
|
|
|
describe("list_comments", () => {
|
|
it("returns comments on a document", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_comments", {
|
|
documentId: document.id,
|
|
});
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((c: { id: string }) => c.id);
|
|
expect(ids).toContain(comment.id);
|
|
});
|
|
|
|
it("includes anchorText when comment is anchored", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const anchorText = "highlighted text";
|
|
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 callMcpTool(server, accessToken, "list_comments", {
|
|
documentId: document.id,
|
|
});
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const match = data.find((c: { id: string }) => c.id === comment.id) as {
|
|
anchorText: string;
|
|
};
|
|
expect(match).toBeDefined();
|
|
expect(match.anchorText).toEqual(anchorText);
|
|
});
|
|
|
|
it("returns undefined anchorText for non-anchored comment", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_comments", {
|
|
documentId: document.id,
|
|
});
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
expect(data.length).toBeGreaterThanOrEqual(1);
|
|
expect(data[0].anchorText).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("create_comment", () => {
|
|
it("creates a comment on a document", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_comment", {
|
|
documentId: document.id,
|
|
text: "This is a test comment",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.id).toBeDefined();
|
|
expect(data.documentId).toEqual(document.id);
|
|
});
|
|
|
|
it("creates a reply to an existing comment", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const parentComment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_comment", {
|
|
documentId: document.id,
|
|
text: "This is a reply",
|
|
parentCommentId: parentComment.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.id).toBeDefined();
|
|
expect(data.parentCommentId).toEqual(parentComment.id);
|
|
});
|
|
|
|
it("includes anchorText in response", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_comment", {
|
|
documentId: document.id,
|
|
text: "A new comment",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
// New comments have no anchor mark in the document, so anchorText is undefined
|
|
expect(data.id).toBeDefined();
|
|
expect(data.anchorText).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("update_comment", () => {
|
|
it("updates text", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_comment", {
|
|
id: comment.id,
|
|
text: "Updated comment text",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.id).toEqual(comment.id);
|
|
expect(data.text).toContain("Updated comment text");
|
|
});
|
|
|
|
it("includes anchorText in response", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const anchorText = "anchored content";
|
|
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 callMcpTool(server, accessToken, "update_comment", {
|
|
id: comment.id,
|
|
text: "Updated text",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.id).toEqual(comment.id);
|
|
expect(data.anchorText).toEqual(anchorText);
|
|
});
|
|
|
|
it("errors when no fields are provided to update", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_comment", {
|
|
id: comment.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
expect(res?.result?.content?.[0]?.text).toContain(
|
|
"The update resulted in no changes to the comment"
|
|
);
|
|
});
|
|
|
|
it("errors when text is identical to the current comment", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const first = await callMcpTool(server, accessToken, "update_comment", {
|
|
id: comment.id,
|
|
text: "Same text",
|
|
});
|
|
expect(first?.result?.isError).toBeUndefined();
|
|
|
|
const second = await callMcpTool(server, accessToken, "update_comment", {
|
|
id: comment.id,
|
|
text: "Same text",
|
|
});
|
|
|
|
expect(second?.result?.isError).toBe(true);
|
|
expect(second?.result?.content?.[0]?.text).toContain(
|
|
"The update resulted in no changes to the comment"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("delete_comment", () => {
|
|
it("deletes own comment", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "delete_comment", {
|
|
id: comment.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.success).toBe(true);
|
|
});
|
|
|
|
it("fails for non-author non-admin", async () => {
|
|
const { user } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const comment = await buildComment({
|
|
userId: user.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const otherUser = await buildUser({ teamId: user.teamId });
|
|
const otherAuth = await buildOAuthAuthentication({
|
|
user: otherUser,
|
|
scope: [Scope.Read, Scope.Write, Scope.Create],
|
|
});
|
|
|
|
const res = await callMcpTool(
|
|
server,
|
|
otherAuth.accessToken!,
|
|
"delete_comment",
|
|
{ id: comment.id }
|
|
);
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
});
|