2026-07-18 20:42:40 +08:00
|
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
2026-07-18 23:36:59 +08:00
|
|
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
2026-07-18 20:42:40 +08:00
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
|
|
import { App } from "./App";
|
2026-07-18 23:36:59 +08:00
|
|
|
|
import type {
|
|
|
|
|
|
ConnectionProfile,
|
|
|
|
|
|
ConnectionProfileInput,
|
|
|
|
|
|
UpdateConnectionProfileRequest,
|
|
|
|
|
|
} from "./types/profile";
|
2026-07-18 20:42:40 +08:00
|
|
|
|
|
|
|
|
|
|
vi.mock("@tauri-apps/api/core", () => ({
|
|
|
|
|
|
invoke: vi.fn(),
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2026-07-18 23:36:59 +08:00
|
|
|
|
const bootstrapStatus = {
|
|
|
|
|
|
appVersion: "0.1.0-alpha.0",
|
|
|
|
|
|
platform: "windows",
|
|
|
|
|
|
rustCore: "ready",
|
|
|
|
|
|
protocolVersion: "desktop-v1alpha1",
|
|
|
|
|
|
databaseSchemaVersion: 2,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-18 20:42:40 +08:00
|
|
|
|
describe("App", () => {
|
|
|
|
|
|
beforeEach(() => {
|
2026-07-18 23:36:59 +08:00
|
|
|
|
vi.mocked(invoke).mockImplementation(async (command) => {
|
|
|
|
|
|
if (command === "bootstrap_status") {
|
|
|
|
|
|
return bootstrapStatus;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "list_connection_profiles") {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Error(`Unexpected command: ${command}`);
|
2026-07-18 20:42:40 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("renders the verified Rust bootstrap status", async () => {
|
|
|
|
|
|
render(<App />);
|
|
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText("ready")).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText("desktop-v1alpha1")).toBeInTheDocument();
|
2026-07-18 23:36:59 +08:00
|
|
|
|
expect(screen.getByText("连接 Profile")).toBeInTheDocument();
|
2026-07-18 20:42:40 +08:00
|
|
|
|
});
|
2026-07-18 22:17:29 +08:00
|
|
|
|
|
|
|
|
|
|
it("rejects an invalid Rust IPC payload", async () => {
|
2026-07-18 23:36:59 +08:00
|
|
|
|
vi.mocked(invoke).mockImplementation(async (command) => {
|
|
|
|
|
|
if (command === "bootstrap_status") {
|
|
|
|
|
|
return {
|
|
|
|
|
|
...bootstrapStatus,
|
|
|
|
|
|
rustCore: "unexpected",
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "list_connection_profiles") {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Error(`Unexpected command: ${command}`);
|
2026-07-18 22:17:29 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
render(<App />);
|
|
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText(/IPC unavailable:/)).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.queryByText("unexpected")).not.toBeInTheDocument();
|
|
|
|
|
|
});
|
2026-07-18 23:36:59 +08:00
|
|
|
|
|
|
|
|
|
|
it("creates, edits, and deletes non-sensitive profile metadata", async () => {
|
|
|
|
|
|
let profiles: ConnectionProfile[] = [];
|
|
|
|
|
|
vi.mocked(invoke).mockImplementation(async (command, args) => {
|
|
|
|
|
|
if (command === "bootstrap_status") {
|
|
|
|
|
|
return bootstrapStatus;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "list_connection_profiles") {
|
|
|
|
|
|
return profiles;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "create_connection_profile") {
|
|
|
|
|
|
const { request } = args as { request: ConnectionProfileInput };
|
|
|
|
|
|
const created: ConnectionProfile = {
|
|
|
|
|
|
id: "2bdedc10-d804-4da5-8e92-39dd7f175d7a",
|
|
|
|
|
|
...request,
|
|
|
|
|
|
autoConnect: false,
|
|
|
|
|
|
createdAt: "2026-07-18T02:00:00Z",
|
|
|
|
|
|
updatedAt: "2026-07-18T02:00:00Z",
|
|
|
|
|
|
};
|
|
|
|
|
|
profiles = [created];
|
|
|
|
|
|
return created;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "update_connection_profile") {
|
|
|
|
|
|
const { request } = args as {
|
|
|
|
|
|
request: UpdateConnectionProfileRequest;
|
|
|
|
|
|
};
|
|
|
|
|
|
const existing = profiles.find((profile) => profile.id === request.id);
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
throw new Error("Profile not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
const updated: ConnectionProfile = {
|
|
|
|
|
|
...existing,
|
|
|
|
|
|
...request.profile,
|
|
|
|
|
|
updatedAt: "2026-07-18T02:10:00Z",
|
|
|
|
|
|
};
|
|
|
|
|
|
profiles = [updated];
|
|
|
|
|
|
return updated;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (command === "delete_connection_profile") {
|
|
|
|
|
|
const { id } = args as { id: string };
|
|
|
|
|
|
profiles = profiles.filter((profile) => profile.id !== id);
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Error(`Unexpected command: ${command}`);
|
|
|
|
|
|
});
|
|
|
|
|
|
vi.spyOn(window, "confirm").mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
render(<App />);
|
|
|
|
|
|
expect(await screen.findByText("尚未保存 Profile。")).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
|
|
fireEvent.change(screen.getByLabelText("显示名称"), {
|
|
|
|
|
|
target: { value: "家庭运维" },
|
|
|
|
|
|
});
|
|
|
|
|
|
fireEvent.change(screen.getByLabelText("角色"), {
|
|
|
|
|
|
target: { value: "operations" },
|
|
|
|
|
|
});
|
|
|
|
|
|
fireEvent.change(screen.getByLabelText("REST 基础 URL"), {
|
|
|
|
|
|
target: { value: "https://ops.example.test" },
|
|
|
|
|
|
});
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "保存 Profile" }));
|
|
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
|
await screen.findByText("已创建 Profile:家庭运维"),
|
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByLabelText("编辑 家庭运维")).toBeEnabled();
|
|
|
|
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByLabelText("编辑 家庭运维"));
|
|
|
|
|
|
fireEvent.change(screen.getByLabelText("显示名称"), {
|
|
|
|
|
|
target: { value: "家庭运维(主)" },
|
|
|
|
|
|
});
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "更新 Profile" }));
|
|
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
|
await screen.findByText("已更新 Profile:家庭运维(主)"),
|
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
|
fireEvent.click(screen.getByLabelText("删除 家庭运维(主)"));
|
|
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
|
expect(
|
|
|
|
|
|
screen.getByText("已删除 Profile:家庭运维(主)"),
|
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
|
});
|
|
|
|
|
|
expect(screen.getByText("尚未保存 Profile。")).toBeInTheDocument();
|
|
|
|
|
|
expect(invoke).toHaveBeenCalledWith(
|
|
|
|
|
|
"create_connection_profile",
|
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
|
request: expect.not.objectContaining({
|
|
|
|
|
|
token: expect.anything(),
|
|
|
|
|
|
password: expect.anything(),
|
|
|
|
|
|
apiKey: expect.anything(),
|
|
|
|
|
|
}),
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2026-07-18 20:42:40 +08:00
|
|
|
|
});
|