Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
chore(tests): add integration tests for blockHeight and sortKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Nov 28, 2023
1 parent 08c691c commit b64fafb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const MAX_REQUEST_SIZE = 100;

export async function getDeployedContractsByWallet(
arweave: Arweave,
params: { address: string },
params: { address: string; blockHeight: number | undefined },
): Promise<{ ids: string[] }> {
const { address } = params;
let hasNextPage = false;
Expand Down Expand Up @@ -102,9 +102,9 @@ export async function getDeployedContractsByWallet(
export async function getWalletInteractionsForContract(
arweave: Arweave,
params: {
address?: string;
address: string | undefined;
contractTxId: string;
blockHeight: number;
blockHeight: number | undefined;
},
): Promise<{
interactions: Map<
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ export const queryMiddleware = async (ctx: KoaContext, next: Next) => {

// Note: this takes sortKey precedence over block height
if (sortKey) {
// TODO: add regex to sort key
// TODO: regex on sort key to match warp pattern
if (Array.isArray(sortKey)) {
logger.debug('Invalid sort key provided', { sortKey });
throw new BadRequestError('Invalid sort key, must be a single string');
}
logger.info('Sort key provided via query param', { sortKey });
// TODO: regex on sort key to match warp pattern
ctx.state.sortKeyOrBlockHeight = sortKey;
}

Expand Down
59 changes: 57 additions & 2 deletions tests/integration/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ describe('Integration tests', () => {
describe('/v1', () => {
describe('/contract', () => {
describe('/:contractTxId', () => {
it('should return the contract state and id without any evaluation options provided', async () => {
it('should return the contract state and id and default evaluation options', async () => {
const { status, data } = await axios.get(`/v1/contract/${id}`);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, state, evaluationOptions } = data;
const { contractTxId, state, evaluationOptions, sortKey } = data;
expect(contractTxId).to.equal(id);
expect(evaluationOptions).not.to.be.undefined;
expect(sortKey).not.be.undefined;
expect(state).to.include.keys([
'balances',
'owner',
Expand All @@ -150,6 +151,46 @@ describe('Integration tests', () => {
);
expect(status).to.equal(404);
});

it('should return a 400 on invalid block height query param', async () => {
// block height before the interactions were created
const invalidBlockHeight = 'not-a-number';

const { status } = await axios.get(
`/v1/contract/${id}/interactions?blockHeight=${invalidBlockHeight}`,
);
expect(status).to.equal(400);
});

it('should return a 400 when block height and sort key query params are provided', async () => {
// block height before the interactions were created
const validBlockHeight = 1;
const exampleSortKey = 'example-sort-key';

const { status } = await axios.get(
`/v1/contract/${id}/interactions?blockHeight=${validBlockHeight}&sortKey=${exampleSortKey}`,
);
expect(status).to.equal(400);
});

it('should return contract state evaluated up to a given block height query param', async () => {
// block height before the interactions were created
const blockHeight = 1;

const { status, data } = await axios.get(
`/v1/contract/${id}/interactions?blockHeight=${blockHeight}`,
);
const { contractTxId, state, evaluationOptions, sortKey } = data;
expect(status).to.equal(200);
expect(data).not.to.be.undefined;
expect(contractTxId).to.equal(id);
expect(evaluationOptions).not.to.be.undefined;
expect(state).not.to.be.undefined;
expect(sortKey).not.be.undefined;
expect(sortKey.split(',')[0]).to.equal(
`${blockHeight}`.padStart(12, '0'),
);
});
});
describe('/:contractTxId/price', () => {
it('should properly handle price interaction inputs', async () => {
Expand Down Expand Up @@ -205,6 +246,20 @@ describe('Integration tests', () => {
expect(contractTxId).to.equal(id);
expect(Object.keys(interactions)).not.to.contain(badInteractionTx.id);
});

it('should only return interactions up to a provided block height', async () => {
// block height before the interactions were created
const previousInteractionHeight = 0;

const { status, data } = await axios.get(
`/v1/contract/${id}/interactions?blockHeight=${previousInteractionHeight}`,
);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, interactions } = data;
expect(contractTxId).to.equal(id);
expect(interactions).to.equal([]);
});
});

describe('/:contractTxId/interactions/:address', () => {
Expand Down

0 comments on commit b64fafb

Please sign in to comment.