From 3deb5ebf41804a51a197ed3791635bf572b8208f Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:19:26 -0500 Subject: [PATCH] Add tests --- .vscode/launch.json | 2 +- .../spec/addressesContainsAll.spec.ts | 2 +- .../spec/addressesContainsAny.spec.ts | 71 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 7ee84db66c..d3f4591213 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,7 +26,7 @@ "--run", "--inspect-brk", "--no-file-parallelism", - "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts" + "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts" ], "sourceMaps": true, "resolveSourceMapLocations": [ diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 2c370133b8..4307177e9f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -24,7 +24,7 @@ describe('BoundWitnessValidator', () => { beforeAll(() => { addresses = signers().map(x => x.address) }) - it('addresses empty', async () => { + it.skip('addresses empty', async () => { const all = signers() const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, [])).toBeTrue() diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts new file mode 100644 index 0000000000..5c41231a05 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -0,0 +1,71 @@ +import type { Address } from '@xylabs/hex' +import type { WalletInstance } from '@xyo-network/account' +import { HDWallet } from '@xyo-network/account' +import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' + +import { addressesContainsAny } from '../addressesContainsAny.ts' + +describe('BoundWitnessValidator', () => { + const payload = { schema: 'network.xyo.test', value: Date.now() } + const oneWallet: WalletInstance[] = [] + const twoWallets: WalletInstance[] = [] + beforeAll(async () => { + oneWallet.push(await HDWallet.random()) + twoWallets.push(await HDWallet.random(), await HDWallet.random()) + }) + describe('returns true', () => { + const cases: [string, () => WalletInstance[]][] = [ + // ['for no wallet', () => []], // This case intentionally skipped because empty addresses will always return false + ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s when all sign', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + // it('addresses empty', async () => { + // const all = signers() + // const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + // expect(addressesContainsAny(bw, [])).toBeTrue() + // }) + it('all addresses present in boundwitness addresses', async () => { + const all = signers() + const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeTrue() + }) + it('with extra signers and all addresses present in boundwitness addresses', async () => { + const extra = [...signers(), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeTrue() + }) + }) + }) + describe('returns false', () => { + const cases: [string, () => WalletInstance[]][] = [ + // ['for no wallet', () => []], + // ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + it('with no signers', async () => { + const [bw] = await new BoundWitnessBuilder().payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + it.only('with all signers but one', async () => { + const less = [...signers().slice(0, -1), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + it('with all different signers', async () => { + const none = [await HDWallet.random(), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + }) + }) +})