refactor(channels): dedupe slack telegram and web monitor tests

This commit is contained in:
Peter Steinberger
2026-03-02 21:31:26 +00:00
parent 34daed1d1e
commit 6a425d189e
6 changed files with 122 additions and 217 deletions

View File

@@ -83,10 +83,15 @@ const makeRunnerStub = (overrides: Partial<RunnerStub> = {}): RunnerStub => ({
isRunning: overrides.isRunning ?? (() => false),
});
async function monitorWithAutoAbort(
opts: Omit<Parameters<typeof monitorTelegramProvider>[0], "abortSignal"> = {},
) {
const abort = new AbortController();
function makeRecoverableFetchError() {
return Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect timeout"), {
code: "UND_ERR_CONNECT_TIMEOUT",
}),
});
}
function mockRunOnceAndAbort(abort: AbortController) {
runSpy.mockImplementationOnce(() =>
makeRunnerStub({
task: async () => {
@@ -94,6 +99,13 @@ async function monitorWithAutoAbort(
},
}),
);
}
async function monitorWithAutoAbort(
opts: Omit<Parameters<typeof monitorTelegramProvider>[0], "abortSignal"> = {},
) {
const abort = new AbortController();
mockRunOnceAndAbort(abort);
await monitorTelegramProvider({
token: "tok",
...opts,
@@ -254,11 +266,7 @@ describe("monitorTelegramProvider (grammY)", () => {
it("retries on recoverable undici fetch errors", async () => {
const abort = new AbortController();
const networkError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect timeout"), {
code: "UND_ERR_CONNECT_TIMEOUT",
}),
});
const networkError = makeRecoverableFetchError();
runSpy
.mockImplementationOnce(() =>
makeRunnerStub({
@@ -305,20 +313,10 @@ describe("monitorTelegramProvider (grammY)", () => {
it("retries recoverable deleteWebhook failures before polling", async () => {
const abort = new AbortController();
const cleanupError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect timeout"), {
code: "UND_ERR_CONNECT_TIMEOUT",
}),
});
const cleanupError = makeRecoverableFetchError();
api.deleteWebhook.mockReset();
api.deleteWebhook.mockRejectedValueOnce(cleanupError).mockResolvedValueOnce(true);
runSpy.mockImplementationOnce(() =>
makeRunnerStub({
task: async () => {
abort.abort();
},
}),
);
mockRunOnceAndAbort(abort);
await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal });
@@ -330,20 +328,9 @@ describe("monitorTelegramProvider (grammY)", () => {
it("retries setup-time recoverable errors before starting polling", async () => {
const abort = new AbortController();
const setupError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect timeout"), {
code: "UND_ERR_CONNECT_TIMEOUT",
}),
});
const setupError = makeRecoverableFetchError();
createTelegramBotErrors.push(setupError);
runSpy.mockImplementationOnce(() =>
makeRunnerStub({
task: async () => {
abort.abort();
},
}),
);
mockRunOnceAndAbort(abort);
await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal });
@@ -354,11 +341,7 @@ describe("monitorTelegramProvider (grammY)", () => {
it("awaits runner.stop before retrying after recoverable polling error", async () => {
const abort = new AbortController();
const recoverableError = Object.assign(new TypeError("fetch failed"), {
cause: Object.assign(new Error("connect timeout"), {
code: "UND_ERR_CONNECT_TIMEOUT",
}),
});
const recoverableError = makeRecoverableFetchError();
let firstStopped = false;
const firstStop = vi.fn(async () => {
await Promise.resolve();