From 86ca57611e36d637460994b5f6bed2e01acf0d54 Mon Sep 17 00:00:00 2001 From: Bruno Menezes Date: Fri, 26 Jan 2024 12:43:11 +1300 Subject: [PATCH] test: Add test cases for ValidatorNodeProviderCreated events. --- src/handlers/ValidatorNodeProviderCreated.ts | 1 + .../ValidatorNodeProviderCreated.test.ts | 136 ++++++++++++++++++ tests/stubs/validatorNodeProvider.ts | 19 +-- 3 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 tests/handlers/ValidatorNodeProviderCreated.test.ts diff --git a/src/handlers/ValidatorNodeProviderCreated.ts b/src/handlers/ValidatorNodeProviderCreated.ts index 8afd7c4..5a12093 100644 --- a/src/handlers/ValidatorNodeProviderCreated.ts +++ b/src/handlers/ValidatorNodeProviderCreated.ts @@ -52,6 +52,7 @@ export default class ValidatorNodeProviderCreated implements Handler { new ValidatorNodeProvider({ id: providerId, token: tokenInstance, + paused: false, authority, payee: payee.toLowerCase(), price, diff --git a/tests/handlers/ValidatorNodeProviderCreated.test.ts b/tests/handlers/ValidatorNodeProviderCreated.test.ts new file mode 100644 index 0000000..78bfa53 --- /dev/null +++ b/tests/handlers/ValidatorNodeProviderCreated.test.ts @@ -0,0 +1,136 @@ +import { EntityClass, FindOneOptions } from '@subsquid/typeorm-store'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import ValidatorNodeProviderCreated from '../../src/handlers/ValidatorNodeProviderCreated'; +import TokenHelper from '../../src/handlers/helpers/TokenHelper'; +import { Authority, Token, ValidatorNodeProvider } from '../../src/model'; +import { + Logs, + TokenAddress, + ValidatorNodeProviderAddress, + authority, + blockData, + ctx, + token, + validatorNodeProvider, +} from '../stubs/validatorNodeProvider'; + +vi.mock('../../src/model/', async () => { + const ValidatorNodeProvider = vi.fn(); + const Authority = vi.fn(); + const Token = vi.fn(); + + return { + ValidatorNodeProvider, + Authority, + Token, + }; +}); + +const ValidatorNodeProviderMock = vi.mocked(ValidatorNodeProvider); +const AuthorityMock = vi.mocked(Authority); + +describe('ValidatorNodeProviderCreated', () => { + let handler: ValidatorNodeProviderCreated; + const providerStorage = new Map(); + const tokenStorage = new Map(); + const authorityStorage = new Map(); + const expectedProvider = { + authority: { + id: '0x83e4283f7eab201b06f749f683f27cfda294ab81', + }, + id: '0x4d22c1f970574ae7b8724457d268d41e6459e288', + paused: false, + payee: '0xd8464d1b3592b6c3786b32931e2a2adac501aaad', + price: 400000000000000n, + token: { + decimals: 18, + id: '0xe15e2add14c26b9ae1e735bf5b444ccb11b0bd15', + name: 'SunodoToken', + symbol: 'SUN', + }, + }; + + beforeEach(() => { + ValidatorNodeProviderMock.mockImplementation( + (args) => ({ ...args } as ValidatorNodeProvider), + ); + AuthorityMock.mockImplementation((args) => ({ ...args } as Authority)); + + handler = new ValidatorNodeProviderCreated( + authorityStorage, + providerStorage, + tokenStorage, + ); + }); + + afterEach(() => { + vi.clearAllMocks(); + providerStorage.clear(); + authorityStorage.clear(); + tokenStorage.clear(); + }); + + it('should create the validator node with correct info including cache data', async () => { + authorityStorage.set(authority.id, authority); + tokenStorage.set(token.id, token); + + await handler.handle(Logs.created, blockData, ctx); + + const provider = providerStorage.get(validatorNodeProvider.id); + + expect(provider).toEqual(expectedProvider); + }); + + it('should create provider with correct information including authority and token info from database', async () => { + vi.spyOn(ctx.store, 'get').mockImplementation( + async ( + entityClass: EntityClass, + id: FindOneOptions | string, + ): Promise => { + console.log(`(ClassName): ${entityClass === Authority}`); + if (entityClass === Authority) return { id }; + if (entityClass === Token) return token; + + return undefined; + }, + ); + + await handler.handle(Logs.created, blockData, ctx); + + expect(providerStorage.size).toBe(1); + const provider = providerStorage.get(ValidatorNodeProviderAddress); + + expect(provider).toEqual(expectedProvider); + }); + + it('should create an authority when it is not indexed yet', async () => { + tokenStorage.set(token.id, token); + vi.spyOn(ctx.store, 'get').mockResolvedValue(undefined); + + expect(authorityStorage.size).toEqual(0); + expect(providerStorage.size).toEqual(0); + + await handler.handle(Logs.created, blockData, ctx); + + expect(authorityStorage.size).toEqual(1); + expect(providerStorage.size).toEqual(1); + }); + + it('should create a Token when it is not indexed yet', async () => { + authorityStorage.set(authority.id, authority); + vi.spyOn(TokenHelper, 'createToken').mockResolvedValue(token); + + expect(tokenStorage.size).toEqual(0); + + await handler.handle(Logs.created, blockData, ctx); + + expect(tokenStorage.size).toEqual(1); + expect(providerStorage.size).toEqual(1); + const expectedToken = tokenStorage.get(TokenAddress); + const expectedProvider = providerStorage.get( + ValidatorNodeProviderAddress, + ); + + expect(expectedToken).toEqual(expectedProvider?.token); + }); +}); diff --git a/tests/stubs/validatorNodeProvider.ts b/tests/stubs/validatorNodeProvider.ts index 6755e9a..52a3e40 100644 --- a/tests/stubs/validatorNodeProvider.ts +++ b/tests/stubs/validatorNodeProvider.ts @@ -5,7 +5,7 @@ import { vi } from 'vitest'; import { events as MarketplaceEvents } from '../../src/abi/Marketplace'; import { events as ValidatorNodeProviderEvents } from '../../src/abi/ValidatorNodeProvider'; import { MarketplaceAddress } from '../../src/config'; -import { ValidatorNodeProvider } from '../../src/model'; +import { Authority, Token, ValidatorNodeProvider } from '../../src/model'; export const TokenAddress = '0xE15E2ADD14c26b9ae1E735bF5B444CCB11B0bd15'.toLowerCase(); @@ -16,16 +16,19 @@ export const AuthorityAddress = export const ValidatorNodeProviderAddress = '0x4d22c1F970574ae7B8724457D268D41E6459E288'.toLowerCase(); +export const authority = { id: AuthorityAddress } satisfies Authority; +export const token = { + id: TokenAddress, + decimals: 18, + name: 'SunodoToken', + symbol: 'SUN', +} satisfies Token; + export const validatorNodeProvider = { id: ValidatorNodeProviderAddress, - authority: { id: AuthorityAddress }, + authority, payee: PayeeAddress, - token: { - id: TokenAddress, - decimals: 18, - name: 'SunodoToken', - symbol: 'SUN', - }, + token, price: 400000000000000n, paused: false, nodes: [],