Skip to content

Commit

Permalink
fix lint / format issues
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 11, 2024
1 parent fd713ee commit bb3a3b2
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 112 deletions.
8 changes: 7 additions & 1 deletion src/__tests__/routes/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/__tests__/utils/graphql-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ export function setupGraphQLMocks(): void {
requestCallCount = 0;
shouldFail = false;

type GraphQLRequestFn = (
query: string,
variables?: Record<string, unknown>
) => Promise<unknown>;

// Override the request method of the GraphQL client
(graphqlClient as any).request = async (
(graphqlClient as { request: GraphQLRequestFn }).request = async (
query: string,
_variables: Record<string, any>
_variables?: Record<string, unknown>
) => {
requestCallCount++;

Expand Down
211 changes: 111 additions & 100 deletions src/__tests__/validation/allocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>
) => Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
>;

describe('Allocation Validation', () => {
let db: PGlite;
let originalRequest: typeof graphqlClient.request;
Expand Down Expand Up @@ -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<AccountDeltasResponse & AccountResponse> => ({
accountDeltas: {
items: [],
},
},
});
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 0,
balance: '1000000000000000000000', // 1000 ETH
},
],
},
claims: {
items: [],
},
},
});

// Clear test data
await db.query('DELETE FROM compacts');
Expand All @@ -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<AccountDeltasResponse & AccountResponse> => ({
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);
Expand Down Expand Up @@ -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<AccountDeltasResponse & AccountResponse> => ({
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);
Expand Down Expand Up @@ -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<AccountDeltasResponse & AccountResponse> => ({
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);
Expand All @@ -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<AccountDeltasResponse & AccountResponse> => ({
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);
Expand All @@ -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<string, unknown>
): Promise<any> => {
): Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
> => {
const query = typeof document === 'string' ? document : document.source;
if (query.includes('GetSupportedChains')) {
return {
Expand Down Expand Up @@ -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<string, unknown>
): Promise<any> => {
): Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
> => {
const query = typeof document === 'string' ? document : document.source;
if (query.includes('GetSupportedChains')) {
return {
Expand Down
28 changes: 22 additions & 6 deletions src/__tests__/validation/compact-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ import {
AccountDeltasResponse,
AccountResponse,
fetchAndCacheSupportedChains,
SupportedChainsResponse,
} from '../../graphql';
import {
setupCompactTestDb,
cleanupCompactTestDb,
setupGraphQLMocks,
} from './utils/compact-test-setup';

interface GraphQLDocument {
source: string;
}

type GraphQLRequestFn = (
query: string | GraphQLDocument,
variables?: Record<string, unknown>
) => Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
>;

describe('Compact GraphQL Validation', () => {
let db: PGlite;
let originalRequest: typeof graphqlClient.request;
Expand Down Expand Up @@ -130,10 +142,12 @@ describe('Compact GraphQL Validation', () => {

it('should reject when allocator ID does not match', async (): Promise<void> => {
// 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<string, unknown>
): Promise<any> => {
): Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
> => {
const query = typeof document === 'string' ? document : document.source;
if (query.includes('GetSupportedChains')) {
return {
Expand Down Expand Up @@ -184,10 +198,12 @@ describe('Compact GraphQL Validation', () => {

it('should reject when allocatorId is missing from chain config', async (): Promise<void> => {
// 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<string, unknown>
): Promise<any> => {
): Promise<
SupportedChainsResponse | (AccountDeltasResponse & AccountResponse)
> => {
const query = typeof document === 'string' ? document : document.source;
if (query.includes('GetSupportedChains')) {
return {
Expand Down
Loading

0 comments on commit bb3a3b2

Please sign in to comment.