Keep library deletes when a still file is locked, and match LoRAs by engine folder.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
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',
|
||||
'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()
|
||||
let writes = 0
|
||||
let stored = null
|
||||
function readCatalog() { return stored }
|
||||
function writeCatalog(_owner, catalog) {
|
||||
stored = catalog === undefined ? _owner : catalog
|
||||
writes += 1
|
||||
}
|
||||
const api = new Function(
|
||||
'rmSync',
|
||||
'existsSync',
|
||||
'console',
|
||||
'stillPath',
|
||||
'stillThumbFile',
|
||||
'stillPreviewFile',
|
||||
'clipDir',
|
||||
'trackDir',
|
||||
'createError',
|
||||
'stillsInSameFamily',
|
||||
'mutate',
|
||||
'readCatalog',
|
||||
'writeCatalog',
|
||||
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents };`
|
||||
)(
|
||||
(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
|
||||
)
|
||||
return {
|
||||
api,
|
||||
files,
|
||||
thrown,
|
||||
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)
|
||||
})
|
||||
Reference in New Issue
Block a user