|
| 1 | +// @flow strict-local |
| 2 | + |
| 3 | +import type {Cache} from '@parcel/types'; |
| 4 | +import {FSCache, LMDBCache} from '@parcel/cache'; |
| 5 | +import * as tempy from 'tempy'; |
| 6 | +import { |
| 7 | + clearRequestTrackerCacheInfo, |
| 8 | + getRequestTrackerCacheInfo, |
| 9 | + storeRequestTrackerCacheInfo, |
| 10 | +} from '../src/RequestTrackerCacheInfo'; |
| 11 | +import assert from 'assert'; |
| 12 | +import {NodeFS} from '@parcel/fs'; |
| 13 | + |
| 14 | +type CacheImplementation = {| |
| 15 | + name: string, |
| 16 | + build: () => Cache, |
| 17 | +|}; |
| 18 | + |
| 19 | +const cacheImplementations: CacheImplementation[] = [ |
| 20 | + { |
| 21 | + name: 'FSCache', |
| 22 | + build: () => new FSCache(new NodeFS(), tempy.directory()), |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'LMDBCache', |
| 26 | + build: () => new LMDBCache(tempy.directory()), |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +describe('RequestTrackerCacheInfo', () => { |
| 31 | + cacheImplementations.forEach(cacheImplementation => { |
| 32 | + describe(`When using ${cacheImplementation.name}`, () => { |
| 33 | + let cache: Cache; |
| 34 | + beforeEach(async () => { |
| 35 | + cache = cacheImplementation.build(); |
| 36 | + await cache.ensure(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('getRequestTrackerCacheInfo - returns null if the cache entry is missing', async () => { |
| 40 | + const entry = await getRequestTrackerCacheInfo(cache); |
| 41 | + assert(entry === null); |
| 42 | + }); |
| 43 | + |
| 44 | + it("getRequestTrackerCacheInfo - returns an entry if it's set with the store method", async () => { |
| 45 | + { |
| 46 | + const entry = await getRequestTrackerCacheInfo(cache); |
| 47 | + assert(entry === null); |
| 48 | + } |
| 49 | + const expectedEntry = { |
| 50 | + snapshotKey: 'snapshot-key', |
| 51 | + timestamp: Date.now(), |
| 52 | + requestGraphKey: 'request-graph-key', |
| 53 | + }; |
| 54 | + await storeRequestTrackerCacheInfo(cache, expectedEntry); |
| 55 | + { |
| 56 | + const entry = await getRequestTrackerCacheInfo(cache); |
| 57 | + assert.deepEqual(entry, expectedEntry); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('entries can be cleared with clearRequestTrackerCacheInfo', async () => { |
| 62 | + const expectedEntry = { |
| 63 | + snapshotKey: 'snapshot-key', |
| 64 | + timestamp: Date.now(), |
| 65 | + requestGraphKey: 'request-graph-key', |
| 66 | + }; |
| 67 | + await storeRequestTrackerCacheInfo(cache, expectedEntry); |
| 68 | + await clearRequestTrackerCacheInfo(cache); |
| 69 | + const entry = await getRequestTrackerCacheInfo(cache); |
| 70 | + assert(entry === null); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments