import { describe, it, expect, vi, beforeEach } from 'vitest';
import fs from 'fs/promises';
import { handlers } from '../src/handlers/utility.js';
import { setAllowedDirectories } from '../src/lib.js';
// Mock fs
vi.mock('fs/promises');
vi.mock('../logger.js', () => ({
logger: { info: vi.fn() },
}));
const mockFs = fs as any;
describe('file_hash', () => {
beforeEach(() => {
setAllowedDirectories(['/tmp']);
vi.clearAllMocks();
mockFs.realpath.mockImplementation((p: string) => Promise.resolve(p));
});
it('should calculate SHA256 hash by default', async () => {
mockFs.readFile.mockResolvedValue('test content');
const result = await handlers.utility({ operation: 'hash', path: '/tmp/file.txt' });
expect(result.content[0].text).toMatch(/^sha256: [a-f0-9]{64}$/);
});
it('should calculate MD5 hash', async () => {
mockFs.readFile.mockResolvedValue('test content');
const result = await handlers.utility({ operation: 'hash', path: '/tmp/file.txt', algorithm: 'md5' });
expect(result.content[0].text).toMatch(/^md5: [a-f0-9]{32}$/);
});
});