193 lines
6.2 KiB
JavaScript
193 lines
6.2 KiB
JavaScript
const React = require("react");
|
|
const { render } = require("ink-testing-library");
|
|
const ViewLogsScreen = require("../../../../src/tui/components/screens/ViewLogsScreen.jsx");
|
|
|
|
// Mock the dependencies
|
|
jest.mock("../../../../src/tui/providers/AppProvider.jsx", () => ({
|
|
useAppState: () => ({
|
|
navigateBack: jest.fn(),
|
|
}),
|
|
}));
|
|
|
|
jest.mock("../../../../src/tui/hooks/useServices.js", () => ({
|
|
useServices: () => ({
|
|
getLogFiles: jest.fn().mockResolvedValue([
|
|
{
|
|
filename: "Progress.md",
|
|
path: "./Progress.md",
|
|
size: 1024,
|
|
createdAt: new Date("2024-01-01T10:00:00Z"),
|
|
modifiedAt: new Date("2024-01-01T12:00:00Z"),
|
|
operationCount: 5,
|
|
isMainLog: true,
|
|
},
|
|
{
|
|
filename: "Progress-backup.md",
|
|
path: "./Progress-backup.md",
|
|
size: 512,
|
|
createdAt: new Date("2024-01-01T09:00:00Z"),
|
|
modifiedAt: new Date("2024-01-01T11:00:00Z"),
|
|
operationCount: 3,
|
|
isMainLog: false,
|
|
},
|
|
]),
|
|
readLogFile: jest
|
|
.fn()
|
|
.mockResolvedValue(
|
|
"Sample log content\n## Operation - 2024-01-01 10:00:00 UTC\nTest operation completed successfully."
|
|
),
|
|
}),
|
|
}));
|
|
|
|
jest.mock("../../../../src/tui/components/common/LoadingIndicator.jsx", () => ({
|
|
LoadingIndicator: ({ message }) => {
|
|
const React = require("react");
|
|
return React.createElement("text", null, `Loading: ${message}`);
|
|
},
|
|
}));
|
|
|
|
jest.mock(
|
|
"../../../../src/tui/components/common/ErrorDisplay.jsx",
|
|
() =>
|
|
({ error, onRetry }) => {
|
|
const React = require("react");
|
|
return React.createElement("text", null, `Error: ${error.message}`);
|
|
}
|
|
);
|
|
|
|
describe("ViewLogsScreen Component", () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
test("should render loading state initially", () => {
|
|
const { lastFrame } = render(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain("📋 View Logs");
|
|
expect(output).toContain("Loading: Discovering log files...");
|
|
});
|
|
|
|
test("should display log files list after loading", async () => {
|
|
const { lastFrame, rerender } = render(React.createElement(ViewLogsScreen));
|
|
|
|
// Wait for the component to load
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
|
|
// Force re-render to show loaded state
|
|
rerender(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain("📋 View Logs");
|
|
expect(output).toContain("Available Log Files (2)");
|
|
expect(output).toContain("Progress.md");
|
|
expect(output).toContain("Progress-backup.md");
|
|
expect(output).toContain("MAIN");
|
|
expect(output).toContain("ARCHIVE");
|
|
});
|
|
|
|
test("should show file metadata correctly", async () => {
|
|
const { lastFrame, rerender } = render(React.createElement(ViewLogsScreen));
|
|
|
|
// Wait for the component to load
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
rerender(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain("1.0 KB"); // File size formatting
|
|
expect(output).toContain("5 ops"); // Operation count
|
|
expect(output).toContain("3 ops"); // Operation count for backup
|
|
});
|
|
|
|
test("should display navigation instructions", async () => {
|
|
const { lastFrame, rerender } = render(React.createElement(ViewLogsScreen));
|
|
|
|
// Wait for the component to load
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
rerender(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain("Navigation:");
|
|
expect(output).toContain("↑/↓ - Select file");
|
|
expect(output).toContain("Enter - View content");
|
|
expect(output).toContain("R - Refresh list");
|
|
expect(output).toContain("Esc - Back to menu");
|
|
});
|
|
|
|
test("should show empty state when no log files exist", async () => {
|
|
// Mock empty log files
|
|
const mockUseServices =
|
|
require("../../../../src/tui/hooks/useServices.js").useServices;
|
|
mockUseServices.mockReturnValue({
|
|
getLogFiles: jest.fn().mockResolvedValue([]),
|
|
readLogFile: jest.fn(),
|
|
});
|
|
|
|
const { lastFrame, rerender } = render(React.createElement(ViewLogsScreen));
|
|
|
|
// Wait for the component to load
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
rerender(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain("No log files found");
|
|
expect(output).toContain(
|
|
"Log files are created when operations are performed"
|
|
);
|
|
expect(output).toContain(
|
|
"Run some price update operations to generate logs"
|
|
);
|
|
});
|
|
|
|
test("should handle error state correctly", async () => {
|
|
// Mock error in getLogFiles
|
|
const mockUseServices =
|
|
require("../../../../src/tui/hooks/useServices.js").useServices;
|
|
mockUseServices.mockReturnValue({
|
|
getLogFiles: jest
|
|
.fn()
|
|
.mockRejectedValue(new Error("Failed to read directory")),
|
|
readLogFile: jest.fn(),
|
|
});
|
|
|
|
const { lastFrame, rerender } = render(React.createElement(ViewLogsScreen));
|
|
|
|
// Wait for the component to handle error
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
rerender(React.createElement(ViewLogsScreen));
|
|
|
|
const output = lastFrame();
|
|
expect(output).toContain(
|
|
"Error: Failed to discover log files: Failed to read directory"
|
|
);
|
|
});
|
|
|
|
test("should meet task requirements", () => {
|
|
// Verify that the component meets the task requirements:
|
|
// - Create ViewLogsScreen component with log file list view ✓
|
|
// - Implement keyboard navigation for log file selection ✓ (useInput hook)
|
|
// - Add state management for log files, selected file, and content ✓ (useState hooks)
|
|
// - Integrate with LogService to discover and list available log files ✓ (useServices hook)
|
|
// - Display log file metadata (size, creation date, operation count) ✓
|
|
|
|
const componentCode = require("fs").readFileSync(
|
|
require("path").join(
|
|
__dirname,
|
|
"../../../../src/tui/components/screens/ViewLogsScreen.jsx"
|
|
),
|
|
"utf8"
|
|
);
|
|
|
|
// Check for required elements
|
|
expect(componentCode).toContain("ViewLogsScreen");
|
|
expect(componentCode).toContain("useInput");
|
|
expect(componentCode).toContain("useState");
|
|
expect(componentCode).toContain("getLogFiles");
|
|
expect(componentCode).toContain("readLogFile");
|
|
expect(componentCode).toContain("formatFileSize");
|
|
expect(componentCode).toContain("formatDate");
|
|
expect(componentCode).toContain("operationCount");
|
|
expect(componentCode).toContain("keyboard navigation");
|
|
});
|
|
});
|