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

docs(blockapis): improve documentation for UtxoApi test suite #4432

Merged
merged 1 commit into from
Apr 12, 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
56 changes: 56 additions & 0 deletions modules/blockapis/test/UtxoApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* This test suite iterates over a set of backends and test methods.
*
* For each backend and method, we test that the backend can fetch the resource.
*
* We then compare the normalized results from all backends to make sure they are the same.
*/

import 'mocha';
import * as assert from 'assert';
import { BlockchairApi, BlockstreamApi, UtxoApi, CachingHttpClient } from '../src';
Expand Down Expand Up @@ -34,13 +42,30 @@ function getTestTransactionIds(coinName: string): string[] {

type MethodArguments = unknown[];

/**
* A test case for a UtxoApi method.
*/
class TestCase<T> {
/**
* @param coinName - coin to test
* @param methodName - method to test
* @param args - method arguments
*/
constructor(public coinName: string, public methodName: keyof UtxoApi, public args: unknown[]) {}

/**
* Call the method on the given API.
* @param api
*/
func(api: UtxoApi) {
return (api[this.methodName] as any)(...this.args);
}

/**
* Get the fixture for this test case.
* @param api
* @param defaultValue
*/
async getFixture(api: UtxoApi, defaultValue?: T): Promise<T> {
const filename = [
'UtxoApi',
Expand All @@ -53,6 +78,10 @@ class TestCase<T> {
return await getFixture(`${__dirname}/fixtures/${filename}`, defaultValue);
}

/**
* Get the fixture, but with the API-specific fields removed.
* @param api
*/
async getFixtureNormal(api: UtxoApi): Promise<T> {
if (this.methodName === 'getTransactionStatus') {
// remove api-specific fields
Expand All @@ -78,6 +107,9 @@ class TestCase<T> {
return false;
}

/**
* @return a human-readable title for this test case.
*/
title(): string {
function elide(s: string, len: number) {
return s.length > len ? `${s.slice(0, len)}...` : s;
Expand All @@ -86,10 +118,18 @@ class TestCase<T> {
}
}

/**
* @param name
* @return a new CachingHttpClient that reads from the given fixture directory.
*/
function getHttpClient(name: string): CachingHttpClient {
return new CachingHttpClient(`${__dirname}/fixtures/responses/` + name, { isHttpEnabled: isHttpEnabled() });
}

/**
* @param coinName
* @return a list of APIs to test.
*/
function getApis(coinName: string): UtxoApi[] {
if (coinName === 'tbtc') {
return [
Expand All @@ -101,6 +141,10 @@ function getApis(coinName: string): UtxoApi[] {
return [];
}

/**
* @param coinName
* @return a list of test cases for the given coin.
*/
function getTestCases(coinName: string): TestCase<unknown>[] {
function getArguments(coinName: string, methodName: keyof UtxoApi): MethodArguments[] {
switch (methodName) {
Expand Down Expand Up @@ -130,6 +174,12 @@ function getTestCases(coinName: string): TestCase<unknown>[] {
);
}

/**
* Set up fetch tests for the given API.
*
* @param api
* @param coinName
*/
function runTestFetch(api: UtxoApi, coinName: string) {
getTestCases(coinName).forEach((testCase) => {
describe(`${api.constructor.name} ${testCase.title()}`, function () {
Expand All @@ -146,6 +196,12 @@ function runTestFetch(api: UtxoApi, coinName: string) {
});
}

/**
* Set up comparison tests for the given API.
*
* @param api
* @param coinName
*/
function runTestCompare(api: UtxoApi, coinName: string) {
getTestCases(coinName).forEach((testCase) => {
describe(`method ${testCase.title()}`, function () {
Expand Down
Loading