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.
- Loading branch information
Showing
12 changed files
with
304 additions
and
24 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
80 changes: 80 additions & 0 deletions
80
packages/processors/test/registry/handlers/profileMetadataUpdated.handler.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,80 @@ | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { ProcessorDependencies } from "../../../src/internal.js"; | ||
import { ProfileMetadataUpdatedHandler } from "../../../src/processors/registry/handlers/profileMetadataUpdated.handler.js"; | ||
|
||
describe("ProfileMetadataUpdatedHandler", () => { | ||
const mockCid = "mockCid"; | ||
const mockEvent: ProcessorEvent<"Registry", "ProfileMetadataUpdated"> = { | ||
params: { | ||
metadata: [0, mockCid], | ||
profileId: "mockProfileId", | ||
}, | ||
// Add other necessary event properties here | ||
} as ProcessorEvent<"Registry", "ProfileMetadataUpdated">; | ||
|
||
const mockDependencies = { | ||
metadataProvider: { | ||
getMetadata: vi.fn(), | ||
}, | ||
logger: { | ||
warn: vi.fn(), | ||
}, | ||
} as unknown as ProcessorDependencies; | ||
|
||
const chainId = 1 as ChainId; | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("handles valid metadata", async () => { | ||
const mockMetadata = { | ||
type: "program", | ||
name: "Test Project", | ||
}; | ||
vi.spyOn(mockDependencies.metadataProvider, "getMetadata").mockResolvedValueOnce( | ||
mockMetadata, | ||
); | ||
|
||
const handler = new ProfileMetadataUpdatedHandler(mockEvent, chainId, mockDependencies); | ||
const result = await handler.handle(); | ||
|
||
expect(mockDependencies.metadataProvider.getMetadata).toHaveBeenCalledWith(mockCid); | ||
expect(result).toEqual([ | ||
{ | ||
type: "UpdateProject", | ||
args: { | ||
chainId, | ||
projectId: mockEvent.params.profileId, | ||
project: { | ||
metadataCid: mockCid, | ||
metadata: mockMetadata, | ||
projectType: "canonical", | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it("returns an empty array for invalid metadata", async () => { | ||
vi.spyOn(mockDependencies.metadataProvider, "getMetadata").mockResolvedValueOnce(null); | ||
|
||
const handler = new ProfileMetadataUpdatedHandler(mockEvent, chainId, mockDependencies); | ||
const result = await handler.handle(); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("throws an error if getMetadata fails", async () => { | ||
vi.spyOn(mockDependencies.metadataProvider, "getMetadata").mockRejectedValueOnce( | ||
new Error("Failed to fetch metadata"), | ||
); | ||
|
||
const handler = new ProfileMetadataUpdatedHandler(mockEvent, chainId, mockDependencies); | ||
|
||
await expect(handler.handle()).rejects.toThrow("Failed to fetch metadata"); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/processors/test/registry/handlers/profileNameUpdated.handler.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,57 @@ | ||
import { getAddress } from "viem"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { Bytes32String, ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { ProfileNameUpdatedHandler } from "../../../src/processors/registry/handlers/profileNameUpdated.handler.js"; | ||
|
||
describe("ProfileNameUpdatedHandler", () => { | ||
const mockEvent: ProcessorEvent<"Registry", "ProfileNameUpdated"> = { | ||
contractName: "Registry", | ||
eventName: "ProfileNameUpdated", | ||
params: { | ||
profileId: "0xprofile1" as Bytes32String, | ||
name: "New Profile Name", | ||
anchor: "0x5aD1D85Bb68791Cb3cE598f56E00F5D5694FAd14", | ||
}, | ||
blockNumber: 1, | ||
blockTimestamp: 1, | ||
chainId: 1 as ChainId, | ||
logIndex: 1, | ||
srcAddress: "0x0", | ||
transactionFields: { | ||
hash: "0x0", | ||
transactionIndex: 1, | ||
}, | ||
}; | ||
|
||
it("returns a changeset with updated project name and anchor address", async () => { | ||
const handler = new ProfileNameUpdatedHandler(mockEvent, 1 as ChainId, { | ||
logger: { | ||
debug: vi.fn(), | ||
error: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
}, | ||
}); | ||
|
||
const result = await handler.handle(); | ||
|
||
const expectedChangeset: Changeset[] = [ | ||
{ | ||
type: "UpdateProject", | ||
args: { | ||
chainId: mockEvent.chainId as ChainId, | ||
projectId: mockEvent.params.profileId, | ||
project: { | ||
name: mockEvent.params.name, | ||
anchorAddress: getAddress(mockEvent.params.anchor), | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
expect(result).toEqual(expectedChangeset); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
packages/processors/test/registry/handlers/profileOwnerUpdated.handler.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,72 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { Bytes32String, ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { ProfileOwnerUpdatedHandler } from "../../../src/processors/registry/handlers/profileOwnerUpdated.handler.js"; | ||
|
||
describe("ProfileOwnerUpdatedHandler", () => { | ||
it("handles ProfileOwnerUpdated event correctly", async () => { | ||
const mockEvent: ProcessorEvent<"Registry", "ProfileOwnerUpdated"> = { | ||
blockNumber: 123456, | ||
blockTimestamp: 123456, | ||
chainId: 1 as ChainId, | ||
contractName: "Registry", | ||
eventName: "ProfileOwnerUpdated", | ||
logIndex: 0, | ||
srcAddress: "0x1234567890123456789012345678901234567890", | ||
params: { | ||
profileId: "0xprofile123" as Bytes32String, | ||
owner: "0x5aD1D85Bb68791Cb3cE598f56E00F5D5694FAd14", | ||
}, | ||
transactionFields: { | ||
hash: "0xhash", | ||
transactionIndex: 0, | ||
}, | ||
}; | ||
|
||
const mockDependencies = { | ||
logger: { | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
debug: vi.fn(), | ||
}, | ||
}; | ||
|
||
const handler = new ProfileOwnerUpdatedHandler( | ||
mockEvent, | ||
mockEvent.chainId as ChainId, | ||
mockDependencies, | ||
); | ||
|
||
const result = await handler.handle(); | ||
|
||
const expectedChangeset: Changeset[] = [ | ||
{ | ||
type: "DeleteAllProjectRolesByRole", | ||
args: { | ||
projectRole: { | ||
chainId: mockEvent.chainId as ChainId, | ||
projectId: mockEvent.params.profileId, | ||
role: "owner", | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: "InsertProjectRole", | ||
args: { | ||
projectRole: { | ||
chainId: mockEvent.chainId as ChainId, | ||
projectId: mockEvent.params.profileId, | ||
address: mockEvent.params.owner, | ||
role: "owner", | ||
createdAtBlock: BigInt(mockEvent.blockNumber), | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
expect(result).toEqual(expectedChangeset); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
packages/processors/test/registry/handlers/roleRevoked.handler.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,80 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { IProjectRepository, Project } from "@grants-stack-indexer/repository"; | ||
import { ChainId, ILogger, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { RoleRevokedHandler } from "../../../src/processors/registry/handlers/roleRevoked.handler.js"; | ||
|
||
describe("RoleRevokedHandler", () => { | ||
let mockEvent: ProcessorEvent<"Registry", "RoleRevoked">; | ||
let mockChainId: ChainId; | ||
let mockDependencies: { projectRepository: IProjectRepository; logger: ILogger }; | ||
let mockedRole: string; | ||
let mockedAccount: string; | ||
|
||
beforeEach(() => { | ||
mockedRole = "0x5aD1D85Bb68791Cb3cE598f56E00F5D5694FAd14ASD"; | ||
mockedAccount = "0x5aD1D85Bb68791Cb3cE598f56E00F5D5694FAd14"; | ||
mockEvent = { | ||
params: { | ||
account: mockedAccount, | ||
role: mockedRole, | ||
}, | ||
} as ProcessorEvent<"Registry", "RoleRevoked">; | ||
|
||
mockChainId = 1 as ChainId; | ||
|
||
mockDependencies = { | ||
projectRepository: { | ||
getProjectById: vi.fn(), | ||
} as unknown as IProjectRepository, | ||
logger: { | ||
info: vi.fn(), | ||
} as unknown as ILogger, | ||
}; | ||
}); | ||
|
||
it("return a changeset when project is found", async () => { | ||
const project = { id: "project-1" }; | ||
vi.spyOn(mockDependencies.projectRepository, "getProjectById").mockResolvedValueOnce({ | ||
...project, | ||
} as Project); | ||
|
||
const handler = new RoleRevokedHandler(mockEvent, mockChainId, mockDependencies); | ||
const result = await handler.handle(); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: "DeleteAllProjectRolesByRoleAndAddress", | ||
args: { | ||
projectRole: { | ||
chainId: mockChainId, | ||
projectId: project.id, | ||
address: mockedAccount, | ||
role: "member", | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it("return an empty array when project is not found", async () => { | ||
vi.spyOn(mockDependencies.projectRepository, "getProjectById").mockResolvedValueOnce( | ||
undefined, | ||
); | ||
|
||
const handler = new RoleRevokedHandler(mockEvent, mockChainId, mockDependencies); | ||
const result = await handler.handle(); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("throw an error when getProjectById fails", async () => { | ||
const error = new Error("Database error"); | ||
vi.spyOn(mockDependencies.projectRepository, "getProjectById").mockRejectedValueOnce(error); | ||
|
||
const handler = new RoleRevokedHandler(mockEvent, mockChainId, mockDependencies); | ||
|
||
await expect(handler.handle()).rejects.toThrow(error); | ||
}); | ||
}); |
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
Oops, something went wrong.