generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update processing service to use events registry and add tests
- Loading branch information
Showing
16 changed files
with
273 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from "./eventsRegistry.js"; | ||
export * from "./cachedEventRegistry.js"; | ||
export * from "./dbEventRegistry.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/data-flow/test/registries/cachedEventRegistry.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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(), | ||
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); | ||
}); | ||
}); |
Oops, something went wrong.