perf(test): cut slow monitor/subagent test overhead

This commit is contained in:
Peter Steinberger
2026-02-18 03:50:22 +00:00
parent 99db4d13e5
commit 797ea7ed27
6 changed files with 50 additions and 84 deletions

View File

@@ -124,43 +124,6 @@ function createCategoryGuildClient() {
}
describe("discord tool result dispatch", () => {
it("sends status replies with responsePrefix", async () => {
const cfg = {
...BASE_CFG,
messages: { responsePrefix: "PFX" },
channels: { discord: { dmPolicy: "open", allowFrom: ["*"], dm: { enabled: true } } },
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const runtimeError = vi.fn();
const handler = await createDmHandler({ cfg, runtimeError });
const client = createDmClient();
await handler(
{
message: {
id: "m1",
content: "/status",
channelId: "c1",
timestamp: new Date().toISOString(),
type: MessageType.Default,
attachments: [],
embeds: [],
mentionedEveryone: false,
mentionedUsers: [],
mentionedRoles: [],
author: { id: "u1", bot: false, username: "Ada" },
},
author: { id: "u1", bot: false, username: "Ada" },
guild_id: null,
},
client,
);
expect(runtimeError).not.toHaveBeenCalled();
expect(sendMock).toHaveBeenCalledTimes(1);
expect(sendMock.mock.calls[0]?.[1]).toMatch(/^PFX /);
}, 30_000);
it("caches channel info lookups between messages", async () => {
const cfg = {
...BASE_CFG,

View File

@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { resolveDiscordRestFetch } from "./rest-fetch.js";
const { undiciFetchMock, proxyAgentSpy } = vi.hoisted(() => ({
undiciFetchMock: vi.fn(),
@@ -31,9 +32,7 @@ describe("resolveDiscordRestFetch", () => {
} as const;
undiciFetchMock.mockReset().mockResolvedValue(new Response("ok", { status: 200 }));
proxyAgentSpy.mockReset();
const { __testing } = await import("./provider.js");
const fetcher = __testing.resolveDiscordRestFetch("http://proxy.test:8080", runtime);
const fetcher = resolveDiscordRestFetch("http://proxy.test:8080", runtime);
await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
@@ -54,9 +53,7 @@ describe("resolveDiscordRestFetch", () => {
error: vi.fn(),
exit: vi.fn(),
} as const;
const { __testing } = await import("./provider.js");
const fetcher = __testing.resolveDiscordRestFetch("bad-proxy", runtime);
const fetcher = resolveDiscordRestFetch("bad-proxy", runtime);
expect(fetcher).toBe(fetch);
expect(runtime.error).toHaveBeenCalled();

View File

@@ -7,7 +7,6 @@ import {
} from "@buape/carbon";
import type { GatewayPlugin } from "@buape/carbon/gateway";
import { Routes } from "discord-api-types/v10";
import { ProxyAgent, fetch as undiciFetch } from "undici";
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
import { listNativeCommandSpecsForConfig } from "../../auto-reply/commands-registry.js";
import type { HistoryEntry } from "../../auto-reply/reply/history.js";
@@ -29,7 +28,6 @@ import type { OpenClawConfig, ReplyToMode } from "../../config/config.js";
import { loadConfig } from "../../config/config.js";
import { danger, logVerbose, shouldLogVerbose, warn } from "../../globals.js";
import { formatErrorMessage } from "../../infra/errors.js";
import { wrapFetchWithAbortSignal } from "../../infra/fetch.js";
import { createDiscordRetryRunner } from "../../infra/retry-policy.js";
import { createSubsystemLogger } from "../../logging/subsystem.js";
import { createNonExitingRuntime, type RuntimeEnv } from "../../runtime.js";
@@ -67,6 +65,7 @@ import {
createDiscordNativeCommand,
} from "./native-command.js";
import { resolveDiscordPresenceUpdate } from "./presence.js";
import { resolveDiscordRestFetch } from "./rest-fetch.js";
export type MonitorDiscordOpts = {
token?: string;
@@ -118,26 +117,6 @@ function dedupeSkillCommandsForDiscord(
return deduped;
}
function resolveDiscordRestFetch(proxyUrl: string | undefined, runtime: RuntimeEnv): typeof fetch {
const proxy = proxyUrl?.trim();
if (!proxy) {
return fetch;
}
try {
const agent = new ProxyAgent(proxy);
const fetcher = ((input: RequestInfo | URL, init?: RequestInit) =>
undiciFetch(input as string | URL, {
...(init as Record<string, unknown>),
dispatcher: agent,
}) as unknown as Promise<Response>) as typeof fetch;
runtime.log?.("discord: rest proxy enabled");
return wrapFetchWithAbortSignal(fetcher);
} catch (err) {
runtime.error?.(danger(`discord: invalid rest proxy: ${String(err)}`));
return fetch;
}
}
async function deployDiscordCommands(params: {
client: Client;
runtime: RuntimeEnv;

View File

@@ -0,0 +1,27 @@
import { ProxyAgent, fetch as undiciFetch } from "undici";
import { danger } from "../../globals.js";
import { wrapFetchWithAbortSignal } from "../../infra/fetch.js";
import type { RuntimeEnv } from "../../runtime.js";
export function resolveDiscordRestFetch(
proxyUrl: string | undefined,
runtime: RuntimeEnv,
): typeof fetch {
const proxy = proxyUrl?.trim();
if (!proxy) {
return fetch;
}
try {
const agent = new ProxyAgent(proxy);
const fetcher = ((input: RequestInfo | URL, init?: RequestInit) =>
undiciFetch(input as string | URL, {
...(init as Record<string, unknown>),
dispatcher: agent,
}) as unknown as Promise<Response>) as typeof fetch;
runtime.log?.("discord: rest proxy enabled");
return wrapFetchWithAbortSignal(fetcher);
} catch (err) {
runtime.error?.(danger(`discord: invalid rest proxy: ${String(err)}`));
return fetch;
}
}