From 6b71476d7c66e10b2fc263300a3949306d313f78 Mon Sep 17 00:00:00 2001 From: Liu <57480598+liu-zhipeng@users.noreply.github.com> Date: Tue, 10 Oct 2023 23:24:09 +0800 Subject: [PATCH] test: watcher unit tests --- packages/agents/watcher/test/mock.ts | 9 ++++- .../test/operations/validateAndSwitch.spec.ts | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/agents/watcher/test/operations/validateAndSwitch.spec.ts diff --git a/packages/agents/watcher/test/mock.ts b/packages/agents/watcher/test/mock.ts index ab1ecf869c..5ba7940af2 100644 --- a/packages/agents/watcher/test/mock.ts +++ b/packages/agents/watcher/test/mock.ts @@ -1,6 +1,6 @@ import { Wallet } from "ethers"; import { createStubInstance } from "sinon"; -import { WatcherAdapter } from "@connext/nxtp-adapters-watcher"; +import { OpModeMonitor, WatcherAdapter } from "@connext/nxtp-adapters-watcher"; import { Logger, mkHash, mock as _mock, mockSequencer } from "@connext/nxtp-utils"; import { mockSubgraph } from "@connext/nxtp-adapters-subgraph/test/mock"; @@ -15,13 +15,18 @@ export const mock = { subgraph: mockSubgraph(), wallet: createStubInstance(Wallet, { getAddress: Promise.resolve(mockSequencer) }), watcher: createStubInstance(WatcherAdapter, { - checkInvariants: Promise.resolve(true), + checkInvariants: Promise.resolve({ needsPause: true }), pause: Promise.resolve([ { domain: mock.domain.A, error: "foo", paused: true, relevantTransaction: mkHash("0x1") }, { domain: mock.domain.B, error: "bar", paused: true, relevantTransaction: mkHash("0x1") }, ]), alert: Promise.resolve(), }), + monitor: createStubInstance(OpModeMonitor, { + validateProposal: Promise.resolve({ needsSwitch: false, reason: "" }), + switch: Promise.resolve(), + alert: Promise.resolve(), + }), }, config: mock.config(), chainData: mock.chainData(), diff --git a/packages/agents/watcher/test/operations/validateAndSwitch.spec.ts b/packages/agents/watcher/test/operations/validateAndSwitch.spec.ts new file mode 100644 index 0000000000..70213faf2d --- /dev/null +++ b/packages/agents/watcher/test/operations/validateAndSwitch.spec.ts @@ -0,0 +1,38 @@ +import { BaseRequestContext, createRequestContext, expect, mkHash } from "@connext/nxtp-utils"; +import { PauseResponse, ReportEventType } from "@connext/nxtp-adapters-watcher"; +import { stub, SinonStub } from "sinon"; +import * as validateAndSwitchFns from "../../src/operations/validateAndSwitch"; +import { ctxMock } from "../globalTestHook"; + +const requestContext = createRequestContext("test"); + +describe("Operations:validateAndSwitch", () => { + describe("validateAndSwitch", () => { + let validateAndSwitchStub: SinonStub< + [requestContext: BaseRequestContext, reason: string], + Promise + >; + beforeEach(() => { + validateAndSwitchStub = stub(validateAndSwitchFns, "switchAndAlert").resolves(); + }); + + it("should not call switchAndAlert if it doesnt need pause", async () => { + (ctxMock.adapters.monitor.validateProposal as SinonStub).resolves({ needsSwitch: false }); + await validateAndSwitchFns.validateAndSwitch(); + expect(validateAndSwitchStub.callCount).to.be.eq(0); + }); + + it("should call switchAndAlert if needs pause", async () => { + (ctxMock.adapters.monitor.validateProposal as SinonStub).resolves({ needsSwitch: true }); + await validateAndSwitchFns.validateAndSwitch(); + expect(validateAndSwitchStub).to.have.been.calledOnce; + }); + }); + + describe("switchAndAlert", () => { + it("should switch and alert", async () => { + await validateAndSwitchFns.switchAndAlert(requestContext, "reason"); + expect(ctxMock.adapters.watcher.alert).to.be.calledOnce; + }); + }); +});