Files
outline/server/presenters/document.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

91 lines
2.9 KiB
TypeScript

import { Document } from "@server/models";
import { presentDocuments } from "@server/presenters/document";
import {
buildCollection,
buildDocument,
buildUser,
} from "@server/test/factories";
import { withContext } from "@server/test/support";
import { sequelize } from "@server/storage/database";
describe("presentDocuments", () => {
describe("deletedBy", () => {
it("should resolve the deleting user in a single query", async () => {
const user = await buildUser();
const collection = await buildCollection({ teamId: user.teamId });
for (let i = 0; i < 3; i++) {
const document = await buildDocument({
teamId: user.teamId,
collectionId: collection.id,
});
await withContext(user, (ctx) => document.destroyWithCtx(ctx));
}
const documents = await Document.scope("withDrafts").findAll({
where: { collectionId: collection.id },
paranoid: false,
});
expect(documents.length).toEqual(3);
const queries: string[] = [];
const query = sequelize.query.bind(sequelize);
const spy = vi
.spyOn(sequelize, "query")
.mockImplementation((sql, options) => {
queries.push(typeof sql === "string" ? sql : sql.query);
return query(sql, options);
});
try {
await presentDocuments(undefined, documents);
} finally {
spy.mockRestore();
}
expect(documents.every((doc) => doc.deletedBy?.id === user.id)).toBe(
true
);
expect(queries.filter((sql) => /FROM "users"/.test(sql)).length).toEqual(
1
);
});
it("should not overwrite an already loaded deleting user", async () => {
const userA = await buildUser();
const userB = await buildUser({ teamId: userA.teamId });
const collection = await buildCollection({ teamId: userA.teamId });
const documentA = await buildDocument({
teamId: userA.teamId,
collectionId: collection.id,
});
const documentB = await buildDocument({
teamId: userA.teamId,
collectionId: collection.id,
});
await withContext(userA, (ctx) => documentA.destroyWithCtx(ctx));
await withContext(userB, (ctx) => documentB.destroyWithCtx(ctx));
// One document arrives with the association loaded, the other without.
const loaded = await Document.scope("withDrafts").findOne({
where: { id: documentA.id },
include: [{ association: "deletedBy", paranoid: false }],
paranoid: false,
rejectOnEmpty: true,
});
const unloaded = await Document.scope("withDrafts").findOne({
where: { id: documentB.id },
paranoid: false,
rejectOnEmpty: true,
});
await presentDocuments(undefined, [loaded, unloaded]);
expect(loaded.deletedBy?.id).toEqual(userA.id);
expect(unloaded.deletedBy?.id).toEqual(userB.id);
});
});
});