diff --git a/packages/cache/test/defineWalletSetup.test.ts b/packages/cache/test/defineWalletSetup.test.ts index e0d1194a..b278d690 100644 --- a/packages/cache/test/defineWalletSetup.test.ts +++ b/packages/cache/test/defineWalletSetup.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest' import { defineWalletSetup } from '../src' const PASSWORD = 'Quack Quack! 🦆' -const EXPECTED_HASH = '8a6a832d282f38a4683a' +const EXPECTED_HASH = 'f9c5ea5bb2c3aac96ff4' const testWalletSetupFunction = async (): Promise => { const result = 1 + 2 diff --git a/packages/cache/test/utils/triggerCacheCreation.test.ts b/packages/cache/test/utils/triggerCacheCreation.test.ts index 02b54495..ed745c15 100644 --- a/packages/cache/test/utils/triggerCacheCreation.test.ts +++ b/packages/cache/test/utils/triggerCacheCreation.test.ts @@ -1,73 +1,84 @@ -import { fs, vol } from 'memfs' -import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' - -import path from 'node:path' -import fsExtra from 'fs-extra' -import type { WalletSetupFunction } from '../../src' -import * as EnsureCacheDirExists from '../../src/ensureCacheDirExists' -import * as CreateCacheForWalletSetupFunction from '../../src/utils/createCacheForWalletSetupFunction' -import { triggerCacheCreation } from '../../src/utils/triggerCacheCreation' - -const ROOT_DIR = '/tmp' -const EXTENSION_PATH = path.join(ROOT_DIR, 'extension') - -vi.mock('fs-extra', async () => { +import { fs, vol } from "memfs"; +import { + afterAll, + afterEach, + beforeEach, + describe, + expect, + it, + vi, +} from "vitest"; + +import path from "node:path"; +import fsExtra from "fs-extra"; +import type { WalletSetupFunction } from "../../src"; +import * as EnsureCacheDirExists from "../../src/ensureCacheDirExists"; +import * as CreateCacheForWalletSetupFunction from "../../src/utils/createCacheForWalletSetupFunction"; +import { triggerCacheCreation } from "../../src/utils/triggerCacheCreation"; + +const ROOT_DIR = "/tmp"; +const EXTENSION_PATH = path.join(ROOT_DIR, "extension"); + +vi.mock("fs-extra", async () => { return { default: { ...fs.promises, exists: async (path: string) => { - return vol.existsSync(path) + return vol.existsSync(path); }, remove: async (path: string) => { - vol.rmdirSync(path) - } - } - } -}) + vol.rmdirSync(path); + }, + }, + }; +}); -vi.mock('../../src/ensureCacheDirExists', async () => { +vi.mock("../../src/ensureCacheDirExists", async () => { return { - ensureCacheDirExists: vi.fn(() => '/tmp') - } -}) + ensureCacheDirExists: vi.fn(() => "/tmp"), + }; +}); -vi.mock('../../src/utils/createCacheForWalletSetupFunction', async () => { +vi.mock("../../src/utils/createCacheForWalletSetupFunction", async () => { return { createCacheForWalletSetupFunction: vi.fn(async () => { - return 'Resolved Quack! 🦆' - }) - } -}) + return "Resolved Quack! 🦆"; + }), + }; +}); // We're not adding a test for code that uses `isDirEmpty` because soon it will be removed. -vi.mock('../../src/utils/isDirEmpty', async () => { +vi.mock("../../src/utils/isDirEmpty", async () => { return { isDirEmpty: vi.fn(async () => { - return false - }) - } -}) + return false; + }), + }; +}); -describe('triggerCacheCreation', () => { +describe("triggerCacheCreation", () => { const createCacheForWalletSetupFunctionSpy = vi.spyOn( CreateCacheForWalletSetupFunction, - 'createCacheForWalletSetupFunction' - ) + "createCacheForWalletSetupFunction" + ); - const downloadExtension = vi.fn(async () => EXTENSION_PATH) - const testSetupFunction = vi.fn() + const downloadExtension = vi.fn(async () => EXTENSION_PATH); + const testSetupFunction = vi.fn(); function prepareSetupFunctions(hashes: string[]) { - const setupFunctions = new Map() + const setupFunctions = new Map< + string, + { fileName: string; setupFunction: WalletSetupFunction } + >(); for (const hash of hashes) { setupFunctions.set(hash, { fileName: path.join(ROOT_DIR, `${hash}.ts`), - setupFunction: testSetupFunction - }) + setupFunction: testSetupFunction, + }); } - return setupFunctions + return setupFunctions; } function expectCreateCacheForWalletSetupFunction( @@ -75,7 +86,9 @@ describe('triggerCacheCreation', () => { setupFunctions: ReturnType, hash: string ) { - const fileNameWithCorrectExtension = setupFunctions.get(hash)?.fileName?.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}') + const fileNameWithCorrectExtension = setupFunctions + .get(hash) + ?.fileName?.replace(/\.(ts|js|mjs)$/, ".{ts,js,mjs}"); expect(createCacheForWalletSetupFunctionSpy).toHaveBeenNthCalledWith( n, @@ -83,119 +96,152 @@ describe('triggerCacheCreation', () => { path.join(ROOT_DIR, hash), testSetupFunction, fileNameWithCorrectExtension - ) + ); } afterAll(() => { - vi.resetAllMocks() - }) + vi.resetAllMocks(); + }); beforeEach(() => { - vol.mkdirSync(ROOT_DIR) - }) + vol.mkdirSync(ROOT_DIR); + }); afterEach(() => { - vi.clearAllMocks() - vol.reset() // Clear the in-memory file system after each test - }) - - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2']) - const functionStrings = ['function1', 'function2'] - - it('calls ensureCacheDirExists', async () => { - const ensureCacheDirExistsSpy = vi.spyOn(EnsureCacheDirExists, 'ensureCacheDirExists') - - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) - - expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce() - }) - - it('calls passed downloadExtension function', async () => { - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2']) - await triggerCacheCreation(setupFunctions, [], downloadExtension, false) - - expect(downloadExtension).toHaveBeenCalledOnce() - }) - - it.skip('calls createCacheForWalletSetupFunction with correct arguments', async () => { - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) - - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') - }) - - it.skip('checks if cache already exists for each entry', async () => { - const existsSpy = vi.spyOn(fsExtra, 'exists') - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) - - expect(existsSpy).toHaveBeenCalledTimes(2) - expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, 'hash1')) - expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, 'hash2')) - }) - - it('returns an array of createCacheForWalletSetupFunction promises', async () => { - const promises = await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) - - console.log(promises) - - expect(promises).toHaveLength(2) - expect(promises[0]).toBeInstanceOf(Promise) - expect(promises[1]).toBeInstanceOf(Promise) - }) - - describe('when force flag is false', () => { - it.skip('ignores setup function for which cache already exists', async () => { - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) + vi.clearAllMocks(); + vol.reset(); // Clear the in-memory file system after each test + }); + + const hashes = ["hash1", "hash2"]; + const setupFunctions = prepareSetupFunctions(hashes); + + it("calls ensureCacheDirExists", async () => { + const ensureCacheDirExistsSpy = vi.spyOn( + EnsureCacheDirExists, + "ensureCacheDirExists" + ); + + await triggerCacheCreation( + setupFunctions, + hashes, + downloadExtension, + false + ); + + expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce(); + }); + + it("calls passed downloadExtension function", async () => { + const setupFunctions = prepareSetupFunctions(hashes); + await triggerCacheCreation( + setupFunctions, + hashes, + downloadExtension, + false + ); + + expect(downloadExtension).toHaveBeenCalledOnce(); + }); + + it.skip("calls createCacheForWalletSetupFunction with correct arguments", async () => { + await triggerCacheCreation( + setupFunctions, + hashes, + downloadExtension, + false + ); + + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); + }); + + it.skip("checks if cache already exists for each entry", async () => { + const existsSpy = vi.spyOn(fsExtra, "exists"); + await triggerCacheCreation( + setupFunctions, + hashes, + downloadExtension, + false + ); + + expect(existsSpy).toHaveBeenCalledTimes(2); + expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, "hash1")); + expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, "hash2")); + }); + + it("returns an array of createCacheForWalletSetupFunction promises", async () => { + const promises = await triggerCacheCreation( + setupFunctions, + hashes, + downloadExtension, + false + ); + + console.log(promises); + + expect(promises).toHaveLength(2); + expect(promises[0]).toBeInstanceOf(Promise); + expect(promises[1]).toBeInstanceOf(Promise); + }); + + describe("when force flag is false", () => { + it.skip("ignores setup function for which cache already exists", async () => { + const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); // Creating cache for 2nd setup function. - fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) + fs.mkdirSync(path.join(ROOT_DIR, "hash2")); const promises = await triggerCacheCreation( setupFunctions, - [...functionStrings, 'function3'], + [...hashes, "hash3"], downloadExtension, false - ) + ); - expect(promises).toHaveLength(2) - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash3') - }) - }) + expect(promises).toHaveLength(2); + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash3"); + }); + }); - describe('when force flag is true', () => { - it.skip('removes cache if it already exists for given setup function', async () => { - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) + describe("when force flag is true", () => { + it.skip("removes cache if it already exists for given setup function", async () => { + const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); // Creating cache for 2nd setup function. - const pathToExistingCache = path.join(ROOT_DIR, 'hash2') - fs.mkdirSync(pathToExistingCache) + const pathToExistingCache = path.join(ROOT_DIR, "hash2"); + fs.mkdirSync(pathToExistingCache); - await triggerCacheCreation(setupFunctions, [...functionStrings, 'function3'], downloadExtension, true) + await triggerCacheCreation( + setupFunctions, + [...hashes, "hash3"], + downloadExtension, + true + ); - expect(fs.existsSync(pathToExistingCache)).toBe(false) - }) + expect(fs.existsSync(pathToExistingCache)).toBe(false); + }); - it.skip('calls createCacheForWalletSetupFunction for setup functions that were previously cached', async () => { - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) + it.skip("calls createCacheForWalletSetupFunction for setup functions that were previously cached", async () => { + const setupFunctions = prepareSetupFunctions([...hashes, "hash3"]); // Creating cache for 2nd setup function. - fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) + fs.mkdirSync(path.join(ROOT_DIR, "hash2")); const promises = await triggerCacheCreation( setupFunctions, - [...functionStrings, 'function3'], + [...hashes, "hash3"], downloadExtension, true - ) - - expect(promises).toHaveLength(3) - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3) - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') - expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3') - }) - }) -}) + ); + + expect(promises).toHaveLength(3); + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3); + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); + expectCreateCacheForWalletSetupFunction(3, setupFunctions, "hash3"); + }); + }); +});