-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcachedEventRegistry.spec.ts
More file actions
124 lines (98 loc) · 4.28 KB
/
cachedEventRegistry.spec.ts
File metadata and controls
124 lines (98 loc) · 4.28 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NewProcessedEvent, ProcessedEvent } from "@grants-stack-indexer/repository";
import { ChainId, ILogger } from "@grants-stack-indexer/shared";
import { IEventsRegistry } from "../../src/internal.js";
import { InMemoryCachedEventRegistry } from "../../src/registries/event/cachedEventRegistry.js";
describe("InMemoryCachedEventRegistry", () => {
const logger: ILogger = {
debug: vi.fn(),
verbose: vi.fn(),
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
};
const mockEventRegistry: IEventsRegistry = {
getLastProcessedEvent: vi.fn(),
saveLastProcessedEvent: vi.fn(),
};
const chainId = 1 as ChainId;
const mockEvent: ProcessedEvent = {
chainId,
blockNumber: 100,
blockTimestamp: 1234567890,
logIndex: 1,
};
beforeEach(() => {
vi.clearAllMocks();
});
it("initialize with existing events", async () => {
vi.mocked(mockEventRegistry.getLastProcessedEvent).mockResolvedValue(mockEvent);
const registry = await InMemoryCachedEventRegistry.initialize(logger, mockEventRegistry, [
chainId,
]);
const cached = await registry.getLastProcessedEvent(chainId);
expect(cached).toEqual(mockEvent);
expect(mockEventRegistry.getLastProcessedEvent).toHaveBeenCalledTimes(1);
});
it("fetch from underlying registry when not in cache", async () => {
vi.mocked(mockEventRegistry.getLastProcessedEvent)
.mockResolvedValueOnce(undefined) // For initialization
.mockResolvedValueOnce(mockEvent); // For actual fetch
const registry = await InMemoryCachedEventRegistry.initialize(logger, mockEventRegistry, [
chainId,
]);
const result = await registry.getLastProcessedEvent(chainId);
expect(result).toEqual(mockEvent);
expect(mockEventRegistry.getLastProcessedEvent).toHaveBeenCalledTimes(2);
});
it("save event and update cache", async () => {
const registry = await InMemoryCachedEventRegistry.initialize(logger, mockEventRegistry, [
chainId,
]);
const newEvent: NewProcessedEvent = {
blockNumber: 200,
blockTimestamp: 1234577890,
logIndex: 2,
};
await registry.saveLastProcessedEvent(chainId, newEvent);
// Verify the event was saved to underlying registry
expect(mockEventRegistry.saveLastProcessedEvent).toHaveBeenCalledWith(chainId, newEvent);
// Verify the cache was updated
const cached = await registry.getLastProcessedEvent(chainId);
expect(cached).toEqual({
...newEvent,
chainId,
});
// Verify no additional calls to underlying registry
expect(mockEventRegistry.getLastProcessedEvent).toHaveBeenCalledTimes(1);
});
it("initialize with multiple chain ids", async () => {
const chainId2 = 5 as ChainId;
const mockEvent2: ProcessedEvent = { ...mockEvent, chainId: chainId2 };
vi.mocked(mockEventRegistry.getLastProcessedEvent).mockImplementation(async (chain) =>
chain === chainId ? mockEvent : mockEvent2,
);
const registry = await InMemoryCachedEventRegistry.initialize(logger, mockEventRegistry, [
chainId,
chainId2,
]);
const cached1 = await registry.getLastProcessedEvent(chainId);
const cached2 = await registry.getLastProcessedEvent(chainId2);
expect(cached1).toEqual(mockEvent);
expect(cached2).toEqual(mockEvent2);
expect(mockEventRegistry.getLastProcessedEvent).toHaveBeenCalledTimes(2);
});
it("throws error when underlying registry throws error", async () => {
vi.mocked(mockEventRegistry.saveLastProcessedEvent).mockRejectedValue(
new Error("Saving error"),
);
const registry = await InMemoryCachedEventRegistry.initialize(logger, mockEventRegistry, [
chainId,
]);
const cacheSetSpy = vi.spyOn(registry["cache"], "set");
await expect(registry.saveLastProcessedEvent(chainId, mockEvent)).rejects.toThrow(
"Saving error",
);
expect(cacheSetSpy).not.toHaveBeenCalled();
});
});