Skip to content

Commit

Permalink
feat: add request function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jul 18, 2024
1 parent 2c33749 commit 5cc0e95
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
70 changes: 35 additions & 35 deletions src/functions/attestor/attestor-request.functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { complement, equals, filter, isEmpty, isNil, join, map, prop } from 'ramda';
import { equals, filter, isEmpty, isNotNil, join, map, prop } from 'ramda';

import {
FundingTXAttestorInfo,
Expand All @@ -7,40 +7,6 @@ import {
import { AttestorError } from '../../models/errors.js';
import { sendRequest } from '../request/request.functions.js';

const processAttestorResponses = async (attestorEndpoints: string[], requestBody: string) => {
const attestorErrorResponses: string[] = filter(
complement(isNil),
await Promise.all(
map(
url => sendRequest(url, requestBody).catch(error => prop('message', error)),
attestorEndpoints
)
)
);

if (equals(attestorEndpoints.length, attestorErrorResponses.length)) {
throw new AttestorError(
`Error sending Transaction to Attestors: ${join('|', attestorErrorResponses)}`
);
}
};

export async function submitPSBT<T>(
attestorRootURLs: string[],
transactionInfo: T,
endpointPath: string,
transformBody: (transactionInfo: T) => object
): Promise<void> {
if (isEmpty(attestorRootURLs)) {
throw new AttestorError('No Attestor URLs provided');
}

const endpoints: string[] = attestorRootURLs.map(url => `${url}${endpointPath}`);
const requestBody: string = JSON.stringify(transformBody(transactionInfo));

await processAttestorResponses(endpoints, requestBody);
}

export async function submitFundingPSBT(
attestorRootURLs: string[],
fundingTXAttestorInfo: FundingTXAttestorInfo
Expand All @@ -63,3 +29,37 @@ export async function submitWithdrawDepositPSBT(
wd_psbt: info.withdrawDepositPSBT,
}));
}

export async function submitPSBT<T>(
attestorRootURLs: string[],
transactionInfo: T,
endpointPath: string,
transformBody: (transactionInfo: T) => object
): Promise<void> {
if (isEmpty(attestorRootURLs)) {
throw new AttestorError('No Attestor URLs provided');
}

const endpoints: string[] = attestorRootURLs.map(url => `${url}${endpointPath}`);
const requestBody: string = JSON.stringify(transformBody(transactionInfo));

await sendAndProcessRequests(endpoints, requestBody);
}

const sendAndProcessRequests = async (attestorEndpoints: string[], requestBody: string) => {
const attestorErrorResponses: string[] = filter(
isNotNil,
await Promise.all(
map(
url => sendRequest(url, requestBody).catch(error => prop('message', error)),
attestorEndpoints
)
)
);

if (equals(attestorEndpoints.length, attestorErrorResponses.length)) {
throw new AttestorError(
`Error sending Transaction to Attestors: ${join('|', attestorErrorResponses)}`
);
}
};
43 changes: 43 additions & 0 deletions tests/unit/request-functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { sendRequest } from '../../src/functions/request/request.functions';
import { TEST_REGTEST_ATTESTOR_APIS } from '../mocks/api.test.constants';

global.fetch = jest.fn();

describe('Request Functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('sendRequest', () => {
it('should not result in an error when the response status is ok', async () => {
jest
.spyOn(global, 'fetch')
.mockImplementationOnce(async () => new Response(null, { status: 200 }));

await expect(
sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody')
).resolves.not.toThrow();
});

it('should result in an error when the response status is not ok', async () => {
jest
.spyOn(global, 'fetch')
.mockImplementationOnce(
async () => new Response(null, { status: 400, statusText: 'Bad Request' })
);

await expect(sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody')).rejects.toThrow(
new Error(`Response ${TEST_REGTEST_ATTESTOR_APIS[0]} was not OK: Bad Request`)
);
});

it('should result in an error when the request fails', async () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(async () => {
throw new Error('Failed to fetch');
});

await expect(sendRequest(TEST_REGTEST_ATTESTOR_APIS[0], 'requestBody')).rejects.toThrow(
new Error(`Failed to fetch`)
);
});
});
});

0 comments on commit 5cc0e95

Please sign in to comment.