|
1 |
| -import { fs, vol } from 'memfs' |
2 |
| -import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
3 |
| - |
4 |
| -import path from 'node:path' |
5 |
| -import fsExtra from 'fs-extra' |
6 |
| -import type { WalletSetupFunction } from '../../src' |
7 |
| -import * as EnsureCacheDirExists from '../../src/ensureCacheDirExists' |
8 |
| -import * as CreateCacheForWalletSetupFunction from '../../src/utils/createCacheForWalletSetupFunction' |
9 |
| -import { triggerCacheCreation } from '../../src/utils/triggerCacheCreation' |
10 |
| - |
11 |
| -const ROOT_DIR = '/tmp' |
12 |
| -const EXTENSION_PATH = path.join(ROOT_DIR, 'extension') |
13 |
| - |
14 |
| -vi.mock('fs-extra', async () => { |
| 1 | +import { fs, vol } from "memfs"; |
| 2 | +import { |
| 3 | + afterAll, |
| 4 | + afterEach, |
| 5 | + beforeEach, |
| 6 | + describe, |
| 7 | + expect, |
| 8 | + it, |
| 9 | + vi, |
| 10 | +} from "vitest"; |
| 11 | + |
| 12 | +import path from "node:path"; |
| 13 | +import fsExtra from "fs-extra"; |
| 14 | +import type { WalletSetupFunction } from "../../src"; |
| 15 | +import * as EnsureCacheDirExists from "../../src/ensureCacheDirExists"; |
| 16 | +import * as CreateCacheForWalletSetupFunction from "../../src/utils/createCacheForWalletSetupFunction"; |
| 17 | +import { triggerCacheCreation } from "../../src/utils/triggerCacheCreation"; |
| 18 | + |
| 19 | +const ROOT_DIR = "/tmp"; |
| 20 | +const EXTENSION_PATH = path.join(ROOT_DIR, "extension"); |
| 21 | + |
| 22 | +vi.mock("fs-extra", async () => { |
15 | 23 | return {
|
16 | 24 | default: {
|
17 | 25 | ...fs.promises,
|
18 | 26 | exists: async (path: string) => {
|
19 |
| - return vol.existsSync(path) |
| 27 | + return vol.existsSync(path); |
20 | 28 | },
|
21 | 29 | remove: async (path: string) => {
|
22 |
| - vol.rmdirSync(path) |
23 |
| - } |
24 |
| - } |
25 |
| - } |
26 |
| -}) |
| 30 | + vol.rmdirSync(path); |
| 31 | + }, |
| 32 | + }, |
| 33 | + }; |
| 34 | +}); |
27 | 35 |
|
28 |
| -vi.mock('../../src/ensureCacheDirExists', async () => { |
| 36 | +vi.mock("../../src/ensureCacheDirExists", async () => { |
29 | 37 | return {
|
30 |
| - ensureCacheDirExists: vi.fn(() => '/tmp') |
31 |
| - } |
32 |
| -}) |
| 38 | + ensureCacheDirExists: vi.fn(() => "/tmp"), |
| 39 | + }; |
| 40 | +}); |
33 | 41 |
|
34 |
| -vi.mock('../../src/utils/createCacheForWalletSetupFunction', async () => { |
| 42 | +vi.mock("../../src/utils/createCacheForWalletSetupFunction", async () => { |
35 | 43 | return {
|
36 | 44 | createCacheForWalletSetupFunction: vi.fn(async () => {
|
37 |
| - return 'Resolved Quack! 🦆' |
38 |
| - }) |
39 |
| - } |
40 |
| -}) |
| 45 | + return "Resolved Quack! 🦆"; |
| 46 | + }), |
| 47 | + }; |
| 48 | +}); |
41 | 49 |
|
42 | 50 | // We're not adding a test for code that uses `isDirEmpty` because soon it will be removed.
|
43 |
| -vi.mock('../../src/utils/isDirEmpty', async () => { |
| 51 | +vi.mock("../../src/utils/isDirEmpty", async () => { |
44 | 52 | return {
|
45 | 53 | isDirEmpty: vi.fn(async () => {
|
46 |
| - return false |
47 |
| - }) |
48 |
| - } |
49 |
| -}) |
| 54 | + return false; |
| 55 | + }), |
| 56 | + }; |
| 57 | +}); |
50 | 58 |
|
51 |
| -describe('triggerCacheCreation', () => { |
| 59 | +describe("triggerCacheCreation", () => { |
52 | 60 | const createCacheForWalletSetupFunctionSpy = vi.spyOn(
|
53 | 61 | CreateCacheForWalletSetupFunction,
|
54 |
| - 'createCacheForWalletSetupFunction' |
55 |
| - ) |
| 62 | + "createCacheForWalletSetupFunction" |
| 63 | + ); |
56 | 64 |
|
57 |
| - const downloadExtension = vi.fn(async () => EXTENSION_PATH) |
58 |
| - const testSetupFunction = vi.fn() |
| 65 | + const downloadExtension = vi.fn(async () => EXTENSION_PATH); |
| 66 | + const testSetupFunction = vi.fn(); |
59 | 67 |
|
60 | 68 | function prepareSetupFunctions(hashes: string[]) {
|
61 |
| - const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>() |
| 69 | + const setupFunctions = new Map< |
| 70 | + string, |
| 71 | + { fileName: string; setupFunction: WalletSetupFunction } |
| 72 | + >(); |
62 | 73 |
|
63 | 74 | for (const hash of hashes) {
|
64 | 75 | setupFunctions.set(hash, {
|
65 | 76 | fileName: path.join(ROOT_DIR, `${hash}.ts`),
|
66 |
| - setupFunction: testSetupFunction |
67 |
| - }) |
| 77 | + setupFunction: testSetupFunction, |
| 78 | + }); |
68 | 79 | }
|
69 | 80 |
|
70 |
| - return setupFunctions |
| 81 | + return setupFunctions; |
71 | 82 | }
|
72 | 83 |
|
73 | 84 | function expectCreateCacheForWalletSetupFunction(
|
74 | 85 | n: number,
|
75 | 86 | setupFunctions: ReturnType<typeof prepareSetupFunctions>,
|
76 | 87 | hash: string
|
77 | 88 | ) {
|
78 |
| - const fileNameWithCorrectExtension = setupFunctions.get(hash)?.fileName?.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}') |
| 89 | + const fileNameWithCorrectExtension = setupFunctions |
| 90 | + .get(hash) |
| 91 | + ?.fileName?.replace(/\.(ts|js|mjs)$/, ".{ts,js,mjs}"); |
79 | 92 |
|
80 | 93 | expect(createCacheForWalletSetupFunctionSpy).toHaveBeenNthCalledWith(
|
81 | 94 | n,
|
82 | 95 | EXTENSION_PATH,
|
83 | 96 | path.join(ROOT_DIR, hash),
|
84 | 97 | testSetupFunction,
|
85 | 98 | fileNameWithCorrectExtension
|
86 |
| - ) |
| 99 | + ); |
87 | 100 | }
|
88 | 101 |
|
89 | 102 | afterAll(() => {
|
90 |
| - vi.resetAllMocks() |
91 |
| - }) |
| 103 | + vi.resetAllMocks(); |
| 104 | + }); |
92 | 105 |
|
93 | 106 | beforeEach(() => {
|
94 |
| - vol.mkdirSync(ROOT_DIR) |
95 |
| - }) |
| 107 | + vol.mkdirSync(ROOT_DIR); |
| 108 | + }); |
96 | 109 |
|
97 | 110 | afterEach(() => {
|
98 |
| - vi.clearAllMocks() |
99 |
| - vol.reset() // Clear the in-memory file system after each test |
100 |
| - }) |
101 |
| - |
102 |
| - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2']) |
103 |
| - const functionStrings = ['function1', 'function2'] |
104 |
| - |
105 |
| - it('calls ensureCacheDirExists', async () => { |
106 |
| - const ensureCacheDirExistsSpy = vi.spyOn(EnsureCacheDirExists, 'ensureCacheDirExists') |
107 |
| - |
108 |
| - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) |
109 |
| - |
110 |
| - expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce() |
111 |
| - }) |
112 |
| - |
113 |
| - it('calls passed downloadExtension function', async () => { |
114 |
| - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2']) |
115 |
| - await triggerCacheCreation(setupFunctions, [], downloadExtension, false) |
116 |
| - |
117 |
| - expect(downloadExtension).toHaveBeenCalledOnce() |
118 |
| - }) |
119 |
| - |
120 |
| - it.skip('calls createCacheForWalletSetupFunction with correct arguments', async () => { |
121 |
| - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) |
122 |
| - |
123 |
| - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) |
124 |
| - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') |
125 |
| - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') |
126 |
| - }) |
127 |
| - |
128 |
| - it.skip('checks if cache already exists for each entry', async () => { |
129 |
| - const existsSpy = vi.spyOn(fsExtra, 'exists') |
130 |
| - await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) |
131 |
| - |
132 |
| - expect(existsSpy).toHaveBeenCalledTimes(2) |
133 |
| - expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, 'hash1')) |
134 |
| - expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, 'hash2')) |
135 |
| - }) |
136 |
| - |
137 |
| - it('returns an array of createCacheForWalletSetupFunction promises', async () => { |
138 |
| - const promises = await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false) |
139 |
| - |
140 |
| - console.log(promises) |
141 |
| - |
142 |
| - expect(promises).toHaveLength(2) |
143 |
| - expect(promises[0]).toBeInstanceOf(Promise) |
144 |
| - expect(promises[1]).toBeInstanceOf(Promise) |
145 |
| - }) |
146 |
| - |
147 |
| - describe('when force flag is false', () => { |
148 |
| - it.skip('ignores setup function for which cache already exists', async () => { |
149 |
| - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) |
| 111 | + vi.clearAllMocks(); |
| 112 | + vol.reset(); // Clear the in-memory file system after each test |
| 113 | + }); |
| 114 | + |
| 115 | + const hashes = ["hash1", "hash2"]; |
| 116 | + const setupFunctions = prepareSetupFunctions(hashes); |
| 117 | + |
| 118 | + it("calls ensureCacheDirExists", async () => { |
| 119 | + const ensureCacheDirExistsSpy = vi.spyOn( |
| 120 | + EnsureCacheDirExists, |
| 121 | + "ensureCacheDirExists" |
| 122 | + ); |
| 123 | + |
| 124 | + await triggerCacheCreation( |
| 125 | + setupFunctions, |
| 126 | + hashes, |
| 127 | + downloadExtension, |
| 128 | + false |
| 129 | + ); |
| 130 | + |
| 131 | + expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("calls passed downloadExtension function", async () => { |
| 135 | + const setupFunctions = prepareSetupFunctions(hashes); |
| 136 | + await triggerCacheCreation( |
| 137 | + setupFunctions, |
| 138 | + hashes, |
| 139 | + downloadExtension, |
| 140 | + false |
| 141 | + ); |
| 142 | + |
| 143 | + expect(downloadExtension).toHaveBeenCalledOnce(); |
| 144 | + }); |
| 145 | + |
| 146 | + it.skip("calls createCacheForWalletSetupFunction with correct arguments", async () => { |
| 147 | + await triggerCacheCreation( |
| 148 | + setupFunctions, |
| 149 | + hashes, |
| 150 | + downloadExtension, |
| 151 | + false |
| 152 | + ); |
| 153 | + |
| 154 | + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); |
| 155 | + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); |
| 156 | + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); |
| 157 | + }); |
| 158 | + |
| 159 | + it.skip("checks if cache already exists for each entry", async () => { |
| 160 | + const existsSpy = vi.spyOn(fsExtra, "exists"); |
| 161 | + await triggerCacheCreation( |
| 162 | + setupFunctions, |
| 163 | + hashes, |
| 164 | + downloadExtension, |
| 165 | + false |
| 166 | + ); |
| 167 | + |
| 168 | + expect(existsSpy).toHaveBeenCalledTimes(2); |
| 169 | + expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, "hash1")); |
| 170 | + expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, "hash2")); |
| 171 | + }); |
| 172 | + |
| 173 | + it("returns an array of createCacheForWalletSetupFunction promises", async () => { |
| 174 | + const promises = await triggerCacheCreation( |
| 175 | + setupFunctions, |
| 176 | + hashes, |
| 177 | + downloadExtension, |
| 178 | + false |
| 179 | + ); |
| 180 | + |
| 181 | + console.log(promises); |
| 182 | + |
| 183 | + expect(promises).toHaveLength(2); |
| 184 | + expect(promises[0]).toBeInstanceOf(Promise); |
| 185 | + expect(promises[1]).toBeInstanceOf(Promise); |
| 186 | + }); |
| 187 | + |
| 188 | + describe("when force flag is false", () => { |
| 189 | + it.skip("ignores setup function for which cache already exists", async () => { |
| 190 | + const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); |
150 | 191 |
|
151 | 192 | // Creating cache for 2nd setup function.
|
152 |
| - fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) |
| 193 | + fs.mkdirSync(path.join(ROOT_DIR, "hash2")); |
153 | 194 |
|
154 | 195 | const promises = await triggerCacheCreation(
|
155 | 196 | setupFunctions,
|
156 |
| - [...functionStrings, 'function3'], |
| 197 | + [...hashes, "hash3"], |
157 | 198 | downloadExtension,
|
158 | 199 | false
|
159 |
| - ) |
| 200 | + ); |
160 | 201 |
|
161 |
| - expect(promises).toHaveLength(2) |
162 |
| - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) |
163 |
| - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') |
164 |
| - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash3') |
165 |
| - }) |
166 |
| - }) |
| 202 | + expect(promises).toHaveLength(2); |
| 203 | + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); |
| 204 | + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); |
| 205 | + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash3"); |
| 206 | + }); |
| 207 | + }); |
167 | 208 |
|
168 |
| - describe('when force flag is true', () => { |
169 |
| - it.skip('removes cache if it already exists for given setup function', async () => { |
170 |
| - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) |
| 209 | + describe("when force flag is true", () => { |
| 210 | + it.skip("removes cache if it already exists for given setup function", async () => { |
| 211 | + const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); |
171 | 212 |
|
172 | 213 | // Creating cache for 2nd setup function.
|
173 |
| - const pathToExistingCache = path.join(ROOT_DIR, 'hash2') |
174 |
| - fs.mkdirSync(pathToExistingCache) |
| 214 | + const pathToExistingCache = path.join(ROOT_DIR, "hash2"); |
| 215 | + fs.mkdirSync(pathToExistingCache); |
175 | 216 |
|
176 |
| - await triggerCacheCreation(setupFunctions, [...functionStrings, 'function3'], downloadExtension, true) |
| 217 | + await triggerCacheCreation( |
| 218 | + setupFunctions, |
| 219 | + [...hashes, "hash3"], |
| 220 | + downloadExtension, |
| 221 | + true |
| 222 | + ); |
177 | 223 |
|
178 |
| - expect(fs.existsSync(pathToExistingCache)).toBe(false) |
179 |
| - }) |
| 224 | + expect(fs.existsSync(pathToExistingCache)).toBe(false); |
| 225 | + }); |
180 | 226 |
|
181 |
| - it.skip('calls createCacheForWalletSetupFunction for setup functions that were previously cached', async () => { |
182 |
| - const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) |
| 227 | + it.skip("calls createCacheForWalletSetupFunction for setup functions that were previously cached", async () => { |
| 228 | + const setupFunctions = prepareSetupFunctions([...hashes, "hash3"]); |
183 | 229 |
|
184 | 230 | // Creating cache for 2nd setup function.
|
185 |
| - fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) |
| 231 | + fs.mkdirSync(path.join(ROOT_DIR, "hash2")); |
186 | 232 |
|
187 | 233 | const promises = await triggerCacheCreation(
|
188 | 234 | setupFunctions,
|
189 |
| - [...functionStrings, 'function3'], |
| 235 | + [...hashes, "hash3"], |
190 | 236 | downloadExtension,
|
191 | 237 | true
|
192 |
| - ) |
193 |
| - |
194 |
| - expect(promises).toHaveLength(3) |
195 |
| - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3) |
196 |
| - expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') |
197 |
| - expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') |
198 |
| - expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3') |
199 |
| - }) |
200 |
| - }) |
201 |
| -}) |
| 238 | + ); |
| 239 | + |
| 240 | + expect(promises).toHaveLength(3); |
| 241 | + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3); |
| 242 | + expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); |
| 243 | + expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); |
| 244 | + expectCreateCacheForWalletSetupFunction(3, setupFunctions, "hash3"); |
| 245 | + }); |
| 246 | + }); |
| 247 | +}); |
0 commit comments