Skip to content

Commit c23a811

Browse files
committed
fix: tests
1 parent bd419b9 commit c23a811

File tree

6 files changed

+206
-4
lines changed

6 files changed

+206
-4
lines changed

apps/processing/test/unit/sharedDependencies.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ vi.mock("@grants-stack-indexer/repository", () => ({
4747
KyselyTransactionManager: vi.fn(),
4848
KyselyPricingCache: vi.fn(),
4949
KyselyMetadataCache: vi.fn(),
50+
KyselyLegacyProjectRepository: vi.fn(),
5051
InMemoryPricingCache: vi.fn(),
5152
InMemoryMetadataCache: vi.fn(),
5253
}));

packages/data-flow/test/unit/eventsProcessor.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22

33
import {
44
AlloProcessor,
5+
AlloV1ToV2ProfileMigrationProcessor,
56
ProcessorDependencies,
67
RegistryProcessor,
78
StrategyProcessor,
@@ -22,24 +23,31 @@ vi.mock("@grants-stack-indexer/processors", () => ({
2223
AlloProcessor: vi.fn(),
2324
RegistryProcessor: vi.fn(),
2425
StrategyProcessor: vi.fn(),
26+
AlloV1ToV2ProfileMigrationProcessor: vi.fn(),
2527
}));
2628

2729
describe("EventsProcessor", () => {
2830
let eventsProcessor: EventsProcessor;
2931
let mockAlloProcessor: AlloProcessor;
3032
let mockRegistryProcessor: RegistryProcessor;
3133
let mockStrategyProcessor: StrategyProcessor;
34+
let mockAlloV1ToV2ProfileMigrationProcessor: AlloV1ToV2ProfileMigrationProcessor;
3235
const chainId = 1 as ChainId;
3336
const mockDependencies = {} as ProcessorDependencies;
3437

3538
beforeEach(() => {
3639
mockAlloProcessor = { process: vi.fn() } as unknown as AlloProcessor;
3740
mockRegistryProcessor = { process: vi.fn() } as unknown as RegistryProcessor;
3841
mockStrategyProcessor = { process: vi.fn() } as unknown as StrategyProcessor;
39-
42+
mockAlloV1ToV2ProfileMigrationProcessor = {
43+
process: vi.fn(),
44+
} as unknown as AlloV1ToV2ProfileMigrationProcessor;
4045
vi.mocked(AlloProcessor).mockImplementation(() => mockAlloProcessor);
4146
vi.mocked(RegistryProcessor).mockImplementation(() => mockRegistryProcessor);
4247
vi.mocked(StrategyProcessor).mockImplementation(() => mockStrategyProcessor);
48+
vi.mocked(AlloV1ToV2ProfileMigrationProcessor).mockImplementation(
49+
() => mockAlloV1ToV2ProfileMigrationProcessor,
50+
);
4351

4452
eventsProcessor = new EventsProcessor(chainId, mockDependencies);
4553
});
@@ -98,6 +106,32 @@ describe("EventsProcessor", () => {
98106
expect(result).toBe(mockChangeset);
99107
});
100108

109+
it("process AlloV1ToV2ProfileMigration events using AlloV1ToV2ProfileMigrationProcessor", async () => {
110+
const v1ChainId = 1 as ChainId;
111+
const v1ProjectId = "1";
112+
const v2ProjectId = "2";
113+
const mockChangeset: Changeset[] = [
114+
{
115+
type: "InsertLegacyProject",
116+
args: { legacyProject: { v1ChainId, v1ProjectId, v2ProjectId } },
117+
},
118+
];
119+
const mockEvent = {
120+
contractName: "AlloV1ToV2ProfileMigration",
121+
eventName: "ProfileMigrated",
122+
args: { v1ChainId, v1ProjectId, v2ProjectId },
123+
} as unknown as ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated">;
124+
125+
vi.spyOn(mockAlloV1ToV2ProfileMigrationProcessor, "process").mockResolvedValue(
126+
mockChangeset,
127+
);
128+
129+
const result = await eventsProcessor.processEvent(mockEvent);
130+
131+
expect(mockAlloV1ToV2ProfileMigrationProcessor.process).toHaveBeenCalledWith(mockEvent);
132+
expect(result).toBe(mockChangeset);
133+
});
134+
101135
it("throw InvalidEvent for unknown event types", async () => {
102136
const mockEvent: ProcessorEvent<ContractName, AnyEvent> = {
103137
contractName: "Unknown" as unknown as ContractName,

packages/processors/src/processors/alloV1ToV2ProfileMigration/handlers/ProfileMigrated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ProfileMigratedHandler
3131
args: {
3232
legacyProject: {
3333
v1ProjectId: alloV1,
34-
v1ChainId: alloV1ChainId,
34+
v1ChainId: Number(alloV1ChainId) as ChainId,
3535
v2ProjectId: alloV2,
3636
},
3737
},
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
import type { EvmProvider } from "@grants-stack-indexer/chain-providers";
4+
import type { IMetadataProvider } from "@grants-stack-indexer/metadata";
5+
import type { IPricingProvider } from "@grants-stack-indexer/pricing";
6+
import type {
7+
IApplicationReadRepository,
8+
IProjectReadRepository,
9+
IRoundReadRepository,
10+
} from "@grants-stack-indexer/repository";
11+
import type { ChainId, ILogger, ProcessorEvent } from "@grants-stack-indexer/shared";
12+
13+
import { UnsupportedEventException } from "../../../src/internal.js";
14+
import {
15+
AlloV1ToV2ProfileMigrationProcessor,
16+
ProfileMigratedHandler,
17+
} from "../../../src/processors/alloV1ToV2ProfileMigration/index.js";
18+
19+
// Mock the handlers
20+
vi.mock("../../../src/processors/alloV1ToV2ProfileMigration/handlers/ProfileMigrated.ts", () => {
21+
const ProfileMigratedHandler = vi.fn();
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
23+
ProfileMigratedHandler.prototype.handle = vi.fn();
24+
return {
25+
ProfileMigratedHandler,
26+
};
27+
});
28+
29+
describe("AlloV1ToV2ProfileMigrationProcessor", () => {
30+
const mockChainId = 10 as ChainId;
31+
let processor: AlloV1ToV2ProfileMigrationProcessor;
32+
let mockEvmProvider: EvmProvider;
33+
let mockPricingProvider: IPricingProvider;
34+
let mockMetadataProvider: IMetadataProvider;
35+
let mockRoundRepository: IRoundReadRepository;
36+
const logger: ILogger = {
37+
debug: vi.fn(),
38+
error: vi.fn(),
39+
info: vi.fn(),
40+
warn: vi.fn(),
41+
};
42+
beforeEach(() => {
43+
mockEvmProvider = {} as EvmProvider;
44+
mockPricingProvider = {} as IPricingProvider;
45+
mockMetadataProvider = {} as IMetadataProvider;
46+
mockRoundRepository = {} as IRoundReadRepository;
47+
48+
processor = new AlloV1ToV2ProfileMigrationProcessor(mockChainId, {
49+
evmProvider: mockEvmProvider,
50+
pricingProvider: mockPricingProvider,
51+
metadataProvider: mockMetadataProvider,
52+
roundRepository: mockRoundRepository,
53+
projectRepository: {} as IProjectReadRepository,
54+
applicationRepository: {} as IApplicationReadRepository,
55+
logger,
56+
});
57+
58+
// Reset mocks before each test
59+
vi.clearAllMocks();
60+
});
61+
62+
it("call ProfileMigratedHandler for PoolCreated event", async () => {
63+
const mockEvent: ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated"> = {
64+
eventName: "ProfileMigrated",
65+
// Add other necessary event properties here
66+
} as ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated">;
67+
68+
vi.spyOn(ProfileMigratedHandler.prototype, "handle").mockResolvedValue([]);
69+
70+
await processor.process(mockEvent);
71+
72+
expect(ProfileMigratedHandler).toHaveBeenCalledWith(
73+
mockEvent,
74+
mockChainId,
75+
processor["dependencies"],
76+
);
77+
expect(ProfileMigratedHandler.prototype.handle).toHaveBeenCalled();
78+
});
79+
80+
it("throw an error for unknown event names", async () => {
81+
const mockEvent = {
82+
eventName: "UnknownEvent",
83+
} as unknown as ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated">;
84+
85+
await expect(() => processor.process(mockEvent)).rejects.toThrow(UnsupportedEventException);
86+
});
87+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
import type {
4+
Bytes32String,
5+
ChainId,
6+
ILogger,
7+
ProcessorEvent,
8+
TimestampMs,
9+
} from "@grants-stack-indexer/shared";
10+
11+
import { ProfileMigratedHandler } from "../../../../src/processors/alloV1ToV2ProfileMigration/index.js";
12+
13+
function createMockEvent(
14+
overrides: Partial<ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated">> = {},
15+
): ProcessorEvent<"AlloV1ToV2ProfileMigration", "ProfileMigrated"> {
16+
return {
17+
blockNumber: 116385567,
18+
blockTimestamp: 1708369911 as TimestampMs,
19+
chainId: 10 as ChainId,
20+
contractName: "AlloV1ToV2ProfileMigration",
21+
eventName: "ProfileMigrated",
22+
logIndex: 123,
23+
srcAddress: "0x1133eA7Af70876e64665ecD07C0A0476d09465a1",
24+
params: {
25+
alloV1ChainId: "10",
26+
alloV1: "0x1133eA7Af70876e64665ecD07C0A0476d09465a1" as Bytes32String,
27+
alloV2: "0x1133eA7Af70876e64665ecD07C0A0476d09465a1" as Bytes32String,
28+
nonce: "1",
29+
},
30+
transactionFields: {
31+
hash: "0x6e5a7115323ac1712f7c27adff46df2216324a4ad615a8c9ce488c32a1f3a035",
32+
transactionIndex: 6,
33+
from: "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5Address",
34+
},
35+
...overrides,
36+
};
37+
}
38+
39+
describe("ProfileMigratedHandler", () => {
40+
let mockLogger: ILogger;
41+
let handler: ProfileMigratedHandler;
42+
43+
const mockDependencies = (): { logger: ILogger } => ({
44+
logger: mockLogger,
45+
});
46+
47+
beforeEach(() => {
48+
mockLogger = {
49+
warn: vi.fn(),
50+
} as unknown as ILogger;
51+
});
52+
53+
afterEach(() => {
54+
vi.clearAllMocks();
55+
});
56+
57+
it("returns a changeset to insert a legacy project", async () => {
58+
const mockEvent = createMockEvent();
59+
60+
handler = new ProfileMigratedHandler(mockEvent, 10 as ChainId, mockDependencies());
61+
62+
const result = await handler.handle();
63+
expect(result).toEqual([
64+
{
65+
type: "InsertLegacyProject",
66+
args: {
67+
legacyProject: {
68+
v1ChainId: Number(mockEvent.params.alloV1ChainId) as ChainId,
69+
v1ProjectId: mockEvent.params.alloV1,
70+
v2ProjectId: mockEvent.params.alloV2,
71+
},
72+
},
73+
},
74+
]);
75+
});
76+
});

packages/shared/src/types/events/common.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hex } from "viem";
22

3-
import { Address, TimestampMs } from "../../internal.js";
3+
import { Address, AlloV1ToV2ProfileMigrationEventParams, TimestampMs } from "../../internal.js";
44
import {
55
AlloEvent,
66
AlloEventParams,
@@ -48,7 +48,11 @@ export type EventParams<T extends ContractName, E extends ContractToEventName<T>
4848
? E extends RegistryEvent
4949
? RegistryEventParams<E>
5050
: never
51-
: never;
51+
: T extends "AlloV1ToV2ProfileMigration"
52+
? E extends AlloV1ToV2ProfileMigrationEvent
53+
? AlloV1ToV2ProfileMigrationEventParams<E>
54+
: never
55+
: never;
5256

5357
/**
5458
* This type represents events fetched from the indexer.

0 commit comments

Comments
 (0)