fix: close browser session lifecycle gaps
This commit is contained in:
@@ -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: [] }));
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import "@testing-library/jest-dom/vitest";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { App } from "../src/App.js";
|
||||
import { createInitialHarnessState, type HarnessClientState } from "../src/store/reducer.js";
|
||||
|
||||
@@ -24,6 +24,7 @@ function makeStore() {
|
||||
subscribe: () => () => undefined,
|
||||
sendMessage,
|
||||
cancel: vi.fn().mockResolvedValue({ cancelled: false }),
|
||||
dispose: vi.fn().mockResolvedValue(undefined),
|
||||
start: vi.fn().mockResolvedValue(undefined),
|
||||
getModelConfig: vi.fn().mockResolvedValue({
|
||||
modelConfig: { configured: true, source: "environment", providerId: "openai", api: "openai-responses", modelId: "gpt-test", supportsImages: true },
|
||||
@@ -31,6 +32,8 @@ function makeStore() {
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
describe("App", () => {
|
||||
it("renders the three primary work areas", () => {
|
||||
render(<App store={makeStore()} autoStart={false} modelConfigured />);
|
||||
@@ -55,4 +58,37 @@ describe("App", () => {
|
||||
expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled();
|
||||
expect(screen.getByText("请先配置模型连接")).toBeVisible();
|
||||
});
|
||||
|
||||
it("switches to the event timeline in the mobile tab layout", async () => {
|
||||
vi.stubGlobal("matchMedia", vi.fn(() => ({
|
||||
matches: true,
|
||||
media: "(max-width: 720px)",
|
||||
onchange: null,
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
addListener: vi.fn(),
|
||||
removeListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
})));
|
||||
const user = userEvent.setup();
|
||||
render(<App store={makeStore()} autoStart={false} modelConfigured />);
|
||||
|
||||
await user.click(screen.getByRole("tab", { name: "Events" }));
|
||||
|
||||
expect(screen.getByRole("tab", { name: "Events" })).toHaveAttribute("aria-selected", "true");
|
||||
expect(screen.getByRole("region", { name: "Event Timeline" })).toBeVisible();
|
||||
expect(screen.queryByRole("main", { name: "Streaming Chat" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("deletes the old session before starting a reset session", async () => {
|
||||
const user = userEvent.setup();
|
||||
const store = makeStore();
|
||||
render(<App store={store} autoStart={false} modelConfigured />);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "重置训练会话" }));
|
||||
|
||||
expect(store.dispose).toHaveBeenCalledTimes(1);
|
||||
expect(store.start).toHaveBeenCalledTimes(1);
|
||||
expect(store.dispose.mock.invocationCallOrder[0]).toBeLessThan(store.start.mock.invocationCallOrder[0] ?? 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { HarnessApi } from "../src/api/harness-api.js";
|
||||
import { HarnessClientStore } from "../src/store/harness-client-store.js";
|
||||
|
||||
describe("HarnessClientStore lifecycle", () => {
|
||||
it("deletes the current session and can start a fresh session after dispose", async () => {
|
||||
const createSession = vi.fn()
|
||||
.mockResolvedValueOnce({ sessionId: "ses_1", requestId: "req_1" })
|
||||
.mockResolvedValueOnce({ sessionId: "ses_2", requestId: "req_2" });
|
||||
const deleteSession = vi.fn().mockResolvedValue({ deleted: true, requestId: "req_delete" });
|
||||
const api = {
|
||||
createSession,
|
||||
deleteSession,
|
||||
getSkills: vi.fn().mockResolvedValue({ skills: [], requestId: "req_skills" }),
|
||||
eventsUrl: (sessionId: string) => `/api/sessions/${sessionId}/events`,
|
||||
} as unknown as HarnessApi;
|
||||
const eventSources: Array<{ close: ReturnType<typeof vi.fn> }> = [];
|
||||
const store = new HarnessClientStore(api, () => {
|
||||
const source = { addEventListener: vi.fn(), close: vi.fn() };
|
||||
eventSources.push(source);
|
||||
return source;
|
||||
});
|
||||
|
||||
await store.start();
|
||||
expect(store.getSnapshot().sessionId).toBe("ses_1");
|
||||
await store.dispose();
|
||||
await store.start();
|
||||
|
||||
expect(deleteSession).toHaveBeenCalledWith("ses_1");
|
||||
expect(createSession).toHaveBeenCalledTimes(2);
|
||||
expect(eventSources[0]?.close).toHaveBeenCalledTimes(1);
|
||||
expect(store.getSnapshot().sessionId).toBe("ses_2");
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,8 @@ describe("ModelSettings", () => {
|
||||
const save = vi.fn().mockResolvedValue({ configured: true, source: "session", keyHint: "••••cret" });
|
||||
render(<ModelSettings open onClose={vi.fn()} busy={false} onSave={save} onTest={vi.fn()} onClear={vi.fn()} />);
|
||||
|
||||
expect(screen.getByLabelText("模型 ID")).toHaveAttribute("autocomplete", "off");
|
||||
expect(screen.getByLabelText("API Key")).toHaveAttribute("autocomplete", "new-password");
|
||||
await user.type(screen.getByLabelText("API Key"), "sk-browser-secret");
|
||||
await user.type(screen.getByLabelText("模型 ID"), "gpt-test");
|
||||
await user.click(screen.getByRole("button", { name: "保存配置" }));
|
||||
|
||||
Reference in New Issue
Block a user