Skip to content

Commit acd0d44

Browse files
committed
perf(test): narrow Tlon monitor SDK mocks
1 parent 2ee5c2f commit acd0d44

1 file changed

Lines changed: 92 additions & 29 deletions

File tree

‎extensions/tlon/src/monitor/index.test.ts‎

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
// Tlon monitor tests cover authentication, inbound context, and shutdown lifecycle.
22
import { createServer, type Server } from "node:http";
33
import type { AddressInfo } from "node:net";
4-
import { buildChannelInboundEventContext } from "openclaw/plugin-sdk/channel-inbound";
5-
import { saveRemoteMedia } from "openclaw/plugin-sdk/media-runtime";
64
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime";
75
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
86

97
const {
108
authenticateMock,
9+
buildChannelInboundEnvelopeMock,
10+
builtInboundContextPayload,
11+
createChannelInboundEnvelopeBuilderMock,
12+
formatInboundMediaUnavailableTextMock,
1113
sleepWithAbortMock,
14+
saveRemoteMediaMock,
1215
sseClientMock,
1316
ingressMock,
1417
inboundRuntimeMock,
1518
settingsManagerMock,
1619
realUrbitFixture,
1720
} = vi.hoisted(() => ({
1821
authenticateMock: vi.fn(),
22+
buildChannelInboundEnvelopeMock: vi.fn(),
23+
builtInboundContextPayload: { kind: "tlon-inbound-context" },
24+
createChannelInboundEnvelopeBuilderMock: vi.fn(),
25+
formatInboundMediaUnavailableTextMock: vi.fn(),
1926
sleepWithAbortMock: vi.fn(),
27+
saveRemoteMediaMock: vi.fn(),
2028
sseClientMock: {
2129
scry: vi.fn().mockResolvedValue({}),
2230
subscribe: vi.fn().mockResolvedValue(undefined),
@@ -59,18 +67,24 @@ const {
5967

6068
const runningServers: Server[] = [];
6169

62-
vi.mock("openclaw/plugin-sdk/runtime-env", async (importOriginal) => {
63-
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/runtime-env")>();
64-
return {
65-
...actual,
66-
sleepWithAbort: sleepWithAbortMock,
67-
};
68-
});
70+
vi.mock("openclaw/plugin-sdk/agent-runtime", () => ({
71+
resolveHumanDelayConfig: vi.fn(() => undefined),
72+
}));
6973

70-
vi.mock("openclaw/plugin-sdk/media-runtime", async (importOriginal) => {
71-
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/media-runtime")>();
72-
return { ...actual, saveRemoteMedia: vi.fn() };
73-
});
74+
vi.mock("openclaw/plugin-sdk/channel-inbound", () => ({
75+
createChannelInboundEnvelopeBuilder: createChannelInboundEnvelopeBuilderMock,
76+
formatInboundMediaUnavailableText: formatInboundMediaUnavailableTextMock,
77+
}));
78+
79+
vi.mock("openclaw/plugin-sdk/runtime-env", () => ({
80+
sleepWithAbort: sleepWithAbortMock,
81+
}));
82+
83+
vi.mock("openclaw/plugin-sdk/media-runtime", () => ({
84+
MAX_IMAGE_BYTES: 6 * 1024 * 1024,
85+
readRemoteMediaBuffer: vi.fn(),
86+
saveRemoteMedia: saveRemoteMediaMock,
87+
}));
7488

7589
vi.mock("../runtime.js", () => ({
7690
getTlonRuntime: () => ({
@@ -138,10 +152,11 @@ vi.mock("./ingress.js", () => ({
138152
import { monitorTlonProvider } from "./index.js";
139153
import { extractMessageText } from "./utils.js";
140154

141-
const saveRemoteMediaMock = vi.mocked(saveRemoteMedia);
142-
143155
beforeEach(() => {
144-
inboundRuntimeMock.buildContext.mockImplementation(buildChannelInboundEventContext);
156+
createChannelInboundEnvelopeBuilderMock.mockReturnValue(buildChannelInboundEnvelopeMock);
157+
buildChannelInboundEnvelopeMock.mockReturnValue("tlon-envelope");
158+
formatInboundMediaUnavailableTextMock.mockReturnValue("formatted-inbound-body");
159+
inboundRuntimeMock.buildContext.mockReturnValue(builtInboundContextPayload);
145160
});
146161

147162
afterEach(async () => {
@@ -228,6 +243,18 @@ describe("monitorTlonProvider inbound media truth", () => {
228243
})),
229244
];
230245
const originalText = extractMessageText(content);
246+
const expectedMedia = Array.from({ length: Math.min(imageCount, 8) }, (_, index) => index)
247+
.filter((index) => !failedIndexes.includes(index))
248+
.map((index) => ({
249+
path: `/tmp/openclaw/media/inbound/photo-${index}.png`,
250+
contentType: "image/png",
251+
}));
252+
const expectedMediaPrompt = [
253+
...expectedMedia.map(
254+
({ path, contentType }) => `[media attached: ${path} (${contentType}) | ${path}]`,
255+
),
256+
originalText,
257+
].join("\n");
231258

232259
const monitor = monitorTlonProvider({ abortSignal: controller.signal, runtime });
233260
try {
@@ -252,26 +279,62 @@ describe("monitorTlonProvider inbound media truth", () => {
252279
},
253280
});
254281

282+
expect(createChannelInboundEnvelopeBuilderMock).toHaveBeenCalledOnce();
283+
expect(createChannelInboundEnvelopeBuilderMock).toHaveBeenCalledWith({
284+
cfg: {
285+
channels: {
286+
tlon: {
287+
code: "code",
288+
network: { dangerouslyAllowPrivateNetwork: true },
289+
ownerShip: "~nec",
290+
ship: "~zod",
291+
url: "https://urbit.example.com",
292+
},
293+
},
294+
},
295+
route: {
296+
accountId: "default",
297+
agentId: "main",
298+
dmScope: "main",
299+
sessionKey: "agent:main:main",
300+
},
301+
});
302+
expect(buildChannelInboundEnvelopeMock).toHaveBeenCalledOnce();
303+
expect(buildChannelInboundEnvelopeMock).toHaveBeenCalledWith({
304+
body: expectedMediaPrompt,
305+
channel: "Tlon",
306+
from: "~nec [owner]",
307+
timestamp: 1_700_000_000_000,
308+
});
309+
expect(formatInboundMediaUnavailableTextMock).toHaveBeenCalledWith({
310+
body: originalText,
311+
notice: expectedNotice,
312+
});
313+
expect(inboundRuntimeMock.buildContext).toHaveBeenCalledOnce();
314+
const buildContextCall = inboundRuntimeMock.buildContext.mock.calls[0];
315+
if (!buildContextCall) {
316+
throw new Error("expected inbound context call");
317+
}
318+
const [contextInput] = buildContextCall;
319+
expect(contextInput.message).toMatchObject({
320+
body: "tlon-envelope",
321+
bodyForAgent: "formatted-inbound-body",
322+
commandBody: originalText,
323+
rawBody: originalText,
324+
});
325+
expect(contextInput.extra.Attachments).toHaveLength(expectedAttachments);
326+
expect(contextInput.extra.Attachments).toEqual(expectedMedia);
327+
255328
expect(inboundRuntimeMock.dispatch).toHaveBeenCalledOnce();
256329
const dispatchCall = inboundRuntimeMock.dispatch.mock.calls[0];
257330
if (!dispatchCall) {
258331
throw new Error("expected inbound dispatch call");
259332
}
260333
const [{ ctxPayload, replyOptions }] = dispatchCall;
261-
expect(ctxPayload.BodyForAgent).toBe(`${originalText}\n\n${expectedNotice}`);
262-
expect(ctxPayload.RawBody).toBe(originalText);
263-
expect(ctxPayload.CommandBody).toBe(originalText);
264-
expect(ctxPayload.BodyForCommands).toBe(originalText);
265-
expect(ctxPayload.Attachments).toHaveLength(expectedAttachments);
334+
expect(contextInput).not.toBe(builtInboundContextPayload);
335+
expect(ctxPayload).toBe(builtInboundContextPayload);
266336
expect(replyOptions.media).toHaveLength(expectedAttachments);
267-
expect(replyOptions.media).toEqual(
268-
Array.from({ length: Math.min(imageCount, 8) }, (_, index) => index)
269-
.filter((index) => !failedIndexes.includes(index))
270-
.map((index) => ({
271-
path: `/tmp/openclaw/media/inbound/photo-${index}.png`,
272-
contentType: "image/png",
273-
})),
274-
);
337+
expect(replyOptions.media).toEqual(expectedMedia);
275338
} finally {
276339
controller.abort();
277340
await monitor;

0 commit comments

Comments
 (0)