fix: close browser session lifecycle gaps

This commit is contained in:
Wenxin Duan
2026-09-08 11:12:12 +08:00
parent 8fa006344d
commit ba45382747
12 changed files with 149 additions and 11 deletions
+34
View File
@@ -0,0 +1,34 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { HarnessApi } from "../src/api/harness-api.js";
afterEach(() => vi.unstubAllGlobals());
describe("HarnessApi request headers", () => {
it("does not claim an empty POST contains JSON", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
sessionId: "ses_1",
requestId: "req_1",
}), { status: 201, headers: { "Content-Type": "application/json" } }));
vi.stubGlobal("fetch", fetchMock);
await new HarnessApi().createSession();
const request = fetchMock.mock.calls[0]?.[1] as RequestInit;
expect(new Headers(request.headers).has("Content-Type")).toBe(false);
expect(request.body).toBeUndefined();
});
it("sets JSON content type when a structured body is present", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
runId: "run_1",
requestId: "req_1",
}), { status: 202, headers: { "Content-Type": "application/json" } }));
vi.stubGlobal("fetch", fetchMock);
await new HarnessApi().sendMessage("ses_1", { text: "hello", attachmentIds: [] });
const request = fetchMock.mock.calls[0]?.[1] as RequestInit;
expect(new Headers(request.headers).get("Content-Type")).toBe("application/json");
expect(request.body).toBe(JSON.stringify({ text: "hello", attachmentIds: [] }));
});
});