-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement EboActor.onRequestFinalized handler #23
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ export type EboEventName = | |
| "ResponseDisputed" | ||
| "DisputeStatusChanged" | ||
| "DisputeEscalated" | ||
| "RequestFinalizable" | ||
| "RequestFinalized"; | ||
|
||
export interface NewEpoch { | ||
|
@@ -49,10 +48,6 @@ export interface DisputeEscalated { | |
blockNumber: bigint; | ||
} | ||
|
||
export interface RequestFinalizable { | ||
requestId: string; | ||
} | ||
|
||
Comment on lines
-52
to
-55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleting stuff related to |
||
export interface RequestFinalized { | ||
requestId: string; | ||
responseId: string; | ||
|
@@ -72,11 +67,9 @@ export type EboEventData<E extends EboEventName> = E extends "NewEpoch" | |
? DisputeStatusChanged | ||
: E extends "DisputeEscalated" | ||
? DisputeEscalated | ||
: E extends "RequestFinalizable" | ||
? RequestFinalizable | ||
: E extends "RequestFinalized" | ||
? RequestFinalized | ||
: never; | ||
: E extends "RequestFinalized" | ||
? RequestFinalized | ||
: never; | ||
|
||
export type EboEvent<T extends EboEventName> = { | ||
name: T; | ||
|
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
69 changes: 69 additions & 0 deletions
69
packages/automated-dispute/tests/eboActor/onRequestFinalized.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,69 @@ | ||
import { ILogger } from "@ebo-agent/shared"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { InvalidActorState } from "../../src/exceptions/invalidActorState.exception.js"; | ||
import { EboEvent } from "../../src/types/events.js"; | ||
import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "./fixtures.js"; | ||
import mocks from "./mocks/index.js"; | ||
|
||
const logger: ILogger = { | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
debug: vi.fn(), | ||
}; | ||
|
||
describe("EboActor", () => { | ||
describe("onRequestFinalized", () => { | ||
const actorRequest = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
|
||
const event: EboEvent<"RequestFinalized"> = { | ||
name: "RequestFinalized", | ||
blockNumber: 1n, | ||
logIndex: 1, | ||
metadata: { | ||
blockNumber: 1n, | ||
caller: "0x01", | ||
requestId: actorRequest.id, | ||
responseId: "0x02", | ||
}, | ||
}; | ||
|
||
it("executes the actor's callback during termination", async () => { | ||
const { actor, onTerminate, registry } = mocks.buildEboActor(actorRequest, logger); | ||
|
||
vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); | ||
|
||
onTerminate.mockImplementation(() => Promise.resolve()); | ||
|
||
await actor.onRequestFinalized(event); | ||
|
||
expect(onTerminate).toHaveBeenCalledWith(actorRequest); | ||
}); | ||
|
||
it("throws if the event's request is not handled by actor", () => { | ||
const { actor } = mocks.buildEboActor(actorRequest, logger); | ||
|
||
const otherRequestEvent = { | ||
...event, | ||
metadata: { | ||
...event.metadata, | ||
requestId: actorRequest.id + "123", | ||
}, | ||
}; | ||
|
||
expect(actor.onRequestFinalized(otherRequestEvent)).rejects.toThrow(InvalidActorState); | ||
}); | ||
|
||
// The one who defines the callback is responsible for handling callback errors | ||
it("throws if the callback throws", () => { | ||
const { actor, onTerminate } = mocks.buildEboActor(actorRequest, logger); | ||
|
||
onTerminate.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
expect(actor.onRequestFinalized(event)).rejects.toThrow(InvalidActorState); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdyt about having the callback function as parameter of the
onRequestFinalized
instead of the constructor?it will make it obvious that the function will be called from there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great proposal, you got me thinking why did I go for this option lol 🤔
=== PR #24 spoiler ahead ===
There are some situations in which that
onTerminate
callback is being used outside theonRequestFinalized
handler (eg the request was "escalated", so the agent stops tracking it as it will be handled by another system outside the EBO agent)