From bb3a3b2c0d03ee2d697f40bfc1276036db224dfc Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:33:33 -0800 Subject: [PATCH] fix lint / format issues --- src/__tests__/routes/health.test.ts | 8 +- src/__tests__/utils/graphql-mock.ts | 9 +- src/__tests__/validation/allocation.test.ts | 211 +++++++++--------- .../validation/compact-graphql.test.ts | 28 ++- .../validation/utils/compact-test-setup.ts | 15 +- 5 files changed, 159 insertions(+), 112 deletions(-) diff --git a/src/__tests__/routes/health.test.ts b/src/__tests__/routes/health.test.ts index 9ee5a36..fcb0e2f 100644 --- a/src/__tests__/routes/health.test.ts +++ b/src/__tests__/routes/health.test.ts @@ -3,6 +3,12 @@ import { createTestServer, cleanupTestServer } from '../utils/test-server'; import { mockSupportedChainsResponse } from '../utils/graphql-mock'; import { getFinalizationThreshold } from '../../chain-config'; +interface SupportedChain { + chainId: string; + allocatorId: string; + finalizationThresholdSeconds: number; +} + describe('Health Check Endpoint', () => { let server: FastifyInstance; @@ -41,7 +47,7 @@ describe('Health Check Endpoint', () => { mockChains.forEach((mockChain) => { const resultChain = result.supportedChains.find( - (chain: any) => chain.chainId === mockChain.chainId + (chain: SupportedChain) => chain.chainId === mockChain.chainId ); expect(resultChain).toBeDefined(); expect(resultChain?.allocatorId).toBe(mockChain.allocatorId); diff --git a/src/__tests__/utils/graphql-mock.ts b/src/__tests__/utils/graphql-mock.ts index d6c9a2f..48fb846 100644 --- a/src/__tests__/utils/graphql-mock.ts +++ b/src/__tests__/utils/graphql-mock.ts @@ -69,10 +69,15 @@ export function setupGraphQLMocks(): void { requestCallCount = 0; shouldFail = false; + type GraphQLRequestFn = ( + query: string, + variables?: Record + ) => Promise; + // Override the request method of the GraphQL client - (graphqlClient as any).request = async ( + (graphqlClient as { request: GraphQLRequestFn }).request = async ( query: string, - _variables: Record + _variables?: Record ) => { requestCallCount++; diff --git a/src/__tests__/validation/allocation.test.ts b/src/__tests__/validation/allocation.test.ts index 8af69cd..08a7536 100644 --- a/src/__tests__/validation/allocation.test.ts +++ b/src/__tests__/validation/allocation.test.ts @@ -7,9 +7,21 @@ import { AccountDeltasResponse, AccountResponse, fetchAndCacheSupportedChains, + SupportedChainsResponse, } from '../../graphql'; import { setupGraphQLMocks } from '../utils/graphql-mock'; +interface GraphQLDocument { + source: string; +} + +type GraphQLRequestFn = ( + query: string | GraphQLDocument, + variables?: Record +) => Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) +>; + describe('Allocation Validation', () => { let db: PGlite; let originalRequest: typeof graphqlClient.request; @@ -55,26 +67,25 @@ describe('Allocation Validation', () => { await fetchAndCacheSupportedChains(process.env.ALLOCATOR_ADDRESS!); // Mock GraphQL response with sufficient balance - (graphqlClient as any).request = async (): Promise< - AccountDeltasResponse & AccountResponse - > => ({ - accountDeltas: { - items: [], - }, - account: { - resourceLocks: { - items: [ - { - withdrawalStatus: 0, - balance: '1000000000000000000000', // 1000 ETH - }, - ], - }, - claims: { + (graphqlClient as { request: GraphQLRequestFn }).request = + async (): Promise => ({ + accountDeltas: { items: [], }, - }, - }); + account: { + resourceLocks: { + items: [ + { + withdrawalStatus: 0, + balance: '1000000000000000000000', // 1000 ETH + }, + ], + }, + claims: { + items: [], + }, + }, + }); // Clear test data await db.query('DELETE FROM compacts'); @@ -96,26 +107,25 @@ describe('Allocation Validation', () => { const compact = getFreshCompact(); // Mock GraphQL response with insufficient balance - (graphqlClient as any).request = async (): Promise< - AccountDeltasResponse & AccountResponse - > => ({ - accountDeltas: { - items: [], - }, - account: { - resourceLocks: { - items: [ - { - withdrawalStatus: 0, - balance: (BigInt(compact.amount) / BigInt(2)).toString(), // Half the compact amount - }, - ], - }, - claims: { + (graphqlClient as { request: GraphQLRequestFn }).request = + async (): Promise => ({ + accountDeltas: { items: [], }, - }, - }); + account: { + resourceLocks: { + items: [ + { + withdrawalStatus: 0, + balance: (BigInt(compact.amount) / BigInt(2)).toString(), // Half the compact amount + }, + ], + }, + claims: { + items: [], + }, + }, + }); const result = await validateAllocation(compact, chainId, db); expect(result.isValid).toBe(false); @@ -159,26 +169,25 @@ describe('Allocation Validation', () => { ); // Mock GraphQL response with balance just enough for one compact - (graphqlClient as any).request = async (): Promise< - AccountDeltasResponse & AccountResponse - > => ({ - accountDeltas: { - items: [], - }, - account: { - resourceLocks: { - items: [ - { - withdrawalStatus: 0, - balance: (BigInt(compact.amount) * BigInt(2)).toString(), // Enough for two compacts - }, - ], - }, - claims: { + (graphqlClient as { request: GraphQLRequestFn }).request = + async (): Promise => ({ + accountDeltas: { items: [], }, - }, - }); + account: { + resourceLocks: { + items: [ + { + withdrawalStatus: 0, + balance: (BigInt(compact.amount) * BigInt(2)).toString(), // Enough for two compacts + }, + ], + }, + claims: { + items: [], + }, + }, + }); const result = await validateAllocation(compact, chainId, db); expect(result.isValid).toBe(true); @@ -221,30 +230,29 @@ describe('Allocation Validation', () => { ); // Mock GraphQL response with processed claim - (graphqlClient as any).request = async (): Promise< - AccountDeltasResponse & AccountResponse - > => ({ - accountDeltas: { - items: [], - }, - account: { - resourceLocks: { - items: [ - { - withdrawalStatus: 0, - balance: compact.amount, // Only enough for one compact - }, - ], + (graphqlClient as { request: GraphQLRequestFn }).request = + async (): Promise => ({ + accountDeltas: { + items: [], }, - claims: { - items: [ - { - claimHash: '0x' + '1'.repeat(64), // Mark the existing compact as processed - }, - ], + account: { + resourceLocks: { + items: [ + { + withdrawalStatus: 0, + balance: compact.amount, // Only enough for one compact + }, + ], + }, + claims: { + items: [ + { + claimHash: '0x' + '1'.repeat(64), // Mark the existing compact as processed + }, + ], + }, }, - }, - }); + }); const result = await validateAllocation(compact, chainId, db); expect(result.isValid).toBe(true); @@ -254,26 +262,25 @@ describe('Allocation Validation', () => { const compact = getFreshCompact(); // Mock GraphQL response with withdrawal enabled - (graphqlClient as any).request = async (): Promise< - AccountDeltasResponse & AccountResponse - > => ({ - accountDeltas: { - items: [], - }, - account: { - resourceLocks: { - items: [ - { - withdrawalStatus: 1, // Withdrawal enabled - balance: '1000000000000000000000', - }, - ], - }, - claims: { + (graphqlClient as { request: GraphQLRequestFn }).request = + async (): Promise => ({ + accountDeltas: { items: [], }, - }, - }); + account: { + resourceLocks: { + items: [ + { + withdrawalStatus: 1, // Withdrawal enabled + balance: '1000000000000000000000', + }, + ], + }, + claims: { + items: [], + }, + }, + }); const result = await validateAllocation(compact, chainId, db); expect(result.isValid).toBe(false); @@ -285,10 +292,12 @@ describe('Allocation Validation', () => { // Override the mock response with a different allocator ID const differentAllocatorId = '999'; - (graphqlClient as any).request = async ( - document: string | { source: string }, + (graphqlClient as { request: GraphQLRequestFn }).request = async ( + document: string | GraphQLDocument, _variables?: Record - ): Promise => { + ): Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) + > => { const query = typeof document === 'string' ? document : document.source; if (query.includes('GetSupportedChains')) { return { @@ -337,10 +346,12 @@ describe('Allocation Validation', () => { const compact = getFreshCompact(); // Override the mock response with empty supported chains - (graphqlClient as any).request = async ( - document: string | { source: string }, + (graphqlClient as { request: GraphQLRequestFn }).request = async ( + document: string | GraphQLDocument, _variables?: Record - ): Promise => { + ): Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) + > => { const query = typeof document === 'string' ? document : document.source; if (query.includes('GetSupportedChains')) { return { diff --git a/src/__tests__/validation/compact-graphql.test.ts b/src/__tests__/validation/compact-graphql.test.ts index d0a59c6..3b49714 100644 --- a/src/__tests__/validation/compact-graphql.test.ts +++ b/src/__tests__/validation/compact-graphql.test.ts @@ -6,6 +6,7 @@ import { AccountDeltasResponse, AccountResponse, fetchAndCacheSupportedChains, + SupportedChainsResponse, } from '../../graphql'; import { setupCompactTestDb, @@ -13,6 +14,17 @@ import { setupGraphQLMocks, } from './utils/compact-test-setup'; +interface GraphQLDocument { + source: string; +} + +type GraphQLRequestFn = ( + query: string | GraphQLDocument, + variables?: Record +) => Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) +>; + describe('Compact GraphQL Validation', () => { let db: PGlite; let originalRequest: typeof graphqlClient.request; @@ -130,10 +142,12 @@ describe('Compact GraphQL Validation', () => { it('should reject when allocator ID does not match', async (): Promise => { // Mock a different allocator ID in the chain config cache - (graphqlClient as any).request = async ( - document: string | { source: string }, + (graphqlClient as { request: GraphQLRequestFn }).request = async ( + document: string | GraphQLDocument, _variables?: Record - ): Promise => { + ): Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) + > => { const query = typeof document === 'string' ? document : document.source; if (query.includes('GetSupportedChains')) { return { @@ -184,10 +198,12 @@ describe('Compact GraphQL Validation', () => { it('should reject when allocatorId is missing from chain config', async (): Promise => { // Mock empty supported chains in the chain config cache - (graphqlClient as any).request = async ( - document: string | { source: string }, + (graphqlClient as { request: GraphQLRequestFn }).request = async ( + document: string | GraphQLDocument, _variables?: Record - ): Promise => { + ): Promise< + SupportedChainsResponse | (AccountDeltasResponse & AccountResponse) + > => { const query = typeof document === 'string' ? document : document.source; if (query.includes('GetSupportedChains')) { return { diff --git a/src/__tests__/validation/utils/compact-test-setup.ts b/src/__tests__/validation/utils/compact-test-setup.ts index 35e107c..4f3a2f2 100644 --- a/src/__tests__/validation/utils/compact-test-setup.ts +++ b/src/__tests__/validation/utils/compact-test-setup.ts @@ -64,14 +64,23 @@ export function cleanupCompactTestDb(db: PGlite): Promise { let requestCallCount = 0; let shouldFail = false; +interface GraphQLDocument { + source: string; +} + +type GraphQLRequestFn = ( + query: string | GraphQLDocument, + variables?: Record +) => Promise; + export function setupGraphQLMocks(): void { requestCallCount = 0; shouldFail = false; // Override the request method of the GraphQL client - (graphqlClient as any).request = async ( - _query: string, - _variables: Record + (graphqlClient as { request: GraphQLRequestFn }).request = async ( + _query: string | GraphQLDocument, + _variables?: Record ): Promise< SupportedChainsResponse & AccountDeltasResponse & AccountResponse > => {