107 lines
4 KiB
JavaScript
107 lines
4 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(function() {
|
|
return cache.clear()
|
|
})
|
|
|
|
t.test('get should work', async function() {
|
|
const testKey = 'get-test-one'
|
|
const testData = { a: 1 }
|
|
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
|
await fs.writeFile(cache.hash(testKey), cache._parseSetData(testKey, testData))
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), testData)
|
|
})
|
|
|
|
t.test('get should work with ttl', async function() {
|
|
const testKey = 'get-test-two'
|
|
const testData = { a: 1 }
|
|
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
|
await fs.writeFile(cache.hash(testKey), cache._parseSetData(testKey, testData, { ttl: 60 }))
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), testData)
|
|
})
|
|
|
|
t.test('get should return fallback with expired ttl', async function() {
|
|
const testKey = 'get-test-three'
|
|
const testData = { a: 1 }
|
|
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
|
await fs.writeFile(cache.hash(testKey), cache._parseSetData(testKey, testData, { ttl: -1 }))
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
|
})
|
|
|
|
t.test('getSync should work', function() {
|
|
const testKey = 'get-sync-test-one'
|
|
const testData = { b: 2 }
|
|
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
|
fsSync.writeFileSync(cache.hash(testKey), cache._parseSetData(testKey, testData))
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), testData)
|
|
})
|
|
|
|
t.test('getSync should work with ttl', function() {
|
|
const testKey = 'get-sync-test-two'
|
|
const testData = { b: 2 }
|
|
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
|
fsSync.writeFileSync(cache.hash(testKey), cache._parseSetData(testKey, testData, { ttl: 60 }))
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), testData)
|
|
})
|
|
|
|
t.test('getSync should return fallback with expired ttl', function() {
|
|
const testKey = 'get-sync-test-three'
|
|
const testData = { b: 2 }
|
|
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
|
fsSync.writeFileSync(cache.hash(testKey), cache._parseSetData(testKey, testData, { ttl: -1 }))
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
|
})
|
|
|
|
t.test('set should work', async function() {
|
|
const testKey = 'set-test-one'
|
|
const assertPath = 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, cache._parseSetData(testKey, { c: 3 }))
|
|
})
|
|
|
|
t.test('set should work', function() {
|
|
const testKey = 'set-sync-test-one'
|
|
const assertPath = 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, cache._parseSetData(testKey, { d: 4 }))
|
|
})
|
|
|
|
t.test('should all work together', async function() {
|
|
const testKey = 'hello world'
|
|
const assertFallback = 'This is fallback'
|
|
const assertPath = 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 })
|
|
})
|