Skip to content

Commit

Permalink
feat: fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Nov 21, 2024
1 parent c8ce76a commit 3ea5b0f
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions test/unit/squid-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('createSubsquidComponent', () => {
let dappsDatabaseMock: IPgComponent
let configMock: IConfigComponent
let ecsClientMock: ECSClient
let UpdateServiceCommandMock: jest.Mock

beforeEach(() => {
fetchMock = { fetch: jest.fn() } as IFetchComponent
Expand All @@ -20,10 +21,13 @@ describe('createSubsquidComponent', () => {

ecsClientMock = new ECSClient({ region: 'us-east-1' })
;(ECSClient as jest.Mock).mockImplementation(() => ecsClientMock)

UpdateServiceCommandMock = jest.fn()
;(UpdateServiceCommand as unknown as jest.Mock).mockImplementation(UpdateServiceCommandMock)
})

describe('list', () => {
it('should list squid services', async () => {
beforeEach(() => {
const services = [{ serviceName: 'test-squid-service' }]
const tasks = [
{
Expand All @@ -46,51 +50,79 @@ describe('createSubsquidComponent', () => {
.mockResolvedValueOnce({ taskArns: ['arn:aws:ecs:task/test'] }) // ListTasksCommand
.mockResolvedValueOnce({ tasks }) // DescribeTasksCommand
;(fetchMock.fetch as jest.Mock).mockResolvedValue({
text: jest.fn().mockResolvedValue('sqd_processor_last_block 1000')
text: jest.fn().mockResolvedValue(`
sqd_processor_last_block 1000
sqd_processor_sync_eta_seconds 120
`)
})
;(dappsDatabaseMock.query as jest.Mock)
.mockResolvedValueOnce({ rows: [{ schema: 'test-schema' }] }) // getSchemaByServiceNameQuery
.mockResolvedValueOnce({ rows: [{ schema: 'active-schema' }] }) // getActiveSchemaQuery
})

it('should list squid services and fetch metrics in parallel', async () => {
const subsquid = await createSubsquidComponent({
fetch: fetchMock,
dappsDatabase: dappsDatabaseMock,
config: configMock
})

const result = await subsquid.list()

expect(result).toHaveLength(1)
expect(result[0].name).toBe('test-squid-service')
expect(result[0].schema_name).toBe('test-schema')
expect(result[0].project_active_schema).toBe('active-schema')
expect(result[0].metrics?.ETHEREUM?.sqd_processor_last_block).toBe(1000)
expect(result[0].metrics?.ETHEREUM?.sqd_processor_sync_eta_seconds).toBe(120)
})
})

describe('promote', () => {
it('should execute the promote query', async () => {
beforeEach(() => {
;(getPromoteQuery as jest.Mock).mockReturnValue('PROMOTE QUERY')
;(dappsDatabaseMock.query as jest.Mock).mockResolvedValue({})
})

it('should execute the promote query', async () => {
const subsquid = await createSubsquidComponent({
fetch: fetchMock,
dappsDatabase: dappsDatabaseMock,
config: configMock
})

await subsquid.promote('test-service-name')

expect(getPromoteQuery).toHaveBeenCalledWith('test-service-name', expect.any(String), expect.any(String))
expect(getPromoteQuery).toHaveBeenCalledWith(
'test-service-name',
expect.stringMatching(/^squid_/), // Ensures schema name starts with "squid_"
expect.stringMatching(/^test/) // Ensures project name starts with "test"
)
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(dappsDatabaseMock.query).toHaveBeenCalledWith('PROMOTE QUERY')
})
})

describe('downgrade', () => {
it('should set desiredCount to 0', async () => {
beforeEach(() => {
;(ecsClientMock.send as jest.Mock).mockResolvedValue({})
})

it('should set desiredCount to 0', async () => {
const subsquid = await createSubsquidComponent({
fetch: fetchMock,
dappsDatabase: dappsDatabaseMock,
config: configMock
})

await subsquid.downgrade('test-service-name')

expect(UpdateServiceCommandMock).toHaveBeenCalledWith({
cluster: 'test-cluster',
service: 'test-service-name',
desiredCount: 0
})

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(ecsClientMock.send).toHaveBeenCalledWith(expect.any(UpdateServiceCommand))
})
Expand Down

0 comments on commit 3ea5b0f

Please sign in to comment.