Skip to content

Commit 4803595

Browse files
committed
test: stabilize cli tests
1 parent d9d8510 commit 4803595

11 files changed

+116
-36
lines changed

dist/cli/base-command.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Command, Flags } from "@oclif/core";
22
import defaults from "../common-utils/defaults.js";
33
import Plebbit from "@plebbit/plebbit-js";
4+
const getPlebbitConnectOverride = () => {
5+
const globalWithOverride = globalThis;
6+
return globalWithOverride.__PLEBBIT_RPC_CONNECT_OVERRIDE;
7+
};
48
export class BaseCommand extends Command {
59
static baseFlags = {
610
plebbitRpcUrl: Flags.url({
@@ -10,6 +14,10 @@ export class BaseCommand extends Command {
1014
})
1115
};
1216
async _connectToPlebbitRpc(plebbitRpcUrl) {
17+
const connectOverride = getPlebbitConnectOverride();
18+
if (connectOverride) {
19+
return connectOverride(plebbitRpcUrl);
20+
}
1321
const plebbit = await Plebbit({ plebbitRpcClientsOptions: [plebbitRpcUrl] });
1422
plebbit.on("error", (err) => console.error("Error from plebbit instance", err));
1523
await new Promise((resolve) => plebbit.once("subplebbitschange", resolve));

dist/tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/cli/base-command.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { Command, Flags } from "@oclif/core";
22
import defaults from "../common-utils/defaults.js";
33
import Plebbit from "@plebbit/plebbit-js";
44
type PlebbitInstance = Awaited<ReturnType<typeof Plebbit>>;
5+
type PlebbitConnectOverride = (plebbitRpcUrl: string) => Promise<PlebbitInstance>;
6+
7+
const getPlebbitConnectOverride = (): PlebbitConnectOverride | undefined => {
8+
const globalWithOverride = globalThis as { __PLEBBIT_RPC_CONNECT_OVERRIDE?: PlebbitConnectOverride };
9+
return globalWithOverride.__PLEBBIT_RPC_CONNECT_OVERRIDE;
10+
};
11+
512
export abstract class BaseCommand extends Command {
613
static override baseFlags = {
714
plebbitRpcUrl: Flags.url({
@@ -12,6 +19,10 @@ export abstract class BaseCommand extends Command {
1219
};
1320

1421
protected async _connectToPlebbitRpc(plebbitRpcUrl: string): Promise<PlebbitInstance> {
22+
const connectOverride = getPlebbitConnectOverride();
23+
if (connectOverride) {
24+
return connectOverride(plebbitRpcUrl);
25+
}
1526
const plebbit = await Plebbit({ plebbitRpcClientsOptions: [plebbitRpcUrl] });
1627
plebbit.on("error", (err) => console.error("Error from plebbit instance", err));
1728
await new Promise((resolve) => plebbit.once("subplebbitschange", resolve));

test/cli/create.community.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
33
import signers from "../fixtures/signers.js";
44
import Sinon from "sinon";
55
import type { CreateSubplebbitOptions } from "../types/communityTypes.js";
6-
7-
import { BaseCommand } from "../../dist/cli/base-command.js";
6+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
87

98
const cliCreateOptions = {
109
privateKeyPath: "test/fixtures/community_0_private_key.pem",
@@ -31,16 +30,18 @@ describe("bitsocial community create", () => {
3130
createSubplebbit: plebbitCreateStub,
3231
destroy: () => {}
3332
});
34-
//@ts-expect-error
35-
sandbox.replace(BaseCommand.prototype, "_connectToPlebbitRpc", plebbitInstanceFake);
33+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
3634
});
3735

3836
afterEach(() => {
3937
plebbitCreateStub.resetHistory();
4038
startFake.resetHistory();
4139
});
4240

43-
afterAll(() => sandbox.restore());
41+
afterAll(() => {
42+
clearPlebbitRpcConnectOverride();
43+
sandbox.restore();
44+
});
4445

4546
it(`Parses minimal create options correctly`, async () => {
4647
const result = await runCreateCommand("community create --description testDescription");

test/cli/edit.community.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
44
import Sinon from "sinon";
55
import type { SubplebbitEditOptions } from "../types/communityTypes.js";
66
import { currentSubProps } from "../fixtures/communityForEditFixture.js";
7-
import { BaseCommand } from "../../dist/cli/base-command.js";
7+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
88

99
describe("bitsocial community edit", () => {
1010
const sandbox = Sinon.createSandbox();
@@ -23,11 +23,12 @@ describe("bitsocial community edit", () => {
2323
subplebbits: ["plebbit.eth"],
2424
destroy: () => {}
2525
});
26-
sandbox.replace(BaseCommand.prototype as any, "_connectToPlebbitRpc", plebbitInstanceFake);
26+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
2727
});
2828

2929
afterEach(() => editFake.resetHistory());
3030
afterAll(() => {
31+
clearPlebbitRpcConnectOverride();
3132
sandbox.restore();
3233
});
3334

test/cli/get.community.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { runCommand } from "@oclif/test";
21
import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
32
import Sinon from "sinon";
4-
import { BaseCommand } from "../../dist/cli/base-command.js";
3+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
4+
import { runCliCommand } from "../helpers/run-cli.js";
55

66
describe("bitsocial community get", () => {
77
const sandbox = Sinon.createSandbox();
@@ -20,25 +20,27 @@ describe("bitsocial community get", () => {
2020
getSubplebbit: getSubplebbitFake,
2121
destroy: destroyFake
2222
});
23-
//@ts-expect-error
24-
sandbox.replace(BaseCommand.prototype, "_connectToPlebbitRpc", plebbitInstanceFake);
23+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
2524
});
2625

2726
afterEach(() => {
2827
getSubplebbitFake.resetHistory();
2928
destroyFake.resetHistory();
3029
});
3130

32-
afterAll(() => sandbox.restore());
31+
afterAll(() => {
32+
clearPlebbitRpcConnectOverride();
33+
sandbox.restore();
34+
});
3335

3436
it("Outputs community json and keeps posts first", async () => {
35-
const result = await runCommand("community get plebbit.eth", process.cwd(), { stripAnsi: true });
37+
const { result, stdout } = await runCliCommand("community get plebbit.eth");
3638

3739
expect(result.error).toBeUndefined();
3840
expect(getSubplebbitFake.calledOnceWith({ address: "plebbit.eth" })).toBe(true);
3941
expect(destroyFake.calledOnce).toBe(true);
4042

41-
const output = result.stdout.trim();
43+
const output = stdout.trim();
4244
const parsed = JSON.parse(output);
4345
expect(parsed).toEqual(fakeCommunity);
4446

test/cli/list.community.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { runCommand } from "@oclif/test";
21
import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
32
import Sinon from "sinon";
4-
import { BaseCommand } from "../../dist/cli/base-command.js";
3+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
4+
import { runCliCommand } from "../helpers/run-cli.js";
55

66
describe("bitsocial community list", () => {
77
const sandbox = Sinon.createSandbox();
@@ -12,17 +12,19 @@ describe("bitsocial community list", () => {
1212
subplebbits: fakeCommunities,
1313
destroy: () => {}
1414
});
15-
//@ts-expect-error
16-
sandbox.replace(BaseCommand.prototype, "_connectToPlebbitRpc", plebbitInstanceFake);
15+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
1716
});
1817

1918
afterEach(() => sandbox.resetHistory());
20-
afterAll(() => sandbox.restore());
19+
afterAll(() => {
20+
clearPlebbitRpcConnectOverride();
21+
sandbox.restore();
22+
});
2123

2224
it(`-q Outputs only community addresses`, async () => {
23-
const result = await runCommand("community list -q", process.cwd());
25+
const { result, stdout } = await runCliCommand("community list -q");
2426
expect(result.error).toBeUndefined();
25-
const trimmedOutput: string[] = result.stdout.trim().split("\n");
27+
const trimmedOutput: string[] = stdout.trim().split("\n");
2628
expect(trimmedOutput).toEqual(fakeCommunities);
2729
});
2830
});

test/cli/start.community.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { runCommand } from "@oclif/test";
21
import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
32
import Sinon from "sinon";
4-
import { BaseCommand } from "../../dist/cli/base-command.js";
3+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
4+
import { runCliCommand } from "../helpers/run-cli.js";
55

66
describe("bitsocial community start", () => {
77
const addresses = ["plebbit.eth", "plebbit2.eth"];
@@ -16,20 +16,22 @@ describe("bitsocial community start", () => {
1616
destroy: () => {}
1717
});
1818

19-
//@ts-expect-error
20-
sandbox.replace(BaseCommand.prototype, "_connectToPlebbitRpc", plebbitInstanceFake);
19+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
2120
});
2221

2322
afterEach(() => startFake.resetHistory());
24-
afterAll(() => sandbox.restore());
23+
afterAll(() => {
24+
clearPlebbitRpcConnectOverride();
25+
sandbox.restore();
26+
});
2527

2628
it(`Parses and submits addresses correctly`, async () => {
27-
const result = await runCommand(["community", "start", ...addresses], process.cwd());
29+
const { result, stdout } = await runCliCommand(["community", "start", ...addresses]);
2830
// Validate calls to start here
2931
expect(startFake.callCount).toBe(addresses.length);
3032

3133
// Validate outputs
32-
const trimmedOutput: string[] = result.stdout.trim().split("\n");
34+
const trimmedOutput: string[] = stdout.trim().split("\n");
3335
expect(trimmedOutput).toEqual(addresses);
3436
expect(result.error).toBeUndefined();
3537
});

test/cli/stop.community.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { runCommand } from "@oclif/test";
21
import { describe, it, beforeAll, afterAll, afterEach, expect } from "vitest";
32
import Sinon from "sinon";
4-
import { BaseCommand } from "../../dist/cli/base-command.js";
3+
import { clearPlebbitRpcConnectOverride, setPlebbitRpcConnectOverride } from "../helpers/plebbit-test-overrides.js";
4+
import { runCliCommand } from "../helpers/run-cli.js";
55

66
describe("bitsocial community stop", () => {
77
const addresses = ["plebbit.eth", "plebbit2.eth"];
@@ -16,20 +16,22 @@ describe("bitsocial community stop", () => {
1616
destroy: () => {}
1717
});
1818

19-
//@ts-expect-error
20-
sandbox.replace(BaseCommand.prototype, "_connectToPlebbitRpc", plebbitInstanceFake);
19+
setPlebbitRpcConnectOverride(plebbitInstanceFake);
2120
});
2221

2322
afterEach(() => stopFake.resetHistory());
24-
afterAll(() => sandbox.restore());
23+
afterAll(() => {
24+
clearPlebbitRpcConnectOverride();
25+
sandbox.restore();
26+
});
2527

2628
it(`Parses and submits addresses correctly`, async () => {
27-
const result = await runCommand(["community", "stop", ...addresses], process.cwd());
29+
const { result, stdout } = await runCliCommand(["community", "stop", ...addresses]);
2830
// Validate calls to stop here
2931
expect(stopFake.callCount).toBe(addresses.length);
3032

3133
// Validate outputs
32-
const trimmedOutput: string[] = result.stdout.trim().split("\n");
34+
const trimmedOutput: string[] = stdout.trim().split("\n");
3335
expect(trimmedOutput).toEqual(addresses);
3436
expect(result.error).toBeUndefined();
3537
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type PlebbitConnectOverride = (plebbitRpcUrl: string) => Promise<unknown>;
2+
3+
type PlebbitConnectOverrideGlobal = {
4+
__PLEBBIT_RPC_CONNECT_OVERRIDE?: PlebbitConnectOverride;
5+
};
6+
7+
export const setPlebbitRpcConnectOverride = (override: PlebbitConnectOverride) => {
8+
(globalThis as PlebbitConnectOverrideGlobal).__PLEBBIT_RPC_CONNECT_OVERRIDE = override;
9+
};
10+
11+
export const clearPlebbitRpcConnectOverride = () => {
12+
delete (globalThis as PlebbitConnectOverrideGlobal).__PLEBBIT_RPC_CONNECT_OVERRIDE;
13+
};

0 commit comments

Comments
 (0)