2024-09-20 13:40:01 +00:00
|
|
|
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') })
|
|
|
|
|
2024-09-20 23:14:22 +00:00
|
|
|
t.before(function() {
|
|
|
|
return cache.clear()
|
2024-09-20 13:40:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('get should work', async function() {
|
|
|
|
const testKey = 'get-test-one'
|
2024-09-20 23:14:22 +00:00
|
|
|
const testData = { a: 1 }
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
assert.deepStrictEqual(await cache.get(testKey, 'HELLO'), 'HELLO')
|
2024-09-20 23:14:22 +00:00
|
|
|
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')
|
2024-09-20 13:40:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('getSync should work', function() {
|
|
|
|
const testKey = 'get-sync-test-one'
|
2024-09-20 23:14:22 +00:00
|
|
|
const testData = { b: 2 }
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
assert.deepStrictEqual(cache.getSync(testKey, 'HELLO'), 'HELLO')
|
2024-09-20 23:14:22 +00:00
|
|
|
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')
|
2024-09-20 13:40:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('set should work', async function() {
|
|
|
|
const testKey = 'set-test-one'
|
2024-09-20 23:14:22 +00:00
|
|
|
const assertPath = cache.hash(testKey)
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
assert.notOk(fsSync.existsSync(assertPath))
|
|
|
|
await cache.set(testKey, { c: 3 })
|
|
|
|
assert.ok(fsSync.existsSync(assertPath))
|
|
|
|
let content = await fs.readFile(assertPath, { encoding: 'utf8' })
|
2024-09-20 23:14:22 +00:00
|
|
|
assert.strictEqual(content, cache._parseSetData(testKey, { c: 3 }))
|
2024-09-20 13:40:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('set should work', function() {
|
|
|
|
const testKey = 'set-sync-test-one'
|
2024-09-20 23:14:22 +00:00
|
|
|
const assertPath = cache.hash(testKey)
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
assert.notOk(fsSync.existsSync(assertPath))
|
|
|
|
cache.setSync(testKey, { d: 4 })
|
|
|
|
assert.ok(fsSync.existsSync(assertPath))
|
|
|
|
let content = fsSync.readFileSync(assertPath, { encoding: 'utf8' })
|
2024-09-20 23:14:22 +00:00
|
|
|
assert.strictEqual(content, cache._parseSetData(testKey, { d: 4 }))
|
2024-09-20 13:40:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should all work together', async function() {
|
|
|
|
const testKey = 'hello world'
|
|
|
|
const assertFallback = 'This is fallback'
|
2024-09-20 23:14:22 +00:00
|
|
|
const assertPath = cache.hash(testKey)
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
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 })
|
|
|
|
})
|