From 5ed62ea87179524d1c0a9d3e2056776dc054e2fc Mon Sep 17 00:00:00 2001 From: Rachel Zabib Date: Mon, 31 Aug 2026 14:46:13 +0300 Subject: [PATCH] Enhance rates test suite by adding support for multiple account types and validating rate responses. --- .../tests/server-tests/rates.test.ts | 82 ++++++++++++------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/v2/api-validator/tests/server-tests/rates.test.ts b/v2/api-validator/tests/server-tests/rates.test.ts index a90605f5..6c021088 100644 --- a/v2/api-validator/tests/server-tests/rates.test.ts +++ b/v2/api-validator/tests/server-tests/rates.test.ts @@ -1,10 +1,28 @@ -import { randomUUID } from 'crypto'; import Client from '../../src/client'; -import { ApiError, BadRequestError } from '../../src/client/generated'; +import { ApiComponents, ApiError, BadRequestError, Rate } from '../../src/client/generated'; import { getAllCapableAccountIds, hasCapability } from '../utils/capable-accounts'; const noRatesCapability = !hasCapability('rates'); -const accountIds = getAllCapableAccountIds('rates'); +const ratesAccountIds = getAllCapableAccountIds('rates'); + +/** + * A rate can only be requested for a pair the account itself advertises, so the account + * under test must support both `rates` and the component that exposes the pair IDs. + */ +function findRatesAccountSupporting(component: keyof ApiComponents): string | undefined { + const capableAccountIds = new Set(getAllCapableAccountIds(component)); + return ratesAccountIds.find((accountId) => capableAccountIds.has(accountId)); +} + +const rampsAccountId = findRatesAccountSupporting('ramps'); +const liquidityAccountId = findRatesAccountSupporting('liquidity'); + +function expectValidRate(rate: Rate): void { + expect(rate).toHaveProperty('rate'); + expect(rate).toHaveProperty('timestamp'); + expect(typeof rate.rate).toBe('string'); + expect(typeof rate.timestamp).toBe('number'); +} describe.skipIf(noRatesCapability)('Rates', () => { let client: Client; @@ -14,7 +32,7 @@ describe.skipIf(noRatesCapability)('Rates', () => { }); describe('Get rate by account and assets', () => { - const accountId = accountIds[0]; + const accountId = ratesAccountIds[0]; if (!accountId) { it('should have at least one account with rates capability', () => { @@ -23,50 +41,52 @@ describe.skipIf(noRatesCapability)('Rates', () => { return; } - describe('Successful rate retrieval', () => { - it('should return rate for conversion pair ID', async () => { - const conversionPairId = randomUUID(); - const response = await client.rates.getRateByAccountAndPairId({ - accountId, - conversionPairId, - rampsPairId: '', - orderBookPairId: '', + // Order book pairs are intentionally not covered: the API exposes no endpoint for + // discovering order book pair IDs, so a supported one cannot be resolved for a provider. + + describe.skipIf(!rampsAccountId)('Ramps pair', () => { + let rampsPairId: string; + + beforeAll(async () => { + const { capabilities } = await client.capabilities.getRampMethods({ + accountId: rampsAccountId as string, }); - expect(response).toHaveProperty('rate'); - expect(response).toHaveProperty('timestamp'); - expect(typeof response.rate).toBe('string'); - expect(typeof response.timestamp).toBe('number'); + expect(capabilities.length).toBeGreaterThan(0); + rampsPairId = capabilities[0].id; }); it('should return rate for ramps pair ID', async () => { - const rampsPairId = randomUUID(); const response = await client.rates.getRateByAccountAndPairId({ - accountId, + accountId: rampsAccountId as string, conversionPairId: '', rampsPairId, orderBookPairId: '', }); - expect(response).toHaveProperty('rate'); - expect(response).toHaveProperty('timestamp'); - expect(typeof response.rate).toBe('string'); - expect(typeof response.timestamp).toBe('number'); + expectValidRate(response); + }); + }); + + describe.skipIf(!liquidityAccountId)('Conversion pair', () => { + let conversionPairId: string; + + beforeAll(async () => { + const { capabilities } = await client.capabilities.getQuoteCapabilities({}); + + expect(capabilities.length).toBeGreaterThan(0); + conversionPairId = capabilities[0].id; }); - it('should return rate for order book pair ID', async () => { - const orderBookPairId = randomUUID(); + it('should return rate for conversion pair ID', async () => { const response = await client.rates.getRateByAccountAndPairId({ - accountId, - conversionPairId: '', + accountId: liquidityAccountId as string, + conversionPairId, rampsPairId: '', - orderBookPairId, + orderBookPairId: '', }); - expect(response).toHaveProperty('rate'); - expect(response).toHaveProperty('timestamp'); - expect(typeof response.rate).toBe('string'); - expect(typeof response.timestamp).toBe('number'); + expectValidRate(response); }); });