From 3d40211e2f57c8bd652d5b73599832c428c5ea1d Mon Sep 17 00:00:00 2001 From: valentine Date: Wed, 21 Feb 2024 22:30:52 +0200 Subject: [PATCH] add tests for unclaimed-swaps --- .../grpc/view-protocol-server/test-utils.ts | 3 +- .../unclaimed-swaps.test.ts | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 packages/router/src/grpc/view-protocol-server/unclaimed-swaps.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 b95f075437..1b925652aa 100644 --- a/packages/router/src/grpc/view-protocol-server/test-utils.ts +++ b/packages/router/src/grpc/view-protocol-server/test-utils.ts @@ -12,9 +12,10 @@ export interface IndexedDbMock { getStateCommitmentTree?: Mock; getSwapByNullifier?: Mock; getTransactionInfo?: Mock; + iterateAssetsMetadata?: () => Partial>; iterateTransactionInfo?: () => Partial>; iterateSpendableNotes?: () => Partial>; - iterateAssetsMetadata?: () => Partial>; + iterateSwaps?: () => Partial>; subscribe?: (table: string) => Partial>; getSwapByCommitment?: Mock; } diff --git a/packages/router/src/grpc/view-protocol-server/unclaimed-swaps.test.ts b/packages/router/src/grpc/view-protocol-server/unclaimed-swaps.test.ts new file mode 100644 index 0000000000..98d6d53fbe --- /dev/null +++ b/packages/router/src/grpc/view-protocol-server/unclaimed-swaps.test.ts @@ -0,0 +1,81 @@ +import { ViewService } from '@buf/penumbra-zone_penumbra.connectrpc_es/penumbra/view/v1/view_connect'; +import { servicesCtx } from '../../ctx'; + +import { createContextValues, createHandlerContext, HandlerContext } from '@connectrpc/connect'; + +import { beforeEach, describe, expect, test, vi } from 'vitest'; + +import { + SwapRecord, + UnclaimedSwapsRequest, + UnclaimedSwapsResponse, +} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; +import { IndexedDbMock, MockServices } from './test-utils'; +import { Services } from '@penumbra-zone/services'; +import { unclaimedSwaps } from './unclaimed-swaps'; + +describe('UnclaimedSwaps request handler', () => { + let mockServices: MockServices; + let mockCtx: HandlerContext; + let mockIndexedDb: IndexedDbMock; + let req: UnclaimedSwapsRequest; + + beforeEach(() => { + vi.resetAllMocks(); + + const mockIterateSwaps = { + next: vi.fn(), + [Symbol.asyncIterator]: () => mockIterateSwaps, + }; + + mockIndexedDb = { + iterateSwaps: () => mockIterateSwaps, + }; + + mockServices = { + getWalletServices: vi.fn(() => Promise.resolve({ indexedDb: mockIndexedDb })), + }; + + mockCtx = createHandlerContext({ + service: ViewService, + method: ViewService.methods.unclaimedSwaps, + protocolName: 'mock', + requestMethod: 'MOCK', + contextValues: createContextValues().set(servicesCtx, mockServices as unknown as Services), + }); + + for (const record of testData) { + mockIterateSwaps.next.mockResolvedValueOnce({ + value: record, + }); + } + mockIterateSwaps.next.mockResolvedValueOnce({ + done: true, + }); + req = new UnclaimedSwapsRequest(); + }); + + test('should get all swaps with undefined heightClaimed', async () => { + const responses: UnclaimedSwapsResponse[] = []; + for await (const res of unclaimedSwaps(req, mockCtx)) { + responses.push(new UnclaimedSwapsResponse(res)); + } + expect(responses.length).toBe(2); + }); +}); + +const testData: SwapRecord[] = [ + SwapRecord.fromJson({ + swapCommitment: { inner: '16VBVkrk+s18q+Sjhl8uEGfS3i0dwF1FrkNm8Db6VAA=' }, + heightClaimed: 122, + nullifier: { inner: '1E7LbhBDgDXHiRvreFyCllcKOOQeuIVsbn2aw8uKhww=' }, + }), + SwapRecord.fromJson({ + swapCommitment: { inner: '26VBVkrk+s18q+Sjhl8uEGfS3i0dwF1FrkNm8Db6VAA=' }, + nullifier: { inner: '2E7LbhBDgDXHiRvreFyCllcKOOQeuIVsbn2aw8uKhww=' }, + }), + SwapRecord.fromJson({ + swapCommitment: { inner: '36VBVkrk+s18q+Sjhl8uEGfS3i0dwF1FrkNm8Db6VAA=' }, + nullifier: { inner: '3E7LbhBDgDXHiRvreFyCllcKOOQeuIVsbn2aw8uKhww=' }, + }), +];