Skip to content

Commit

Permalink
break out tests even further
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 8, 2024
1 parent 2c0f88e commit a2a3d45
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 332 deletions.
203 changes: 203 additions & 0 deletions src/__tests__/validation/compact-graphql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { validateCompact } from '../../validation/compact';
import { getFreshCompact } from '../utils/test-server';
import { PGlite } from '@electric-sql/pglite';
import {
graphqlClient,
AllocatorResponse,
AccountDeltasResponse,
AccountResponse,
} from '../../graphql';
import {
setupCompactTestDb,
cleanupCompactTestDb,
} from './utils/compact-test-setup';

describe('Compact GraphQL Validation', () => {
let db: PGlite;
let originalRequest: typeof graphqlClient.request;

beforeAll(async (): Promise<void> => {
db = await setupCompactTestDb();
});

afterAll(async (): Promise<void> => {
await cleanupCompactTestDb(db);
});

beforeEach((): void => {
originalRequest = graphqlClient.request;
});

afterEach((): void => {
graphqlClient.request = originalRequest;
});

it('should validate with sufficient balance', async (): Promise<void> => {
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [{ allocatorId: '1' }],
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 0,
balance: '1000000000000000000000', // 1000 ETH
},
],
},
claims: {
items: [],
},
},
});

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(true);
});

it('should reject with insufficient balance', async (): Promise<void> => {
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [{ allocatorId: '1' }],
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 0,
balance: '1', // Very small balance
},
],
},
claims: {
items: [],
},
},
});

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('Insufficient');
});

it('should reject when withdrawal is enabled', async (): Promise<void> => {
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [{ allocatorId: '1' }],
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 1, // Withdrawal enabled
balance: '1000000000000000000000',
},
],
},
claims: {
items: [],
},
},
});

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('withdrawals enabled');
});

it('should reject when allocator ID does not match', async (): Promise<void> => {
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [{ allocatorId: '999' }], // Different allocator ID
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 0,
balance: '1000000000000000000000',
},
],
},
claims: {
items: [],
},
},
});

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('Invalid allocator ID');
});

it('should reject when no supported chains are found', async (): Promise<void> => {
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [], // Empty supported chains
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [
{
withdrawalStatus: 0,
balance: '1000000000000000000000',
},
],
},
claims: {
items: [],
},
},
});

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('Invalid allocator ID');
});

it('should handle GraphQL request errors', async (): Promise<void> => {
graphqlClient.request = async (): Promise<never> => {
throw new Error('GraphQL request failed');
};

const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('GraphQL request failed');
});
});
82 changes: 82 additions & 0 deletions src/__tests__/validation/compact-validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { validateCompact } from '../../validation/compact';
import { getFreshCompact } from '../utils/test-server';
import { PGlite } from '@electric-sql/pglite';
import { graphqlClient } from '../../graphql';
import {
setupCompactTestDb,
cleanupCompactTestDb,
mockGraphQLResponse,
} from './utils/compact-test-setup';

describe('Compact Basic Validation', () => {
let db: PGlite;
let originalRequest: typeof graphqlClient.request;

beforeAll(async (): Promise<void> => {
db = await setupCompactTestDb();
});

afterAll(async (): Promise<void> => {
await cleanupCompactTestDb(db);
});

beforeEach((): void => {
originalRequest = graphqlClient.request;
mockGraphQLResponse(originalRequest);
});

afterEach((): void => {
graphqlClient.request = originalRequest;
});

it('should validate correct compact', async (): Promise<void> => {
const result = await validateCompact(getFreshCompact(), '1', db);
expect(result.isValid).toBe(true);
});

it('should reject invalid arbiter address', async (): Promise<void> => {
const invalidCompact = {
...getFreshCompact(),
arbiter: 'invalid-address',
};
const result = await validateCompact(invalidCompact, '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('Invalid arbiter address');
});

it('should reject invalid sponsor address', async (): Promise<void> => {
const invalidCompact = {
...getFreshCompact(),
sponsor: 'invalid-address',
};
const result = await validateCompact(invalidCompact, '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toBeTruthy();
});

it('should reject invalid expires timestamp', async (): Promise<void> => {
const invalidCompact = {
...getFreshCompact(),
expires: BigInt(-1),
};
const result = await validateCompact(invalidCompact, '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toBeTruthy();
});

it('should reject invalid amount', async (): Promise<void> => {
const invalidCompact = {
...getFreshCompact(),
amount: '-1',
};
const result = await validateCompact(invalidCompact, '1', db);
expect(result.isValid).toBe(false);
expect(result.error).toBeTruthy();
});

it('should reject invalid chain id', async (): Promise<void> => {
const result = await validateCompact(getFreshCompact(), 'invalid', db);
expect(result.isValid).toBe(false);
expect(result.error).toContain('Invalid chain ID');
});
});
Loading

0 comments on commit a2a3d45

Please sign in to comment.