mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add backend unit test framework and modularize settings UI
Introduced a Vitest-based backend unit testing framework with supporting scripts, helpers, and GitHub Actions integration. Refactored the admin settings page to a modular architecture, splitting monolithic logic into feature-specific tabs and hooks for improved maintainability and testability. Updated documentation to reflect the new testing setup and settings architecture, and added new dependencies for testing utilities.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Component: Paths Settings Tab - Custom Hook
|
||||
* Documentation: documentation/settings-pages.md
|
||||
*/
|
||||
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { PathsSettings, TestResult } from '../../lib/types';
|
||||
|
||||
interface UsePathsSettingsProps {
|
||||
paths: PathsSettings;
|
||||
onChange: (paths: PathsSettings) => void;
|
||||
onValidationChange: (isValid: boolean) => void;
|
||||
}
|
||||
|
||||
export function usePathsSettings({ paths, onChange, onValidationChange }: UsePathsSettingsProps) {
|
||||
const [testing, setTesting] = useState(false);
|
||||
const [testResult, setTestResult] = useState<TestResult | null>(null);
|
||||
|
||||
/**
|
||||
* Update a single path field
|
||||
*/
|
||||
const updatePath = (field: keyof PathsSettings, value: string | boolean) => {
|
||||
onChange({ ...paths, [field]: value });
|
||||
onValidationChange(false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Test if paths are valid and writable
|
||||
*/
|
||||
const testPaths = async () => {
|
||||
setTesting(true);
|
||||
setTestResult(null);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/setup/test-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
downloadDir: paths.downloadDir,
|
||||
mediaDir: paths.mediaDir,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
const result: TestResult = {
|
||||
success: true,
|
||||
message: 'All paths are valid and writable'
|
||||
};
|
||||
setTestResult(result);
|
||||
onValidationChange(true);
|
||||
return result;
|
||||
} else {
|
||||
const result: TestResult = {
|
||||
success: false,
|
||||
message: data.error || 'Path validation failed'
|
||||
};
|
||||
setTestResult(result);
|
||||
onValidationChange(false);
|
||||
return result;
|
||||
}
|
||||
} catch (error) {
|
||||
const result: TestResult = {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to test paths'
|
||||
};
|
||||
setTestResult(result);
|
||||
onValidationChange(false);
|
||||
return result;
|
||||
} finally {
|
||||
setTesting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
testing,
|
||||
testResult,
|
||||
updatePath,
|
||||
testPaths,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user