mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-30 18:06:53 +00:00
refactor(memory): share post-json helper across remote fetchers
This commit is contained in:
53
src/memory/post-json.test.ts
Normal file
53
src/memory/post-json.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { postJson } from "./post-json.js";
|
||||
import { withRemoteHttpResponse } from "./remote-http.js";
|
||||
|
||||
vi.mock("./remote-http.js", () => ({
|
||||
withRemoteHttpResponse: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("postJson", () => {
|
||||
const remoteHttpMock = vi.mocked(withRemoteHttpResponse);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("parses JSON payload on successful response", async () => {
|
||||
remoteHttpMock.mockImplementationOnce(async (params) => {
|
||||
return await params.onResponse(
|
||||
new Response(JSON.stringify({ data: [{ embedding: [1, 2] }] }), { status: 200 }),
|
||||
);
|
||||
});
|
||||
|
||||
const result = await postJson({
|
||||
url: "https://memory.example/v1/post",
|
||||
headers: { Authorization: "Bearer test" },
|
||||
body: { input: ["x"] },
|
||||
errorPrefix: "post failed",
|
||||
parse: (payload) => payload,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ data: [{ embedding: [1, 2] }] });
|
||||
});
|
||||
|
||||
it("attaches status to thrown error when requested", async () => {
|
||||
remoteHttpMock.mockImplementationOnce(async (params) => {
|
||||
return await params.onResponse(new Response("bad gateway", { status: 502 }));
|
||||
});
|
||||
|
||||
await expect(
|
||||
postJson({
|
||||
url: "https://memory.example/v1/post",
|
||||
headers: {},
|
||||
body: {},
|
||||
errorPrefix: "post failed",
|
||||
attachStatus: true,
|
||||
parse: () => ({}),
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
message: expect.stringContaining("post failed: 502 bad gateway"),
|
||||
status: 502,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user