mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-12 17:51:22 +00:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { extractToolPayload, type ToolPayloadCarrier } from "./tool-payload.js";
|
|
|
|
describe("extractToolPayload", () => {
|
|
it("returns undefined for missing results", () => {
|
|
expect(extractToolPayload(undefined)).toBeUndefined();
|
|
expect(extractToolPayload(null)).toBeUndefined();
|
|
});
|
|
|
|
it("prefers explicit details payloads", () => {
|
|
expect(
|
|
extractToolPayload({
|
|
details: { ok: true },
|
|
content: [{ type: "text", text: '{"ignored":true}' }],
|
|
}),
|
|
).toEqual({ ok: true });
|
|
});
|
|
|
|
it("parses JSON text blocks and falls back to raw text, content, or the whole result", () => {
|
|
expect(
|
|
extractToolPayload({
|
|
content: [
|
|
{ type: "image", url: "https://example.com/a.png" },
|
|
{ type: "text", text: '{"ok":true,"count":2}' },
|
|
],
|
|
}),
|
|
).toEqual({ ok: true, count: 2 });
|
|
|
|
expect(
|
|
extractToolPayload({
|
|
content: [{ type: "text", text: "not json" }],
|
|
}),
|
|
).toBe("not json");
|
|
|
|
const content = [{ type: "image", url: "https://example.com/a.png" }];
|
|
expect(
|
|
extractToolPayload({
|
|
content,
|
|
}),
|
|
).toBe(content);
|
|
|
|
const result = { status: "ok" } as ToolPayloadCarrier & { status: string };
|
|
expect(extractToolPayload(result)).toBe(result);
|
|
});
|
|
});
|