import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { FilesTab } from "../FilesTab";
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/useFiles", () => ({
	useDirectoryListing: () => ({
		data: {
			count: 2,
			entries: [
				{ name: "movies", type: "d", size: 0, mtime: 1700000000 },
				{ name: "video.mkv", type: "f", size: 1024, mtime: 1700000000 },
			],
		},
		isLoading: false,
		error: null,
		refetch: vi.fn(),
	}),
	useFfprobe: () => ({ data: undefined, isLoading: false, error: null }),
	useJobTemplates: () => ({ data: [] }),
	useRunJob: () => ({ mutate: vi.fn(), isPending: false, data: undefined }),
}));

vi.mock("../../../hooks/usePersistentState", () => ({
	usePersistentState: vi.fn((_key: string, initial: () => unknown) => [
		initial(),
		vi.fn(),
	]),
}));

function renderTab(path = "/services/ssh_tasks/ssh-1") {
	return render(
		<MemoryRouter initialEntries={[path]}>
			<FilesTab instance={instance} />
		</MemoryRouter>,
	);
}

describe("FilesTab", () => {
	it("renders the directory listing with instance-scoped hooks", () => {
		renderTab();
		expect(screen.getByText("movies")).toBeInTheDocument();
		expect(screen.getByText("video.mkv")).toBeInTheDocument();
	});

	it("renders the path bar and browser section", () => {
		renderTab();
		expect(screen.getByText("Browser")).toBeInTheDocument();
		expect(screen.getByLabelText("Remote path")).toBeInTheDocument();
	});
});
