85 lines
3.8 KiB
JavaScript
85 lines
3.8 KiB
JavaScript
'use strict'
|
|
|
|
const assert = require('assert')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const vm = require('vm')
|
|
|
|
const root = path.resolve(__dirname, '..')
|
|
const sandbox = {}
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'gallery-audit.js'), 'utf8'), sandbox)
|
|
const A = sandbox.IWBGalleryAudit
|
|
|
|
// 1) 三态判定:原图 / 仅缩略图 / 全丢
|
|
const orig = A.classify({ name: 'a.png', url: 'http://x/a.png', origIdb: 'out_orig_a.png', idb: 'out_a.png' }, { orig: true, thumb: true, origPath: 'images/gallery/aaa.png' })
|
|
assert.strictEqual(orig.state, 'orig')
|
|
assert.strictEqual(orig.label, '原图 OK')
|
|
const thumb = A.classify({ name: 'b.png', url: 'http://x/b.png' }, { orig: false, thumb: true, origPath: '', origKeyKnown: true })
|
|
assert.strictEqual(thumb.state, 'thumb')
|
|
assert.ok(/原图未落盘/.test(thumb.reason))
|
|
assert.strictEqual(thumb.origKey, 'out_orig_b.png')
|
|
const lost = A.classify({ name: 'c.png', url: 'http://x/c.png' }, { orig: false, thumb: false })
|
|
assert.strictEqual(lost.state, 'lost')
|
|
assert.ok(/只剩远程 URL/.test(lost.reason))
|
|
// 登记了但磁盘没有:原因必须与「从未写」区分开
|
|
const missingFile = A.classify({ name: 'd.png' }, { orig: false, thumb: true, origPath: 'images/gallery/ddd.png', origKeyKnown: true })
|
|
assert.ok(/文件缺失/.test(missingFile.reason))
|
|
// 只在磁盘有原图、没有缩略图:仍算已落盘
|
|
const origOnly = A.classify({ name: 'e.png' }, { orig: true, thumb: false })
|
|
assert.strictEqual(origOnly.state, 'orig')
|
|
|
|
const sum = A.summarize([orig, thumb, lost, missingFile, origOnly])
|
|
assert.deepStrictEqual({ total: sum.total, orig: sum.orig, thumb: sum.thumb, lost: sum.lost }, { total: 5, orig: 2, thumb: 2, lost: 1 })
|
|
|
|
// 2) run:桌面端以磁盘事实为准,并找出未被索引引用的原图
|
|
const index = [
|
|
{ name: 'x.png', url: 'https://cdn/x.png', origIdb: 'out_orig_x.png', idb: 'out_x.png' },
|
|
{ name: 'y.png', url: 'https://cdn/y.png', idb: 'out_y.png' },
|
|
]
|
|
const fakeFile = {
|
|
async galleryAudit () {
|
|
return {
|
|
ok: true,
|
|
dir: '/tmp/iwb',
|
|
rows: [
|
|
{ name: 'x.png', orig: true, thumb: true, origPath: 'images/gallery/x.png', thumbPath: 'images/thumbs/x.jpg', origKey: true },
|
|
{ name: 'y.png', orig: false, thumb: true, origPath: '', thumbPath: 'images/thumbs/y.jpg', origKey: false },
|
|
],
|
|
galleryFiles: ['x.png', 'orphan.png'],
|
|
}
|
|
},
|
|
}
|
|
;(async () => {
|
|
const r = await A.run({ index, storageFile: fakeFile, idbKeys: [] })
|
|
assert.strictEqual(r.desktop, true)
|
|
assert.strictEqual(r.dir, '/tmp/iwb')
|
|
assert.strictEqual(r.summary.orig, 1)
|
|
assert.strictEqual(r.summary.thumb, 1)
|
|
assert.strictEqual(r.rows[1].reason.indexOf('未登记') >= 0, true)
|
|
assert.deepStrictEqual(r.orphans, ['orphan.png'])
|
|
|
|
// 3) 浏览器路径:只看键集合
|
|
const web = await A.run({ index, storageFile: null, idbKeys: ['out_orig_x.png', 'out_x.png', 'out_y.png'] })
|
|
assert.strictEqual(web.desktop, false)
|
|
assert.strictEqual(web.summary.orig, 1)
|
|
assert.strictEqual(web.summary.thumb, 1)
|
|
|
|
// 4) 会话计数:只认 out_orig_,失败原因聚合
|
|
A.resetSession()
|
|
A.noteWrite('out_orig_a.png', true)
|
|
A.noteWrite('out_orig_b.png', false, 'quota exceeded')
|
|
A.noteWrite('out_c.png', false, '忽略:不是原图')
|
|
const st = A.stats()
|
|
assert.strictEqual(st.origOk, 1)
|
|
assert.strictEqual(st.origFail, 1)
|
|
assert.strictEqual(st.reasons[0].reason, 'quota exceeded')
|
|
const text = A.report(Object.assign({}, r, { session: A.stats() }))
|
|
assert.ok(/图库落盘自检/.test(text))
|
|
assert.ok(/原图 OK 1/.test(text))
|
|
assert.ok(/仅缩略图 1/.test(text))
|
|
assert.ok(/失败 1/.test(text))
|
|
assert.ok(!/https:\/\//.test(text), '报告不得包含远程 URL')
|
|
|
|
console.log('gallery-audit: 全部通过')
|
|
})().catch((e) => { console.error(e); process.exit(1) })
|