Files
outline/server/models/Document.test.ts
T
Claude 4717c029aa feat: Document verification and freshness tracking
Adds a verification lifecycle to documents. A user with admin-level access
to a document (document admin membership or team admin) can mark it as
verified, vouching that its content is accurate. Verification carries an
optional expiry deadline derived from a re-verification interval resolved
through a document → collection → team preference inheritance chain.

- documents.verify / documents.unverify API endpoints, gated by new
  verify/unverify abilities
- verificationInterval override on documents.update and a collection
  default on collections.update (team admin only), plus a team-wide
  default via the verificationInterval team preference
- verificationStatus filter (verified/unverified/expired/expiring) on
  documents.list
- daily VerificationExpirySweepTask emits idempotent
  documents.verification_expired events without mutating documents;
  expiry is derived from the materialized verificationExpiresAt deadline
- notification and email to the verifier when their verification lapses;
  suppressed when the verifier is deleted, suspended, or lost access
- verification state excluded from duplicates and public shares;
  presenter exposes verifiedAt/verifiedById/verificationExpiresAt/
  verificationInterval and a derived editedSinceVerification flag

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mb5McXaNbEDyyCZXPAdGAY
2026-07-05 15:50:13 +00:00

567 lines
17 KiB
TypeScript

import { EmptyResultError } from "sequelize";
import { CollectionPermission } from "@shared/types";
import slugify from "@shared/utils/slugify";
import { parser } from "@server/editor";
import Document from "@server/models/Document";
import {
buildDocument,
buildDraftDocument,
buildCollection,
buildComment,
buildResolvedComment,
buildTeam,
buildUser,
buildGuestUser,
} from "@server/test/factories";
import { withAPIContext } from "@server/test/support";
import UserMembership from "./UserMembership";
beforeEach(() => {
vi.resetAllMocks();
});
describe("#getSummary", () => {
test("should strip markdown", async () => {
const document = await buildDocument({
version: 1,
text: `*paragraph*
paragraph 2`,
});
expect(document.getSummary()).toBe("paragraph");
});
test("should strip title when no version", async () => {
const document = await buildDocument({
version: 0,
text: `# Heading
*paragraph*`,
});
expect(document.getSummary()).toBe("paragraph");
});
});
describe("#destroyWithCtx", () => {
test("should soft delete and set last modified", async () => {
const document = await buildDocument();
const user = await buildUser();
await withAPIContext(user, (ctx) => document.destroyWithCtx(ctx));
const newDocument = await Document.findByPk(document.id, {
paranoid: false,
});
expect(newDocument?.lastModifiedById).toBe(user.id);
expect(newDocument?.deletedAt).toBeTruthy();
});
test("should soft delete archived", async () => {
const document = await buildDocument({
archivedAt: new Date(),
});
const user = await buildUser();
await withAPIContext(user, (ctx) => document.destroyWithCtx(ctx));
const newDocument = await Document.findByPk(document.id, {
paranoid: false,
});
expect(newDocument?.lastModifiedById).toBe(user.id);
expect(newDocument?.deletedAt).toBeTruthy();
});
test("should soft delete archived document in an archived collection", async () => {
const user = await buildUser();
const collection = await buildCollection({
archivedAt: new Date(),
createdById: user.id,
teamId: user.teamId,
});
const document = await buildDocument({
archivedAt: new Date(),
collectionId: collection.id,
userId: user.id,
teamId: user.teamId,
});
await collection.addDocumentToStructure(document, 0);
await withAPIContext(user, (ctx) => document.destroyWithCtx(ctx));
const [newDocument, newCollection] = await Promise.all([
document.reload({ paranoid: false }),
collection.reload(),
]);
expect(newDocument?.lastModifiedById).toEqual(user.id);
expect(newDocument?.deletedAt).toBeTruthy();
expect(newCollection?.documentStructure).toEqual([]);
});
it("should delete draft without collection", async () => {
const user = await buildUser();
const document = await buildDraftDocument();
await withAPIContext(user, (ctx) => document.destroyWithCtx(ctx));
const deletedDocument = await Document.findByPk(document.id, {
paranoid: false,
});
expect(deletedDocument?.lastModifiedById).toBe(user.id);
expect(deletedDocument?.deletedAt).toBeTruthy();
});
});
describe("#save", () => {
test("should have empty previousTitles by default", async () => {
const document = await buildDocument();
expect(document.previousTitles).toBe(null);
});
test("should include previousTitles on save", async () => {
const document = await buildDocument();
document.title = "test";
await document.save();
expect(document.previousTitles.length).toBe(1);
});
test("should not duplicate previousTitles", async () => {
const document = await buildDocument();
document.title = "test";
await document.save();
document.title = "example";
await document.save();
document.title = "test";
await document.save();
expect(document.previousTitles.length).toBe(3);
});
});
describe("#findAllChildDocumentIds", () => {
test("should return empty array if no 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,
teamId: team.id,
collectionId: collection.id,
title: "test",
});
const results = await document.findAllChildDocumentIds();
expect(results.length).toBe(0);
});
test("should return nested child document ids", 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,
teamId: team.id,
collectionId: collection.id,
title: "test",
});
const document2 = await buildDocument({
userId: user.id,
teamId: team.id,
collectionId: collection.id,
parentDocumentId: document.id,
title: "test",
});
const document3 = await buildDocument({
userId: user.id,
teamId: team.id,
collectionId: collection.id,
parentDocumentId: document2.id,
title: "test",
});
const results = await document.findAllChildDocumentIds();
expect(results.length).toBe(2);
expect(results[0]).toBe(document2.id);
expect(results[1]).toBe(document3.id);
});
});
describe("#findByPk", () => {
test("should return document when urlId is correct", async () => {
const document = await buildDocument();
const id = `${slugify(document.title)}-${document.urlId}`;
const response = await Document.findByPk(id);
expect(response?.id).toBe(document.id);
});
test("should return document when urlId is given without the slug prefix", async () => {
const document = await buildDocument();
const id = document.urlId;
const response = await Document.findByPk(id);
expect(response?.id).toBe(document.id);
});
it("should test with rejectOnEmpty flag", async () => {
const user = await buildUser();
const document = await buildDocument({
teamId: user.teamId,
createdById: user.id,
});
await expect(
Document.findByPk(document.id, {
userId: user.id,
rejectOnEmpty: true,
})
).resolves.not.toBeNull();
await expect(
Document.findByPk(document.urlId, {
userId: user.id,
rejectOnEmpty: true,
})
).resolves.not.toBeNull();
await expect(
Document.findByPk("0e8280ea-7b4c-40e5-98ba-ec8a2f00f5e8", {
userId: user.id,
rejectOnEmpty: true,
})
).rejects.toThrow(EmptyResultError);
});
});
describe("findByIds", () => {
test("should return documents by ids", async () => {
const document1 = await buildDocument();
const document2 = await buildDocument();
const documents = await Document.findByIds([document1.id, document2.id]);
expect(documents.length).toBe(2);
});
test("should return documents filtered to user access", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document1 = await buildDocument({ teamId: team.id });
const document2 = await buildDocument({ teamId: team.id });
const document3 = await buildDocument();
const documents = await Document.findByIds(
[document1.id, document2.id, document3.id],
{
userId: user.id,
}
);
expect(documents.length).toBe(2);
});
test("should return documents filtered to private collection access", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
const document1 = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
const document2 = await buildDocument({ teamId: team.id });
const document3 = await buildDocument();
const documents = await Document.findByIds(
[document1.id, document2.id, document3.id],
{
userId: user.id,
}
);
expect(documents.length).toBe(1);
});
test("should return documents filtered to guest access", async () => {
const team = await buildTeam();
const user = await buildGuestUser({ teamId: team.id });
const document1 = await buildDocument({ teamId: team.id });
const collection = await buildCollection({ teamId: team.id });
await UserMembership.create({
createdById: user.id,
collectionId: collection.id,
userId: user.id,
permission: CollectionPermission.Read,
});
const document2 = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
const document3 = await buildDocument();
const documents = await Document.findByIds(
[document1.id, document2.id, document3.id],
{
userId: user.id,
}
);
expect(documents.length).toBe(1);
});
});
describe("tasks", () => {
test("should return tasks keys set to 0 if check items isn't present", async () => {
const document = await buildDocument({
text: `text`,
});
const tasks = document.tasks;
expect(tasks.completed).toBe(0);
expect(tasks.total).toBe(0);
});
test("should return tasks keys set to 0 if the text contains broken check items", async () => {
const document = await buildDocument({
text: `
- [x ] test
- [ x ] test
- [ ] test`,
});
const tasks = document.tasks;
expect(tasks.completed).toBe(0);
expect(tasks.total).toBe(0);
});
test("should return tasks", async () => {
const document = await buildDocument({
text: `
- [x] list item
- [ ] list item`,
});
const tasks = document.tasks;
expect(tasks.completed).toBe(1);
expect(tasks.total).toBe(2);
});
test("should update tasks on save", async () => {
const document = await buildDocument({
text: `
- [x] list item
- [ ] list item`,
});
const tasks = document.tasks;
expect(tasks.completed).toBe(1);
expect(tasks.total).toBe(2);
document.content = parser
.parse(
`
- [x] list item
- [ ] list item
- [ ] list item`
)
?.toJSON();
await document.save();
const newTasks = document.tasks;
expect(newTasks.completed).toBe(1);
expect(newTasks.total).toBe(3);
});
});
describe("commentCount", () => {
it("returns 0 for a document with no comments", async () => {
const document = await buildDocument();
expect(await document.commentCount).toEqual(0);
});
it("counts unresolved threads and their replies", async () => {
const document = await buildDocument();
const thread = await buildComment({
documentId: document.id,
userId: document.createdById,
});
await buildComment({
documentId: document.id,
userId: document.createdById,
parentCommentId: thread.id,
});
expect(await document.commentCount).toEqual(2);
});
it("excludes resolved threads and their replies", async () => {
const document = await buildDocument();
const user = await buildUser({ teamId: document.teamId });
const resolved = await buildResolvedComment(user, {
documentId: document.id,
userId: user.id,
});
await buildComment({
documentId: document.id,
userId: user.id,
parentCommentId: resolved.id,
});
await buildComment({
documentId: document.id,
userId: user.id,
});
expect(await document.commentCount).toEqual(1);
});
it("invalidates the cached count when a comment is destroyed", async () => {
const document = await buildDocument();
const comment = await buildComment({
documentId: document.id,
userId: document.createdById,
});
await buildComment({
documentId: document.id,
userId: document.createdById,
});
expect(await document.commentCount).toEqual(2);
await comment.destroy();
expect(await document.commentCount).toEqual(1);
});
it("invalidates the cached count when a thread is resolved", async () => {
const document = await buildDocument();
const user = await buildUser({ teamId: document.teamId });
const thread = await buildComment({
documentId: document.id,
userId: user.id,
});
await buildComment({
documentId: document.id,
userId: user.id,
parentCommentId: thread.id,
});
// Prime the cache.
expect(await document.commentCount).toEqual(2);
thread.resolve(user);
await thread.save();
expect(await document.commentCount).toEqual(0);
});
it("invalidates the cached count when a resolved thread is unresolved", async () => {
const document = await buildDocument();
const user = await buildUser({ teamId: document.teamId });
const thread = await buildResolvedComment(user, {
documentId: document.id,
userId: user.id,
});
// Prime the cache (resolved thread is excluded).
expect(await document.commentCount).toEqual(0);
thread.unresolve();
await thread.save();
expect(await document.commentCount).toEqual(1);
});
});
describe("verification", () => {
describe("#resolveVerificationInterval", () => {
it("should prefer the document override over all other sources", async () => {
const team = await buildTeam({
preferences: { verificationInterval: 365 },
});
const collection = await buildCollection({
teamId: team.id,
verificationInterval: 180,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
verificationInterval: 30,
});
expect(await document.resolveVerificationInterval()).toEqual(30);
});
it("should fall back to the collection default", async () => {
const team = await buildTeam({
preferences: { verificationInterval: 365 },
});
const collection = await buildCollection({
teamId: team.id,
verificationInterval: 180,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
expect(await document.resolveVerificationInterval()).toEqual(180);
});
it("should fall back to the team preference", async () => {
const team = await buildTeam({
preferences: { verificationInterval: 365 },
});
const collection = await buildCollection({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
expect(await document.resolveVerificationInterval()).toEqual(365);
});
it("should return null when no interval is configured anywhere", async () => {
const document = await buildDocument();
expect(await document.resolveVerificationInterval()).toEqual(null);
});
});
describe("#verifyWithCtx", () => {
it("should set the verification fields and compute the deadline", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
userId: user.id,
verificationInterval: 90,
});
const updatedAt = document.updatedAt;
await withAPIContext(user, (ctx) => document.verifyWithCtx(ctx));
expect(document.verifiedAt).toBeTruthy();
expect(document.verifiedById).toEqual(user.id);
expect(document.verificationExpiresAt).toEqual(
new Date(document.verifiedAt!.getTime() + 90 * 24 * 60 * 60 * 1000)
);
// verifying must not count as an edit of the document
expect(document.updatedAt).toEqual(updatedAt);
});
it("should leave the deadline empty when no interval is configured", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
userId: user.id,
});
await withAPIContext(user, (ctx) => document.verifyWithCtx(ctx));
expect(document.verifiedAt).toBeTruthy();
expect(document.verificationExpiresAt).toEqual(null);
});
});
describe("#unverifyWithCtx", () => {
it("should clear verification state but keep the interval override", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
userId: user.id,
verificationInterval: 90,
});
await withAPIContext(user, (ctx) => document.verifyWithCtx(ctx));
await withAPIContext(user, (ctx) => document.unverifyWithCtx(ctx));
expect(document.verifiedAt).toEqual(null);
expect(document.verifiedById).toEqual(null);
expect(document.verificationExpiresAt).toEqual(null);
expect(document.verificationInterval).toEqual(90);
});
});
});