25 lines
1.4 KiB
TypeScript
25 lines
1.4 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { ModelSettings } from "../src/components/settings/ModelSettings.js";
|
|
|
|
describe("ModelSettings", () => {
|
|
it("saves through the server, clears the local key, and never uses browser storage", async () => {
|
|
const user = userEvent.setup();
|
|
const localSpy = vi.spyOn(Storage.prototype, "setItem");
|
|
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: "保存配置" }));
|
|
|
|
expect(save).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "sk-browser-secret", modelId: "gpt-test" }));
|
|
expect(screen.getByLabelText("API Key")).toHaveValue("");
|
|
expect(screen.getByText("••••cret")).toBeVisible();
|
|
expect(localSpy).not.toHaveBeenCalled();
|
|
});
|
|
});
|