Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testMatch: ['**/*.test.ts'],
preset: "ts-jest",
testEnvironment: "node",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testMatch: ["**/*.test.ts"],
transform: {
'^.+\\.tsx?$': 'ts-jest',
"^.+\\.tsx?$": "ts-jest",
},
setupFiles: ['dotenv/config'],
};
setupFiles: ["dotenv/config"],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"typescript": "^5.7.3",
"typescript-eslint": "^8.5.0"
}
}
}
101 changes: 61 additions & 40 deletions script/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios, { AxiosResponse } from 'axios';
import chalk from 'chalk';
import axios, { AxiosResponse } from "axios";
import chalk from "chalk";

const PROD_URL = 'https://ref-sdk-api.fly.dev';
const LOCAL_URL = 'http://localhost:3000';
const PROD_URL = "https://ref-sdk-api.fly.dev";
const LOCAL_URL = "http://localhost:3000";

type QueryParams = {
token?: string;
Expand All @@ -25,51 +25,66 @@ interface EndpointResponse {
const endpoints = [
// '/api/token-metadata',
// '/api/whitelist-tokens', I made some changes to whitelist-tokens so they differ
'/api/swap',
'/api/near-price',
'/api/ft-tokens',
"/api/swap",
"/api/near-price",
"/api/ft-tokens",
// '/api/all-token-balance-history',
// '/api/transactions-transfer-history'
] as const;

type Endpoint = typeof endpoints[number];
type Endpoint = (typeof endpoints)[number];

async function compareEndpoints(endpoint: Endpoint, queryParams: QueryParams = {}): Promise<void> {
async function compareEndpoints(
endpoint: Endpoint,
queryParams: QueryParams = {}
): Promise<void> {
console.log(chalk.blue(`\nTesting ${endpoint}`));
console.log(chalk.blue('Query params:', JSON.stringify(queryParams, null, 2)));

console.log(
chalk.blue("Query params:", JSON.stringify(queryParams, null, 2))
);

try {
const prodUrl = `${PROD_URL}${endpoint}`;
const localUrl = `${LOCAL_URL}${endpoint}`;

console.log(chalk.blue(`Making requests to:`));
console.log(chalk.blue(`PROD: ${prodUrl}`));
console.log(chalk.blue(`LOCAL: ${localUrl}`));

let prodRes: AxiosResponse | null = null;
let localRes: AxiosResponse | null = null;

try {
prodRes = await axios.get(prodUrl, { params: queryParams });
console.log(chalk.green('✓ Production request successful'));
console.log(chalk.green("✓ Production request successful"));
} catch (error) {
console.log(chalk.red('✗ Production request failed:'));
console.log(chalk.red("✗ Production request failed:"));
if (axios.isAxiosError(error)) {
console.log(chalk.red(`Status: ${error.response?.status}`));
console.log(chalk.red(`Error: ${error.message}`));
console.log(chalk.red(`Response data:`, JSON.stringify(error.response?.data, null, 2)));
console.log(
chalk.red(
`Response data:`,
JSON.stringify(error.response?.data, null, 2)
)
);
}
}

try {
localRes = await axios.get(localUrl, { params: queryParams });
console.log(chalk.green('✓ Local request successful'));
console.log(chalk.green("✓ Local request successful"));
} catch (error) {
console.log(chalk.red('✗ Local request failed:'));
console.log(chalk.red("✗ Local request failed:"));
if (axios.isAxiosError(error)) {
console.log(chalk.red(`Status: ${error.response?.status}`));
console.log(chalk.red(`Error: ${error.message}`));
console.log(chalk.red(`Response data:`, JSON.stringify(error.response?.data, null, 2)));
console.log(
chalk.red(
`Response data:`,
JSON.stringify(error.response?.data, null, 2)
)
);
}
}

Expand All @@ -81,34 +96,40 @@ async function compareEndpoints(endpoint: Endpoint, queryParams: QueryParams = {
const localData = JSON.stringify(localRes.data, null, 2);

if (prodData === localData) {
console.log(chalk.green('✓ Responses match'));
console.log(chalk.green("✓ Responses match"));
} else {
const prodLines = prodData.split('\n');
const localLines = localData.split('\n');
const diffCount = prodLines.reduce((count, line, index) =>
count + (line !== localLines[index] ? 1 : 0), 0);

const prodLines = prodData.split("\n");
const localLines = localData.split("\n");
const diffCount = prodLines.reduce(
(count, line, index) => count + (line !== localLines[index] ? 1 : 0),
0
);

console.log(chalk.red(`✗ Responses differ (${diffCount} lines):`));
console.log('\nDifferences:');
console.log("\nDifferences:");

prodLines.forEach((line, index) => {
if (line !== localLines[index]) {
console.log(chalk.red(`Line ${index + 1}:`));
console.log(chalk.yellow(`Prod: ${line}`));
console.log(chalk.yellow(`Local: ${localLines[index] || '(missing)'}`));
console.log(
chalk.yellow(`Local: ${localLines[index] || "(missing)"}`)
);
console.log();
}
});
}
} catch (error) {
console.log(chalk.red(`Error in comparison:`));
console.log(chalk.red(error instanceof Error ? error.stack : String(error)));
console.log(
chalk.red(error instanceof Error ? error.stack : String(error))
);
}
}

async function runTests(queryParams: QueryParams): Promise<void> {
console.log(chalk.cyan('Starting endpoint comparison tests...'));
console.log(chalk.cyan('Using query parameters:'));
console.log(chalk.cyan("Starting endpoint comparison tests..."));
console.log(chalk.cyan("Using query parameters:"));
console.log(queryParams);

for (const endpoint of endpoints) {
Expand All @@ -119,15 +140,15 @@ async function runTests(queryParams: QueryParams): Promise<void> {
// Usage example:
// Replace this object with your desired query parameters
const queryParams: QueryParams = {
token: 'near',
account: 'testing-treasury.sputnik-dao.near',
account_id: 'testing-treasury.sputnik-dao.near',
token_id: 'near',
treasuryDaoID: 'testing-treasury.sputnik-dao.near',
tokenIn: 'near',
tokenOut: 'wrap.near',
amountIn: '1000000000000000000000000', // 1 NEAR (24 decimals)
slippage: '0.01'
token: "near",
account: "testing-treasury.sputnik-dao.near",
account_id: "testing-treasury.sputnik-dao.near",
token_id: "near",
treasuryDaoID: "testing-treasury.sputnik-dao.near",
tokenIn: "near",
tokenOut: "wrap.near",
amountIn: "1000000000000000000000000", // 1 NEAR (24 decimals)
slippage: "0.01",
};

runTests(queryParams);
Loading
Loading