-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
179 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
310 changes: 178 additions & 132 deletions
310
packages/cache/test/utils/triggerCacheCreation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,201 +1,247 @@ | ||
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<string, { fileName: string; setupFunction: WalletSetupFunction }>() | ||
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( | ||
n: number, | ||
setupFunctions: ReturnType<typeof prepareSetupFunctions>, | ||
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, | ||
EXTENSION_PATH, | ||
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"); | ||
}); | ||
}); | ||
}); |