Stop a folder wipe from restoring the pre-wipe library catalog.
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>
This commit is contained in:
@@ -9,6 +9,8 @@ const names = [
|
||||
'safeRm',
|
||||
'removeStillFiles',
|
||||
'detachDraftStills',
|
||||
'sameFolder',
|
||||
'emptyFolderRows',
|
||||
'pruneMissingStillMasters',
|
||||
'catalogWithPrunedStills',
|
||||
'deleteStill',
|
||||
@@ -24,6 +26,8 @@ const js = ts.transpileModule(functions, { compilerOptions: { target: ts.ScriptT
|
||||
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 }
|
||||
@@ -31,6 +35,7 @@ function harness() {
|
||||
stored = catalog === undefined ? _owner : catalog
|
||||
writes += 1
|
||||
}
|
||||
function invalidateCatalogCache(owner) { invalidated.push(owner) }
|
||||
const api = new Function(
|
||||
'rmSync',
|
||||
'existsSync',
|
||||
@@ -45,7 +50,9 @@ function harness() {
|
||||
'mutate',
|
||||
'readCatalog',
|
||||
'writeCatalog',
|
||||
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents };`
|
||||
'catalogEpoch',
|
||||
'invalidateCatalogCache',
|
||||
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents, emptyFolderRows };`
|
||||
)(
|
||||
(path) => {
|
||||
if ([...thrown].some(id => String(path).includes(id))) throw new Error('EPERM')
|
||||
@@ -67,12 +74,15 @@ function harness() {
|
||||
return result
|
||||
},
|
||||
readCatalog,
|
||||
writeCatalog
|
||||
writeCatalog,
|
||||
catalogEpoch,
|
||||
invalidateCatalogCache
|
||||
)
|
||||
return {
|
||||
api,
|
||||
files,
|
||||
thrown,
|
||||
invalidated,
|
||||
get writes() { return writes },
|
||||
set writes(value) { writes = value },
|
||||
load(catalog) { stored = catalog }
|
||||
@@ -132,4 +142,52 @@ test('wipeFolderContents still clears the folder when a file remove throws', asy
|
||||
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'])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user