-
Notifications
You must be signed in to change notification settings - Fork 17
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
Valentine1898
merged 1 commit into
main
from
410-add-tests-for-transactionplannerrequest
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
packages/router/src/grpc/view-protocol-server/transaction-planner.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
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'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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