The in-memory catalog could outlive the write, so delete-all came back as thousands of empty tiles and a stale badge. Co-authored-by: Cursor <cursoragent@cursor.com>
194 lines
6.2 KiB
JavaScript
194 lines
6.2 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import ts from 'typescript'
|
|
|
|
const source = readFileSync(new URL('../server/utils/library.ts', import.meta.url), 'utf8')
|
|
const ast = ts.createSourceFile('library.ts', source, ts.ScriptTarget.Latest, true)
|
|
const names = [
|
|
'safeRm',
|
|
'removeStillFiles',
|
|
'detachDraftStills',
|
|
'sameFolder',
|
|
'emptyFolderRows',
|
|
'pruneMissingStillMasters',
|
|
'catalogWithPrunedStills',
|
|
'deleteStill',
|
|
'deleteStillFamily',
|
|
'wipeFolderContents'
|
|
]
|
|
const functions = ast.statements
|
|
.filter(node => ts.isFunctionDeclaration(node) && names.includes(node.name?.text || ''))
|
|
.map(node => node.getText(ast).replace(/^export /, ''))
|
|
.join('\n')
|
|
const js = ts.transpileModule(functions, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
|
|
|
|
function harness() {
|
|
const files = new Set()
|
|
const thrown = new Set()
|
|
const invalidated = []
|
|
const catalogEpoch = new Map()
|
|
let writes = 0
|
|
let stored = null
|
|
function readCatalog() { return stored }
|
|
function writeCatalog(_owner, catalog) {
|
|
stored = catalog === undefined ? _owner : catalog
|
|
writes += 1
|
|
}
|
|
function invalidateCatalogCache(owner) { invalidated.push(owner) }
|
|
const api = new Function(
|
|
'rmSync',
|
|
'existsSync',
|
|
'console',
|
|
'stillPath',
|
|
'stillThumbFile',
|
|
'stillPreviewFile',
|
|
'clipDir',
|
|
'trackDir',
|
|
'createError',
|
|
'stillsInSameFamily',
|
|
'mutate',
|
|
'readCatalog',
|
|
'writeCatalog',
|
|
'catalogEpoch',
|
|
'invalidateCatalogCache',
|
|
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents, emptyFolderRows };`
|
|
)(
|
|
(path) => {
|
|
if ([...thrown].some(id => String(path).includes(id))) throw new Error('EPERM')
|
|
files.delete(path)
|
|
},
|
|
(path) => files.has(path),
|
|
console,
|
|
(owner, id) => `${owner}/stills/${id}`,
|
|
(owner, id) => `${owner}/stills/${id}.thumb.jpg`,
|
|
(owner, id) => `${owner}/stills/${id}.preview.jpg`,
|
|
(owner, id) => `${owner}/clips/${id}`,
|
|
(owner, id) => `${owner}/tracks/${id}`,
|
|
(error) => Object.assign(new Error(error.statusMessage), error),
|
|
(catalog, still) => catalog.stills.filter(item => item.id === still.id || (still.familyId && item.familyId === still.familyId)),
|
|
async (_owner, fn) => {
|
|
const catalog = readCatalog()
|
|
const result = fn(catalog)
|
|
writeCatalog(catalog)
|
|
return result
|
|
},
|
|
readCatalog,
|
|
writeCatalog,
|
|
catalogEpoch,
|
|
invalidateCatalogCache
|
|
)
|
|
return {
|
|
api,
|
|
files,
|
|
thrown,
|
|
invalidated,
|
|
get writes() { return writes },
|
|
set writes(value) { writes = value },
|
|
load(catalog) { stored = catalog }
|
|
}
|
|
}
|
|
|
|
test('deleteStill drops the catalog row when the file remove fails', async () => {
|
|
const h = harness()
|
|
h.thrown.add('locked')
|
|
h.files.add('owner/stills/locked')
|
|
const catalog = {
|
|
stills: [{ id: 'locked', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
|
|
drafts: [{ id: 'd', stillId: 'locked', stillFilename: 'locked.png' }],
|
|
clips: [],
|
|
tracks: [],
|
|
folders: [{ id: 'inbox' }]
|
|
}
|
|
h.load(catalog)
|
|
h.files.add('owner/stills/keep')
|
|
await h.api.deleteStill('owner', 'locked')
|
|
assert.equal(h.writes, 1)
|
|
assert.deepEqual(catalog.stills.map(item => item.id), ['keep'])
|
|
assert.equal(catalog.drafts[0].stillId, undefined)
|
|
assert.equal(h.files.has('owner/stills/keep'), true)
|
|
})
|
|
|
|
test('pruneMissingStillMasters runs on delete and on catalog read', async () => {
|
|
const h = harness()
|
|
const catalog = {
|
|
stills: [{ id: 'gone', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
|
|
drafts: [{ id: 'd', stillId: 'gone' }],
|
|
clips: [],
|
|
tracks: [],
|
|
folders: [{ id: 'inbox' }]
|
|
}
|
|
h.load(structuredClone(catalog))
|
|
h.files.add('owner/stills/keep')
|
|
const page = h.api.catalogWithPrunedStills('owner')
|
|
assert.equal(h.writes, 1)
|
|
assert.deepEqual(page.stills.map(item => item.id), ['keep'])
|
|
assert.equal(page.drafts[0].stillId, undefined)
|
|
})
|
|
|
|
test('wipeFolderContents still clears the folder when a file remove throws', async () => {
|
|
const h = harness()
|
|
h.thrown.add('locked')
|
|
const catalog = {
|
|
stills: [{ id: 'locked', folderId: 'inbox' }],
|
|
clips: [{ id: 'clip', folderId: 'inbox' }],
|
|
tracks: [],
|
|
drafts: [],
|
|
folders: [{ id: 'inbox', name: 'Inbox' }]
|
|
}
|
|
h.load(catalog)
|
|
const result = await h.api.wipeFolderContents('owner', 'inbox')
|
|
assert.equal(result.deleted, 2)
|
|
assert.equal(catalog.stills.length, 0)
|
|
assert.equal(catalog.clips.length, 0)
|
|
assert.equal(h.writes, 1)
|
|
assert.deepEqual(h.invalidated, ['owner'])
|
|
})
|
|
|
|
test('wipeFolderContents leaves zero rows for that folder and does not touch another folder', async () => {
|
|
const h = harness()
|
|
const catalog = {
|
|
stills: [
|
|
{ id: 'a', folderId: 'inbox' },
|
|
{ id: 'b', folderId: 'other' }
|
|
],
|
|
clips: [{ id: 'c', folderId: 'inbox' }],
|
|
tracks: [
|
|
{ id: 't', folderId: 'inbox' },
|
|
{ id: 'u', folderId: 'other' }
|
|
],
|
|
drafts: [],
|
|
folders: [{ id: 'inbox', name: 'Inbox' }, { id: 'other', name: 'Other' }]
|
|
}
|
|
h.load(catalog)
|
|
h.files.add('owner/stills/a')
|
|
h.files.add('owner/stills/b')
|
|
const result = await h.api.wipeFolderContents('owner', 'inbox')
|
|
assert.equal(result.deleted, 3)
|
|
assert.deepEqual(catalog.stills.map(item => item.id), ['b'])
|
|
assert.deepEqual(catalog.clips, [])
|
|
assert.deepEqual(catalog.tracks.map(item => item.id), ['u'])
|
|
assert.deepEqual(h.invalidated, ['owner'])
|
|
})
|
|
|
|
test('pruneMissingStillMasters only drops and a second read does not put the still back', async () => {
|
|
const h = harness()
|
|
const catalog = {
|
|
stills: [{ id: 'gone', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
|
|
drafts: [],
|
|
clips: [],
|
|
tracks: [],
|
|
folders: [{ id: 'inbox' }]
|
|
}
|
|
h.load(catalog)
|
|
h.files.add('owner/stills/keep')
|
|
const before = new Set(catalog.stills.map(item => item.id))
|
|
const page = h.api.catalogWithPrunedStills('owner')
|
|
assert.deepEqual(page.stills.map(item => item.id), ['keep'])
|
|
for (const still of page.stills) assert.equal(before.has(still.id), true)
|
|
assert.ok(page.stills.length < before.size)
|
|
h.files.add('owner/stills/gone')
|
|
const again = h.api.catalogWithPrunedStills('owner')
|
|
assert.deepEqual(again.stills.map(item => item.id), ['keep'])
|
|
})
|