76 lines
2.9 KiB
JavaScript
76 lines
2.9 KiB
JavaScript
|
import { Eltro as t, assert, spy } from 'eltro'
|
||
|
import { fileURLToPath } from 'url'
|
||
|
import fs from 'fs/promises'
|
||
|
import fsSync from 'fs'
|
||
|
import path from 'path'
|
||
|
import Cache from '../index.mjs'
|
||
|
|
||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||
|
|
||
|
let cache = new Cache({ cache_dir: path.join(__dirname, 'temp') })
|
||
|
|
||
|
t.before(async function() {
|
||
|
for (let file of await fs.readdir(cache.cache_dir)) {
|
||
|
if (file !== '.gitkeep') {
|
||
|
await fs.rm(path.join(cache.cache_dir, file))
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.test('get should work', async function() {
|
||
|
const testKey = 'get-test-one'
|
||
|
|
||
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
||
|
await fs.writeFile(path.join(cache.cache_dir, cache.hash(testKey)), JSON.stringify({ a: 1 }))
|
||
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), { a: 1 })
|
||
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO', { parse_json: false }), JSON.stringify({ a: 1 }))
|
||
|
})
|
||
|
|
||
|
t.test('getSync should work', function() {
|
||
|
const testKey = 'get-sync-test-one'
|
||
|
|
||
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
||
|
fsSync.writeFileSync(path.join(cache.cache_dir, cache.hash(testKey)), JSON.stringify({ b: 2 }))
|
||
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), { b: 2 })
|
||
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO', { parse_json: false }), JSON.stringify({ b: 2 }))
|
||
|
})
|
||
|
|
||
|
t.test('set should work', async function() {
|
||
|
const testKey = 'set-test-one'
|
||
|
const assertPath = path.join(cache.cache_dir, cache.hash(testKey))
|
||
|
|
||
|
assert.notOk(fsSync.existsSync(assertPath))
|
||
|
await cache.set(testKey, { c: 3 })
|
||
|
assert.ok(fsSync.existsSync(assertPath))
|
||
|
let content = await fs.readFile(assertPath, { encoding: 'utf8' })
|
||
|
assert.strictEqual(content, JSON.stringify({ c: 3 }))
|
||
|
})
|
||
|
|
||
|
t.test('set should work', function() {
|
||
|
const testKey = 'set-sync-test-one'
|
||
|
const assertPath = path.join(cache.cache_dir, cache.hash(testKey))
|
||
|
|
||
|
assert.notOk(fsSync.existsSync(assertPath))
|
||
|
cache.setSync(testKey, { d: 4 })
|
||
|
assert.ok(fsSync.existsSync(assertPath))
|
||
|
let content = fsSync.readFileSync(assertPath, { encoding: 'utf8' })
|
||
|
assert.strictEqual(content, JSON.stringify({ d: 4 }))
|
||
|
})
|
||
|
|
||
|
t.test('should all work together', async function() {
|
||
|
const testKey = 'hello world'
|
||
|
const assertFallback = 'This is fallback'
|
||
|
const assertPath = path.join(cache.cache_dir, cache.hash(testKey))
|
||
|
|
||
|
assert.notOk(fsSync.existsSync(assertPath))
|
||
|
assert.strictEqual(await cache.get(testKey, assertFallback), assertFallback)
|
||
|
await cache.set(testKey, { e: 5 })
|
||
|
assert.notStrictEqual(await cache.get(testKey, assertFallback), assertFallback)
|
||
|
assert.deepStrictEqual(await cache.get(testKey), { e: 5 })
|
||
|
assert.notStrictEqual(cache.getSync(testKey, assertFallback), assertFallback)
|
||
|
assert.deepStrictEqual(cache.getSync(testKey), { e: 5 })
|
||
|
cache.setSync(testKey, { f: 6 })
|
||
|
assert.deepStrictEqual(await cache.get(testKey), { f: 6 })
|
||
|
assert.deepStrictEqual(cache.getSync(testKey), { f: 6 })
|
||
|
})
|