Files
Triple-C/app/src/components/settings/hosts/AdminBundleModal.test.tsx
T
shadowdaoandClaude Opus 5.5 98e330060e fix(aws): EC2 name check by owner tag; keep unusable records; bundle warning
The pre-apply check now decides ownership by the resources' triple-c:owner
tag (record as fallback), so a failed apply of one's own capacity can be
re-applied; any cluster status but INACTIVE holds the name. An onboarding
seam (OnboardApi) makes the record read and the name lookups testable, and
prepare_onboarding settles everything before launch. With no choice given,
an unreadable or invalid record is refused instead of removing capacity.
Bundles warn when their cluster name looks taken; the README check prints
owner tags. The UI keeps the recorded type when the instance list fails and
says when an apply removes EC2 capacity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:31:30 -08:00

155 lines
7.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import AdminBundleModal from "./AdminBundleModal";
import { instanceTable } from "./testkit";
const invoke = vi.fn();
vi.mock("@tauri-apps/api/core", () => ({
invoke: (cmd: string, args?: unknown) => invoke(cmd, args),
}));
describe("AdminBundleModal", () => {
beforeEach(() => vi.clearAllMocks());
it("saves the bundle and shows the folder and the two commands, with Copy", async () => {
invoke.mockImplementation(async (cmd: string) =>
cmd === "list_ec2_instance_types" ? instanceTable : { folder: "/home/me/triple-c-onboard-dev", warning: null },
);
const writeText = vi.fn().mockResolvedValue(undefined);
Object.assign(navigator, { clipboard: { writeText } });
render(
<AdminBundleModal connectionId="h1" region="eu-west-1" kind="user_onboard" environment="dev" ownerId="AIDAEXAMPLE" onClose={vi.fn()} />,
);
expect(screen.getByRole("dialog", { name: "Onboarding bundle" })).toBeInTheDocument();
expect(screen.getByText(/environment admin/)).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
expect(await screen.findByText("/home/me/triple-c-onboard-dev")).toBeInTheDocument();
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", {
connectionId: "h1",
region: "eu-west-1",
kind: "user_onboard",
environment: "dev",
ownerId: "AIDAEXAMPLE",
network: null,
ec2: null,
});
const commands = "tofu init -backend-config=backend.hcl\ntofu apply";
expect(screen.getByText((_, el) => el?.tagName === "PRE" && el.textContent === commands)).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Copy commands" }));
await waitFor(() => expect(writeText).toHaveBeenCalledWith(commands));
expect(invoke.mock.calls.filter((c) => c[0] === "save_aws_admin_bundle")).toHaveLength(1);
});
it("an onboarding bundle carries the chosen EC2 capacity", async () => {
invoke.mockImplementation(async (cmd: string) =>
cmd === "list_ec2_instance_types" ? instanceTable : { folder: "/tmp/onboard", warning: "An ECS cluster named triple-c-dev--u-bob already belongs to another onboarding." },
);
render(
<AdminBundleModal connectionId="h1" region="eu-west-1" kind="user_onboard" environment="dev" ownerId="AIDAEXAMPLE" onClose={vi.fn()} />,
);
fireEvent.click(screen.getByRole("radio", { name: "My own EC2 instances" }));
await waitFor(() => expect((screen.getByLabelText("Instance type") as HTMLSelectElement).value).toBe("m7i.large"));
const save = screen.getByRole("button", { name: "Save bundle to folder…" });
// The triple-c-user value is required with EC2.
expect(save).toBeDisabled();
fireEvent.change(screen.getByLabelText("Your triple-c-user value"), { target: { value: " bob " } });
fireEvent.change(screen.getByLabelText("Maximum instances"), { target: { value: "4" } });
fireEvent.click(save);
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", {
connectionId: "h1",
region: "eu-west-1",
kind: "user_onboard",
environment: "dev",
ownerId: "AIDAEXAMPLE",
network: null,
ec2: { instance_type: "m7i.large", max_instances: 4, user_tag: "bob" },
}),
);
// The app's partial check of the name (cluster only) warns, never refuses.
expect(await screen.findByText(/already belongs to another onboarding/)).toBeInTheDocument();
expect(screen.getByText("/tmp/onboard")).toBeInTheDocument();
});
it("keeps the recorded instance type, read-only, when the list can't load", async () => {
invoke.mockImplementation(async (cmd: string) => {
if (cmd === "list_ec2_instance_types") throw new Error("boom");
return null;
});
render(
<AdminBundleModal
connectionId="h1"
region="eu-west-1"
kind="user_onboard"
environment="dev"
ownerId="AIDAEXAMPLE"
recordedEc2={{
instance_type: "r7g.2xlarge",
max_instances: 2,
user_tag: "alice",
cluster_arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/triple-c-dev--u-alice",
capacity_provider: "triple-c-dev--u-alice",
}}
onClose={vi.fn()}
/>,
);
expect(await screen.findByText(/Could not load the instance types/)).toBeInTheDocument();
const type = screen.getByLabelText("Instance type") as HTMLSelectElement;
expect(type).toBeDisabled();
expect(type.value).toBe("r7g.2xlarge");
expect(screen.getByText(/keeping r7g.2xlarge/)).toBeInTheDocument();
// The kept values can still be saved.
expect(screen.getByRole("button", { name: "Save bundle to folder…" })).toBeEnabled();
});
it("an environment bundle has no EC2 capacity", () => {
invoke.mockResolvedValue(instanceTable);
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
expect(screen.queryByRole("radio", { name: "My own EC2 instances" })).toBeNull();
});
it("shows nothing new when the folder picker is dismissed", async () => {
invoke.mockResolvedValue(null);
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="permissions_boundary" onClose={vi.fn()} />);
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
await waitFor(() => expect(invoke).toHaveBeenCalled());
expect(screen.queryByRole("button", { name: "Copy commands" })).toBeNull();
});
it("an environment bundle asks for the name and network", async () => {
invoke.mockResolvedValue({ folder: "/tmp/env", warning: null });
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "team" } });
fireEvent.click(screen.getByRole("radio", { name: "Private + NAT" }));
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", {
connectionId: "h1",
region: "eu-west-1",
kind: "environment",
environment: "team",
ownerId: null,
network: { mode: "private-nat" },
maxWorkspaceHours: 0,
}),
);
});
it("an environment bundle carries the lifetime cap", async () => {
invoke.mockResolvedValue({ folder: "/tmp/env", warning: null });
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
fireEvent.change(screen.getByLabelText("Maximum workspace lifetime (hours, 0 = none)"), { target: { value: "24" } });
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", expect.objectContaining({ maxWorkspaceHours: 24 })),
);
});
it("shows a save failure through the AWS error copy", async () => {
invoke.mockRejectedValue({ kind: "sso_expired", profile: "prod-sso" });
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="permissions_boundary" onClose={vi.fn()} />);
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
expect(await screen.findByText("Your SSO session for profile prod-sso has expired.")).toBeInTheDocument();
});
});