Files
outline/server/commands/documentMover.test.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

367 lines
11 KiB
TypeScript

import { Collection } from "@server/models";
import Pin from "@server/models/Pin";
import {
buildDocument,
buildDraftDocument,
buildCollection,
buildPersonalCollection,
buildTeam,
buildUser,
} from "@server/test/factories";
import documentMover from "./documentMover";
import { withContext } from "@server/test/support";
describe("documentMover", () => {
it("should move within 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,
collectionId: collection.id,
teamId: team.id,
});
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: collection.id,
})
);
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(1);
});
it("should succeed when not in source collection documentStructure", 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,
collectionId: collection.id,
teamId: team.id,
});
const newDocument = await buildDocument({
parentDocumentId: document.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.createdById,
title: "Child document",
text: "content",
});
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: collection.id,
parentDocumentId: undefined,
index: 0,
})
);
expect(response.collections[0].documentStructure![0].children[0].id).toBe(
newDocument.id
);
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(1);
expect(response.documents[0].collection?.id).toEqual(collection.id);
expect(response.documents[0].updatedBy.id).toEqual(user.id);
});
it("should move with children", 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,
collectionId: collection.id,
teamId: team.id,
});
const newDocument = await buildDocument({
parentDocumentId: document.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.createdById,
title: "Child document",
text: "content",
});
await collection.addDocumentToStructure(newDocument);
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: collection.id,
parentDocumentId: undefined,
index: 0,
})
);
expect(response.collections[0].documentStructure![0].children[0].id).toBe(
newDocument.id
);
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(1);
expect(response.documents[0].collection?.id).toEqual(collection.id);
expect(response.documents[0].updatedBy.id).toEqual(user.id);
});
it("should move with children to another 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,
collectionId: collection.id,
teamId: team.id,
});
const newCollection = await buildCollection({
teamId: collection.teamId,
});
const newDocument = await buildDocument({
parentDocumentId: document.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.createdById,
title: "Child document",
text: "content",
});
await collection.addDocumentToStructure(newDocument);
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: newCollection.id,
parentDocumentId: undefined,
index: 0,
})
);
// check document ids where updated
await newDocument.reload();
expect(newDocument.collectionId).toBe(newCollection.id);
// check collection structure updated
expect(response.collections[0].id).toBe(collection.id);
expect(response.collections[1].id).toBe(newCollection.id);
expect(response.collections[1].documentStructure![0].children[0].id).toBe(
newDocument.id
);
expect(response.collections.length).toEqual(2);
expect(response.documents.length).toEqual(2);
expect(response.documents[0].collection?.id).toEqual(newCollection.id);
expect(response.documents[0].updatedBy.id).toEqual(user.id);
expect(response.documents[1].collection?.id).toEqual(newCollection.id);
expect(response.documents[1].updatedBy.id).toEqual(user.id);
});
it("should remove associated collection pin if moved to another 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,
collectionId: collection.id,
teamId: team.id,
});
const newCollection = await buildCollection({
teamId: collection.teamId,
});
await Pin.create({
createdById: user.id,
collectionId: collection.id,
documentId: document.id,
teamId: collection.teamId,
});
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: newCollection.id,
parentDocumentId: undefined,
index: 0,
})
);
const pinCount = await Pin.count({
where: {
teamId: collection.teamId,
},
});
expect(pinCount).toBe(0);
// check collection structure updated
expect(response.collections[0].id).toBe(collection.id);
expect(response.collections[1].id).toBe(newCollection.id);
expect(response.collections.length).toEqual(2);
expect(response.documents.length).toEqual(1);
expect(response.documents[0].collection?.id).toEqual(newCollection.id);
expect(response.documents[0].updatedBy.id).toEqual(user.id);
});
it("should remove collection pins of child documents if moved to another 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,
collectionId: collection.id,
teamId: team.id,
});
const child = await buildDocument({
userId: user.id,
collectionId: collection.id,
parentDocumentId: document.id,
teamId: team.id,
});
const newCollection = await buildCollection({
teamId: team.id,
});
await Pin.create({
createdById: user.id,
collectionId: collection.id,
documentId: child.id,
teamId: team.id,
});
const homePin = await Pin.create({
createdById: user.id,
collectionId: null,
documentId: child.id,
teamId: team.id,
});
await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: newCollection.id,
parentDocumentId: undefined,
index: 0,
})
);
const pins = await Pin.findAll({ where: { teamId: team.id } });
expect(pins.map((pin) => pin.id)).toEqual([homePin.id]);
});
it("should remove all pins if moved to a personal 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,
collectionId: collection.id,
teamId: team.id,
});
const child = await buildDocument({
userId: user.id,
collectionId: collection.id,
parentDocumentId: document.id,
teamId: team.id,
});
const personal = await buildPersonalCollection(user);
for (const documentId of [document.id, child.id]) {
await Pin.create({
createdById: user.id,
collectionId: null,
documentId,
teamId: team.id,
});
}
await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: personal.id,
parentDocumentId: undefined,
index: 0,
})
);
expect(await Pin.count({ where: { teamId: team.id } })).toBe(0);
});
it("should publish a draft moved to a personal 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 buildDraftDocument({
userId: user.id,
collectionId: collection.id,
teamId: team.id,
});
const personal = await buildPersonalCollection(user);
const existing = await buildDocument({
userId: user.id,
collectionId: personal.id,
teamId: team.id,
});
await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: personal.id,
})
);
await document.reload();
expect(document.collectionId).toEqual(personal.id);
expect(document.publishedAt).toBeTruthy();
const reloaded = await Collection.findByPk(personal.id, {
includeDocumentStructure: true,
});
expect(reloaded?.documentStructure?.map((node) => node.id)).toEqual([
existing.id,
document.id,
]);
});
it("should detach document from collection and move it to drafts", 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,
collectionId: collection.id,
teamId: team.id,
});
const response = await withContext(user, (ctx) =>
documentMover(ctx, {
document,
collectionId: null,
index: 0,
})
);
expect(response.collections[0].id).toBe(collection.id);
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(1);
expect(response.documents[0].collection).toBeNull();
expect(response.documents[0].updatedBy.id).toEqual(user.id);
expect(response.documents[0].publishedAt).toBeNull();
});
});