test: flatten twitch send mocks

This commit is contained in:
Peter Steinberger 2026-03-17 08:08:14 +00:00
parent 94a48912de
commit 63c5932e84

View File

@ -11,12 +11,16 @@
*/
import { describe, expect, it, vi } from "vitest";
import { getClientManager } from "./client-manager-registry.js";
import { getAccountConfig } from "./config.js";
import { sendMessageTwitchInternal } from "./send.js";
import {
BASE_TWITCH_TEST_ACCOUNT,
installTwitchTestHooks,
makeTwitchTestConfig,
} from "./test-fixtures.js";
import { stripMarkdownForTwitch } from "./utils/markdown.js";
import { isAccountConfigured } from "./utils/twitch.js";
// Mock dependencies
vi.mock("./config.js", () => ({
@ -55,15 +59,16 @@ describe("send", () => {
installTwitchTestHooks();
describe("sendMessageTwitchInternal", () => {
function setupBaseAccount(params?: { configured?: boolean }) {
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(params?.configured ?? true);
}
async function mockSuccessfulSend(params: {
messageId: string;
stripMarkdown?: (text: string) => string;
}) {
const { getAccountConfig } = await import("./config.js");
const { getClientManager } = await import("./client-manager-registry.js");
const { stripMarkdownForTwitch } = await import("./utils/markdown.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
setupBaseAccount();
vi.mocked(getClientManager).mockReturnValue({
sendMessage: vi.fn().mockResolvedValue({
ok: true,
@ -112,8 +117,6 @@ describe("send", () => {
});
it("should return error when account not found", async () => {
const { getAccountConfig } = await import("./config.js");
vi.mocked(getAccountConfig).mockReturnValue(null);
const result = await sendMessageTwitchInternal(
@ -130,11 +133,7 @@ describe("send", () => {
});
it("should return error when account not configured", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(false);
setupBaseAccount({ configured: false });
const result = await sendMessageTwitchInternal(
"#testchannel",
@ -150,9 +149,6 @@ describe("send", () => {
});
it("should return error when no channel specified", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
// Set channel to undefined to trigger the error (bypassing type check)
const accountWithoutChannel = {
...mockAccount,
@ -175,12 +171,7 @@ describe("send", () => {
});
it("should skip sending empty message after markdown stripping", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
const { stripMarkdownForTwitch } = await import("./utils/markdown.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(true);
setupBaseAccount();
vi.mocked(stripMarkdownForTwitch).mockReturnValue("");
const result = await sendMessageTwitchInternal(
@ -197,12 +188,7 @@ describe("send", () => {
});
it("should return error when client manager not found", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
const { getClientManager } = await import("./client-manager-registry.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(true);
setupBaseAccount();
vi.mocked(getClientManager).mockReturnValue(undefined);
const result = await sendMessageTwitchInternal(
@ -219,12 +205,7 @@ describe("send", () => {
});
it("should handle send errors gracefully", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
const { getClientManager } = await import("./client-manager-registry.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(true);
setupBaseAccount();
vi.mocked(getClientManager).mockReturnValue({
sendMessage: vi.fn().mockRejectedValue(new Error("Connection lost")),
} as unknown as ReturnType<typeof getClientManager>);
@ -244,12 +225,7 @@ describe("send", () => {
});
it("should use account channel when channel parameter is empty", async () => {
const { getAccountConfig } = await import("./config.js");
const { isAccountConfigured } = await import("./utils/twitch.js");
const { getClientManager } = await import("./client-manager-registry.js");
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
vi.mocked(isAccountConfigured).mockReturnValue(true);
setupBaseAccount();
const mockSend = vi.fn().mockResolvedValue({
ok: true,
messageId: "twitch-msg-789",