* feat(tests): add comprehensive unit tests for various utility functions and views - Implement tests for cache utility to ensure proper behavior with no cache and TTL keys. - Add tests for common utilities including string manipulation and path handling. - Introduce tests for directory import functionality to validate file imports and pattern matching. - Create tests for git hash retrieval to handle fallback scenarios. - Develop tests for deprecated got utility to verify response handling and retry logic. - Enhance got utility tests to include request hooks and search parameter handling. - Mock header generator tests to validate user agent handling. - Expand helpers tests to cover current path retrieval and duration parsing. - Implement tests for ofetch utility to ensure proper proxy handling and logging. - Add OpenTelemetry metric tests to validate metric serialization. - Create proxy tests to verify multi-proxy selection and failure handling. - Introduce request rewriter tests to validate fetch and get wrapper functionality. - Add timezone utility tests to handle various input types. - Implement view tests for Atom and RSS rendering to ensure correct output. - Create index view tests to validate debug information display based on configuration. * refactor: remove deprecated got implementation and associated tests * test: expand coverage for api, middleware, and utils
69 lines
2.6 KiB
TypeScript
69 lines
2.6 KiB
TypeScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
|
|
import { directoryImport } from '@/utils/directory-import';
|
|
|
|
const createTempDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'rsshub-dir-import-'));
|
|
|
|
const writeFile = (filePath: string, content: string) => {
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
fs.writeFileSync(filePath, content, 'utf8');
|
|
};
|
|
|
|
describe('directory-import', () => {
|
|
let tempDir = '';
|
|
|
|
afterEach(() => {
|
|
if (tempDir) {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
tempDir = '';
|
|
}
|
|
});
|
|
|
|
it('imports valid files and skips invalid ones', () => {
|
|
tempDir = createTempDir();
|
|
const rootModule = path.join(tempDir, 'valid.cjs');
|
|
const jsonModule = path.join(tempDir, 'data.json');
|
|
const ignoredText = path.join(tempDir, 'note.txt');
|
|
const declaration = path.join(tempDir, 'types.d.ts');
|
|
const nestedModule = path.join(tempDir, 'sub', 'child.cjs');
|
|
|
|
writeFile(rootModule, "module.exports = { value: 'root' };");
|
|
writeFile(jsonModule, JSON.stringify({ value: 'json' }));
|
|
writeFile(ignoredText, 'ignore');
|
|
writeFile(declaration, 'export {};');
|
|
writeFile(nestedModule, "module.exports = { value: 'child' };");
|
|
|
|
const modules = directoryImport({ targetDirectoryPath: tempDir });
|
|
const keyFor = (filePath: string) => filePath.slice(tempDir.length);
|
|
|
|
expect(modules).toHaveProperty(keyFor(rootModule));
|
|
expect(modules).toHaveProperty(keyFor(jsonModule));
|
|
expect(modules).toHaveProperty(keyFor(nestedModule));
|
|
expect(modules).not.toHaveProperty(keyFor(ignoredText));
|
|
expect(modules).not.toHaveProperty(keyFor(declaration));
|
|
});
|
|
|
|
it('can skip subdirectories and apply patterns', () => {
|
|
tempDir = createTempDir();
|
|
const rootModule = path.join(tempDir, 'keep.cjs');
|
|
const nestedModule = path.join(tempDir, 'sub', 'skip.cjs');
|
|
|
|
writeFile(rootModule, "module.exports = { value: 'keep' };");
|
|
writeFile(nestedModule, "module.exports = { value: 'skip' };");
|
|
|
|
const modules = directoryImport({
|
|
targetDirectoryPath: tempDir,
|
|
includeSubdirectories: false,
|
|
importPattern: /keep/,
|
|
});
|
|
const keyFor = (filePath: string) => filePath.slice(tempDir.length);
|
|
|
|
expect(modules).toHaveProperty(keyFor(rootModule));
|
|
expect(modules).not.toHaveProperty(keyFor(nestedModule));
|
|
});
|
|
});
|