test: dedupe and optimize test suites

This commit is contained in:
Peter Steinberger
2026-02-19 15:18:50 +00:00
parent b0e55283d5
commit a1cb700a05
80 changed files with 2627 additions and 2962 deletions

View File

@@ -1,5 +1,5 @@
import { Command } from "commander";
import { describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const listChannelPairingRequests = vi.fn();
const approveChannelPairingCode = vi.fn();
@@ -45,167 +45,153 @@ vi.mock("../config/config.js", () => ({
}));
describe("pairing cli", () => {
it("evaluates pairing channels when registering the CLI (not at import)", async () => {
let registerPairingCli: typeof import("./pairing-cli.js").registerPairingCli;
beforeAll(async () => {
({ registerPairingCli } = await import("./pairing-cli.js"));
});
beforeEach(() => {
listChannelPairingRequests.mockReset();
approveChannelPairingCode.mockReset();
notifyPairingApproved.mockReset();
normalizeChannelId.mockClear();
getPairingAdapter.mockClear();
listPairingChannels.mockClear();
});
const { registerPairingCli } = await import("./pairing-cli.js");
expect(listPairingChannels).not.toHaveBeenCalled();
function createProgram() {
const program = new Command();
program.name("test");
registerPairingCli(program);
return program;
}
async function runPairing(args: string[]) {
const program = createProgram();
await program.parseAsync(args, { from: "user" });
}
function mockApprovedPairing() {
approveChannelPairingCode.mockResolvedValueOnce({
id: "123",
entry: {
id: "123",
code: "ABCDEFGH",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
},
});
}
it("evaluates pairing channels when registering the CLI (not at import)", async () => {
expect(listPairingChannels).not.toHaveBeenCalled();
createProgram();
expect(listPairingChannels).toHaveBeenCalledTimes(1);
});
it("labels Telegram ids as telegramUserId", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
it.each([
{
name: "telegram ids",
channel: "telegram",
id: "123",
label: "telegramUserId",
meta: { username: "peter" },
},
{
name: "discord ids",
channel: "discord",
id: "999",
label: "discordUserId",
meta: { tag: "Ada#0001" },
},
])("labels $name correctly", async ({ channel, id, label, meta }) => {
listChannelPairingRequests.mockResolvedValueOnce([
{
id: "123",
id,
code: "ABC123",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
meta: { username: "peter" },
meta,
},
]);
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "--channel", "telegram"], {
from: "user",
});
const output = log.mock.calls.map((call) => call.join(" ")).join("\n");
expect(output).toContain("telegramUserId");
expect(output).toContain("123");
try {
await runPairing(["pairing", "list", "--channel", channel]);
const output = log.mock.calls.map((call) => call.join(" ")).join("\n");
expect(output).toContain(label);
expect(output).toContain(id);
} finally {
log.mockRestore();
}
});
it("accepts channel as positional for list", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listChannelPairingRequests.mockResolvedValueOnce([]);
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "telegram"], { from: "user" });
await runPairing(["pairing", "list", "telegram"]);
expect(listChannelPairingRequests).toHaveBeenCalledWith("telegram");
});
it("forwards --account for list", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listChannelPairingRequests.mockResolvedValueOnce([]);
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "--channel", "telegram", "--account", "yy"], {
from: "user",
});
await runPairing(["pairing", "list", "--channel", "telegram", "--account", "yy"]);
expect(listChannelPairingRequests).toHaveBeenCalledWith("telegram", process.env, "yy");
});
it("normalizes channel aliases", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listChannelPairingRequests.mockResolvedValueOnce([]);
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "imsg"], { from: "user" });
await runPairing(["pairing", "list", "imsg"]);
expect(normalizeChannelId).toHaveBeenCalledWith("imsg");
expect(listChannelPairingRequests).toHaveBeenCalledWith("imessage");
});
it("accepts extension channels outside the registry", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listChannelPairingRequests.mockResolvedValueOnce([]);
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "zalo"], { from: "user" });
await runPairing(["pairing", "list", "zalo"]);
expect(normalizeChannelId).toHaveBeenCalledWith("zalo");
expect(listChannelPairingRequests).toHaveBeenCalledWith("zalo");
});
it("labels Discord ids as discordUserId", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
listChannelPairingRequests.mockResolvedValueOnce([
{
id: "999",
code: "DEF456",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
meta: { tag: "Ada#0001" },
},
]);
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "list", "--channel", "discord"], {
from: "user",
});
const output = log.mock.calls.map((call) => call.join(" ")).join("\n");
expect(output).toContain("discordUserId");
expect(output).toContain("999");
});
it("accepts channel as positional for approve (npm-run compatible)", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
approveChannelPairingCode.mockResolvedValueOnce({
id: "123",
entry: {
id: "123",
code: "ABCDEFGH",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
},
});
mockApprovedPairing();
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(["pairing", "approve", "telegram", "ABCDEFGH"], {
from: "user",
});
try {
await runPairing(["pairing", "approve", "telegram", "ABCDEFGH"]);
expect(approveChannelPairingCode).toHaveBeenCalledWith({
channel: "telegram",
code: "ABCDEFGH",
});
expect(log).toHaveBeenCalledWith(expect.stringContaining("Approved"));
expect(approveChannelPairingCode).toHaveBeenCalledWith({
channel: "telegram",
code: "ABCDEFGH",
});
expect(log).toHaveBeenCalledWith(expect.stringContaining("Approved"));
} finally {
log.mockRestore();
}
});
it("forwards --account for approve", async () => {
const { registerPairingCli } = await import("./pairing-cli.js");
approveChannelPairingCode.mockResolvedValueOnce({
id: "123",
entry: {
id: "123",
code: "ABCDEFGH",
createdAt: "2026-01-08T00:00:00Z",
lastSeenAt: "2026-01-08T00:00:00Z",
},
});
mockApprovedPairing();
const program = new Command();
program.name("test");
registerPairingCli(program);
await program.parseAsync(
["pairing", "approve", "--channel", "telegram", "--account", "yy", "ABCDEFGH"],
{
from: "user",
},
);
await runPairing([
"pairing",
"approve",
"--channel",
"telegram",
"--account",
"yy",
"ABCDEFGH",
]);
expect(approveChannelPairingCode).toHaveBeenCalledWith({
channel: "telegram",