Files
outline/server/queues/tasks/ExpireDocumentsInTrashTask.test.ts
T
Claude 597acedf0b fix: Complete customizable document retention
Follow-up fixes to the retention feature.

The superseded CleanupDeletedDocumentsTask was still registered and ran
hourly, hard deleting documents 30 days after `deletedAt` regardless of the
team's configured period. Any retention longer than 30 days — including
infinite — had no effect.

The `destroyedAt` column was declared with a bare `@Column`. Decorator
metadata is disabled in the server build, so sequelize-typescript could not
infer its type and threw while loading the Document model.

Documents pending permanent deletion stayed readable and restorable through
`documents.info`, `documents.restore` and the MCP restore tool. They are now
treated as if they no longer exist.

Also:

- Add trash and data retention controls to workspace security settings, the
  only way to configure either was to call the API directly.
- Derive the retention tranches to process from the periods teams actually
  use, so a team holding a period that is no longer offered is not skipped.
- Bound the trash expiry task by `limit`, it previously ran an unbounded
  UPDATE across the whole documents table every hour.
- Match team preferences on JSON text rather than casting to int, so one bad
  value cannot fail the query for every other team in the batch.
- Derive the API validation schema from the same presets the UI offers.
- Send `ASC` rather than `asc` from the trash list, the sort direction was
  silently coerced back to descending.
- Move the retention badge in with the other document badges, pluralize it,
  and never show a negative day count.
- Drop the unused `Template.destroyedAt` computed, which returned a future
  date under a name meaning the opposite on the sibling model.
- Rename the migration to match its position in the sequence and the column
  it adds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U3kXbDxLMJesYLM5E4P6HQ
2026-08-16 14:36:32 -04:00

95 lines
2.6 KiB
TypeScript

import { buildTeam } from "@server/test/factories";
import { TeamPreferenceDefaults } from "@shared/constants";
import { TeamPreference } from "@shared/types";
import ExpireDocumentsInTrashTask from "./ExpireDocumentsInTrashTask";
import ExpireDocumentsInTrashByRetentionTask from "./ExpireDocumentsInTrashByRetentionTask";
const props = {
limit: 100,
partition: {
partitionIndex: 0,
partitionCount: 1,
},
};
const defaultRetentionDays = TeamPreferenceDefaults[
TeamPreference.TrashRetentionDays
] as number;
describe("ExpireDocumentsInTrashTask", () => {
it("should schedule worker tasks for default and custom retention periods", async () => {
const scheduleSpy = vi.spyOn(
ExpireDocumentsInTrashByRetentionTask.prototype,
"schedule"
);
// Team with custom retention
const teamCustom = await buildTeam();
const customDays = 7;
teamCustom.setPreference(TeamPreference.TrashRetentionDays, customDays);
await teamCustom.save();
const task = new ExpireDocumentsInTrashTask();
await task.perform(props);
// Verify that the default retention task was scheduled
expect(scheduleSpy).toHaveBeenCalledWith(
expect.objectContaining({
retentionDays: defaultRetentionDays,
partition: props.partition,
})
);
// Verify that the custom retention task was scheduled.
expect(scheduleSpy).toHaveBeenCalledWith(
expect.objectContaining({
limit: props.limit,
retentionDays: customDays,
partition: props.partition,
})
);
scheduleSpy.mockRestore();
});
it("should always schedule a worker for the default retention period", async () => {
const scheduleSpy = vi.spyOn(
ExpireDocumentsInTrashByRetentionTask.prototype,
"schedule"
);
const task = new ExpireDocumentsInTrashTask();
await task.perform(props);
expect(scheduleSpy).toHaveBeenCalledWith(
expect.objectContaining({
retentionDays: defaultRetentionDays,
partition: props.partition,
})
);
scheduleSpy.mockRestore();
});
it("should not schedule a worker for infinite retention", async () => {
const scheduleSpy = vi.spyOn(
ExpireDocumentsInTrashByRetentionTask.prototype,
"schedule"
);
const team = await buildTeam();
team.setPreference(TeamPreference.TrashRetentionDays, 0);
await team.save();
const task = new ExpireDocumentsInTrashTask();
await task.perform(props);
const scheduled = scheduleSpy.mock.calls.map(
([{ retentionDays }]) => retentionDays
);
expect(scheduled).not.toContain(0);
scheduleSpy.mockRestore();
});
});