Skip to content

Commit

Permalink
✨ feat(core): Add createCache function
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 26, 2023
1 parent 84e7e15 commit 007fcfd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/core/src/createCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getUniqueWalletSetupFunctions } from './utils/getUniqueWalletSetupFunctions'
import { triggerCacheCreation } from './utils/triggerCacheCreation'

export async function createCache(walletSetupDirPath: string, downloadExtension: () => Promise<string>, force = false) {
const setupFunctions = await getUniqueWalletSetupFunctions(walletSetupDirPath)

const cacheCreationPromises = await triggerCacheCreation(setupFunctions, downloadExtension, force)

if (cacheCreationPromises.length === 0) {
console.log('No new setup functions to cache. Exiting...')
return
}

// TODO: This line has no unit test. Not sure how to do it. Look into it later.
await Promise.all(cacheCreationPromises)

console.log('All wallet setup functions are now cached!')
}
78 changes: 78 additions & 0 deletions packages/core/test/createCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'

import path from 'node:path'
import { createCache } from '../src/createCache'
import type { WalletSetupFunction } from '../src/defineWalletSetup'
import * as GetUniqueWalletSetupFunctions from '../src/utils/getUniqueWalletSetupFunctions'
import * as TriggerCacheCreation from '../src/utils/triggerCacheCreation'

const ROOT_DIR = '/tmp'

const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>()

setupFunctions.set('hash1', { fileName: path.join(ROOT_DIR, 'hash1'), setupFunction: vi.fn() })
setupFunctions.set('hash2', { fileName: path.join(ROOT_DIR, 'hash2'), setupFunction: vi.fn() })
setupFunctions.set('hash3', { fileName: path.join(ROOT_DIR, 'hash3'), setupFunction: vi.fn() })

vi.mock('../src/utils/getUniqueWalletSetupFunctions', async () => {
return {
getUniqueWalletSetupFunctions: vi.fn().mockImplementation(async () => {
return setupFunctions
})
}
})

vi.mock('../src/utils/triggerCacheCreation', async () => {
return {
triggerCacheCreation: vi.fn().mockImplementation(async () => {
return ['hash1', 'hash2', 'hash3']
})
}
})

describe('createCache', () => {
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined)

afterAll(() => {
vi.resetAllMocks()
})

afterEach(() => {
vi.clearAllMocks()
})

it('calls getUniqueWalletSetupFunctions with correct arguments', async () => {
const getUniqueWalletSetupFunctionsSpy = vi.spyOn(GetUniqueWalletSetupFunctions, 'getUniqueWalletSetupFunctions')

await createCache(ROOT_DIR, vi.fn(), false)

expect(getUniqueWalletSetupFunctionsSpy).toHaveBeenCalledOnce()
expect(getUniqueWalletSetupFunctionsSpy).toHaveBeenCalledWith(ROOT_DIR)
})

it('calls triggerCacheCreation with correct arguments', async () => {
const triggerCacheCreationSpy = vi.spyOn(TriggerCacheCreation, 'triggerCacheCreation')

const downloadExtension = vi.fn(async () => path.join(ROOT_DIR, 'extension'))
await createCache(ROOT_DIR, downloadExtension, false)

expect(triggerCacheCreationSpy).toHaveBeenCalledOnce()
expect(triggerCacheCreationSpy).toHaveBeenCalledWith(setupFunctions, downloadExtension, false)
})

it('does nothing if no setup functions need caching', async () => {
vi.spyOn(TriggerCacheCreation, 'triggerCacheCreation').mockResolvedValueOnce([])

await createCache(ROOT_DIR, vi.fn(), false)

expect(consoleLogSpy).toHaveBeenCalledOnce()
expect(consoleLogSpy).toHaveBeenCalledWith('No new setup functions to cache. Exiting...')
})

it('console.logs at the end', async () => {
await createCache(ROOT_DIR, vi.fn(), false)

expect(consoleLogSpy).toHaveBeenCalledOnce()
expect(consoleLogSpy).toHaveBeenCalledWith('All wallet setup functions are now cached!')
})
})

0 comments on commit 007fcfd

Please sign in to comment.