test: share send cfg threading helpers
This commit is contained in:
parent
55ebdce9c3
commit
74e50d3be3
@ -1,4 +1,9 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createSendCfgThreadingRuntime,
|
||||
expectProvidedCfgSkipsRuntimeLoad,
|
||||
expectRuntimeCfgFallback,
|
||||
} from "../../test-utils/send-config.js";
|
||||
import type { IrcClient } from "./client.js";
|
||||
import type { CoreConfig } from "./types.js";
|
||||
|
||||
@ -27,20 +32,7 @@ const hoisted = vi.hoisted(() => {
|
||||
});
|
||||
|
||||
vi.mock("./runtime.js", () => ({
|
||||
getIrcRuntime: () => ({
|
||||
config: {
|
||||
loadConfig: hoisted.loadConfig,
|
||||
},
|
||||
channel: {
|
||||
text: {
|
||||
resolveMarkdownTableMode: hoisted.resolveMarkdownTableMode,
|
||||
convertMarkdownTables: hoisted.convertMarkdownTables,
|
||||
},
|
||||
activity: {
|
||||
record: hoisted.record,
|
||||
},
|
||||
},
|
||||
}),
|
||||
getIrcRuntime: () => createSendCfgThreadingRuntime(hoisted),
|
||||
}));
|
||||
|
||||
vi.mock("./accounts.js", () => ({
|
||||
@ -87,8 +79,9 @@ describe("sendMessageIrc cfg threading", () => {
|
||||
accountId: "work",
|
||||
});
|
||||
|
||||
expect(hoisted.loadConfig).not.toHaveBeenCalled();
|
||||
expect(hoisted.resolveIrcAccount).toHaveBeenCalledWith({
|
||||
expectProvidedCfgSkipsRuntimeLoad({
|
||||
loadConfig: hoisted.loadConfig,
|
||||
resolveAccount: hoisted.resolveIrcAccount,
|
||||
cfg: providedCfg,
|
||||
accountId: "work",
|
||||
});
|
||||
@ -106,8 +99,9 @@ describe("sendMessageIrc cfg threading", () => {
|
||||
|
||||
await sendMessageIrc("#ops", "ping", { client });
|
||||
|
||||
expect(hoisted.loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(hoisted.resolveIrcAccount).toHaveBeenCalledWith({
|
||||
expectRuntimeCfgFallback({
|
||||
loadConfig: hoisted.loadConfig,
|
||||
resolveAccount: hoisted.resolveIrcAccount,
|
||||
cfg: runtimeCfg,
|
||||
accountId: undefined,
|
||||
});
|
||||
|
||||
@ -1,4 +1,8 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
expectProvidedCfgSkipsRuntimeLoad,
|
||||
expectRuntimeCfgFallback,
|
||||
} from "../../../test-utils/send-config.js";
|
||||
import { parseMattermostTarget, sendMessageMattermost } from "./send.js";
|
||||
import { resetMattermostOpaqueTargetCacheForTests } from "./target-resolution.js";
|
||||
|
||||
@ -107,8 +111,9 @@ describe("sendMessageMattermost", () => {
|
||||
accountId: "work",
|
||||
});
|
||||
|
||||
expect(mockState.loadConfig).not.toHaveBeenCalled();
|
||||
expect(mockState.resolveMattermostAccount).toHaveBeenCalledWith({
|
||||
expectProvidedCfgSkipsRuntimeLoad({
|
||||
loadConfig: mockState.loadConfig,
|
||||
resolveAccount: mockState.resolveMattermostAccount,
|
||||
cfg: providedCfg,
|
||||
accountId: "work",
|
||||
});
|
||||
@ -126,8 +131,9 @@ describe("sendMessageMattermost", () => {
|
||||
|
||||
await sendMessageMattermost("channel:town-square", "hello");
|
||||
|
||||
expect(mockState.loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(mockState.resolveMattermostAccount).toHaveBeenCalledWith({
|
||||
expectRuntimeCfgFallback({
|
||||
loadConfig: mockState.loadConfig,
|
||||
resolveAccount: mockState.resolveMattermostAccount,
|
||||
cfg: runtimeCfg,
|
||||
accountId: undefined,
|
||||
});
|
||||
|
||||
@ -1,4 +1,9 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createSendCfgThreadingRuntime,
|
||||
expectProvidedCfgSkipsRuntimeLoad,
|
||||
expectRuntimeCfgFallback,
|
||||
} from "../../test-utils/send-config.js";
|
||||
|
||||
const hoisted = vi.hoisted(() => ({
|
||||
loadConfig: vi.fn(),
|
||||
@ -17,20 +22,7 @@ const hoisted = vi.hoisted(() => ({
|
||||
}));
|
||||
|
||||
vi.mock("./runtime.js", () => ({
|
||||
getNextcloudTalkRuntime: () => ({
|
||||
config: {
|
||||
loadConfig: hoisted.loadConfig,
|
||||
},
|
||||
channel: {
|
||||
text: {
|
||||
resolveMarkdownTableMode: hoisted.resolveMarkdownTableMode,
|
||||
convertMarkdownTables: hoisted.convertMarkdownTables,
|
||||
},
|
||||
activity: {
|
||||
record: hoisted.record,
|
||||
},
|
||||
},
|
||||
}),
|
||||
getNextcloudTalkRuntime: () => createSendCfgThreadingRuntime(hoisted),
|
||||
}));
|
||||
|
||||
vi.mock("./accounts.js", () => ({
|
||||
@ -72,8 +64,9 @@ describe("nextcloud-talk send cfg threading", () => {
|
||||
accountId: "work",
|
||||
});
|
||||
|
||||
expect(hoisted.loadConfig).not.toHaveBeenCalled();
|
||||
expect(hoisted.resolveNextcloudTalkAccount).toHaveBeenCalledWith({
|
||||
expectProvidedCfgSkipsRuntimeLoad({
|
||||
loadConfig: hoisted.loadConfig,
|
||||
resolveAccount: hoisted.resolveNextcloudTalkAccount,
|
||||
cfg,
|
||||
accountId: "work",
|
||||
});
|
||||
@ -95,8 +88,9 @@ describe("nextcloud-talk send cfg threading", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({ ok: true });
|
||||
expect(hoisted.loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(hoisted.resolveNextcloudTalkAccount).toHaveBeenCalledWith({
|
||||
expectRuntimeCfgFallback({
|
||||
loadConfig: hoisted.loadConfig,
|
||||
resolveAccount: hoisted.resolveNextcloudTalkAccount,
|
||||
cfg: runtimeCfg,
|
||||
accountId: "default",
|
||||
});
|
||||
|
||||
65
extensions/test-utils/send-config.ts
Normal file
65
extensions/test-utils/send-config.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { expect } from "vitest";
|
||||
|
||||
type MockFn = (...args: never[]) => unknown;
|
||||
|
||||
type CfgThreadingAssertion<TCfg> = {
|
||||
loadConfig: MockFn;
|
||||
resolveAccount: MockFn;
|
||||
cfg: TCfg;
|
||||
accountId?: string;
|
||||
};
|
||||
|
||||
type SendRuntimeState = {
|
||||
loadConfig: MockFn;
|
||||
resolveMarkdownTableMode: MockFn;
|
||||
convertMarkdownTables: MockFn;
|
||||
record: MockFn;
|
||||
};
|
||||
|
||||
export function expectProvidedCfgSkipsRuntimeLoad<TCfg>({
|
||||
loadConfig,
|
||||
resolveAccount,
|
||||
cfg,
|
||||
accountId,
|
||||
}: CfgThreadingAssertion<TCfg>): void {
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(resolveAccount).toHaveBeenCalledWith({
|
||||
cfg,
|
||||
accountId,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectRuntimeCfgFallback<TCfg>({
|
||||
loadConfig,
|
||||
resolveAccount,
|
||||
cfg,
|
||||
accountId,
|
||||
}: CfgThreadingAssertion<TCfg>): void {
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(resolveAccount).toHaveBeenCalledWith({
|
||||
cfg,
|
||||
accountId,
|
||||
});
|
||||
}
|
||||
|
||||
export function createSendCfgThreadingRuntime({
|
||||
loadConfig,
|
||||
resolveMarkdownTableMode,
|
||||
convertMarkdownTables,
|
||||
record,
|
||||
}: SendRuntimeState) {
|
||||
return {
|
||||
config: {
|
||||
loadConfig,
|
||||
},
|
||||
channel: {
|
||||
text: {
|
||||
resolveMarkdownTableMode,
|
||||
convertMarkdownTables,
|
||||
},
|
||||
activity: {
|
||||
record,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user