test: speed up cli and command suites

This commit is contained in:
Peter Steinberger
2026-03-31 02:12:23 +01:00
parent 6b6ddcd2a6
commit 3f1d6fe147
83 changed files with 1161 additions and 1054 deletions

View File

@@ -1,43 +1,56 @@
import { Command } from "commander";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { registerPairingCli } from "./pairing-cli.js";
const mocks = vi.hoisted(() => ({
listChannelPairingRequests: vi.fn(),
approveChannelPairingCode: vi.fn(),
notifyPairingApproved: vi.fn(),
normalizeChannelId: vi.fn((raw: string) => {
if (!raw) {
return null;
}
if (raw === "imsg") {
return "imessage";
}
if (["telegram", "discord", "imessage"].includes(raw)) {
return raw;
}
return null;
}),
getPairingAdapter: vi.fn((channel: string) => ({
idLabel: pairingIdLabels[channel] ?? "userId",
})),
listPairingChannels: vi.fn(() => ["telegram", "discord", "imessage"]),
}));
const {
listChannelPairingRequests,
approveChannelPairingCode,
notifyPairingApproved,
normalizeChannelId,
getPairingAdapter,
listPairingChannels,
} = mocks;
const listChannelPairingRequests = vi.fn();
const approveChannelPairingCode = vi.fn();
const notifyPairingApproved = vi.fn();
const pairingIdLabels: Record<string, string> = {
telegram: "telegramUserId",
discord: "discordUserId",
};
const normalizeChannelId = vi.fn((raw: string) => {
if (!raw) {
return null;
}
if (raw === "imsg") {
return "imessage";
}
if (["telegram", "discord", "imessage"].includes(raw)) {
return raw;
}
return null;
});
const getPairingAdapter = vi.fn((channel: string) => ({
idLabel: pairingIdLabels[channel] ?? "userId",
}));
const listPairingChannels = vi.fn(() => ["telegram", "discord", "imessage"]);
vi.mock("../pairing/pairing-store.js", () => ({
listChannelPairingRequests,
approveChannelPairingCode,
listChannelPairingRequests: mocks.listChannelPairingRequests,
approveChannelPairingCode: mocks.approveChannelPairingCode,
}));
vi.mock("../channels/plugins/pairing.js", () => ({
listPairingChannels,
notifyPairingApproved,
getPairingAdapter,
listPairingChannels: mocks.listPairingChannels,
notifyPairingApproved: mocks.notifyPairingApproved,
getPairingAdapter: mocks.getPairingAdapter,
}));
vi.mock("../channels/plugins/index.js", () => ({
normalizeChannelId,
normalizeChannelId: mocks.normalizeChannelId,
}));
vi.mock("../config/config.js", () => ({
@@ -45,13 +58,6 @@ vi.mock("../config/config.js", () => ({
}));
describe("pairing cli", () => {
let registerPairingCli: typeof import("./pairing-cli.js").registerPairingCli;
beforeAll(async () => {
vi.resetModules();
({ registerPairingCli } = await import("./pairing-cli.js"));
});
beforeEach(() => {
listChannelPairingRequests.mockClear();
listChannelPairingRequests.mockResolvedValue([]);