Skip to content

Commit

Permalink
test: watcher unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-zhipeng committed Oct 10, 2023
1 parent 43a2bbc commit 6b71476
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/agents/watcher/test/mock.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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(),
Expand Down
38 changes: 38 additions & 0 deletions packages/agents/watcher/test/operations/validateAndSwitch.spec.ts
Original file line number Diff line number Diff line change
@@ -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<PauseResponse[]>
>;
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;
});
});
});

0 comments on commit 6b71476

Please sign in to comment.