Skip to content

Commit

Permalink
add tests for unclaimed-swaps (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 authored Feb 22, 2024
1 parent 3960479 commit 521ae47
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/router/src/grpc/view-protocol-server/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export interface IndexedDbMock {
getStateCommitmentTree?: Mock;
getSwapByNullifier?: Mock;
getTransactionInfo?: Mock;
iterateAssetsMetadata?: () => Partial<AsyncIterable<Mock>>;
iterateTransactionInfo?: () => Partial<AsyncIterable<Mock>>;
iterateSpendableNotes?: () => Partial<AsyncIterable<Mock>>;
iterateAssetsMetadata?: () => Partial<AsyncIterable<Mock>>;
iterateSwaps?: () => Partial<AsyncIterable<Mock>>;
subscribe?: (table: string) => Partial<AsyncIterable<Mock>>;
getSwapByCommitment?: Mock;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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=' },
}),
];

0 comments on commit 521ae47

Please sign in to comment.