-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbaseDistributionUpdated.handler.spec.ts
More file actions
146 lines (125 loc) · 5.3 KB
/
baseDistributionUpdated.handler.spec.ts
File metadata and controls
146 lines (125 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { getAddress } from "viem";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { IMetadataProvider } from "@grants-stack-indexer/metadata";
import { PartialRound } from "@grants-stack-indexer/repository";
import { Bytes32String, ChainId, Logger, ProcessorEvent } from "@grants-stack-indexer/shared";
import {
BaseDistributionUpdatedHandler,
MetadataNotFound,
MetadataParsingFailed,
} from "../../../src/internal.js";
import { createMockEvent } from "../../mocks/index.js";
describe("BaseDistributionUpdatedHandler", () => {
let handler: BaseDistributionUpdatedHandler;
let mockMetadataProvider: IMetadataProvider;
let mockLogger: Logger;
let mockEvent: ProcessorEvent<"Strategy", "DistributionUpdated">;
const chainId = 10 as ChainId;
const eventName = "DistributionUpdated";
const defaultParams = {
metadata: ["1", "ipfs://QmTestHash"] as [string, string],
merkleRoot: "0xroot" as Bytes32String,
};
const defaultStrategyId = "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0";
beforeEach(() => {
mockMetadataProvider = {
getMetadata: vi.fn(),
} as unknown as IMetadataProvider;
mockLogger = {
warn: vi.fn(),
error: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
verbose: vi.fn(),
} as unknown as Logger;
});
it("handles a valid distribution update event", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const mockDistribution = {
matchingDistribution: [
{
applicationId: "app1",
projectPayoutAddress: "projectPayoutAddress",
projectId: "projectId",
projectName: "projectName",
matchPoolPercentage: 100,
contributionsCount: 100,
originalMatchAmountInToken: "9",
matchAmountInToken: "10",
},
],
};
vi.spyOn(mockMetadataProvider, "getMetadata").mockResolvedValue(mockDistribution);
handler = new BaseDistributionUpdatedHandler(mockEvent, chainId, {
metadataProvider: mockMetadataProvider,
logger: mockLogger,
});
const result = await handler.handle();
expect(result).toEqual([
{
type: "UpdateRoundByStrategyAddress",
args: {
chainId,
strategyAddress: getAddress(mockEvent.srcAddress),
round: {
readyForPayoutTransaction: mockEvent.transactionFields.hash,
matchingDistribution: mockDistribution.matchingDistribution,
},
},
},
]);
});
it("throws MetadataNotFound if distribution metadata is not found", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
vi.spyOn(mockMetadataProvider, "getMetadata").mockResolvedValue(undefined);
handler = new BaseDistributionUpdatedHandler(mockEvent, chainId, {
metadataProvider: mockMetadataProvider,
logger: mockLogger,
});
await expect(handler.handle()).rejects.toThrow(MetadataNotFound);
expect(mockLogger.warn).toHaveBeenCalledWith(
expect.stringContaining("No matching distribution found for pointer:"),
);
});
it("throw MatchingDistributionParsingError if distribution format is invalid", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const invalidDistribution = {
matchingDistribution: [
{
amount: "not_a_number", // Invalid amount format
applicationId: "app1",
recipientAddress: "0x1234567890123456789012345678901234567890",
},
],
};
vi.spyOn(mockMetadataProvider, "getMetadata").mockResolvedValue(invalidDistribution);
handler = new BaseDistributionUpdatedHandler(mockEvent, chainId, {
metadataProvider: mockMetadataProvider,
logger: mockLogger,
});
await expect(handler.handle()).rejects.toThrow(MetadataParsingFailed);
expect(mockLogger.warn).toHaveBeenCalledWith(
expect.stringContaining("Failed to parse matching distribution:"),
);
});
it("handles empty matching distribution array", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const emptyDistribution = {
matchingDistribution: [],
};
vi.spyOn(mockMetadataProvider, "getMetadata").mockResolvedValue(emptyDistribution);
handler = new BaseDistributionUpdatedHandler(mockEvent, chainId, {
metadataProvider: mockMetadataProvider,
logger: mockLogger,
});
const result = await handler.handle();
expect(result).toHaveLength(1);
const changeset = result[0] as {
type: "UpdateRoundByStrategyAddress";
args: {
round: PartialRound;
};
};
expect(changeset.args.round.matchingDistribution).toEqual([]);
});
});