Skip to content

Commit

Permalink
Upgrade dependencies, use vitest for test
Browse files Browse the repository at this point in the history
  • Loading branch information
rjyo committed Sep 10, 2024
1 parent a13b952 commit 321a927
Show file tree
Hide file tree
Showing 4 changed files with 1,055 additions and 1,482 deletions.
35 changes: 16 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build": "tsc",
"prepublishOnly": "rm -rf dist && yarn build",
"lint": "eslint src/*.ts test/*.ts",
"test": "NODE_ENV=test mocha --require ts-node/register test/**/*.test.ts",
"test-cov": "NODE_ENV=test nyc mocha test/*.test.ts"
"test": "vitest",
"test-cov": "vitest --coverage --coverage.exclude=src/bench.ts --coverage.exclude=test/*.ts"
},
"files": [
"dist",
Expand All @@ -25,26 +25,23 @@
},
"license": "MIT",
"dependencies": {
"better-sqlite3": "^8.5.2",
"fs-extra": "^10.0.0"
"better-sqlite3": "^11.3.0",
"fs-extra": "^11.2.0"
},
"devDependencies": {
"@types/better-sqlite3": "^7.4.0",
"@types/chai": "^4.2.12",
"@types/fs-extra": "^9.0.1",
"@types/mocha": "^9.0.0",
"@types/node": "^16.7.10",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"chai": "^4.2.0",
"eslint": "^7.9.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.1.1",
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"ts-node": "^10.2.1",
"typescript": "^4.0.3"
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.5.4",
"@typescript-eslint/eslint-plugin": "^8.5.0",
"@typescript-eslint/parser": "^8.5.0",
"@vitest/coverage-v8": "^2.0.5",
"eslint": "^9.10.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.6.2",
"vitest": "^2.0.5"
},
"nyc": {
"include": [
Expand Down
66 changes: 36 additions & 30 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,90 @@
import { expect } from 'chai'
import { expect, describe, it, beforeEach, afterEach } from 'vitest'
import fs from 'fs'
import Cache from '../src'

export const sleep = async (t: number) => {
const sleep = async (t: number) => {
return new Promise((resolve) => setTimeout(resolve, t))
}

describe('disk cache with ttl', () => {
let cache: Cache

beforeEach(() => {
cache = new Cache()
})

afterEach(async () => {
// Clean up cache after each test
await cache.purge()
})

it('init', () => {
const cache = new Cache()
expect(fs.existsSync(cache.path)).to.be.true
expect(fs.existsSync(cache.path)).toBe(true)
})

it('init with params', () => {
let cache = new Cache({ path: '/tmp', ttl: 100, tbd: 300 })
expect(cache.ttl).to.eq(100)
expect(cache.tbd).to.eq(300)
expect(cache.path).to.eq('/tmp')
let customCache = new Cache({ path: '/tmp', ttl: 100, tbd: 300 })
expect(customCache.ttl).toBe(100)
expect(customCache.tbd).toBe(300)
expect(customCache.path).toBe('/tmp')

delete process.env.TMPDIR
cache = new Cache({ ttl: 100, tbd: 300 })
expect(cache.path).to.eq('/tmp/hdc')
customCache = new Cache({ ttl: 100, tbd: 300 })
expect(customCache.path).toBe('/tmp/hdc')
})

it('set / get', async () => {
const cache = new Cache()
const v = Buffer.from('B')
await cache.set('A', v)
expect(await cache.get('A')).to.deep.eq(v)
expect(await cache.get('A')).toEqual(v)

const v2 = Buffer.from('AAA')
await cache.set('A', v2)
expect(await cache.get('A')).to.deep.eq(v2)
expect(await cache.get('A')).toEqual(v2)

const defaultValue = Buffer.from('AA')
expect(await cache.get('B', defaultValue)).to.eq(defaultValue)
expect(await cache.get('B', defaultValue)).toBe(defaultValue)
})

it('set / get stale / hit / miss', async () => {
const cache = new Cache()
const key = 'key:1'
await cache.set(key, Buffer.from('1'), 0.8)
let s = await cache.has(key)
expect(s).to.eq('hit')
expect(s).toBe('hit')
await sleep(1000)
s = await cache.has(key)
expect(s).to.eq('stale')
expect(s).toBe('stale')
const v = await cache.get(key)
expect(v).to.deep.eq(Buffer.from('1'))
expect(v).toEqual(Buffer.from('1'))
s = await cache.has('key:2')
expect(s).to.eq('miss')
expect(s).toBe('miss')
})

it('set / get large buffer', async () => {
const cache = new Cache()
const key1 = 'key:l1'
const d = new Array(20000).fill('A')
const buf = Buffer.from(d)
await cache.set(key1, buf, 0.8)
expect(await cache.get(key1)).to.deep.eq(buf)
expect(await cache.get(key1)).toEqual(buf)
})

it('del / get miss', async () => {
const cache = new Cache()
cache.set('A', Buffer.from('1'))
expect(await cache.get('A')).to.deep.eq(Buffer.from('1'))
await cache.set('A', Buffer.from('1'))
expect(await cache.get('A')).toEqual(Buffer.from('1'))
await cache.del('A')
expect(await cache.get('A')).to.be.undefined
expect(await cache.get('A')).toBeUndefined()
await cache.del('not-exist')
})

it('purge', async () => {
const cache = new Cache({ ttl: 0.1, tbd: 0.1 })
const purgeCache = new Cache({ ttl: 0.1, tbd: 0.1 })
const key1 = 'key:l1'
const d = new Array(20000).fill('A')
const buf = Buffer.from(d)
await cache.set(key1, buf)
expect(await cache.get(key1)).to.deep.eq(buf)
await purgeCache.set(key1, buf)
expect(await purgeCache.get(key1)).toEqual(buf)
await sleep(500)
await cache.purge()
expect(await cache.get(key1)).to.be.undefined
await purgeCache.purge()
expect(await purgeCache.get(key1)).toBeUndefined()
})
})
14 changes: 7 additions & 7 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, describe, it } from 'vitest'
import fs from 'fs-extra'
import { join as pathJoin } from 'path'
import { purgeEmptyPath } from '../src/utils'
Expand All @@ -21,14 +21,14 @@ describe('utils', () => {

await purgeEmptyPath(root)

expect(fs.existsSync(pAA)).to.be.false
expect(fs.existsSync(pBA)).to.be.false
expect(fs.existsSync(pB)).to.be.true
expect(fs.existsSync(pCA)).to.be.true
expect(fs.existsSync(pD)).to.be.false
expect(fs.existsSync(pAA)).toBe(false)
expect(fs.existsSync(pBA)).toBe(false)
expect(fs.existsSync(pB)).toBe(true)
expect(fs.existsSync(pCA)).toBe(true)
expect(fs.existsSync(pD)).toBe(false)
})

it('no error on non-exist directory', async () => {
await purgeEmptyPath('/tmp/not-hello-123')
await expect(purgeEmptyPath('/tmp/not-hello-123')).resolves.not.toThrow()
})
})
Loading

0 comments on commit 321a927

Please sign in to comment.