From 165c8e8fc2a6405ad5708e79483f184419469e39 Mon Sep 17 00:00:00 2001 From: Yaco 0x Date: Mon, 9 Sep 2024 11:55:15 -0300 Subject: [PATCH] fix: replace noop with FinalizeRequest command --- packages/automated-dispute/src/eboActor.ts | 7 ++- .../services/eboRegistry/commands/index.ts | 1 - .../src/services/eboRegistry/commands/noop.ts | 22 ------- .../tests/eboActor/onRequestFinalized.spec.ts | 63 ++++++++++++------- .../eboMemoryRegistry/commands/noop.spec.ts | 24 ------- 5 files changed, 45 insertions(+), 72 deletions(-) delete mode 100644 packages/automated-dispute/src/services/eboRegistry/commands/noop.ts delete mode 100644 packages/automated-dispute/tests/services/eboMemoryRegistry/commands/noop.spec.ts diff --git a/packages/automated-dispute/src/eboActor.ts b/packages/automated-dispute/src/eboActor.ts index 2821fa0..eba43f0 100644 --- a/packages/automated-dispute/src/eboActor.ts +++ b/packages/automated-dispute/src/eboActor.ts @@ -20,7 +20,7 @@ import { AddDispute, AddRequest, AddResponse, - Noop, + FinalizeRequest, UpdateDisputeStatus, } from "./services/index.js"; import { @@ -202,7 +202,10 @@ export class EboActor { ); case "RequestFinalized": - return Noop.buildFromEvent(); + return FinalizeRequest.buildFromEvent( + event as EboEvent<"RequestFinalized">, + this.registry, + ); default: throw new UnknownEvent(event.name); diff --git a/packages/automated-dispute/src/services/eboRegistry/commands/index.ts b/packages/automated-dispute/src/services/eboRegistry/commands/index.ts index e7d58ac..aa77846 100644 --- a/packages/automated-dispute/src/services/eboRegistry/commands/index.ts +++ b/packages/automated-dispute/src/services/eboRegistry/commands/index.ts @@ -2,5 +2,4 @@ export * from "./addDispute.js"; export * from "./addRequest.js"; export * from "./addResponse.js"; export * from "./finalizeRequest.js"; -export * from "./noop.js"; export * from "./updateDisputeStatus.js"; diff --git a/packages/automated-dispute/src/services/eboRegistry/commands/noop.ts b/packages/automated-dispute/src/services/eboRegistry/commands/noop.ts deleted file mode 100644 index a15af24..0000000 --- a/packages/automated-dispute/src/services/eboRegistry/commands/noop.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { CommandAlreadyRun, CommandNotRun } from "../../../exceptions/index.js"; -import { EboRegistryCommand } from "../../../interfaces/index.js"; - -export class Noop implements EboRegistryCommand { - private wasRun: boolean = false; - - private constructor() {} - - public static buildFromEvent(): Noop { - return new Noop(); - } - - run(): void { - if (this.wasRun) throw new CommandAlreadyRun(Noop.name); - - this.wasRun = true; - } - - undo(): void { - if (!this.wasRun) throw new CommandNotRun(Noop.name); - } -} diff --git a/packages/automated-dispute/tests/eboActor/onRequestFinalized.spec.ts b/packages/automated-dispute/tests/eboActor/onRequestFinalized.spec.ts index 6f716eb..5767ced 100644 --- a/packages/automated-dispute/tests/eboActor/onRequestFinalized.spec.ts +++ b/packages/automated-dispute/tests/eboActor/onRequestFinalized.spec.ts @@ -1,6 +1,7 @@ import { ILogger } from "@ebo-agent/shared"; import { describe, expect, it, vi } from "vitest"; +import { FinalizeRequest } from "../../src/services/index.js"; import { EboEvent } from "../../src/types/index.js"; import mocks from "../mocks/index.js"; import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "./fixtures.js"; @@ -8,36 +9,52 @@ import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "./fixtures.js"; const logger: ILogger = mocks.mockLogger(); describe("EboActor", () => { - describe("onRequestFinalized", () => { - const actorRequest = DEFAULT_MOCKED_REQUEST_CREATED_DATA; - - const event: EboEvent<"RequestFinalized"> = { - name: "RequestFinalized", - requestId: actorRequest.id, - blockNumber: 1n, - logIndex: 1, - metadata: { - blockNumber: 1n, - caller: "0x01", + describe("processEvents", () => { + describe("when RequestFinalized is enqueued", () => { + const actorRequest = DEFAULT_MOCKED_REQUEST_CREATED_DATA; + + const event: EboEvent<"RequestFinalized"> = { + name: "RequestFinalized", requestId: actorRequest.id, - responseId: "0x02", - }, - }; + blockNumber: 1n, + logIndex: 1, + metadata: { + blockNumber: 1n, + caller: "0x01", + requestId: actorRequest.id, + responseId: "0x02", + }, + }; + + it("logs a message during request finalization", async () => { + const { actor, registry } = mocks.buildEboActor(actorRequest, logger); + + vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); + + const mockInfo = vi.spyOn(logger, "info"); + + actor.enqueue(event); + + await actor.processEvents(); + + expect(mockInfo).toHaveBeenCalledWith( + expect.stringMatching(`Request ${actorRequest.id} has been finalized.`), + ); + }); - it("logs a message during request finalization", async () => { - const { actor, registry } = mocks.buildEboActor(actorRequest, logger); + it("uses the FinalizeRequest registry command", async () => { + const { actor, registry } = mocks.buildEboActor(actorRequest, logger); - vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); + vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); - const mockInfo = vi.spyOn(logger, "info"); + const mockBuildFromEvent = vi.spyOn(FinalizeRequest, "buildFromEvent"); - actor.enqueue(event); + actor.enqueue(event); - await actor.processEvents(); + await actor.processEvents(); - expect(mockInfo).toHaveBeenCalledWith( - expect.stringMatching(`Request ${actorRequest.id} has been finalized.`), - ); + expect(mockBuildFromEvent).toHaveBeenCalledWith(event, registry); + }); }); }); }); diff --git a/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/noop.spec.ts b/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/noop.spec.ts deleted file mode 100644 index fbd6c31..0000000 --- a/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/noop.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { CommandAlreadyRun, CommandNotRun } from "../../../../src/exceptions/index.js"; -import { Noop } from "../../../../src/services/index.js"; - -describe("Noop", () => { - describe("run", () => { - it("throws if the command was already run", () => { - const command = Noop.buildFromEvent(); - - command.run(); - - expect(() => command.run()).toThrow(CommandAlreadyRun); - }); - }); - - describe("undo", () => { - it("throws if undoing the command before being run", () => { - const command = Noop.buildFromEvent(); - - expect(() => command.undo()).toThrow(CommandNotRun); - }); - }); -});