chore: We have a sleep at home. The sleep at home:

This commit is contained in:
cpojer
2026-02-02 21:34:47 +09:00
parent dfef943f0a
commit 6b0d6e2540
18 changed files with 69 additions and 49 deletions

View File

@@ -1,4 +1,5 @@
import { afterEach, expect, test } from "vitest";
import { sleep } from "../utils.ts";
import {
getFinishedSession,
getSession,
@@ -7,8 +8,6 @@ import {
import { createExecTool } from "./bash-tools.exec";
import { killProcessTree } from "./shell-utils";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
afterEach(() => {
resetProcessRegistryForTests();
});

View File

@@ -1,10 +1,9 @@
import { afterEach, expect, test } from "vitest";
import { sleep } from "../utils";
import { resetProcessRegistryForTests } from "./bash-process-registry";
import { createExecTool } from "./bash-tools.exec";
import { createProcessTool } from "./bash-tools.process";
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
afterEach(() => {
resetProcessRegistryForTests();
});
@@ -31,7 +30,7 @@ test("process send-keys encodes Enter for pty sessions", async () => {
const deadline = Date.now() + (process.platform === "win32" ? 4000 : 2000);
while (Date.now() < deadline) {
await wait(50);
await sleep(50);
const poll = await processTool.execute("toolcall", { action: "poll", sessionId });
const details = poll.details as { status?: string; aggregated?: string };
if (details.status !== "running") {
@@ -65,7 +64,7 @@ test("process submit sends Enter for pty sessions", async () => {
const deadline = Date.now() + (process.platform === "win32" ? 4000 : 2000);
while (Date.now() < deadline) {
await wait(50);
await sleep(50);
const poll = await processTool.execute("toolcall", { action: "poll", sessionId });
const details = poll.details as { status?: string; aggregated?: string };
if (details.status !== "running") {

View File

@@ -2,6 +2,7 @@ import fs from "node:fs";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { peekSystemEvents, resetSystemEventsForTest } from "../infra/system-events.js";
import { sleep } from "../utils.js";
import { getFinishedSession, resetProcessRegistryForTests } from "./bash-process-registry.js";
import { createExecTool, createProcessTool, execTool, processTool } from "./bash-tools.js";
import { buildDockerExecArgs } from "./bash-tools.shared.js";
@@ -45,8 +46,6 @@ const normalizeText = (value?: string) =>
.join("\n")
.trim();
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
async function waitForCompletion(sessionId: string) {
let status = "running";
const deadline = Date.now() + (process.platform === "win32" ? 8000 : 2000);

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { sleep } from "../utils.js";
import { runClaudeCliAgent } from "./claude-cli-runner.js";
const runCommandWithTimeoutMock = vi.fn();
@@ -22,7 +23,7 @@ async function waitForCalls(mockFn: { mock: { calls: unknown[][] } }, count: num
if (mockFn.mock.calls.length >= count) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
}
throw new Error(`Expected ${count} calls, got ${mockFn.mock.calls.length}`);
}

View File

@@ -21,6 +21,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
});
import "./test-helpers/fast-core-tools.js";
import { sleep } from "../utils.js";
import { createOpenClawTools } from "./openclaw-tools.js";
const waitForCalls = async (getCount: () => number, count: number, timeoutMs = 2000) => {
@@ -29,7 +30,7 @@ const waitForCalls = async (getCount: () => number, count: number, timeoutMs = 2
if (Date.now() - start > timeoutMs) {
throw new Error(`timed out waiting for ${count} calls`);
}
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
}
};
@@ -185,7 +186,10 @@ describe("sessions tools", () => {
const sessionId = "sess-group";
const targetKey = "agent:main:discord:channel:1457165743010611293";
callGatewayMock.mockImplementation(async (opts: unknown) => {
const request = opts as { method?: string; params?: Record<string, unknown> };
const request = opts as {
method?: string;
params?: Record<string, unknown>;
};
if (request.method === "sessions.resolve") {
return {
key: targetKey,
@@ -388,7 +392,10 @@ describe("sessions tools", () => {
const sessionId = "sess-send";
const targetKey = "agent:main:discord:channel:123";
callGatewayMock.mockImplementation(async (opts: unknown) => {
const request = opts as { method?: string; params?: Record<string, unknown> };
const request = opts as {
method?: string;
params?: Record<string, unknown>;
};
if (request.method === "sessions.resolve") {
return { key: targetKey };
}
@@ -514,8 +521,8 @@ describe("sessions tools", () => {
status: "ok",
reply: "initial",
});
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
await sleep(0);
const agentCalls = calls.filter((call) => call.method === "agent");
expect(agentCalls).toHaveLength(4);

View File

@@ -22,6 +22,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
});
import "./test-helpers/fast-core-tools.js";
import { sleep } from "../utils.js";
import { createOpenClawTools } from "./openclaw-tools.js";
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
@@ -107,9 +108,9 @@ describe("openclaw-tools: subagents", () => {
runId: "run-1",
});
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
await sleep(0);
await sleep(0);
const childWait = waitCalls.find((call) => call.runId === childRunId);
expect(childWait?.timeoutMs).toBe(1000);

View File

@@ -23,6 +23,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
import { emitAgentEvent } from "../infra/agent-events.js";
import "./test-helpers/fast-core-tools.js";
import { sleep } from "../utils.js";
import { createOpenClawTools } from "./openclaw-tools.js";
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
@@ -165,7 +166,12 @@ describe("openclaw-tools: subagents", () => {
if (request.method === "agent.wait") {
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
waitCalls.push(params ?? {});
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
return {
runId: params?.runId ?? "run-1",
status: "ok",
startedAt: 1000,
endedAt: 2000,
};
}
if (request.method === "sessions.delete") {
const params = request.params as { key?: string } | undefined;
@@ -206,9 +212,9 @@ describe("openclaw-tools: subagents", () => {
},
});
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
await sleep(0);
await sleep(0);
const childWait = waitCalls.find((call) => call.runId === childRunId);
expect(childWait?.timeoutMs).toBe(1000);
@@ -272,7 +278,12 @@ describe("openclaw-tools: subagents", () => {
}
if (request.method === "agent.wait") {
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
return {
runId: params?.runId ?? "run-1",
status: "ok",
startedAt: 1000,
endedAt: 2000,
};
}
if (request.method === "sessions.delete" || request.method === "sessions.patch") {
return { ok: true };
@@ -312,9 +323,9 @@ describe("openclaw-tools: subagents", () => {
},
});
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
await sleep(0);
await sleep(0);
const agentCalls = calls.filter((call) => call.method === "agent");
expect(agentCalls).toHaveLength(2);

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { sleep } from "../utils.ts";
const callGatewayMock = vi.fn();
vi.mock("../gateway/call.js", () => ({
@@ -82,7 +83,12 @@ describe("openclaw-tools: subagents", () => {
if (request.method === "agent.wait") {
const params = request.params as { runId?: string; timeoutMs?: number } | undefined;
waitCalls.push(params ?? {});
return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 1000, endedAt: 2000 };
return {
runId: params?.runId ?? "run-1",
status: "ok",
startedAt: 1000,
endedAt: 2000,
};
}
if (request.method === "sessions.patch") {
const params = request.params as { key?: string; label?: string } | undefined;
@@ -126,9 +132,9 @@ describe("openclaw-tools: subagents", () => {
},
});
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
await sleep(0);
await sleep(0);
await sleep(0);
const childWait = waitCalls.find((call) => call.runId === childRunId);
expect(childWait?.timeoutMs).toBe(1000);