Files
outline/server/tools/collections.test.ts
T
Tom Moor d551ead073 feat: Filter DSL for collections.list (#13442)
* feat: Filter DSL for collections.list

Adds the structured `filters` expression language to collections.list,
matching documents.list and documents.search. Supported fields are name,
createdAt, updatedAt, archivedAt, createdById and permission, each validated
against its column type at the input layer.

Deprecates the ad-hoc `query` and `statusFilter` params, which now translate
into filter leaves so that one code path serves both shapes, and returns a 400
when a caller combines them with `filters`.

Note that `query` matching is no longer accent-insensitive, as the unaccent
comparison is replaced by the shared iLike conversion.

* fix: Make filter expressions assignable to a JSON request body

Filters are sent as a JSON body, but `FilterCondition` and `FilterGroup` were
interfaces, which TypeScript does not give an implicit index signature, and
`FilterValue` admitted a `Date` that JSON cannot carry. Both stores worked
around this by asserting the request body to JSONObject, which suppressed
checking of every other field as well.

Declare the two shapes as type aliases and drop `Date` from the value union so
a filter type-checks against the request body directly, and remove the
assertions from the collection and document stores.

* feat: Restrict filter fields to a set of accepted values

Add an optional `values` set to a filter field definition, checked against
scalar and array values at the input layer so that an unsupported value returns
a 400 rather than an empty result or, for a field backed by a Postgres enum, a
database error. A field with a fixed value set is matched exactly, so pattern
operators are rejected for it in the same way as for non-text columns.

Apply it to `permission` on collections.list.

* refactor: Build the list_collections name filter with the filter DSL

The MCP tool carried its own copy of the unaccent name comparison that
collections.list used before it moved to the DSL. Build the clause with
buildWhere instead, so both paths match names the same way, and drop the
replacements plumbing it needed.
2026-08-23 16:03:10 -04:00

154 lines
4.9 KiB
TypeScript

import { Collection } from "@server/models";
import { buildCollection, buildUser } from "@server/test/factories";
import { getTestServer } from "@server/test/support";
import {
buildOAuthUser,
callMcpTool,
parseMcpListContent,
} from "@server/test/McpHelper";
const server = getTestServer();
describe("collection tools", () => {
it("list_collections returns user collections", async () => {
const { user, accessToken } = await buildOAuthUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "list_collections");
const data = parseMcpListContent<{ id: string; url: string }>(
res?.result?.content
);
expect(data.length).toBeGreaterThanOrEqual(1);
const ids = data.map((c) => c.id);
expect(ids).toContain(collection.id);
const match = data.find((c) => c.id === collection.id);
expect(match).toBeDefined();
expect(match!.url).toMatch(/^https?:\/\//);
});
it("list_collections filters by name ignoring case", async () => {
const { user, accessToken } = await buildOAuthUser();
const collection = await buildCollection({
name: "Product Design",
teamId: user.teamId,
userId: user.id,
});
const other = await buildCollection({
name: "Something else",
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "list_collections", {
query: "duct des",
});
const data = parseMcpListContent<{ id: string }>(res?.result?.content);
const ids = data.map((c) => c.id);
expect(ids).toContain(collection.id);
expect(ids).not.toContain(other.id);
});
it("list_collections does not return collections from another team", async () => {
const { accessToken } = await buildOAuthUser();
const otherUser = await buildUser();
const otherCollection = await buildCollection({
teamId: otherUser.teamId,
userId: otherUser.id,
});
const res = await callMcpTool(server, accessToken, "list_collections");
const data = parseMcpListContent<{ id: string }>(res?.result?.content);
const ids = data.map((c: { id: string }) => c.id);
expect(ids).not.toContain(otherCollection.id);
});
it("create_collection creates with name, description, icon, color", async () => {
const { accessToken } = await buildOAuthUser();
const res = await callMcpTool(server, accessToken, "create_collection", {
name: "Test Collection",
description: "A **test** description",
icon: "rocket",
color: "#FF0000",
});
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
expect(data.success).toBe(true);
expect(data.name).toEqual("Test Collection");
expect(data.id).toBeDefined();
expect(data.url).toMatch(/^https?:\/\//);
const collection = await Collection.findByPk(data.id, {
rejectOnEmpty: true,
});
expect(collection.description).toEqual("A **test** description");
expect(collection.icon).toEqual("rocket");
expect(collection.color).toEqual("#FF0000");
expect(collection.permission).toEqual(null);
});
it("update_collection updates fields on existing collection", async () => {
const { user, accessToken } = await buildOAuthUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "update_collection", {
id: collection.id,
name: "Updated Name",
description: "Updated description",
});
const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}");
expect(data.success).toBe(true);
expect(data.name).toEqual("Updated Name");
expect(data.url).toMatch(/^https?:\/\//);
await collection.reload();
expect(collection.description).toEqual("Updated description");
});
it("update_collection 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 res = await callMcpTool(server, accessToken, "update_collection", {
id: collection.id,
});
expect(res?.result?.isError).toBe(true);
expect(res?.result?.content?.[0]?.text).toContain(
"The update resulted in no changes to the collection"
);
});
it("update_collection errors when provided fields are identical to the current collection", async () => {
const { user, accessToken } = await buildOAuthUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const res = await callMcpTool(server, accessToken, "update_collection", {
id: collection.id,
name: collection.name,
});
expect(res?.result?.isError).toBe(true);
expect(res?.result?.content?.[0]?.text).toContain(
"The update resulted in no changes to the collection"
);
});
});