-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for asset-metadata-by-id.test (#523)
- Loading branch information
1 parent
2e455ba
commit 97b635b
Showing
2 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
packages/router/src/grpc/view-protocol-server/asset-metadata-by-id.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,122 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { | ||
AssetMetadataByIdRequest, | ||
AssetMetadataByIdResponse, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; | ||
import { ServicesInterface } from '@penumbra-zone/types'; | ||
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, ShieldedPoolMock } from './test-utils'; | ||
import { | ||
AssetId, | ||
Metadata, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
import { assetMetadataById } from './asset-metadata-by-id'; | ||
|
||
describe('AssetMetadataById request handler', () => { | ||
let mockServices: MockServices; | ||
let mockIndexedDb: IndexedDbMock; | ||
let mockCtx: HandlerContext; | ||
let mockShieldedPool: ShieldedPoolMock; | ||
let request: AssetMetadataByIdRequest; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
|
||
mockIndexedDb = { | ||
getAssetsMetadata: vi.fn(), | ||
}; | ||
mockShieldedPool = { | ||
assetMetadata: vi.fn(), | ||
}; | ||
mockServices = { | ||
getWalletServices: vi.fn(() => | ||
Promise.resolve({ | ||
indexedDb: mockIndexedDb, | ||
querier: { | ||
shieldedPool: mockShieldedPool, | ||
}, | ||
}), | ||
), | ||
}; | ||
mockCtx = createHandlerContext({ | ||
service: ViewService, | ||
method: ViewService.methods.assetMetadataById, | ||
protocolName: 'mock', | ||
requestMethod: 'MOCK', | ||
contextValues: createContextValues().set( | ||
servicesCtx, | ||
mockServices as unknown as ServicesInterface, | ||
), | ||
}); | ||
|
||
request = new AssetMetadataByIdRequest({ | ||
assetId: assetId, | ||
}); | ||
}); | ||
|
||
test('should successfully get metadata from idb when idb has them', async () => { | ||
mockIndexedDb.getAssetsMetadata?.mockResolvedValue(metadataFromIdb); | ||
const metadataByIdResponse = new AssetMetadataByIdResponse( | ||
await assetMetadataById(request, mockCtx), | ||
); | ||
expect(metadataByIdResponse.denomMetadata?.equals(metadataFromIdb)).toBeTruthy(); | ||
}); | ||
|
||
test('should successfully get metadata from node if record is not found in idb', async () => { | ||
mockIndexedDb.getAssetsMetadata?.mockResolvedValue(undefined); | ||
mockShieldedPool.assetMetadata.mockResolvedValueOnce(metadataFromNode); | ||
const metadataByIdResponse = new AssetMetadataByIdResponse( | ||
await assetMetadataById(request, mockCtx), | ||
); | ||
expect(metadataByIdResponse.denomMetadata?.equals(metadataFromNode)).toBeTruthy(); | ||
}); | ||
|
||
test('should fail to get metadata when metadata not found in idb and node', async () => { | ||
mockIndexedDb.getAssetsMetadata?.mockResolvedValue(undefined); | ||
mockShieldedPool.assetMetadata.mockResolvedValueOnce(undefined); | ||
await expect(assetMetadataById(request, mockCtx)).rejects.toThrow(); | ||
}); | ||
|
||
test('should fail if assetId is missing in request', async () => { | ||
await expect(assetMetadataById(new AssetMetadataByIdRequest(), mockCtx)).rejects.toThrow( | ||
'No asset id passed in request', | ||
); | ||
}); | ||
}); | ||
|
||
const assetId = AssetId.fromJson({ | ||
inner: 'nwPDkQq3OvLnBwGTD+nmv1Ifb2GEmFCgNHrU++9BsRE=', | ||
}); | ||
|
||
const metadataFromNode = Metadata.fromJson({ | ||
description: '', | ||
denomUnits: [ | ||
{ denom: 'gm', exponent: 9, aliases: [] }, | ||
{ denom: 'mgm', exponent: 3, aliases: [] }, | ||
{ denom: 'ugm', exponent: 0, aliases: [] }, | ||
], | ||
base: 'ugm', | ||
display: 'gm', | ||
name: '', | ||
symbol: '', | ||
penumbraAssetId: { | ||
inner: 'nwPDkQq3OvLnBwGTD+nmv1Ifb2GEmFCgNHrU++9BsRE=', | ||
}, | ||
}); | ||
const metadataFromIdb = Metadata.fromJson({ | ||
description: '', | ||
denomUnits: [ | ||
{ denom: 'gn', exponent: 6, aliases: [] }, | ||
{ denom: 'mgn', exponent: 3, aliases: [] }, | ||
{ denom: 'ugn', exponent: 0, aliases: [] }, | ||
], | ||
base: 'ugn', | ||
display: 'gn', | ||
name: '', | ||
symbol: '', | ||
penumbraAssetId: { | ||
inner: 'nwPDkQq3OvLnBwGTD+nmv1Ifb2GEmFCgNHrU++9BsRE=', | ||
}, | ||
}); |
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