


























@@ -200,6 +200,26 @@ type SessionHistorySseStream = {
200200streamState: { buffer: string };
201201};
202202203+function expectOpenClawMetadata(
204+metadata: { id?: string; seq?: number } | undefined,
205+expected: { id?: string; seq: number },
206+) {
207+if (expected.id !== undefined) {
208+expect(metadata?.id).toBe(expected.id);
209+}
210+expect(metadata?.seq).toBe(expected.seq);
211+}
212+213+function expectErrorResponse(body: unknown, expected: { type: string; message: string }) {
214+expect(body).toEqual({
215+ok: false,
216+error: {
217+type: expected.type,
218+message: expected.message,
219+},
220+});
221+}
222+203223async function openSessionHistorySse(
204224port: number,
205225sessionKey: string,
@@ -240,13 +260,14 @@ async function expectMessageEventMatch(
240260).toBe(params.text);
241261expect((event.data as { messageSeq?: number }).messageSeq).toBe(params.seq);
242262if (params.id !== undefined) {
243-expect(
263+expectOpenClawMetadata(
244264(event.data as { message?: { __openclaw?: { id?: string; seq?: number } } }).message
245265?.__openclaw,
246-).toMatchObject({
247-id: params.id,
248-seq: params.seq,
249-});
266+{
267+id: params.id,
268+seq: params.seq,
269+},
270+);
250271}
251272return event;
252273}
@@ -282,15 +303,16 @@ describe("session history HTTP endpoints", () => {
282303expect(body.sessionKey).toBe("agent:main:main");
283304expect(body.messages).toHaveLength(1);
284305expect(body.messages?.[0]?.content?.[0]?.text).toBe("hello from history");
285-expect(
306+expectOpenClawMetadata(
286307(
287308body.messages?.[0] as {
288309__openclaw?: { id?: string; seq?: number };
289310}
290311)?.__openclaw,
291-).toMatchObject({
292-seq: 1,
293-});
312+{
313+seq: 1,
314+},
315+);
294316});
295317});
296318@@ -299,12 +321,9 @@ describe("session history HTTP endpoints", () => {
299321await withGatewayHarness(async (harness) => {
300322const res = await fetchSessionHistory(harness.port, "agent:main:missing");
301323expect(res.status).toBe(404);
302-await expect(res.json()).resolves.toMatchObject({
303-ok: false,
304-error: {
305-type: "not_found",
306-message: "Session not found: agent:main:missing",
307-},
324+expectErrorResponse(await res.json(), {
325+type: "not_found",
326+message: "Session not found: agent:main:missing",
308327});
309328});
310329});
@@ -438,7 +457,7 @@ describe("session history HTTP endpoints", () => {
438457}>;
439458};
440459expect(nextData.messages?.[0]?.content?.[0]?.text).toBe("third message");
441-expect(nextData.messages?.[0]?.__openclaw).toMatchObject({
460+expectOpenClawMetadata(nextData.messages?.[0]?.__openclaw, {
442461id: thirdMessageId,
443462seq: 3,
444463});
@@ -528,7 +547,7 @@ describe("session history HTTP endpoints", () => {
528547expect(body.sessionKey).toBe("agent:main:main");
529548expect(body.messages).toHaveLength(1);
530549expect(body.messages?.[0]?.content?.[0]?.text).toBe("Done.");
531-expect(body.messages?.[0]?.__openclaw).toMatchObject({
550+expectOpenClawMetadata(body.messages?.[0]?.__openclaw, {
532551id: visibleMessageId,
533552seq: 2,
534553});
@@ -710,12 +729,9 @@ describe("session history HTTP endpoints", () => {
710729},
711730);
712731expect(httpHistory.status).toBe(403);
713-await expect(httpHistory.json()).resolves.toMatchObject({
714-ok: false,
715-error: {
716-type: "forbidden",
717-message: "missing scope: operator.read",
718-},
732+expectErrorResponse(await httpHistory.json(), {
733+type: "forbidden",
734+message: "missing scope: operator.read",
719735});
720736721737const httpHistoryWithoutScopes = await fetch(
@@ -725,12 +741,9 @@ describe("session history HTTP endpoints", () => {
725741},
726742);
727743expect(httpHistoryWithoutScopes.status).toBe(403);
728-await expect(httpHistoryWithoutScopes.json()).resolves.toMatchObject({
729-ok: false,
730-error: {
731-type: "forbidden",
732-message: "missing scope: operator.read",
733-},
744+expectErrorResponse(await httpHistoryWithoutScopes.json(), {
745+type: "forbidden",
746+message: "missing scope: operator.read",
734747});
735748} finally {
736749ws.close();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。