-
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.
Merge remote-tracking branch 'origin/dev' into feat/on-dispute-updated
- Loading branch information
Showing
13 changed files
with
175 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { EboActor } from "./eboActor.js"; | ||
import { RequestAlreadyHandled } from "./exceptions/index.js"; | ||
|
||
export class EboActorsManager { | ||
private readonly requestActorMap: Map<string, EboActor>; | ||
|
||
constructor() { | ||
this.requestActorMap = new Map(); | ||
} | ||
|
||
/** | ||
* Registers the actor and makes it fetchable by the ID of the request the actor is handling. | ||
* | ||
* @param actor an `EboActor` instance that handles the request | ||
*/ | ||
public registerActor(actor: EboActor): void { | ||
const requestId = actor.getRequestId(); | ||
|
||
if (this.requestActorMap.has(requestId)) throw new RequestAlreadyHandled(requestId); | ||
|
||
this.requestActorMap.set(requestId, actor); | ||
} | ||
|
||
/** | ||
* Get the `EboActor` instance linked with the `requestId`. | ||
* | ||
* @param requestId request ID | ||
* @returns an `EboActor` instance if found by `requestId`, otherwise `undefined` | ||
*/ | ||
public getActor(requestId: string): EboActor | undefined { | ||
return this.requestActorMap.get(requestId); | ||
} | ||
|
||
/** | ||
* Deletes an actor from the manager, based on its linked request. | ||
* | ||
* @param requestId request ID | ||
* @returns `true` if there was a linked actor for the request ID and it was removed, or `false` if the request was not linked to any actor. | ||
*/ | ||
public deleteActor(requestId: string): boolean { | ||
return this.requestActorMap.delete(requestId); | ||
} | ||
} |
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,5 +1,6 @@ | ||
export * from "./rpcUrlsEmpty.exception.js"; | ||
export * from "./invalidActorState.exception.js"; | ||
export * from "./invalidDisputeStatus.exception.js"; | ||
export * from "./requestAlreadyHandled.exception.js"; | ||
export * from "./requestMismatch.js"; | ||
export * from "./responseAlreadyProposed.js"; | ||
export * from "./rpcUrlsEmpty.exception.js"; |
7 changes: 7 additions & 0 deletions
7
packages/automated-dispute/src/exceptions/requestAlreadyHandled.exception.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,7 @@ | ||
export class RequestAlreadyHandled extends Error { | ||
constructor(requestId: string) { | ||
super(`Request ${requestId} is already being handled by another actor.`); | ||
|
||
this.name = "RequestAlreadyHandled"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ILogger } from "@ebo-agent/shared"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { EboActorsManager } from "../src/eboActorsManager.js"; | ||
import { RequestAlreadyHandled } from "../src/exceptions/index.js"; | ||
import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "./eboActor/fixtures.js"; | ||
import mocks from "./mocks/index.js"; | ||
|
||
const logger: ILogger = mocks.mockLogger(); | ||
|
||
describe("EboActorsManager", () => { | ||
describe("registerActor", () => { | ||
it("registers the actor correctly", () => { | ||
const request = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
const { actor } = mocks.buildEboActor(request, logger); | ||
const actorsManager = new EboActorsManager(); | ||
const mockSetRequestActorMap = vi.spyOn(actorsManager["requestActorMap"], "set"); | ||
|
||
actorsManager.registerActor(actor); | ||
|
||
expect(mockSetRequestActorMap).toHaveBeenCalledWith(request.id, actor); | ||
expect(actorsManager.getActor(actor.getRequestId())).toBe(actor); | ||
}); | ||
|
||
it("throws if the request has already an actor linked to it", () => { | ||
const request = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
const { actor: firstActor } = mocks.buildEboActor(request, logger); | ||
const { actor: secondActor } = mocks.buildEboActor(request, logger); | ||
const actorsManager = new EboActorsManager(); | ||
|
||
actorsManager.registerActor(firstActor); | ||
|
||
expect(() => actorsManager.registerActor(secondActor)).toThrowError( | ||
RequestAlreadyHandled, | ||
); | ||
}); | ||
}); | ||
|
||
describe("getActor", () => { | ||
it("returns undefined if the request is not linked to any actor", () => { | ||
const actorsManager = new EboActorsManager(); | ||
|
||
expect(actorsManager.getActor("0x9999")).toBeUndefined(); | ||
}); | ||
|
||
it("returns the request's linked actor", () => { | ||
const request = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
const { actor } = mocks.buildEboActor(request, logger); | ||
const actorsManager = new EboActorsManager(); | ||
|
||
actorsManager.registerActor(actor); | ||
|
||
expect(actorsManager.getActor(request.id)).toBe(actor); | ||
}); | ||
}); | ||
|
||
describe("deleteActor", () => { | ||
it("deletes the actor linked to the request", () => { | ||
const request = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
const { actor } = mocks.buildEboActor(request, logger); | ||
const actorsManager = new EboActorsManager(); | ||
|
||
actorsManager.registerActor(actor); | ||
|
||
expect(actorsManager.getActor(request.id)).toBe(actor); | ||
|
||
actorsManager.deleteActor(request.id); | ||
|
||
expect(actorsManager.getActor(request.id)).toBeUndefined(); | ||
}); | ||
|
||
it("returns false if the request has no actors linked", () => { | ||
const requestId = "0x01"; | ||
const actorsManager = new EboActorsManager(); | ||
|
||
expect(actorsManager.getActor(requestId)).toBeUndefined(); | ||
expect(actorsManager.deleteActor(requestId)).toEqual(false); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { buildDispute, buildEboActor, buildResponse } from "./eboActor.js"; | ||
import { mockLogger } from "./logger.js"; | ||
|
||
export default { buildEboActor, buildResponse, buildDispute, mockLogger }; |
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,9 @@ | ||
import { ILogger } from "@ebo-agent/shared"; | ||
import { vi } from "vitest"; | ||
|
||
export const mockLogger: () => ILogger = () => ({ | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
debug: vi.fn(), | ||
}); |