-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
636 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Oops, something went wrong.