mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
* feat: Add document owners Documents can now have owners, stored as an array of user IDs. Owners must be active members or admins, and changing them requires the existing manage users permission on the document, gated behind a new DocumentOwners plan entitlement that is included in every plan. - ownerIds accepted by documents.create and documents.update and returned by the document presenter - ownerIds filter field (eq/in) for documents.list and documents.search - ownerIds on the MCP create_document and update_document tools, and an ownerId filter on list_documents - Owners section in the document Insights dialog to add and remove owners - Owner filter on the search page - In-app and email notification when a user is made an owner Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFdSZ39ZGPf31jyGcYz * feat: Give document owners responsibility for their documents - Access requests are routed to owners that can manage the document before falling back to document, collection and workspace admins - Owners are subscribed to a document when they are added, respecting a previous unsubscribe - Owners that can edit a document can also manage its access, owners and archive it; ownership never grants access on its own - Owners are shown in the document byline, list items and search results, and document hover previews Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFdSZ39ZGPf31jyGcYz * feat: Manage document owners from the share panel - Owner is a role in the share panel permission menu and invite picker for individual users, adding an owner gives them the manage permission - Lowering an owner's permission or removing their access removes the ownership - Owners are sorted to the top of the member list - Owners added through the API or MCP are given the manage permission - Owners do not also receive the "added to document" notification - Only added owners are validated, so suspended owners can still be removed - Deleted users are removed from document owners - Removes the owners section from Insights * fix: Address review feedback on document owners - Authorize owner changes before assigning owners on create - Lock the document when adding an owner so concurrent invitations are kept - Enforce the owner limit on every path - Run MCP document updates in a transaction - Broadcast owner removals to connected clients * feat: Add collection owners - Collections can have owners, who also own every document in the collection, shown first in document bylines - Owner is a role in the collection share panel and invite picker for individual users, adding an owner gives them the manage permission - Lowering an owner's permission or removing their access removes the ownership - ownerIds on collections.create, collections.update and the MCP collection tools - The document owner filter includes documents in owned collections - Access requests go to document and collection owners together - Collection owners are subscribed to the collection and notified when they are added - Deleted users are removed from collection owners - Renames the personal collection policy helper to isPersonalCollectionOwner * feat: Show profile cards on owner names, hide inherited owners in collection lists * fix: Allow adding yourself as a collection owner, include collection owners in MCP exact match * fix: Correct singular owner translations * feat: Allow users to make themselves a document owner - The share panel suggests the current user when they can manage owners, and only offers the Owner permission for them - documents.add_user accepts the actor's own ID when changing ownership, without changing their own access - An owner's own row offers "Remove as owner", which keeps their access - Hides the duplicate "You have full access" row when the current user is listed as a member * fix: Restore singular owner translations * fix: Limit owners to 5, use no key update locks when adding owners * refactor: Move owner logic into Document and Collection model methods - addOwner, removeOwner, grantOwnerAccess and setOwnerIds on both models, locking the row so concurrent owner changes are kept - Owner events are created by an update hook from the change in ownerIds - The manageOwners check runs at the route level when ownerIds is sent - Removes the documentOwners and collectionOwners command modules - Removes duplicate owner tests * fix: Owner selection and membership edge cases - Remove document owners from the legacy plan - Selecting yourself in the share picker only suggests other users that can be owners, and you are not suggested next to groups or guests - Users that make themselves an owner through the API get the manage permission, so they are listed in the share panel * perf: Scope the collection owner filter subquery to the team * chore: Drop redundant teamId UUID check --------- Co-authored-by: Claude <noreply@anthropic.com>
1713 lines
52 KiB
TypeScript
1713 lines
52 KiB
TypeScript
import { faker } from "@faker-js/faker";
|
|
import { DeprecationValidation } from "@shared/validations";
|
|
import {
|
|
CollectionPermission,
|
|
DocumentPermission,
|
|
Scope,
|
|
TeamPreference,
|
|
} from "@shared/types";
|
|
import {
|
|
buildUser,
|
|
buildViewer,
|
|
buildCollection,
|
|
buildDocument,
|
|
buildDraftDocument,
|
|
buildTemplate,
|
|
buildOAuthAuthentication,
|
|
buildGroup,
|
|
} from "@server/test/factories";
|
|
import {
|
|
Attachment,
|
|
Collection,
|
|
Document,
|
|
GroupMembership,
|
|
SearchQuery,
|
|
UserMembership,
|
|
} from "@server/models";
|
|
import { SearchQuerySource } from "@server/models/SearchQuery";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { getTestServer } from "@server/test/support";
|
|
import {
|
|
buildOAuthUser,
|
|
callMcpTool,
|
|
parseMcpListContent,
|
|
} from "@server/test/McpHelper";
|
|
|
|
const server = getTestServer();
|
|
|
|
describe("list_documents", () => {
|
|
it("returns recent documents", 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, "list_documents");
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toContain(document.id);
|
|
|
|
const match = data.find(
|
|
(d: { document: { id: string } }) => d.document.id === document.id
|
|
) as { document: { url: string } };
|
|
expect(match.document.url).toMatch(/^https?:\/\//);
|
|
expect(
|
|
(match.document as { commentCount?: number }).commentCount
|
|
).toBeUndefined();
|
|
});
|
|
|
|
it("does not return templates in recent documents", 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 template = await buildTemplate({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents");
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toContain(document.id);
|
|
expect(ids).not.toContain(template.id);
|
|
});
|
|
|
|
it("filters by collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection1 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const collection2 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const doc1 = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection1.id,
|
|
});
|
|
await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection2.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents", {
|
|
collectionId: collection1.id,
|
|
});
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toContain(doc1.id);
|
|
expect(
|
|
data.every(
|
|
(d: { document: { collectionId: string } }) =>
|
|
d.document.collectionId === collection1.id
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("filters by a collection referenced by urlId", 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, "list_documents", {
|
|
collectionId: collection.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toEqual([document.id]);
|
|
});
|
|
|
|
it("returns an exact match in a collection referenced by urlId", 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, "list_documents", {
|
|
query: document.id,
|
|
collectionId: collection.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
expect(data[0].document.id).toEqual(document.id);
|
|
});
|
|
|
|
it("returns an exact match owned through its collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const owner = await buildUser({ teamId: user.teamId });
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
ownerIds: [owner.id],
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents", {
|
|
query: document.id,
|
|
ownerId: owner.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
expect(data[0].document.id).toEqual(document.id);
|
|
});
|
|
|
|
it("returns documents shared directly with the user", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const author = await buildUser({ teamId: user.teamId });
|
|
// A document in a private collection the user is not a member of, shared
|
|
// directly with the user via a membership ("Shared with me").
|
|
const collection = await buildCollection({
|
|
teamId: author.teamId,
|
|
userId: author.id,
|
|
permission: null,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: author.teamId,
|
|
userId: author.id,
|
|
collectionId: collection.id,
|
|
title: "Metaphysics",
|
|
});
|
|
await UserMembership.create({
|
|
documentId: document.id,
|
|
userId: user.id,
|
|
createdById: author.id,
|
|
permission: DocumentPermission.Read,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents");
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toContain(document.id);
|
|
});
|
|
|
|
it("returns documents shared with the user via a group", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const author = await buildUser({ teamId: user.teamId });
|
|
const collection = await buildCollection({
|
|
teamId: author.teamId,
|
|
userId: author.id,
|
|
permission: null,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: author.teamId,
|
|
userId: author.id,
|
|
collectionId: collection.id,
|
|
title: "Metaphysics",
|
|
});
|
|
|
|
const group = await buildGroup({ teamId: user.teamId });
|
|
await group.$add("user", user, { through: { createdById: author.id } });
|
|
await GroupMembership.create({
|
|
createdById: author.id,
|
|
groupId: group.id,
|
|
documentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents");
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
expect(ids).toContain(document.id);
|
|
});
|
|
|
|
it("does not return private documents via exact urlId match", async () => {
|
|
const owner = await buildUser();
|
|
const otherUser = await buildUser({ teamId: owner.teamId });
|
|
|
|
const privateCollection = await buildCollection({
|
|
teamId: owner.teamId,
|
|
userId: owner.id,
|
|
permission: null,
|
|
});
|
|
const privateDocument = await buildDocument({
|
|
teamId: owner.teamId,
|
|
userId: owner.id,
|
|
collectionId: privateCollection.id,
|
|
title: "Confidential",
|
|
});
|
|
|
|
const auth = await buildOAuthAuthentication({
|
|
user: otherUser,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
const res = await callMcpTool(server, auth.accessToken!, "list_documents", {
|
|
query: privateDocument.urlId,
|
|
});
|
|
|
|
expect(res?.result?.content).toEqual([{ type: "text", text: "[]" }]);
|
|
|
|
const data = parseMcpListContent<{ document: { id: string } }>(
|
|
res?.result?.content
|
|
);
|
|
const ids = data.map((d) => d.document.id);
|
|
|
|
expect(res?.result?.isError).not.toBe(true);
|
|
expect(ids).toHaveLength(0);
|
|
expect(ids).not.toContain(privateDocument.id);
|
|
});
|
|
|
|
it("returns documents via exact urlId match when the user has access", async () => {
|
|
const user = await buildUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
permission: CollectionPermission.ReadWrite,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const auth = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
const res = await callMcpTool(server, auth.accessToken!, "list_documents", {
|
|
query: document.urlId,
|
|
});
|
|
|
|
const data = (res?.result?.content ?? []).map((c: { text?: string }) =>
|
|
JSON.parse(c.text ?? "{}")
|
|
);
|
|
const ids = data.map((d: { document: { id: string } }) => d.document.id);
|
|
|
|
expect(ids).toContain(document.id);
|
|
});
|
|
|
|
it("excludes archived documents from search by default", 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,
|
|
title: "Blackberry",
|
|
});
|
|
const archived = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
title: "Blackberry",
|
|
archivedAt: new Date(),
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents", {
|
|
query: "Blackberry",
|
|
});
|
|
const data = parseMcpListContent<{ document: { id: string } }>(
|
|
res?.result?.content
|
|
);
|
|
const ids = data.map((d) => d.document.id);
|
|
|
|
expect(ids).toContain(document.id);
|
|
expect(ids).not.toContain(archived.id);
|
|
});
|
|
|
|
it("includes archived documents in search when requested", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const archived = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
title: "Blackberry",
|
|
archivedAt: new Date(),
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents", {
|
|
query: "Blackberry",
|
|
includeArchived: true,
|
|
});
|
|
const data = parseMcpListContent<{ document: { id: string } }>(
|
|
res?.result?.content
|
|
);
|
|
const ids = data.map((d) => d.document.id);
|
|
|
|
expect(ids).toContain(archived.id);
|
|
});
|
|
|
|
it("includes archived documents in recent documents when requested", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const archived = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
archivedAt: new Date(),
|
|
});
|
|
|
|
const excluded = parseMcpListContent<{ document: { id: string } }>(
|
|
(await callMcpTool(server, accessToken, "list_documents"))?.result
|
|
?.content
|
|
).map((d) => d.document.id);
|
|
expect(excluded).not.toContain(archived.id);
|
|
|
|
const included = parseMcpListContent<{ document: { id: string } }>(
|
|
(
|
|
await callMcpTool(server, accessToken, "list_documents", {
|
|
includeArchived: true,
|
|
})
|
|
)?.result?.content
|
|
).map((d) => d.document.id);
|
|
expect(included).toContain(archived.id);
|
|
});
|
|
|
|
it("records the search query with an mcp source", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
title: "Metaphysics",
|
|
});
|
|
|
|
await callMcpTool(server, accessToken, "list_documents", {
|
|
query: "Metaphysics",
|
|
});
|
|
|
|
const searchQuery = await SearchQuery.findOne({
|
|
where: { teamId: user.teamId, userId: user.id },
|
|
});
|
|
expect(searchQuery?.source).toEqual(SearchQuerySource.MCP);
|
|
expect(searchQuery?.query).toEqual("Metaphysics");
|
|
});
|
|
|
|
it("does not record a search query when listing recent documents", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
await callMcpTool(server, accessToken, "list_documents");
|
|
|
|
expect(await SearchQuery.count({ where: { teamId: user.teamId } })).toEqual(
|
|
0
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("create_document", () => {
|
|
it("attributes Markdown mentions to the authenticated user", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const mentioned = await buildUser({ teamId: user.teamId });
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "Mentions",
|
|
collectionId: collection.id,
|
|
text: `@[${mentioned.name}](mention://user/${mentioned.id})`,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
expect(data.success).toBe(true);
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(DocumentHelper.parseMentions(document)).toEqual([
|
|
expect.objectContaining({ modelId: mentioned.id, actorId: user.id }),
|
|
]);
|
|
});
|
|
|
|
it("creates in the personal collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
user.team.setPreference(TeamPreference.PersonalDocs, true);
|
|
await user.team.save();
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "Personal Document",
|
|
collectionId: "personal",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
expect(data.success).toBe(true);
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
const collection = await Collection.findOne({
|
|
where: { ownerId: user.id },
|
|
rejectOnEmpty: true,
|
|
});
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
});
|
|
|
|
it("creates in a collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "New Document",
|
|
text: "Hello world",
|
|
collectionId: collection.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("New Document");
|
|
expect(data.id).toBeDefined();
|
|
expect(data.url).toMatch(/^https?:\/\//);
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
});
|
|
|
|
it("creates from HTML and preserves images as attachments", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const image =
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "HTML Document",
|
|
text: `<p>Hello <strong>HTML</strong></p><img alt="chart" src="data:image/png;base64,${image}" width="1" height="1">`,
|
|
format: "html",
|
|
collectionId: collection.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
const attachmentCount = await Attachment.count({
|
|
where: {
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
contentType: "image/png",
|
|
},
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("HTML Document");
|
|
expect(attachmentCount).toEqual(1);
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
expect(document.text).toContain("Hello **HTML**");
|
|
expect(document.text).toContain("/api/attachments.redirect?id=");
|
|
});
|
|
|
|
it("creates nested under parent document", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const parent = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "Child Document",
|
|
text: "Nested content",
|
|
parentDocumentId: parent.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("Child Document");
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(document.parentDocumentId).toEqual(parent.id);
|
|
});
|
|
|
|
it.each(["markdown", "html"] as const)(
|
|
"creates %s nested under a parent document referenced by urlId",
|
|
async (format) => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const parent = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "Child Document",
|
|
text: format === "html" ? "<p>Nested content</p>" : "Nested content",
|
|
format,
|
|
parentDocumentId: parent.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
const document = await Document.findByPk(data.id, {
|
|
rejectOnEmpty: true,
|
|
});
|
|
expect(document.parentDocumentId).toEqual(parent.id);
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
}
|
|
);
|
|
|
|
it("creates from a template", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const template = await buildTemplate({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
text: "Content from the template",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "From Template",
|
|
collectionId: collection.id,
|
|
templateId: template.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(res?.result?.isError).not.toBe(true);
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("From Template");
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(document.templateId).toEqual(template.id);
|
|
expect(document.text).toContain("Content from the template");
|
|
});
|
|
|
|
it("defaults the title to the template title", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const template = await buildTemplate({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
title: "Template Title",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
collectionId: collection.id,
|
|
templateId: template.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(res?.result?.isError).not.toBe(true);
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("Template Title");
|
|
});
|
|
|
|
it("does not allow creating from a template the user cannot access", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const otherUser = await buildUser();
|
|
const otherCollection = await buildCollection({
|
|
teamId: otherUser.teamId,
|
|
userId: otherUser.id,
|
|
});
|
|
const template = await buildTemplate({
|
|
teamId: otherUser.teamId,
|
|
userId: otherUser.id,
|
|
collectionId: otherCollection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "From Template",
|
|
collectionId: collection.id,
|
|
templateId: template.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
|
|
it("does not allow a viewer to create a draft", async () => {
|
|
const user = await buildViewer();
|
|
const auth = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read, Scope.Write, Scope.Create],
|
|
});
|
|
|
|
const res = await callMcpTool(
|
|
server,
|
|
auth.accessToken!,
|
|
"create_document",
|
|
{
|
|
title: "Viewer Draft",
|
|
text: "Should not be created",
|
|
publish: false,
|
|
}
|
|
);
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
|
|
const documents = await Document.unscoped().findAll({
|
|
where: { createdById: user.id },
|
|
});
|
|
expect(documents.length).toEqual(0);
|
|
});
|
|
|
|
it("does not allow a viewer to create a document in a collection", async () => {
|
|
const user = await buildViewer();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
permission: CollectionPermission.ReadWrite,
|
|
});
|
|
const auth = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read, Scope.Write, Scope.Create],
|
|
});
|
|
|
|
const res = await callMcpTool(
|
|
server,
|
|
auth.accessToken!,
|
|
"create_document",
|
|
{
|
|
title: "Viewer Document",
|
|
collectionId: collection.id,
|
|
}
|
|
);
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
|
|
const documents = await Document.unscoped().findAll({
|
|
where: { createdById: user.id },
|
|
});
|
|
expect(documents.length).toEqual(0);
|
|
});
|
|
});
|
|
|
|
describe("update_document", () => {
|
|
it.each([
|
|
{ input: { findText: "original", text: "replacement" }, field: "findText" },
|
|
{
|
|
input: { editMode: "replace", findText: "original", text: "replacement" },
|
|
field: "findText",
|
|
},
|
|
{
|
|
input: { editMode: "replace", findText: " ", text: "replacement" },
|
|
field: "findText",
|
|
},
|
|
{ input: { editMode: "patch", findText: "original" }, field: "text" },
|
|
{ input: { editMode: "patch", text: "replacement" }, field: "findText" },
|
|
{
|
|
input: { editMode: "patch", findText: "", text: "replacement" },
|
|
field: "findText",
|
|
},
|
|
{ input: { editMode: "append" }, field: "text" },
|
|
{ input: { editMode: "append", text: "" }, field: "text" },
|
|
{ input: { editMode: "prepend" }, field: "text" },
|
|
{ input: { editMode: "prepend", text: "" }, field: "text" },
|
|
])("rejects invalid edit parameters: $input", async ({ input, field }) => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
text: "original content",
|
|
});
|
|
const { title, text, revisionCount } = document;
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
title: "Must not change",
|
|
...input,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
expect(res?.result?.content?.[0]?.text).toContain(field);
|
|
expect(res?.result?.content?.[0]?.text).toContain("editMode");
|
|
|
|
await document.reload();
|
|
expect(document.title).toEqual(title);
|
|
expect(document.text).toEqual(text);
|
|
expect(document.revisionCount).toEqual(revisionCount);
|
|
});
|
|
|
|
it.each([undefined, "replace"])(
|
|
"allows an empty findText with editMode %s",
|
|
async (editMode) => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
text: "original content",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
editMode,
|
|
findText: "",
|
|
text: "replacement content",
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
await document.reload();
|
|
expect(document.text).toEqual("replacement content");
|
|
}
|
|
);
|
|
|
|
it.each([
|
|
{ editMode: "append", expected: "original contentadded content" },
|
|
{ editMode: "prepend", expected: "added contentoriginal content" },
|
|
])(
|
|
"allows findText with $editMode for REST parity",
|
|
async ({ editMode, expected }) => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
text: "original content",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
editMode,
|
|
findText: "original",
|
|
text: "added content",
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
await document.reload();
|
|
expect(document.text).toEqual(expected);
|
|
}
|
|
);
|
|
|
|
it("allows empty patch text to delete the matched content", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
text: "the original sentence",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
editMode: "patch",
|
|
findText: "original ",
|
|
text: "",
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
await document.reload();
|
|
expect(document.text).toEqual("the sentence");
|
|
});
|
|
|
|
it("attributes Markdown mentions to the authenticated editor", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const author = await buildUser({ teamId: user.teamId });
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: author.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
text: `@[Author](mention://user/${author.id})`,
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
|
|
await document.reload();
|
|
expect(DocumentHelper.parseMentions(document)).toEqual([
|
|
expect.objectContaining({ modelId: author.id, actorId: user.id }),
|
|
]);
|
|
});
|
|
|
|
it("does not create the personal collection when it is not used", 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, "update_document", {
|
|
id: document.id,
|
|
title: "Renamed",
|
|
collectionId: "personal",
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
expect(await Collection.count({ where: { ownerId: user.id } })).toEqual(0);
|
|
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
});
|
|
|
|
it("publishes a draft to the personal collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const document = await buildDraftDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: null,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
publish: true,
|
|
collectionId: "personal",
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
|
|
const personal = await Collection.findOne({
|
|
where: { ownerId: user.id },
|
|
rejectOnEmpty: true,
|
|
});
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(personal.id);
|
|
expect(document.publishedAt).not.toBeNull();
|
|
});
|
|
|
|
it("publishes a draft to a collection referenced by urlId", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDraftDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: null,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
publish: true,
|
|
collectionId: collection.urlId,
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(collection.id);
|
|
expect(document.publishedAt).not.toBeNull();
|
|
});
|
|
|
|
it("updates title and 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 res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
title: "Updated Title",
|
|
text: "Updated content",
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.title).toEqual("Updated Title");
|
|
expect(data.url).toMatch(/^https?:\/\//);
|
|
expect(res?.result?.content?.length).toEqual(1);
|
|
|
|
await document.reload();
|
|
expect(document.text).toContain("Updated content");
|
|
});
|
|
|
|
it("returns the resulting content when patching", 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,
|
|
text: "the original sentence",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
editMode: "patch",
|
|
findText: "original",
|
|
text: "patched",
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
expect(res?.result?.content?.[1]?.text).toContain("the patched sentence");
|
|
});
|
|
|
|
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,
|
|
text: "original text",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
expect(res?.result?.content?.[0]?.text).toContain(
|
|
"The update resulted in no changes to the document"
|
|
);
|
|
|
|
const reloaded = await Document.unscoped().findByPk(document.id);
|
|
expect(reloaded?.text).toEqual("original text");
|
|
});
|
|
|
|
it("errors when provided fields are identical to the current 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, "update_document", {
|
|
id: document.id,
|
|
title: document.title,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
expect(res?.result?.content?.[0]?.text).toContain(
|
|
"The update resulted in no changes to the document"
|
|
);
|
|
});
|
|
|
|
it("unpublishes 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, "update_document", {
|
|
id: document.id,
|
|
publish: false,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.id).toEqual(document.id);
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
});
|
|
|
|
it("fails to unpublish a document with children", 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 buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
parentDocumentId: document.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
publish: false,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
|
|
it("does not allow a viewer to publish their draft into a restricted collection", async () => {
|
|
const viewer = await buildViewer();
|
|
const collection = await buildCollection({
|
|
teamId: viewer.teamId,
|
|
permission: CollectionPermission.ReadWrite,
|
|
});
|
|
const draft = await buildDocument({
|
|
teamId: viewer.teamId,
|
|
userId: viewer.id,
|
|
collectionId: null,
|
|
publishedAt: null,
|
|
});
|
|
const auth = await buildOAuthAuthentication({
|
|
user: viewer,
|
|
scope: [Scope.Read, Scope.Write, Scope.Create],
|
|
});
|
|
|
|
const res = await callMcpTool(
|
|
server,
|
|
auth.accessToken!,
|
|
"update_document",
|
|
{
|
|
id: draft.id,
|
|
publish: true,
|
|
collectionId: collection.id,
|
|
}
|
|
);
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
|
|
const reloaded = await Document.unscoped().findByPk(draft.id);
|
|
expect(reloaded?.collectionId).toBeNull();
|
|
expect(reloaded?.publishedAt).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("move_document", () => {
|
|
it("moves a document to the personal collection", 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, "move_document", {
|
|
id: document.id,
|
|
collectionId: "personal",
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
|
|
const personal = await Collection.findOne({
|
|
where: { ownerId: user.id },
|
|
rejectOnEmpty: true,
|
|
});
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(personal.id);
|
|
});
|
|
|
|
it("moves to a different collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection1 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const collection2 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection1.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "move_document", {
|
|
id: document.id,
|
|
collectionId: collection2.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
expect(data.success).toBe(true);
|
|
expect(data.id).toEqual(document.id);
|
|
expect(data.breadcrumb).toEqual(collection2.name);
|
|
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(collection2.id);
|
|
});
|
|
|
|
it("moves under a parent document", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const parent = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const child = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "move_document", {
|
|
id: child.id,
|
|
parentDocumentId: parent.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
expect(data.success).toBe(true);
|
|
expect(data.id).toEqual(child.id);
|
|
|
|
await child.reload();
|
|
expect(child.parentDocumentId).toEqual(parent.id);
|
|
});
|
|
|
|
it("moves under a parent document referenced by urlId", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const parent = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const child = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "move_document", {
|
|
id: child.id,
|
|
parentDocumentId: parent.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
|
|
await child.reload();
|
|
expect(child.parentDocumentId).toEqual(parent.id);
|
|
});
|
|
|
|
it("moves to a collection referenced by urlId", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const collection1 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const collection2 = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection1.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "move_document", {
|
|
id: document.id,
|
|
collectionId: collection2.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
|
|
await document.reload();
|
|
expect(document.collectionId).toEqual(collection2.id);
|
|
});
|
|
|
|
it("fails without collectionId or parentDocumentId", 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, "move_document", {
|
|
id: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
|
|
it("fails when nesting a document inside itself", 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, "move_document", {
|
|
id: document.id,
|
|
parentDocumentId: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
|
|
it("fails when nesting a document inside itself by urlId", 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, "move_document", {
|
|
id: document.id,
|
|
parentDocumentId: document.urlId,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("restore_document", () => {
|
|
it("restores an archived 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,
|
|
archivedAt: new Date(),
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "restore_document", {
|
|
id: document.id,
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
expect(data.success).toBe(true);
|
|
expect(data.id).toEqual(document.id);
|
|
|
|
const reloaded = await Document.unscoped().findByPk(document.id);
|
|
expect(reloaded?.archivedAt).toBeNull();
|
|
});
|
|
|
|
it("restores a trashed 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,
|
|
});
|
|
await document.destroy();
|
|
|
|
const res = await callMcpTool(server, accessToken, "restore_document", {
|
|
id: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
|
|
const reloaded = await Document.unscoped().findByPk(document.id, {
|
|
paranoid: false,
|
|
});
|
|
expect(reloaded?.deletedAt).toBeNull();
|
|
});
|
|
|
|
it("restores into a different collection", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const source = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const destination = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: source.id,
|
|
archivedAt: new Date(),
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "restore_document", {
|
|
id: document.id,
|
|
collectionId: destination.id,
|
|
});
|
|
expect(res?.result?.isError).toBeUndefined();
|
|
|
|
const reloaded = await Document.unscoped().findByPk(document.id);
|
|
expect(reloaded?.collectionId).toEqual(destination.id);
|
|
});
|
|
|
|
it("fails for a viewer with read_write membership", async () => {
|
|
const { user } = await buildOAuthUser();
|
|
const viewer = await buildViewer({ teamId: user.teamId });
|
|
const { accessToken } = await buildOAuthAuthentication({
|
|
user: viewer,
|
|
scope: [Scope.Read, Scope.Write, Scope.Create],
|
|
});
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
collectionId: collection.id,
|
|
archivedAt: new Date(),
|
|
});
|
|
await UserMembership.create({
|
|
userId: viewer.id,
|
|
documentId: document.id,
|
|
permission: DocumentPermission.ReadWrite,
|
|
createdById: user.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken!, "restore_document", {
|
|
id: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
const reloaded = await Document.unscoped().findByPk(document.id);
|
|
expect(reloaded?.archivedAt).not.toBeNull();
|
|
});
|
|
|
|
it("fails when the document is not archived or trashed", 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, "restore_document", {
|
|
id: document.id,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
});
|
|
|
|
it("returns the same error for a document in another team", async () => {
|
|
const { accessToken } = await buildOAuthUser();
|
|
const document = await buildDocument({ archivedAt: new Date() });
|
|
|
|
const res = await callMcpTool(server, accessToken, "restore_document", {
|
|
id: document.id,
|
|
});
|
|
const unknown = await callMcpTool(server, accessToken, "restore_document", {
|
|
id: faker.string.uuid(),
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
expect(res?.result?.content).toEqual(unknown?.result?.content);
|
|
|
|
const reloaded = await Document.unscoped().findByPk(document.id);
|
|
expect(reloaded?.archivedAt).not.toBeNull();
|
|
});
|
|
});
|
|
|
|
describe.each([false, true])("delete_document with archive=%s", (archive) => {
|
|
it.each([undefined, " Replaced by a new guide. ", null, " "])(
|
|
"saves reason %j with the status change",
|
|
async (reason) => {
|
|
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, "delete_document", {
|
|
id: document.id,
|
|
archive,
|
|
reason,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
expect(JSON.parse(res?.result?.content?.[0]?.text ?? "{}").success).toBe(
|
|
true
|
|
);
|
|
await document.reload({ paranoid: false });
|
|
expect(document.deprecatedReason).toBe(reason?.trim() || null);
|
|
expect(archive ? document.archivedAt : document.deletedAt).not.toBeNull();
|
|
}
|
|
);
|
|
|
|
it("rejects an oversized reason without changing the item", 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, "delete_document", {
|
|
id: document.id,
|
|
archive,
|
|
reason: "x".repeat(DeprecationValidation.maxReasonLength + 1),
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
await document.reload();
|
|
expect(document.deprecatedReason).toBeNull();
|
|
expect(document.archivedAt).toBeNull();
|
|
expect(document.deletedAt).toBeNull();
|
|
});
|
|
|
|
it("checks permissions before saving the reason", async () => {
|
|
const { user } = await buildOAuthUser();
|
|
const { 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, "delete_document", {
|
|
id: document.id,
|
|
archive,
|
|
reason: "No longer needed",
|
|
});
|
|
|
|
expect(res?.result?.isError).toBe(true);
|
|
await document.reload();
|
|
expect(document.deprecatedReason).toBeNull();
|
|
expect(document.archivedAt).toBeNull();
|
|
expect(document.deletedAt).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("delete_document", () => {
|
|
it.each([undefined, null, " "])(
|
|
"preserves or clears the archive reason when deleting with reason %j",
|
|
async (reason) => {
|
|
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 document.update({
|
|
archivedAt: new Date(),
|
|
deprecatedReason: "Archived reason",
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "delete_document", {
|
|
id: document.id,
|
|
reason,
|
|
});
|
|
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
expect(JSON.parse(res?.result?.content?.[0]?.text ?? "{}").success).toBe(
|
|
true
|
|
);
|
|
await document.reload({ paranoid: false });
|
|
expect(document.deletedAt).not.toBeNull();
|
|
expect(document.deprecatedReason).toBe(
|
|
reason === undefined ? "Archived reason" : null
|
|
);
|
|
}
|
|
);
|
|
});
|
|
|
|
describe("document owners", () => {
|
|
it("creates a document with owners", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const owner = await buildUser({ teamId: user.teamId });
|
|
|
|
const res = await callMcpTool(server, accessToken, "create_document", {
|
|
title: "Owned",
|
|
publish: false,
|
|
ownerIds: [owner.id],
|
|
});
|
|
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
|
|
expect(data.success).toBe(true);
|
|
|
|
const document = await Document.findByPk(data.id, { rejectOnEmpty: true });
|
|
expect(document.ownerIds).toEqual([owner.id]);
|
|
});
|
|
|
|
it("updates the owners of a document", async () => {
|
|
const { user, accessToken } = await buildOAuthUser({ role: "admin" });
|
|
const owner = await buildUser({ teamId: user.teamId });
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
ownerIds: [owner.id],
|
|
});
|
|
expect(res?.result?.isError).toBeFalsy();
|
|
|
|
await document.reload();
|
|
expect(document.ownerIds).toEqual([owner.id]);
|
|
|
|
const membership = await UserMembership.findOne({
|
|
where: { documentId: document.id, userId: owner.id },
|
|
rejectOnEmpty: true,
|
|
});
|
|
expect(membership.permission).toEqual(DocumentPermission.Admin);
|
|
});
|
|
|
|
it("rejects owner changes without manage permission", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const owner = await buildUser({ teamId: user.teamId });
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
permission: CollectionPermission.ReadWrite,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
const res = await callMcpTool(server, accessToken, "update_document", {
|
|
id: document.id,
|
|
ownerIds: [owner.id],
|
|
});
|
|
expect(res?.result?.isError).toBe(true);
|
|
|
|
await document.reload();
|
|
expect(document.ownerIds).toEqual([]);
|
|
});
|
|
|
|
it("filters documents by owner", async () => {
|
|
const { user, accessToken } = await buildOAuthUser();
|
|
const owner = await buildUser({ teamId: user.teamId });
|
|
const owned = await buildDocument({
|
|
teamId: user.teamId,
|
|
userId: user.id,
|
|
ownerIds: [owner.id],
|
|
});
|
|
await buildDocument({ teamId: user.teamId, userId: user.id });
|
|
|
|
const res = await callMcpTool(server, accessToken, "list_documents", {
|
|
ownerId: owner.id,
|
|
});
|
|
const data = (res?.result?.content ?? []).map((c: { text: string }) =>
|
|
JSON.parse(c.text)
|
|
);
|
|
expect(
|
|
data.map((d: { document: { id: string } }) => d.document.id)
|
|
).toEqual([owned.id]);
|
|
});
|
|
});
|