Skip to content

Commit

Permalink
test: Add cases for ValidatorNodeMachineLocation event
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes committed Jan 26, 2024
1 parent cd44178 commit 82560e9
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 1 deletion.
115 changes: 115 additions & 0 deletions tests/handlers/ValidatorNodeMachineLocation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { EntityClass, FindOneOptions } from '@subsquid/typeorm-store';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import ValidatorNodeMachineLocation from '../../src/handlers/ValidatorNodeMachineLocation';
import {
Application,
ValidatorNode,
ValidatorNodeProvider,
} from '../../src/model';
import {
Logs,
application,
blockData,
ctx,
validatorNodeProvider,
} from '../stubs/validatorNodeProvider';

vi.mock('../../src/model/', async () => {
const ValidatorNodeProvider = vi.fn();
const ValidatorNode = vi.fn();
const Application = vi.fn();

return {
ValidatorNodeProvider,
ValidatorNode,
Application,
};
});

const ValidatorNodeMock = vi.mocked(ValidatorNode);

describe('ValidatorNodeMachineLocation', () => {
let handler: ValidatorNodeMachineLocation;
const providersStorage = new Map<string, ValidatorNodeProvider>();
const nodesStorage = new Map<string, ValidatorNode>();
const applicationStorage = new Map<string, Application>();

beforeEach(() => {
ValidatorNodeMock.mockImplementation(
(args) => ({ ...args } as ValidatorNode),
);
// defaults to find nothing in the "DB"
vi.spyOn(ctx.store, 'get').mockResolvedValue(undefined);

handler = new ValidatorNodeMachineLocation(
applicationStorage,
nodesStorage,
providersStorage,
);
});

afterEach(() => {
vi.clearAllMocks();
providersStorage.clear();
applicationStorage.clear();
nodesStorage.clear();
});

it('should not create a validator-node when the provider does not exist', async () => {
vi.spyOn(ctx.store, 'get').mockResolvedValue(undefined);

expect(nodesStorage.size).toEqual(0);

await handler.handle(Logs.machineLocation, blockData, ctx);

expect(nodesStorage.size).toEqual(0);
});

it('should create the validator-node when the provider exist and set the node location', async () => {
providersStorage.set(
validatorNodeProvider.id,
structuredClone(validatorNodeProvider),
);
applicationStorage.set(application.id, application);

expect(nodesStorage.size).toEqual(0);

await handler.handle(Logs.machineLocation, blockData, ctx);

expect(nodesStorage.size).toEqual(1);
const [[id, node]] = nodesStorage.entries();

expect(node.id).toEqual(`${node.provider.id}-${node.application.id}`);
expect(node.application).toBeDefined();
expect(node.provider).toBeDefined();
expect(node.location).toEqual(
'QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR',
);
});

it('should create the validator-node with provider and application data from database', async () => {
vi.spyOn(ctx.store, 'get').mockImplementation(
async (
entityClass: EntityClass<any>,
id: FindOneOptions<any> | string,
): Promise<any | undefined> => {
if (entityClass === Application) return application;
if (entityClass === ValidatorNodeProvider)
return validatorNodeProvider;

return undefined;
},
);

await handler.handle(Logs.machineLocation, blockData, ctx);

expect(nodesStorage.size).toEqual(1);
const [[_, node]] = nodesStorage.entries();

expect(node.application).toEqual(application);
expect(node.provider).toEqual(validatorNodeProvider);
expect(node.location).toEqual(
'QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR',
);
});
});
43 changes: 42 additions & 1 deletion tests/stubs/validatorNodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
ValidatorNodeProvider,
} from '../../src/model';

const UntilTimestamp = 1702321200000n;
export const CID = 'QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR';
export const UntilTimestamp = 1702321200000n;
export const DappAddress = '0x028367fE226CD9E5699f4288d512fE3a4a4a0012';
export const TokenAddress =
'0xE15E2ADD14c26b9ae1E735bF5B444CCB11B0bd15'.toLowerCase();
Expand Down Expand Up @@ -214,6 +215,46 @@ export const Logs = {
stateDiffs: [],
},
},
machineLocation: {
id: '0004867730-000035-2c78f',
address: ValidatorNodeProviderAddress,
logIndex: 129,
transactionIndex: 24,
topics: [
ValidatorNodeProviderEvents.MachineLocation.topic,
encodeAbiParameters([{ type: 'address' }], [DappAddress]),
],
data: encodeAbiParameters([{ type: 'string' }], [CID]),
getTransaction() {
return this.transaction;
},
block: {
id: '0004867730-2c78f',
height: 4867730,
hash: '0x8f998edf202fe3449e61849c2207432833721d9e6a5ffda1c5388187826ac9a7',
parentHash:
'0x91f4d079445be915f9a9226a05a6c33a9112dda3cd10cffa4fe28406975220c6',
timestamp: 1702321200000,
},
transaction: {
id: '0004867730-000024-2c78f',
transactionIndex: 24,
from: '0xd8464d1b3592b6c3786b32931e2a2adac501aaad',
to: ValidatorNodeProviderAddress,
hash: '0xa4fd02a167a6a4a876283aa682febe6502278be2a2882d99a2d31e52bd3e5a52',
block: {
id: '0004867730-2c78f',
height: 4867730,
hash: '0x8f998edf202fe3449e61849c2207432833721d9e6a5ffda1c5388187826ac9a7',
parentHash:
'0x91f4d079445be915f9a9226a05a6c33a9112dda3cd10cffa4fe28406975220c6',
timestamp: 1702321200000,
},
logs: [],
traces: [],
stateDiffs: [],
},
},
} satisfies Record<string, Log>;

export const blockData = {
Expand Down

0 comments on commit 82560e9

Please sign in to comment.