2020-11-24 23:37:10 +00:00
|
|
|
/* global it describe beforeEach */
|
|
|
|
|
2021-07-05 04:19:04 +01:00
|
|
|
import '../indexedDBShims.js'
|
2020-11-24 23:37:10 +00:00
|
|
|
import assert from 'assert'
|
|
|
|
import {
|
2021-03-16 00:25:13 +00:00
|
|
|
getCachedMediaFile, setCachedMediaFile, deleteCachedMediaFile, getAllCachedFileIds, setDeleteCachedMediaFilesAfter, DELETE_AFTER
|
2021-07-05 04:19:04 +01:00
|
|
|
} from '../../src/routes/_database/mediaUploadFileCache.js'
|
2020-11-24 23:37:10 +00:00
|
|
|
|
|
|
|
describe('test-database.js', function () {
|
|
|
|
this.timeout(60000)
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
for (const key of await getAllCachedFileIds()) {
|
|
|
|
await deleteCachedMediaFile(key)
|
|
|
|
}
|
2021-03-16 00:25:13 +00:00
|
|
|
setDeleteCachedMediaFilesAfter(DELETE_AFTER)
|
2020-11-24 23:37:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('can store media files', async () => {
|
|
|
|
await setCachedMediaFile('woot', 'woot')
|
|
|
|
const result = await getCachedMediaFile('woot')
|
|
|
|
assert.deepStrictEqual(result, 'woot')
|
|
|
|
await deleteCachedMediaFile('woot')
|
|
|
|
const result2 = await getCachedMediaFile('woot')
|
|
|
|
assert.deepStrictEqual(result2, undefined)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if you set() the same id twice', async () => {
|
|
|
|
await setCachedMediaFile('woot', 'woot')
|
|
|
|
await setCachedMediaFile('woot', 'woot2')
|
|
|
|
const result = await getCachedMediaFile('woot')
|
|
|
|
assert.deepStrictEqual(result, 'woot')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns undefined if not found', async () => {
|
|
|
|
const result = await getCachedMediaFile('woot')
|
|
|
|
assert.deepStrictEqual(result, undefined)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing when deleting an unfound key', async () => {
|
|
|
|
await deleteCachedMediaFile('doesnt-exist')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('only stores up to 4 files', async () => {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 4)) // delay to avoid timing collisions
|
|
|
|
await setCachedMediaFile(i.toString(), i)
|
|
|
|
}
|
|
|
|
const ids = await getAllCachedFileIds()
|
|
|
|
assert.deepStrictEqual(ids, [6, 7, 8, 9].map(_ => _.toString()))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes old files during set()', async () => {
|
2021-03-16 00:25:13 +00:00
|
|
|
setDeleteCachedMediaFilesAfter(0)
|
2020-11-24 23:37:10 +00:00
|
|
|
await setCachedMediaFile('woot', 'woot')
|
|
|
|
await setCachedMediaFile('woot2', 'woot2')
|
|
|
|
assert.deepStrictEqual(await getCachedMediaFile('woot'), undefined)
|
|
|
|
assert.deepStrictEqual(await getCachedMediaFile('woot2'), 'woot2')
|
|
|
|
})
|
|
|
|
})
|