2026-06-09 18:28:30 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
|
|
|
import { LogStreamPanel } from "@/components/Kubernetes/LogStreamPanel";
|
|
|
|
|
|
|
|
|
|
vi.mock("@tauri-apps/api/event", () => ({
|
|
|
|
|
listen: vi.fn().mockResolvedValue(() => {}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/lib/tauriCommands", () => ({
|
|
|
|
|
streamPodLogsCmd: vi.fn().mockResolvedValue("stream-123"),
|
|
|
|
|
stopLogStreamCmd: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
describe("LogStreamPanel — ANSI color support", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders ANSI colored text correctly", () => {
|
|
|
|
|
const containers = ["app"];
|
|
|
|
|
const { rerender } = render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={containers}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Component should render the ANSI-colored line
|
|
|
|
|
rerender(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={containers}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText(/Log Stream/)).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("LogStreamPanel — Download functionality", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders "Download Visible" button', () => {
|
|
|
|
|
render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={["app"]}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByRole("button", { name: /download visible/i })).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders "Download All" button', () => {
|
|
|
|
|
render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={["app"]}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByRole("button", { name: /download all/i })).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
it("download visible creates blob with current visible lines", async () => {
|
2026-06-09 18:28:30 +00:00
|
|
|
const createObjectURL = vi.fn(() => "blob:url");
|
|
|
|
|
const revokeObjectURL = vi.fn();
|
2026-06-09 18:31:39 +00:00
|
|
|
const mockClick = vi.fn();
|
2026-06-09 18:28:30 +00:00
|
|
|
global.URL.createObjectURL = createObjectURL;
|
|
|
|
|
global.URL.revokeObjectURL = revokeObjectURL;
|
|
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
// Mock createElement to intercept the anchor creation
|
|
|
|
|
const originalCreateElement = document.createElement;
|
|
|
|
|
document.createElement = vi.fn((tagName: string) => {
|
|
|
|
|
const element = originalCreateElement.call(document, tagName);
|
|
|
|
|
if (tagName === "a") {
|
|
|
|
|
element.click = mockClick;
|
|
|
|
|
}
|
|
|
|
|
return element;
|
|
|
|
|
}) as typeof document.createElement;
|
|
|
|
|
|
2026-06-09 18:28:30 +00:00
|
|
|
render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={["app"]}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
// Download button should be disabled when no lines
|
2026-06-09 18:28:30 +00:00
|
|
|
const downloadBtn = screen.getByRole("button", { name: /download visible/i });
|
2026-06-09 18:31:39 +00:00
|
|
|
expect(downloadBtn).toHaveAttribute("disabled");
|
2026-06-09 18:28:30 +00:00
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
// Cleanup
|
|
|
|
|
document.createElement = originalCreateElement;
|
2026-06-09 18:28:30 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("LogStreamPanel — Search highlighting", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("highlights search matches in yellow", async () => {
|
|
|
|
|
render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={["app"]}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const searchInput = screen.getByPlaceholderText(/filter log lines/i);
|
|
|
|
|
fireEvent.change(searchInput, { target: { value: "error" } });
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(searchInput).toHaveValue("error");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
it("does not show navigation buttons when no matching lines", () => {
|
2026-06-09 18:28:30 +00:00
|
|
|
render(
|
|
|
|
|
<LogStreamPanel
|
|
|
|
|
clusterId="c1"
|
|
|
|
|
namespace="default"
|
|
|
|
|
podName="test-pod"
|
|
|
|
|
containers={["app"]}
|
|
|
|
|
open={true}
|
|
|
|
|
onOpenChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const searchInput = screen.getByPlaceholderText(/filter log lines/i);
|
|
|
|
|
fireEvent.change(searchInput, { target: { value: "test" } });
|
|
|
|
|
|
2026-06-09 18:31:39 +00:00
|
|
|
// Navigation buttons should not be visible when there are no lines
|
|
|
|
|
expect(screen.queryByRole("button", { name: /previous match/i })).toBeNull();
|
|
|
|
|
expect(screen.queryByRole("button", { name: /next match/i })).toBeNull();
|
2026-06-09 18:28:30 +00:00
|
|
|
});
|
|
|
|
|
});
|