import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { ActionsTab } from "../ActionsTab";
import type { ServiceInstance } from "../../../types";

const instance: ServiceInstance = {
	id: "ssh-1",
	service_type: "ssh_tasks",
	name: "Storage Server",
	config: {},
	secrets_set: {},
	enabled: true,
	created_at: 1_700_000_000,
	updated_at: 1_700_000_000,
};

vi.mock("../../../hooks/useSettings", () => ({
	useTasks: () => ({
		data: [
			{
				id: "t1",
				name: "Disk usage",
				task_type: "shell",
				content: "df -h",
				enabled: true,
				default_service_id: "",
				notes: "",
			},
		],
	}),
	useTaskRuns: () => ({ data: { items: [] } }),
	useSaveTask: () => ({ mutateAsync: vi.fn(), isPending: false }),
	useDeleteTask: () => ({ mutate: vi.fn(), isPending: false }),
	useRunTask: () => ({ mutateAsync: vi.fn(), isPending: false }),
}));

function renderTab() {
	return render(
		<MemoryRouter>
			<ActionsTab instance={instance} />
		</MemoryRouter>,
	);
}

describe("ActionsTab", () => {
	it("renders the saved-actions rail and task detail", () => {
		renderTab();
		expect(screen.getByText("Saved actions")).toBeInTheDocument();
		expect(screen.getByText("Disk usage")).toBeInTheDocument();
	});

	it("renders the Add action button", () => {
		renderTab();
		expect(
			screen.getByRole("button", { name: "Add action" }),
		).toBeInTheDocument();
	});
});
