Skip to content

Commit 6b0d6e2

Browse files
committed
chore: We have a sleep at home. The sleep at home:
1 parent dfef943 commit 6b0d6e2

18 files changed

Lines changed: 69 additions & 49 deletions

src/agents/bash-tools.exec.background-abort.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, expect, test } from "vitest";
2+
import { sleep } from "../utils.ts";
23
import {
34
getFinishedSession,
45
getSession,
@@ -7,8 +8,6 @@ import {
78
import { createExecTool } from "./bash-tools.exec";
89
import { killProcessTree } from "./shell-utils";
910

10-
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
11-
1211
afterEach(() => {
1312
resetProcessRegistryForTests();
1413
});

src/agents/bash-tools.process.send-keys.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { afterEach, expect, test } from "vitest";
2+
import { sleep } from "../utils";
23
import { resetProcessRegistryForTests } from "./bash-process-registry";
34
import { createExecTool } from "./bash-tools.exec";
45
import { createProcessTool } from "./bash-tools.process";
56

6-
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
7-
87
afterEach(() => {
98
resetProcessRegistryForTests();
109
});
@@ -31,7 +30,7 @@ test("process send-keys encodes Enter for pty sessions", async () => {
3130

3231
const deadline = Date.now() + (process.platform === "win32" ? 4000 : 2000);
3332
while (Date.now() < deadline) {
34-
await wait(50);
33+
await sleep(50);
3534
const poll = await processTool.execute("toolcall", { action: "poll", sessionId });
3635
const details = poll.details as { status?: string; aggregated?: string };
3736
if (details.status !== "running") {
@@ -65,7 +64,7 @@ test("process submit sends Enter for pty sessions", async () => {
6564

6665
const deadline = Date.now() + (process.platform === "win32" ? 4000 : 2000);
6766
while (Date.now() < deadline) {
68-
await wait(50);
67+
await sleep(50);
6968
const poll = await processTool.execute("toolcall", { action: "poll", sessionId });
7069
const details = poll.details as { status?: string; aggregated?: string };
7170
if (details.status !== "running") {

src/agents/bash-tools.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { afterEach, beforeEach, describe, expect, it } from "vitest";
44
import { peekSystemEvents, resetSystemEventsForTest } from "../infra/system-events.js";
5+
import { sleep } from "../utils.js";
56
import { getFinishedSession, resetProcessRegistryForTests } from "./bash-process-registry.js";
67
import { createExecTool, createProcessTool, execTool, processTool } from "./bash-tools.js";
78
import { buildDockerExecArgs } from "./bash-tools.shared.js";
@@ -45,8 +46,6 @@ const normalizeText = (value?: string) =>
4546
.join("\n")
4647
.trim();
4748

48-
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
49-
5049
async function waitForCompletion(sessionId: string) {
5150
let status = "running";
5251
const deadline = Date.now() + (process.platform === "win32" ? 8000 : 2000);

src/agents/claude-cli-runner.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { sleep } from "../utils.js";
23
import { runClaudeCliAgent } from "./claude-cli-runner.js";
34

45
const runCommandWithTimeoutMock = vi.fn();
@@ -22,7 +23,7 @@ async function waitForCalls(mockFn: { mock: { calls: unknown[][] } }, count: num
2223
if (mockFn.mock.calls.length >= count) {
2324
return;
2425
}
25-
await new Promise((resolve) => setTimeout(resolve, 0));
26+
await sleep(0);
2627
}
2728
throw new Error(`Expected ${count} calls, got ${mockFn.mock.calls.length}`);
2829
}

src/agents/openclaw-tools.sessions.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
2121
});
2222

2323
import "./test-helpers/fast-core-tools.js";
24+
import { sleep } from "../utils.js";
2425
import { createOpenClawTools } from "./openclaw-tools.js";
2526

2627
const waitForCalls = async (getCount: () => number, count: number, timeoutMs = 2000) => {
@@ -29,7 +30,7 @@ const waitForCalls = async (getCount: () => number, count: number, timeoutMs = 2
2930
if (Date.now() - start > timeoutMs) {
3031
throw new Error(`timed out waiting for ${count} calls`);
3132
}
32-
await new Promise((resolve) => setTimeout(resolve, 0));
33+
await sleep(0);
3334
}
3435
};
3536

@@ -185,7 +186,10 @@ describe("sessions tools", () => {
185186
const sessionId = "sess-group";
186187
const targetKey = "agent:main:discord:channel:1457165743010611293";
187188
callGatewayMock.mockImplementation(async (opts: unknown) => {
188-
const request = opts as { method?: string; params?: Record<string, unknown> };
189+
const request = opts as {
190+
method?: string;
191+
params?: Record<string, unknown>;
192+
};
189193
if (request.method === "sessions.resolve") {
190194
return {
191195
key: targetKey,
@@ -388,7 +392,10 @@ describe("sessions tools", () => {
388392
const sessionId = "sess-send";
389393
const targetKey = "agent:main:discord:channel:123";
390394
callGatewayMock.mockImplementation(async (opts: unknown) => {
391-
const request = opts as { method?: string; params?: Record<string, unknown> };
395+
const request = opts as {
396+
method?: string;
397+
params?: Record<string, unknown>;
398+
};
392399
if (request.method === "sessions.resolve") {
393400
return { key: targetKey };
394401
}
@@ -514,8 +521,8 @@ describe("sessions tools", () => {
514521
status: "ok",
515522
reply: "initial",
516523
});
517-
await new Promise((resolve) => setTimeout(resolve, 0));
518-
await new Promise((resolve) => setTimeout(resolve, 0));
524+
await sleep(0);
525+
await sleep(0);
519526

520527
const agentCalls = calls.filter((call) => call.method === "agent");
521528
expect(agentCalls).toHaveLength(4);

src/agents/openclaw-tools.subagents.sessions-spawn-announces-agent-wait-lifecycle-events.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
2222
});
2323

2424
import "./test-helpers/fast-core-tools.js";
25+
import { sleep } from "../utils.js";
2526
import { createOpenClawTools } from "./openclaw-tools.js";
2627
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
2728

@@ -107,9 +108,9 @@ describe("openclaw-tools: subagents", () => {
107108
runId: "run-1",
108109
});
109110

110-
await new Promise((resolve) => setTimeout(resolve, 0));
111-
await new Promise((resolve) => setTimeout(resolve, 0));
112-
await new Promise((resolve) => setTimeout(resolve, 0));
111+
await sleep(0);
112+
await sleep(0);
113+
await sleep(0);
113114

114115
const childWait = waitCalls.find((call) => call.runId === childRunId);
115116
expect(childWait?.timeoutMs).toBe(1000);

src/agents/openclaw-tools.subagents.sessions-spawn-normalizes-allowlisted-agent-ids.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
2323

2424
import { emitAgentEvent } from "../infra/agent-events.js";
2525
import "./test-helpers/fast-core-tools.js";
26+
import { sleep } from "../utils.js";
2627
import { createOpenClawTools } from "./openclaw-tools.js";
2728
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
2829

@@ -165,7 +166,12 @@ describe("openclaw-tools: subagents", () => {
165166
if (request.method === "agent.wait") {
166167
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
167168
waitCalls.push(params ?? {});
168-
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
169+
return {
170+
runId: params?.runId ?? "run-1",
171+
status: "ok",
172+
startedAt: 1000,
173+
endedAt: 2000,
174+
};
169175
}
170176
if (request.method === "sessions.delete") {
171177
const params = request.params as { key?: string } | undefined;
@@ -206,9 +212,9 @@ describe("openclaw-tools: subagents", () => {
206212
},
207213
});
208214

209-
await new Promise((resolve) => setTimeout(resolve, 0));
210-
await new Promise((resolve) => setTimeout(resolve, 0));
211-
await new Promise((resolve) => setTimeout(resolve, 0));
215+
await sleep(0);
216+
await sleep(0);
217+
await sleep(0);
212218

213219
const childWait = waitCalls.find((call) => call.runId === childRunId);
214220
expect(childWait?.timeoutMs).toBe(1000);
@@ -272,7 +278,12 @@ describe("openclaw-tools: subagents", () => {
272278
}
273279
if (request.method === "agent.wait") {
274280
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
275-
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
281+
return {
282+
runId: params?.runId ?? "run-1",
283+
status: "ok",
284+
startedAt: 1000,
285+
endedAt: 2000,
286+
};
276287
}
277288
if (request.method === "sessions.delete" || request.method === "sessions.patch") {
278289
return { ok: true };
@@ -312,9 +323,9 @@ describe("openclaw-tools: subagents", () => {
312323
},
313324
});
314325

315-
await new Promise((resolve) => setTimeout(resolve, 0));
316-
await new Promise((resolve) => setTimeout(resolve, 0));
317-
await new Promise((resolve) => setTimeout(resolve, 0));
326+
await sleep(0);
327+
await sleep(0);
328+
await sleep(0);
318329

319330
const agentCalls = calls.filter((call) => call.method === "agent");
320331
expect(agentCalls).toHaveLength(2);

src/agents/openclaw-tools.subagents.sessions-spawn-resolves-main-announce-target-from.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { sleep } from "../utils.ts";
23

34
const callGatewayMock = vi.fn();
45
vi.mock("../gateway/call.js", () => ({
@@ -82,7 +83,12 @@ describe("openclaw-tools: subagents", () => {
8283
if (request.method === "agent.wait") {
8384
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
8485
waitCalls.push(params ?? {});
85-
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
86+
return {
87+
runId: params?.runId ?? "run-1",
88+
status: "ok",
89+
startedAt: 1000,
90+
endedAt: 2000,
91+
};
8692
}
8793
if (request.method === "sessions.patch") {
8894
const params = request.params as { key?: string; label?: string } | undefined;
@@ -126,9 +132,9 @@ describe("openclaw-tools: subagents", () => {
126132
},
127133
});
128134

129-
await new Promise((resolve) => setTimeout(resolve, 0));
130-
await new Promise((resolve) => setTimeout(resolve, 0));
131-
await new Promise((resolve) => setTimeout(resolve, 0));
135+
await sleep(0);
136+
await sleep(0);
137+
await sleep(0);
132138

133139
const childWait = waitCalls.find((call) => call.runId === childRunId);
134140
expect(childWait?.timeoutMs).toBe(1000);

src/auto-reply/reply/reply-dispatcher.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { HumanDelayConfig } from "../../config/types.js";
22
import type { GetReplyOptions, ReplyPayload } from "../types.js";
33
import type { ResponsePrefixContext } from "./response-prefix-template.js";
44
import type { TypingController } from "./typing.js";
5+
import { sleep } from "../../utils.js";
56
import { normalizeReplyPayload, type NormalizeReplySkipReason } from "./normalize-reply.js";
67

78
export type ReplyDispatchKind = "tool" | "block" | "final";
@@ -37,9 +38,6 @@ function getHumanDelay(config: HumanDelayConfig | undefined): number {
3738
return Math.floor(Math.random() * (max - min + 1)) + min;
3839
}
3940

40-
/** Sleep for a given number of milliseconds. */
41-
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
42-
4341
export type ReplyDispatcherOptions = {
4442
deliver: ReplyDispatchDeliverer;
4543
responsePrefix?: string;

src/channels/ack-reactions.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2+
import { sleep } from "../utils.ts";
23
import {
34
removeAckReactionAfterReply,
45
shouldAckReaction,
@@ -237,7 +238,7 @@ describe("removeAckReactionAfterReply", () => {
237238
remove,
238239
onError,
239240
});
240-
await new Promise((resolve) => setTimeout(resolve, 0));
241+
await sleep(0);
241242
expect(remove).toHaveBeenCalledTimes(1);
242243
expect(onError).not.toHaveBeenCalled();
243244
});
@@ -250,7 +251,7 @@ describe("removeAckReactionAfterReply", () => {
250251
ackReactionValue: "👀",
251252
remove,
252253
});
253-
await new Promise((resolve) => setTimeout(resolve, 0));
254+
await sleep(0);
254255
expect(remove).not.toHaveBeenCalled();
255256
});
256257

@@ -262,7 +263,7 @@ describe("removeAckReactionAfterReply", () => {
262263
ackReactionValue: "👀",
263264
remove,
264265
});
265-
await new Promise((resolve) => setTimeout(resolve, 0));
266+
await sleep(0);
266267
expect(remove).not.toHaveBeenCalled();
267268
});
268269
});

0 commit comments

Comments
 (0)