Files
outline/server/mcp/tools/notifications.test.ts
T
Tom Moor e23a9566fb feat: Add notification inbox tools to MCP (#14905)
* feat: Add notification inbox tools to MCP

* fix: Mark update_notifications as destructive
2026-12-05 13:29:37 -05:00

263 lines
7.8 KiB
TypeScript

import { NotificationEventType, Scope } from "@shared/types";
import { Notification } from "@server/models";
import {
buildComment,
buildDocument,
buildNotification,
buildOAuthAuthentication,
buildUser,
} from "@server/test/factories";
import { getTestServer } from "@server/test/support";
import {
buildOAuthUser,
callMcpTool,
parseMcpListContent,
} from "@server/test/McpHelper";
const server = getTestServer();
interface PresentedNotification {
id: string;
event: string;
read: boolean;
archived: boolean;
document?: { id: string; title: string; url: string };
comment?: { id: string; text: string };
}
describe("list_notifications", () => {
it("returns the user's unarchived notifications with document and comment", async () => {
const { user, accessToken } = await buildOAuthUser();
const actor = await buildUser({ teamId: user.teamId });
const document = await buildDocument({
teamId: user.teamId,
userId: actor.id,
});
const comment = await buildComment({
userId: actor.id,
documentId: document.id,
});
const notification = await buildNotification({
teamId: user.teamId,
userId: user.id,
actorId: actor.id,
documentId: document.id,
commentId: comment.id,
event: NotificationEventType.CreateComment,
});
const archived = await buildNotification({
teamId: user.teamId,
userId: user.id,
archivedAt: new Date(),
});
const other = await buildNotification({ teamId: user.teamId });
const res = await callMcpTool(server, accessToken, "list_notifications");
const data = parseMcpListContent<PresentedNotification>(
res?.result?.content
);
const ids = data.map((n) => n.id);
expect(ids).toEqual([notification.id]);
expect(ids).not.toContain(archived.id);
expect(ids).not.toContain(other.id);
expect(data[0].read).toBe(false);
expect(data[0].document?.id).toEqual(document.id);
expect(data[0].document?.url).toMatch(/^https?:\/\//);
expect(data[0].comment?.id).toEqual(comment.id);
expect(typeof data[0].comment?.text).toBe("string");
});
it("filters unread and archived notifications with the filter DSL", async () => {
const { user, accessToken } = await buildOAuthUser();
const unread = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
await buildNotification({
teamId: user.teamId,
userId: user.id,
viewedAt: new Date(),
});
const archived = await buildNotification({
teamId: user.teamId,
userId: user.id,
archivedAt: new Date(),
});
const unreadRes = await callMcpTool(
server,
accessToken,
"list_notifications",
{ filters: [{ field: "viewedAt", operator: "isNull" }] }
);
expect(
parseMcpListContent<PresentedNotification>(
unreadRes?.result?.content
).map((n) => n.id)
).toEqual([unread.id]);
const archivedRes = await callMcpTool(
server,
accessToken,
"list_notifications",
{ filters: [{ field: "archivedAt", operator: "isNotNull" }] }
);
expect(
parseMcpListContent<PresentedNotification>(
archivedRes?.result?.content
).map((n) => n.id)
).toEqual([archived.id]);
});
it("supports groups and relative dates in filters", async () => {
const { user, accessToken } = await buildOAuthUser();
const mention = await buildNotification({
teamId: user.teamId,
userId: user.id,
event: NotificationEventType.MentionedInDocument,
});
const comment = await buildNotification({
teamId: user.teamId,
userId: user.id,
event: NotificationEventType.CreateComment,
});
await buildNotification({
teamId: user.teamId,
userId: user.id,
event: NotificationEventType.UpdateDocument,
});
const res = await callMcpTool(server, accessToken, "list_notifications", {
filters: [
{
operator: "OR",
filters: [
{
field: "event",
operator: "eq",
value: NotificationEventType.MentionedInDocument,
},
{
field: "event",
operator: "eq",
value: NotificationEventType.CreateComment,
},
],
},
{ field: "createdAt", operator: "gte", value: "-P1D" },
],
});
const ids = parseMcpListContent<PresentedNotification>(
res?.result?.content
).map((n) => n.id);
expect(ids.sort()).toEqual([mention.id, comment.id].sort());
});
it("rejects unknown filter fields", async () => {
const { accessToken } = await buildOAuthUser();
const res = await callMcpTool(server, accessToken, "list_notifications", {
filters: [{ field: "userId", operator: "eq", value: "x" }],
});
expect(res?.error ?? res?.result?.isError).toBeTruthy();
});
});
describe("update_notifications", () => {
it("marks notifications as read and archived", async () => {
const { user, accessToken } = await buildOAuthUser();
const first = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const second = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "update_notifications", {
ids: [first.id, second.id],
read: true,
archived: true,
});
expect(res?.result?.isError).toBeFalsy();
for (const notification of [first, second]) {
await notification.reload();
expect(notification.viewedAt).not.toBeNull();
expect(notification.archivedAt).not.toBeNull();
}
});
it("marks a notification unread without changing archived state", async () => {
const { user, accessToken } = await buildOAuthUser();
const archivedAt = new Date();
const notification = await buildNotification({
teamId: user.teamId,
userId: user.id,
viewedAt: new Date(),
archivedAt,
});
await callMcpTool(server, accessToken, "update_notifications", {
ids: [notification.id],
read: false,
});
await notification.reload();
expect(notification.viewedAt).toBeNull();
expect(notification.archivedAt).toEqual(archivedAt);
});
it("does not update another user's notifications", async () => {
const { user, accessToken } = await buildOAuthUser();
const own = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const other = await buildNotification({ teamId: user.teamId });
const res = await callMcpTool(server, accessToken, "update_notifications", {
ids: [own.id, other.id],
read: true,
});
expect(res?.result?.isError).toBe(true);
const [ownAfter, otherAfter] = await Promise.all([
Notification.findByPk(own.id, { rejectOnEmpty: true }),
Notification.findByPk(other.id, { rejectOnEmpty: true }),
]);
expect(ownAfter.viewedAt).toBeNull();
expect(otherAfter.viewedAt).toBeNull();
});
it("requires read or archived", async () => {
const { user, accessToken } = await buildOAuthUser();
const notification = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "update_notifications", {
ids: [notification.id],
});
expect(res?.result?.isError).toBe(true);
});
it("is not available to a read-only token", async () => {
const user = await buildUser();
const auth = await buildOAuthAuthentication({ user, scope: [Scope.Read] });
const notification = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(
server,
auth.accessToken!,
"update_notifications",
{ ids: [notification.id], read: true }
);
expect(res?.error).toMatchObject({ code: -32602 });
});
});