perf(test): fake queue timers and merge telegram reply-mode checks

This commit is contained in:
Peter Steinberger
2026-02-18 16:01:14 +00:00
parent 797a47c3ce
commit c7bc94436b
2 changed files with 67 additions and 78 deletions

View File

@@ -57,16 +57,23 @@ describe("command queue", () => {
return id;
};
const results = await Promise.all([
enqueueCommand(makeTask(1)),
enqueueCommand(makeTask(2)),
enqueueCommand(makeTask(3)),
]);
vi.useFakeTimers();
try {
const resultsPromise = Promise.all([
enqueueCommand(makeTask(1)),
enqueueCommand(makeTask(2)),
enqueueCommand(makeTask(3)),
]);
await vi.advanceTimersByTimeAsync(20);
const results = await resultsPromise;
expect(results).toEqual([1, 2, 3]);
expect(calls).toEqual([1, 2, 3]);
expect(maxActive).toBe(1);
expect(getQueueSize()).toBe(0);
expect(results).toEqual([1, 2, 3]);
expect(calls).toEqual([1, 2, 3]);
expect(maxActive).toBe(1);
expect(getQueueSize()).toBe(0);
} finally {
vi.useRealTimers();
}
});
it("logs enqueue depth after push", async () => {
@@ -82,24 +89,30 @@ describe("command queue", () => {
let waited: number | null = null;
let queuedAhead: number | null = null;
// First task holds the queue long enough to trigger wait notice.
const first = enqueueCommand(async () => {
await new Promise((resolve) => setTimeout(resolve, 8));
});
vi.useFakeTimers();
try {
// First task holds the queue long enough to trigger wait notice.
const first = enqueueCommand(async () => {
await new Promise((resolve) => setTimeout(resolve, 8));
});
const second = enqueueCommand(async () => {}, {
warnAfterMs: 5,
onWait: (ms, ahead) => {
waited = ms;
queuedAhead = ahead;
},
});
const second = enqueueCommand(async () => {}, {
warnAfterMs: 5,
onWait: (ms, ahead) => {
waited = ms;
queuedAhead = ahead;
},
});
await Promise.all([first, second]);
await vi.advanceTimersByTimeAsync(30);
await Promise.all([first, second]);
expect(waited).not.toBeNull();
expect(waited as unknown as number).toBeGreaterThanOrEqual(5);
expect(queuedAhead).toBe(0);
expect(waited).not.toBeNull();
expect(waited as unknown as number).toBeGreaterThanOrEqual(5);
expect(queuedAhead).toBe(0);
} finally {
vi.useRealTimers();
}
});
it("getActiveTaskCount returns count of currently executing tasks", async () => {

View File

@@ -1508,35 +1508,6 @@ describe("createTelegramBot", () => {
).toBeUndefined();
}
});
it("honors replyToMode=first for threaded replies across all chunks of the first reply", async () => {
onSpy.mockReset();
sendMessageSpy.mockReset();
replySpy.mockReset();
replySpy.mockResolvedValue({
text: "a".repeat(4500),
replyToId: "101",
});
createTelegramBot({ token: "tok", replyToMode: "first" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
chat: { id: 5, type: "private" },
text: "hi",
date: 1736380800,
message_id: 101,
},
me: { username: "openclaw_bot" },
getFile: async () => ({ download: async () => new Uint8Array() }),
});
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
for (const call of sendMessageSpy.mock.calls) {
expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe(
101,
);
}
});
it("prefixes final replies with responsePrefix", async () => {
onSpy.mockReset();
sendMessageSpy.mockReset();
@@ -1564,33 +1535,38 @@ describe("createTelegramBot", () => {
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
expect(sendMessageSpy.mock.calls[0][1]).toBe("PFX final reply");
});
it("honors replyToMode=all for threaded replies", async () => {
onSpy.mockReset();
sendMessageSpy.mockReset();
replySpy.mockReset();
replySpy.mockResolvedValue({
text: "a".repeat(4500),
replyToId: "101",
});
it("honors threaded replies for replyToMode=first/all", async () => {
for (const [mode, messageId] of [
["first", 101],
["all", 102],
] as const) {
onSpy.mockReset();
sendMessageSpy.mockReset();
replySpy.mockReset();
replySpy.mockResolvedValue({
text: "a".repeat(4500),
replyToId: String(messageId),
});
createTelegramBot({ token: "tok", replyToMode: "all" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
chat: { id: 5, type: "private" },
text: "hi",
date: 1736380800,
message_id: 101,
},
me: { username: "openclaw_bot" },
getFile: async () => ({ download: async () => new Uint8Array() }),
});
createTelegramBot({ token: "tok", replyToMode: mode });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
chat: { id: 5, type: "private" },
text: "hi",
date: 1736380800,
message_id: messageId,
},
me: { username: "openclaw_bot" },
getFile: async () => ({ download: async () => new Uint8Array() }),
});
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
for (const call of sendMessageSpy.mock.calls) {
expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe(
101,
);
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
for (const call of sendMessageSpy.mock.calls) {
expect((call[2] as { reply_to_message_id?: number } | undefined)?.reply_to_message_id).toBe(
messageId,
);
}
}
});
it("blocks group messages when telegram.groups is set without a wildcard", async () => {