Files
openidea/src/features/database/SourceDialog.test.tsx
T
l0ng-ai 934e4ca0e7 test(database): keep the SSH tunnel test's command off Tauri's invoke (#3357)
The SSH tunnel test mocked call but not the command it receives, so commands.dbTestTunnel ran Tauri's invoke in jsdom and its unhandled rejection failed the Frontend job on main. The test stubs dbTest/dbTestTunnel now.
2026-11-14 18:52:31 +08:00

78 lines
3.5 KiB
TypeScript

import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { SourceDialog } from "./SourceDialog";
const ipc = vi.hoisted(() => ({ call: vi.fn() }));
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
// The commands `call` receives must not reach Tauri's invoke, which jsdom lacks.
const request =
(name: string) =>
(...args: unknown[]) => [name, ...args];
return {
...real,
call: ipc.call,
commands: { ...real.commands, dbTest: request("dbTest"), dbTestTunnel: request("dbTestTunnel") },
};
});
const savePassword = () => screen.getByRole("checkbox", { name: "Save password" }) as HTMLInputElement;
describe("new data source", () => {
it("does not save the database password unless asked to", () => {
render(<SourceDialog initial={null} />);
expect(savePassword().checked).toBe(false);
});
it("does not save the SSH tunnel password unless asked to", () => {
render(<SourceDialog initial={null} />);
fireEvent.click(screen.getByRole("tab", { name: "SSH/SSL" }));
fireEvent.click(screen.getByRole("checkbox", { name: "Use SSH tunnel" }));
fireEvent.click(screen.getByRole("button", { name: "SSH authentication type" }));
fireEvent.click(within(screen.getByRole("listbox")).getByText("Password"));
expect(savePassword().checked).toBe(false);
});
it("names the secret of a Snowflake source after its authentication method", () => {
render(
<SourceDialog
initial={null}
preset={{
driver: "snowflake",
host: "",
snowflake: { warehouse: "", role: "", auth: "keyPair", privateKeyFile: "" },
}}
/>,
);
expect(screen.getByText("Account:")).toBeTruthy();
expect(screen.queryByRole("tab", { name: "SSH/SSL" })).toBeNull();
expect(screen.getByLabelText("Private key:")).toBeTruthy();
expect((screen.getByLabelText("Passphrase:") as HTMLInputElement).type).toBe("password");
expect(savePassword().checked).toBe(false);
fireEvent.click(screen.getByRole("button", { name: "Snowflake authentication" }));
fireEvent.click(within(screen.getByRole("listbox")).getByText("Password"));
expect(screen.queryByLabelText("Private key:")).toBeNull();
expect((screen.getByLabelText("Password:") as HTMLInputElement).type).toBe("password");
for (const field of ["Schema:", "Warehouse:", "Role:"]) expect(screen.getByLabelText(field)).toBeTruthy();
});
});
describe("Test SSH Tunnel", () => {
it("tests the tunnel typed in the SSH/SSL tab, without the database login", async () => {
ipc.call.mockResolvedValue("dev@bastion:22 reaches 127.0.0.1:5432");
render(<SourceDialog initial={null} preset={{ driver: "postgres", host: "127.0.0.1" }} />);
fireEvent.click(screen.getByRole("tab", { name: "SSH/SSL" }));
const button = () => screen.getByRole("button", { name: "Test SSH Tunnel" }) as HTMLButtonElement;
expect(button().disabled).toBe(true);
fireEvent.click(screen.getByRole("checkbox", { name: "Use SSH tunnel" }));
expect(button().disabled).toBe(true);
fireEvent.change(screen.getByLabelText("Host:", { selector: "#db-ssh-host" }), { target: { value: "bastion" } });
fireEvent.click(button());
await waitFor(() =>
expect(screen.getByRole("status").textContent).toBe("SSH tunnel works: dev@bastion:22 reaches 127.0.0.1:5432"),
);
expect(ipc.call.mock.calls[0]?.[0]?.[0]).toBe("dbTestTunnel");
ipc.call.mockClear();
});
});