chore: chore: Fix types in tests 12/N.

This commit is contained in:
cpojer
2026-02-17 11:17:45 +09:00
parent e02feaff83
commit 262b7a157a
7 changed files with 107 additions and 90 deletions

View File

@@ -1,5 +1,7 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { getReplyFromConfig } from "../../auto-reply/reply.js";
import { HEARTBEAT_TOKEN } from "../../auto-reply/tokens.js";
import type { sendMessageWhatsApp } from "../outbound.js";
const state = vi.hoisted(() => ({
visibility: { showAlerts: true, showOk: true, useIndicator: false },
@@ -87,8 +89,10 @@ vi.mock("../session.js", () => ({
}));
describe("runWebHeartbeatOnce", () => {
let sender: ReturnType<typeof vi.fn>;
let replyResolver: ReturnType<typeof vi.fn>;
let senderMock: ReturnType<typeof vi.fn>;
let sender: typeof sendMessageWhatsApp;
let replyResolverMock: ReturnType<typeof vi.fn>;
let replyResolver: typeof getReplyFromConfig;
const getModules = async () => await import("./heartbeat-runner.js");
@@ -105,8 +109,10 @@ describe("runWebHeartbeatOnce", () => {
};
state.events = [];
sender = vi.fn(async () => ({ messageId: "m1" }));
replyResolver = vi.fn(async () => undefined);
senderMock = vi.fn(async () => ({ messageId: "m1" }));
sender = senderMock as unknown as typeof sendMessageWhatsApp;
replyResolverMock = vi.fn(async () => undefined);
replyResolver = replyResolverMock as unknown as typeof getReplyFromConfig;
});
it("supports manual override body dry-run without sending", async () => {
@@ -119,7 +125,7 @@ describe("runWebHeartbeatOnce", () => {
overrideBody: "hello",
dryRun: true,
});
expect(sender).not.toHaveBeenCalled();
expect(senderMock).not.toHaveBeenCalled();
expect(state.events).toHaveLength(0);
});
@@ -131,7 +137,7 @@ describe("runWebHeartbeatOnce", () => {
sender,
replyResolver,
});
expect(sender).toHaveBeenCalledWith("+123", HEARTBEAT_TOKEN, { verbose: false });
expect(senderMock).toHaveBeenCalledWith("+123", HEARTBEAT_TOKEN, { verbose: false });
expect(state.events).toEqual(
expect.arrayContaining([expect.objectContaining({ status: "ok-empty", silent: false })]),
);
@@ -147,13 +153,13 @@ describe("runWebHeartbeatOnce", () => {
dryRun: true,
});
expect(replyResolver).toHaveBeenCalledTimes(1);
const ctx = replyResolver.mock.calls[0]?.[0];
const ctx = replyResolverMock.mock.calls[0]?.[0];
expect(ctx?.Body).toContain("Ops check");
expect(ctx?.Body).toContain("Current time: 2026-02-15T00:00:00Z (mock)");
});
it("treats heartbeat token-only replies as ok-token and preserves session updatedAt", async () => {
replyResolver.mockResolvedValue({ text: HEARTBEAT_TOKEN });
replyResolverMock.mockResolvedValue({ text: HEARTBEAT_TOKEN });
const { runWebHeartbeatOnce } = await getModules();
await runWebHeartbeatOnce({
cfg: { agents: { defaults: {} }, session: {} } as never,
@@ -162,7 +168,7 @@ describe("runWebHeartbeatOnce", () => {
replyResolver,
});
expect(state.store.k?.updatedAt).toBe(123);
expect(sender).toHaveBeenCalledWith("+123", HEARTBEAT_TOKEN, { verbose: false });
expect(senderMock).toHaveBeenCalledWith("+123", HEARTBEAT_TOKEN, { verbose: false });
expect(state.events).toEqual(
expect.arrayContaining([expect.objectContaining({ status: "ok-token", silent: false })]),
);
@@ -170,7 +176,7 @@ describe("runWebHeartbeatOnce", () => {
it("skips sending alerts when showAlerts is disabled but still emits a skipped event", async () => {
state.visibility = { showAlerts: false, showOk: true, useIndicator: true };
replyResolver.mockResolvedValue({ text: "ALERT" });
replyResolverMock.mockResolvedValue({ text: "ALERT" });
const { runWebHeartbeatOnce } = await getModules();
await runWebHeartbeatOnce({
cfg: { agents: { defaults: {} }, session: {} } as never,
@@ -178,7 +184,7 @@ describe("runWebHeartbeatOnce", () => {
sender,
replyResolver,
});
expect(sender).not.toHaveBeenCalled();
expect(senderMock).not.toHaveBeenCalled();
expect(state.events).toEqual(
expect.arrayContaining([
expect.objectContaining({ status: "skipped", reason: "alerts-disabled", preview: "ALERT" }),
@@ -187,8 +193,8 @@ describe("runWebHeartbeatOnce", () => {
});
it("emits failed events when sending throws and rethrows the error", async () => {
replyResolver.mockResolvedValue({ text: "ALERT" });
sender.mockRejectedValueOnce(new Error("nope"));
replyResolverMock.mockResolvedValue({ text: "ALERT" });
senderMock.mockRejectedValueOnce(new Error("nope"));
const { runWebHeartbeatOnce } = await getModules();
await expect(
runWebHeartbeatOnce({