feat(storage): add team auto-archive assets policy and background job (#516)

* feat(storage): add team auto-archive assets policy and background job

* fix(storage): address code review feedback on lock safety, starvation, and permissions

* docs: update readme and expand archive storage documentation with auto-archive details

* feat(storage): log info message on auto-archive job start and finish with archived count

* style(storage): stack age threshold and grace period vertically with updated grace period description

* fix(storage): only log auto-archive info messages when candidate assets are found to process
This commit is contained in:
Yiling-J
2026-11-04 14:40:44 +08:00
committed by GitHub
parent ba3c6dbfee
commit 2ecbd49d18
23 changed files with 1805 additions and 39 deletions
+84
View File
@@ -1,3 +1,4 @@
import * as path from 'path'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { teamService } from '@shumai/core/src/team/team'
@@ -388,6 +389,13 @@ describe('TeamService', () => {
username: undefined,
},
appearance: { hideAgent: false },
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
await teamService.updateSettings(team.id, 'theme', 'dark')
@@ -417,6 +425,13 @@ describe('TeamService', () => {
username: undefined,
},
appearance: { hideAgent: false },
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'multi')
@@ -456,6 +471,13 @@ describe('TeamService', () => {
},
appearance: { hideAgent: true },
semanticSearchEnabled: false,
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
})
@@ -633,4 +655,66 @@ describe('TeamService', () => {
delete process.env.ENABLE_EMBEDDING_AGENT
}
})
it('rejects enabling auto-archive when archive storage is not configured', async () => {
delete process.env.ARCHIVE_STORAGE_BACKEND
const owner = await prisma.user.create({
data: { name: 'Owner AA', email: 'owner-aa@example.com', password: 'pw' },
})
const team = await teamService.createTeam(owner, { name: 'AA Team' })
await expect(
teamService.updateSettings(team.id, 'autoArchive', {
enabled: true,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
}),
).rejects.toThrow('Cannot enable auto archive when archive storage is not configured')
await expect(teamService.updateSettings(team.id, 'autoArchive.enabled', true)).rejects.toThrow(
'Cannot enable auto archive when archive storage is not configured',
)
})
it('allows updating auto-archive settings when archive storage is configured', async () => {
const saved = { ...process.env }
process.env.ARCHIVE_STORAGE_BACKEND = 'local'
process.env.ARCHIVE_LOCAL_ROOT = '/tmp/shumai-archive-test-team'
try {
const owner = await prisma.user.create({
data: { name: 'Owner AA 2', email: 'owner-aa2@example.com', password: 'pw' },
})
const team = await teamService.createTeam(owner, { name: 'AA Team 2' })
await teamService.updateSettings(team.id, 'autoArchive', {
enabled: true,
method: 'after_created',
days: 45,
postRestoreGraceDays: 15,
})
const settings = await teamService.getSettings(team.id)
expect(settings.autoArchive).toEqual({
enabled: true,
method: 'after_created',
days: 45,
postRestoreGraceDays: 15,
})
expect(settings.archiveStorage).toEqual({
enabled: true,
backend: 'local',
localRoot: path.resolve('/tmp/shumai-archive-test-team'),
})
// Non-admin call should mask backend/root details
const readerSettings = await teamService.getSettings(team.id, { isAdmin: false })
expect(readerSettings.archiveStorage).toEqual({
enabled: true,
})
} finally {
process.env = saved
}
})
})