



























@@ -130,6 +130,23 @@ describe("gateway server chat", () => {
130130})
131131.filter((value): value is string => typeof value === "string");
132132133+const expectRecordFields = (value: unknown, expected: Record<string, unknown>) => {
134+expect(value).toBeDefined();
135+expect(typeof value).toBe("object");
136+expect(value).not.toBeNull();
137+const actual = value as Record<string, unknown>;
138+for (const [key, expectedValue] of Object.entries(expected)) {
139+expect(actual[key]).toEqual(expectedValue);
140+}
141+return actual;
142+};
143+144+const expectStringRunId = (payload: unknown) => {
145+const actual = expectRecordFields(payload, {});
146+expect(typeof actual.runId).toBe("string");
147+return actual.runId as string;
148+};
149+133150const expectAgentWaitTimeout = (res: Awaited<ReturnType<typeof rpcReq>>) => {
134151expect(res.ok).toBe(true);
135152expect(res.payload?.status).toBe("timeout");
@@ -308,7 +325,7 @@ describe("gateway server chat", () => {
308325if (abortRes.payload?.status === "aborted") {
309326expect(abortRes.payload?.abortedRunId).toBe("idem-sessions-abort-1");
310327const cancelledEvent = await cancelledEventP;
311-expect(cancelledEvent.payload?.data).toMatchObject({
328+expectRecordFields(cancelledEvent.payload?.data, {
312329phase: "end",
313330status: "cancelled",
314331aborted: true,
@@ -319,7 +336,7 @@ describe("gateway server chat", () => {
319336timeoutMs: 0,
320337});
321338expect(waitRes.ok).toBe(true);
322-expect(waitRes.payload).toMatchObject({
339+expectRecordFields(waitRes.payload, {
323340runId: "idem-sessions-abort-1",
324341status: "timeout",
325342stopReason: "rpc",
@@ -539,7 +556,7 @@ describe("gateway server chat", () => {
539556CHAT_RESPONSE_TIMEOUT_MS,
540557);
541558expect(imgRes.ok).toBe(true);
542-expect(imgRes.payload).toEqual(expect.objectContaining({ runId: expect.any(String) }));
559+expectStringRunId(imgRes.payload);
543560const reqIdOnly = "chat-img-only";
544561ws.send(
545562JSON.stringify({
@@ -568,7 +585,7 @@ describe("gateway server chat", () => {
568585CHAT_RESPONSE_TIMEOUT_MS,
569586);
570587expect(imgOnlyRes.ok).toBe(true);
571-expect(imgOnlyRes.payload).toEqual(expect.objectContaining({ runId: expect.any(String) }));
588+expectStringRunId(imgOnlyRes.payload);
572589573590const historyDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gw-"));
574591tempDirs.push(historyDir);
@@ -804,14 +821,14 @@ describe("gateway server chat", () => {
804821});
805822const sideResult = await sideResultPromise;
806823const finalEvent = await finalPromise;
807-expect(sideResult.payload).toMatchObject({
824+expectRecordFields(sideResult.payload, {
808825kind: "btw",
809826runId: "idem-btw-1",
810827sessionKey: "agent:main:main",
811828question: "what is 17 * 19?",
812829text: "323",
813830});
814-expect(finalEvent.payload).toMatchObject({
831+expectRecordFields(finalEvent.payload, {
815832runId: "idem-btw-1",
816833sessionKey: "agent:main:main",
817834state: "final",
@@ -886,7 +903,7 @@ describe("gateway server chat", () => {
886903expect(dispatchInboundMessageMock).toHaveBeenCalled();
887904});
888905const sideResult = await sideResultPromise;
889-expect(sideResult.payload).toMatchObject({
906+expectRecordFields(sideResult.payload, {
890907kind: "btw",
891908runId: "idem-btw-block-1",
892909question: "what changed?",
@@ -966,18 +983,17 @@ describe("gateway server chat", () => {
966983throw new Error("expected assistant history message");
967984}
968985const assistantContent = (assistantMessage as { content?: unknown[] }).content ?? [];
969-expect(assistantContent).toEqual([
970-{ type: "text", text: "Image reply" },
971-expect.objectContaining({
972-type: "image",
973-url: expect.stringContaining("/api/chat/media/outgoing/"),
974-openUrl: expect.stringContaining("/full"),
975-alt: "Generated image 1",
976-mimeType: "image/png",
977-width: 1,
978-height: 1,
979-}),
980-]);
986+expect(assistantContent).toHaveLength(2);
987+expect(assistantContent[0]).toEqual({ type: "text", text: "Image reply" });
988+const imageBlock = expectRecordFields(assistantContent[1], {
989+type: "image",
990+alt: "Generated image 1",
991+mimeType: "image/png",
992+width: 1,
993+height: 1,
994+});
995+expect(String(imageBlock.url)).toContain("/api/chat/media/outgoing/");
996+expect(String(imageBlock.openUrl)).toContain("/full");
981997const serializedAssistant = JSON.stringify(assistantMessage);
982998expect(serializedAssistant).not.toContain("data:image/png;base64");
983999expect(serializedAssistant).not.toContain(pngB64);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。