Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed Dec 3, 2024
1 parent 135f033 commit 0f1df5c
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 133 deletions.
2 changes: 1 addition & 1 deletion packages/cache/test/defineWalletSetup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const result = 1 + 2
Expand Down
310 changes: 178 additions & 132 deletions packages/cache/test/utils/triggerCacheCreation.test.ts
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");
});
});
});

0 comments on commit 0f1df5c

Please sign in to comment.