|
| 1 | +import { describe, beforeEach, test } from '@jest/globals' |
| 2 | +import path from 'node:path' |
| 3 | +import * as fs from 'node:fs/promises' |
| 4 | +import { FileSystemObjectCache } from './fileSystemObjectCache' |
| 5 | + |
| 6 | +const testDir = path.join(__dirname, '..', '.cache') |
| 7 | + |
| 8 | +describe('FileSystemObjectCache', () => { |
| 9 | + beforeEach(async () => { |
| 10 | + if (await fs.stat(testDir).catch(() => false)) { |
| 11 | + await fs.rm(testDir, { force: true, recursive: true }) |
| 12 | + } |
| 13 | + }) |
| 14 | + |
| 15 | + test('Creates the directory when instantiated', async () => { |
| 16 | + new FileSystemObjectCache(testDir, true) |
| 17 | + expect(await fs.stat(testDir).catch(() => false)).toBeTruthy() |
| 18 | + }) |
| 19 | + |
| 20 | + test('Puts an object in cache as json', async () => { |
| 21 | + const cache = new FileSystemObjectCache(testDir, true) |
| 22 | + |
| 23 | + await cache.put('test', { test: 1 }) |
| 24 | + |
| 25 | + const data = await fs.readFile(path.join(testDir, 'test.json'), { encoding: 'utf-8' }) |
| 26 | + expect(data).toMatchInlineSnapshot(` |
| 27 | + "{ |
| 28 | + "test": 1 |
| 29 | + }" |
| 30 | + `) |
| 31 | + }) |
| 32 | + |
| 33 | + test("Get an object from cache if it didn't exist", async () => { |
| 34 | + const cache = new FileSystemObjectCache(testDir, true) |
| 35 | + |
| 36 | + const cached = await cache.getAndCache<{ test: number }>('test', async (_) => ({ |
| 37 | + test: 1, |
| 38 | + })) |
| 39 | + |
| 40 | + expect(cached.test).toBe(1) |
| 41 | + }) |
| 42 | + |
| 43 | + test('Get an object from cache if it did exist', async () => { |
| 44 | + const cache = new FileSystemObjectCache(testDir, true) |
| 45 | + await cache.put('test', { test: 1 }) |
| 46 | + |
| 47 | + const cached = await cache.getAndCache<{ test: number }>('test', async (_) => ({ |
| 48 | + test: 2, |
| 49 | + })) |
| 50 | + |
| 51 | + expect(cached.test).toBe(1) |
| 52 | + }) |
| 53 | + |
| 54 | + test('Get an object from cache if it is not yet stale', async () => { |
| 55 | + const cache = new FileSystemObjectCache(testDir, true) |
| 56 | + await cache.put('test', { test: 1 }) |
| 57 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 58 | + |
| 59 | + const cached = await cache.getAndCache<{ test: number }>( |
| 60 | + 'test', |
| 61 | + async (_) => ({ |
| 62 | + test: 2, |
| 63 | + }), |
| 64 | + { staleAfterSeconds: 2 }, |
| 65 | + ) |
| 66 | + |
| 67 | + expect(cached.test).toBe(1) |
| 68 | + }) |
| 69 | + |
| 70 | + test('Get a fresh object if it is stale', async () => { |
| 71 | + const cache = new FileSystemObjectCache(testDir, true) |
| 72 | + await cache.put('test', { test: 1 }) |
| 73 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 74 | + |
| 75 | + const cached = await cache.getAndCache<{ test: number }>( |
| 76 | + 'test', |
| 77 | + async (_) => ({ |
| 78 | + test: 2, |
| 79 | + }), |
| 80 | + { staleAfterSeconds: 0 }, |
| 81 | + ) |
| 82 | + |
| 83 | + expect(cached.test).toBe(2) |
| 84 | + }) |
| 85 | + |
| 86 | + test('Get an object from cache if it is stale but there is an error and returnStaleOnError is set', async () => { |
| 87 | + const cache = new FileSystemObjectCache(testDir, true) |
| 88 | + await cache.put('test', { test: 1 }) |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 90 | + |
| 91 | + const cached = await cache.getAndCache<{ test: number }>( |
| 92 | + 'test', |
| 93 | + async (_) => { |
| 94 | + throw new Error('error') |
| 95 | + }, |
| 96 | + { |
| 97 | + staleAfterSeconds: 0, |
| 98 | + returnStaleResultOnError: true, |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + expect(cached.test).toBe(1) |
| 103 | + }) |
| 104 | + |
| 105 | + test('Throw an error if it is stale and there is an error refreshing', async () => { |
| 106 | + const cache = new FileSystemObjectCache(testDir, true) |
| 107 | + await cache.put('test', { test: 1 }) |
| 108 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 109 | + |
| 110 | + await expect(() => |
| 111 | + cache.getAndCache<{ test: number }>( |
| 112 | + 'test', |
| 113 | + async (_) => { |
| 114 | + throw new Error('_ERROR_') |
| 115 | + }, |
| 116 | + { |
| 117 | + staleAfterSeconds: 0, |
| 118 | + }, |
| 119 | + ), |
| 120 | + ).rejects.toThrow('_ERROR_') |
| 121 | + }) |
| 122 | +}) |
0 commit comments