From 3ac4b84ad6d956632dcff18e26043889c2638cf4 Mon Sep 17 00:00:00 2001 From: Valentine Date: Thu, 22 Feb 2024 15:33:05 +0000 Subject: [PATCH] add tests for wallet-id (#594) --- .../grpc/view-protocol-server/test-utils.ts | 4 ++ .../view-protocol-server/wallet-id.test.ts | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 packages/router/src/grpc/view-protocol-server/wallet-id.test.ts diff --git a/packages/router/src/grpc/view-protocol-server/test-utils.ts b/packages/router/src/grpc/view-protocol-server/test-utils.ts index 6d6dee1a8d..5773c8dc95 100644 --- a/packages/router/src/grpc/view-protocol-server/test-utils.ts +++ b/packages/router/src/grpc/view-protocol-server/test-utils.ts @@ -40,3 +40,7 @@ export interface MockServices { shieldedPool?: ShieldedPoolMock; }; } + +export interface MockExtLocalCtx { + get: Mock; +} diff --git a/packages/router/src/grpc/view-protocol-server/wallet-id.test.ts b/packages/router/src/grpc/view-protocol-server/wallet-id.test.ts new file mode 100644 index 0000000000..bed6f27bb2 --- /dev/null +++ b/packages/router/src/grpc/view-protocol-server/wallet-id.test.ts @@ -0,0 +1,48 @@ +import { beforeEach, describe, expect, test, vi } from 'vitest'; +import { + WalletIdRequest, + WalletIdResponse, +} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; +import { createContextValues, createHandlerContext, HandlerContext } from '@connectrpc/connect'; +import { ViewService } from '@buf/penumbra-zone_penumbra.connectrpc_es/penumbra/view/v1/view_connect'; +import { extLocalCtx } from '../../ctx'; +import { walletId } from './wallet-id'; +import { MockExtLocalCtx } from './test-utils'; + +describe('WalletId request handler', () => { + let mockExtLocalCtx: MockExtLocalCtx; + let mockCtx: HandlerContext; + + beforeEach(() => { + vi.resetAllMocks(); + + mockExtLocalCtx = { + get: vi.fn().mockImplementation((key: string) => { + if (key === 'wallets') { + return Promise.resolve([{ id: 'mockWalletId' }]); + } else { + return Promise.resolve([]); + } + }), + }; + mockCtx = createHandlerContext({ + service: ViewService, + method: ViewService.methods.walletId, + protocolName: 'mock', + requestMethod: 'MOCK', + contextValues: createContextValues().set(extLocalCtx, mockExtLocalCtx as unknown), + }); + }); + + test('should successfully get wallet-id if wallet exists', async () => { + const walletIdResponse = new WalletIdResponse(await walletId(new WalletIdRequest(), mockCtx)); + expect(walletIdResponse).toBeDefined(); + }); + + test('should fail to get wallet-id if wallet does not exist', async () => { + mockExtLocalCtx.get.mockImplementation(() => { + return Promise.resolve([]); + }); + await expect(walletId(new WalletIdRequest(), mockCtx)).rejects.toThrow('No wallet'); + }); +});