Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed Dec 3, 2024
1 parent 0f1df5c commit 609f1a5
Showing 1 changed file with 134 additions and 190 deletions.
324 changes: 134 additions & 190 deletions packages/cache/test/utils/triggerCacheCreation.test.ts
Original file line number Diff line number Diff line change
@@ -1,247 +1,191 @@
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 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"]);
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"));

const promises = await triggerCacheCreation(
setupFunctions,
[...hashes, "hash3"],
downloadExtension,
false
);

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"]);
fs.mkdirSync(path.join(ROOT_DIR, 'hash2'))

const promises = await triggerCacheCreation(setupFunctions, [...hashes, 'hash3'], downloadExtension, false)

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'])

// 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,
[...hashes, "hash3"],
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([...hashes, "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"));

const promises = await triggerCacheCreation(
setupFunctions,
[...hashes, "hash3"],
downloadExtension,
true
);

expect(promises).toHaveLength(3);
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3);
expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1");
expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2");
expectCreateCacheForWalletSetupFunction(3, setupFunctions, "hash3");
});
});
});
fs.mkdirSync(path.join(ROOT_DIR, 'hash2'))

const promises = await triggerCacheCreation(setupFunctions, [...hashes, 'hash3'], downloadExtension, true)

expect(promises).toHaveLength(3)
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3)
expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1')
expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2')
expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3')
})
})
})

0 comments on commit 609f1a5

Please sign in to comment.