Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for transaction-planner #690

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { TransactionPlannerRequest } 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 { servicesCtx } from '../../ctx';
import { IndexedDbMock, MockServices, ViewServerMock } from '../test-utils';
import type { Services } from '@penumbra-zone/services';
import { FmdParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/shielded_pool/v1/shielded_pool_pb';
import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb';
import { SctParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/sct/v1/sct_pb';
import { GasPrices } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/fee/v1/fee_pb';
import { transactionPlanner } from './transaction-planner';

const mockPlanTransaction = vi.hoisted(() => vi.fn());
vi.mock('@penumbra-zone/wasm', () => ({
planTransaction: mockPlanTransaction,
}));
Comment on lines +14 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't get rid of the indexed-db global in wasm and we don't have that goal anymore, so we have no choice but to use mock here

describe('TransactionPlanner request handler', () => {
let mockServices: MockServices;
let mockIndexedDb: IndexedDbMock;
let mockViewServer: ViewServerMock;
let mockCtx: HandlerContext;
let req: TransactionPlannerRequest;

beforeEach(() => {
vi.resetAllMocks();

mockIndexedDb = {
getFmdParams: vi.fn(),
getAppParams: vi.fn(),
getGasPrices: vi.fn(),
constants: vi.fn(),
};
mockViewServer = {
fullViewingKey: vi.fn(),
};
mockServices = {
getWalletServices: vi.fn(() =>
Promise.resolve({
indexedDb: mockIndexedDb,
viewServer: mockViewServer,
}),
),
};

mockCtx = createHandlerContext({
service: ViewService,
method: ViewService.methods.transactionPlanner,
protocolName: 'mock',
requestMethod: 'MOCK',
url: '/mock',
contextValues: createContextValues().set(servicesCtx, mockServices as unknown as Services),
});
mockViewServer.fullViewingKey?.mockReturnValueOnce(
'penumbrafullviewingkey1vzfytwlvq067g2kz095vn7sgcft47hga40atrg5zu2crskm6tyyjysm28qg5nth2fqmdf5n0q530jreumjlsrcxjwtfv6zdmfpe5kqsa5lg09',
);

req = new TransactionPlannerRequest({});
});

test('should create a transaction plan if all necessary data exists in indexed-db', async () => {
mockIndexedDb.getFmdParams?.mockResolvedValueOnce(
new FmdParameters({
precisionBits: 12,
asOfBlockHeight: 2n,
}),
);
mockIndexedDb.getAppParams?.mockResolvedValueOnce(
new AppParameters({
chainId: 'penumbra-testnet-mock',
sctParams: new SctParameters({
epochDuration: 719n,
}),
}),
);
mockIndexedDb.getGasPrices?.mockResolvedValueOnce(
new GasPrices({
verificationPrice: 22n,
executionPrice: 222n,
blockSpacePrice: 2n,
compactBlockSpacePrice: 120n,
}),
);
await transactionPlanner(req, mockCtx);

expect(mockPlanTransaction.mock.calls.length === 1).toBeTruthy();
});

test('should throw error if FmdParameters not available', async () => {
await expect(transactionPlanner(req, mockCtx)).rejects.toThrow('FmdParameters not available');
});

test('should throw error if SctParameters not available', async () => {
mockIndexedDb.getFmdParams?.mockResolvedValueOnce(new FmdParameters());
mockIndexedDb.getAppParams?.mockResolvedValueOnce(
new AppParameters({
chainId: 'penumbra-testnet-mock',
}),
);
await expect(transactionPlanner(req, mockCtx)).rejects.toThrow('SctParameters not available');
});

test('should throw error if ChainId not available', async () => {
mockIndexedDb.getFmdParams?.mockResolvedValueOnce(new FmdParameters());
mockIndexedDb.getAppParams?.mockResolvedValueOnce(
new AppParameters({
sctParams: new SctParameters({
epochDuration: 719n,
}),
}),
);
await expect(transactionPlanner(req, mockCtx)).rejects.toThrow('ChainId not available');
});

test('should throw error if Gas prices is not available', async () => {
mockIndexedDb.getFmdParams?.mockResolvedValueOnce(new FmdParameters());
mockIndexedDb.getAppParams?.mockResolvedValueOnce(
new AppParameters({
chainId: 'penumbra-testnet-mock',
sctParams: new SctParameters({
epochDuration: 719n,
}),
}),
);
await expect(transactionPlanner(req, mockCtx)).rejects.toThrow('Gas prices is not available');
});
});
Loading