-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): Add
getUniqueWalletSetupFunctions
function (#953)
- Loading branch information
1 parent
cbe2365
commit d488acf
Showing
2 changed files
with
206 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import path from 'node:path' | ||
import type { WalletSetupFunction } from '../defineWalletSetup' | ||
import { getWalletSetupFiles } from './getWalletSetupFiles' | ||
import { importWalletSetupFile } from './importWalletSetupFile' | ||
|
||
type DuplicatesMap = Record<string, { fileName: string; setupFunction: WalletSetupFunction }[]> | ||
|
||
export async function getUniqueWalletSetupFunctions(walletSetupDirPath: string) { | ||
const fileList = await getWalletSetupFiles(walletSetupDirPath) | ||
|
||
const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>() | ||
|
||
const duplicatesMap: DuplicatesMap = {} | ||
|
||
for (const fileName of fileList) { | ||
const walletSetupFilePath = path.join(walletSetupDirPath, fileName) | ||
const { hash, fn } = await importWalletSetupFile(walletSetupFilePath) | ||
|
||
const entry = { | ||
fileName, | ||
setupFunction: fn | ||
} | ||
|
||
// Since we're checking for duplicates later, we can safely forget about overwriting. | ||
setupFunctions.set(hash, entry) | ||
|
||
if (duplicatesMap[hash]) { | ||
duplicatesMap[hash]?.push(entry) | ||
} else { | ||
duplicatesMap[hash] = [entry] | ||
} | ||
} | ||
|
||
throwIfThereAreDuplicatedHashes(duplicatesMap) | ||
|
||
return setupFunctions | ||
} | ||
|
||
function throwIfThereAreDuplicatedHashes(duplicatesMap: DuplicatesMap) { | ||
const duplicatedHashes = Object.entries(duplicatesMap).filter( | ||
([, setupFunctionsArray]) => setupFunctionsArray.length > 1 | ||
) | ||
|
||
if (duplicatedHashes.length === 0) { | ||
return | ||
} | ||
|
||
const sortedDuplicatedHashes = duplicatedHashes.sort(([hashA], [hashB]) => hashA.localeCompare(hashB)) | ||
|
||
throw new Error( | ||
[ | ||
'[GetUniqueWalletSetupFunctions] There are identical wallet setup functions:', | ||
sortedDuplicatedHashes | ||
.map(([hash, setupFunctionsArray]) => { | ||
return `\t${hash} - [${setupFunctionsArray.map(({ fileName }) => fileName).join(', ')}]` | ||
}) | ||
.join('\n') | ||
].join('\n') | ||
) | ||
} |
146 changes: 146 additions & 0 deletions
146
packages/core/test/utils/getUniqueWalletSetupFunctions.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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { afterAll, describe, expect, it, vi } from 'vitest' | ||
import { getUniqueWalletSetupFunctions } from '../../src/utils/getUniqueWalletSetupFunctions' | ||
|
||
vi.mock('../../src/utils/getWalletSetupFiles', async () => { | ||
return { | ||
getWalletSetupFiles: async (path: string) => { | ||
if (path === 'invalid') { | ||
return [ | ||
'mallard-duck-1.setup.ts', | ||
'mallard-duck-2.setup.ts', | ||
'mandarin-duck-1.setup.ts', | ||
'mandarin-duck-2.setup.ts', | ||
'perry-the-platypus.setup.ts', | ||
'dr-heinz-doofenshmirtz.setup.ts' | ||
] | ||
} | ||
|
||
// Happy valid path. | ||
return ['perry-the-platypus.setup.ts', 'dr-heinz-doofenshmirtz.setup.ts'] | ||
} | ||
} | ||
}) | ||
|
||
vi.mock('../../src/utils/importWalletSetupFile', async () => { | ||
return { | ||
importWalletSetupFile: async (filePath: string) => { | ||
const fn = async () => { | ||
return | ||
} | ||
|
||
if (filePath.includes('mallard-duck')) { | ||
return { | ||
hash: '1ba3f33e2d4ee1b7567c', | ||
fn | ||
} | ||
} | ||
|
||
if (filePath.includes('mandarin-duck')) { | ||
return { | ||
hash: '5b7e41ab761187064b2a', | ||
fn | ||
} | ||
} | ||
|
||
if (filePath.includes('dr-heinz-doofenshmirtz')) { | ||
return { | ||
hash: '0a547b6910d7f1034196', | ||
fn | ||
} | ||
} | ||
|
||
// Perry the platypus 🕵️♂️ | ||
return { | ||
hash: 'f992037c880ad53e72f2', | ||
fn | ||
} | ||
} | ||
} | ||
}) | ||
|
||
vi.mock('./mallard-duck-1.setup.ts', async () => ({ | ||
default: { | ||
hash: '1ba3f33e2d4ee1b7567c', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
vi.mock('./mallard-duck-2.setup.ts', async () => ({ | ||
default: { | ||
hash: '1ba3f33e2d4ee1b7567c', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
vi.mock('./mandarin-duck-1.setup.ts', async () => ({ | ||
default: { | ||
hash: '5b7e41ab761187064b2a', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
vi.mock('./mandarin-duck-2.setup.ts', async () => ({ | ||
default: { | ||
hash: '5b7e41ab761187064b2a', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
vi.mock('./perry-the-platypus.setup.ts', async () => ({ | ||
default: { | ||
hash: 'f992037c880ad53e72f2', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
vi.mock('./dr-heinz-doofenshmirtz.setup.ts', async () => ({ | ||
default: { | ||
hash: '0a547b6910d7f1034196', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
})) | ||
|
||
describe('getUniqueWalletSetupFunctions', () => { | ||
afterAll(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
it('throws if there are identical setup functions', async () => { | ||
await expect(getUniqueWalletSetupFunctions('invalid')).rejects.toThrowError( | ||
[ | ||
'[GetUniqueWalletSetupFunctions] There are identical wallet setup functions:', | ||
'\t1ba3f33e2d4ee1b7567c - [mallard-duck-1.setup.ts, mallard-duck-2.setup.ts]', | ||
'\t5b7e41ab761187064b2a - [mandarin-duck-1.setup.ts, mandarin-duck-2.setup.ts]' | ||
].join('\n') | ||
) | ||
}) | ||
|
||
it('returns a map of unique setup functions', async () => { | ||
const setupFunctions = await getUniqueWalletSetupFunctions('valid') | ||
|
||
expect(setupFunctions.size).toEqual(2) | ||
expect(Array.from(setupFunctions.keys())).toEqual(['f992037c880ad53e72f2', '0a547b6910d7f1034196']) | ||
expect(Array.from(setupFunctions.values())).toEqual([ | ||
{ | ||
fileName: 'perry-the-platypus.setup.ts', | ||
setupFunction: expect.any(Function) | ||
}, | ||
{ | ||
fileName: 'dr-heinz-doofenshmirtz.setup.ts', | ||
setupFunction: expect.any(Function) | ||
} | ||
]) | ||
}) | ||
}) |