2025-08-14 14:27:23 +01:00
|
|
|
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
|
|
|
import { renderHook } from '@testing-library/react';
|
|
|
|
import { useChangePermissionsOperation } from './useChangePermissionsOperation';
|
|
|
|
import type { ChangePermissionsParameters } from './useChangePermissionsParameters';
|
|
|
|
|
|
|
|
// Mock the useToolOperation hook
|
2025-08-27 14:51:52 +01:00
|
|
|
vi.mock('../shared/useToolOperation', async () => {
|
|
|
|
const actual = await vi.importActual('../shared/useToolOperation'); // Need to keep ToolType etc.
|
|
|
|
return {
|
|
|
|
...actual,
|
|
|
|
useToolOperation: vi.fn()
|
|
|
|
};
|
|
|
|
});
|
2025-08-14 14:27:23 +01:00
|
|
|
|
|
|
|
// Mock the translation hook
|
|
|
|
const mockT = vi.fn((key: string) => `translated-${key}`);
|
|
|
|
vi.mock('react-i18next', () => ({
|
|
|
|
useTranslation: () => ({ t: mockT })
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Mock the error handler
|
|
|
|
vi.mock('../../../utils/toolErrorHandler', () => ({
|
|
|
|
createStandardErrorHandler: vi.fn(() => 'error-handler-function')
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Import the mocked function
|
2025-08-27 14:51:52 +01:00
|
|
|
import { SingleFileToolOperationConfig, ToolOperationHook, ToolType, useToolOperation } from '../shared/useToolOperation';
|
2025-08-14 14:27:23 +01:00
|
|
|
|
|
|
|
describe('useChangePermissionsOperation', () => {
|
|
|
|
const mockUseToolOperation = vi.mocked(useToolOperation);
|
|
|
|
|
2025-08-27 14:51:52 +01:00
|
|
|
const getToolConfig = () => mockUseToolOperation.mock.calls[0][0] as SingleFileToolOperationConfig<ChangePermissionsParameters>;
|
2025-08-14 14:27:23 +01:00
|
|
|
|
|
|
|
const mockToolOperationReturn: ToolOperationHook<unknown> = {
|
|
|
|
files: [],
|
|
|
|
thumbnails: [],
|
|
|
|
downloadUrl: null,
|
|
|
|
downloadFilename: '',
|
|
|
|
isLoading: false,
|
|
|
|
errorMessage: null,
|
|
|
|
status: '',
|
|
|
|
isGeneratingThumbnails: false,
|
|
|
|
progress: null,
|
|
|
|
executeOperation: vi.fn(),
|
|
|
|
resetResults: vi.fn(),
|
|
|
|
clearError: vi.fn(),
|
|
|
|
cancelOperation: vi.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
vi.clearAllMocks();
|
|
|
|
mockUseToolOperation.mockReturnValue(mockToolOperationReturn);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
{
|
|
|
|
preventAssembly: false,
|
|
|
|
preventExtractContent: false,
|
|
|
|
preventExtractForAccessibility: false,
|
|
|
|
preventFillInForm: false,
|
|
|
|
preventModify: false,
|
|
|
|
preventModifyAnnotations: false,
|
|
|
|
preventPrinting: false,
|
|
|
|
preventPrintingFaithful: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preventAssembly: true,
|
|
|
|
preventExtractContent: false,
|
|
|
|
preventExtractForAccessibility: true,
|
|
|
|
preventFillInForm: false,
|
|
|
|
preventModify: true,
|
|
|
|
preventModifyAnnotations: false,
|
|
|
|
preventPrinting: true,
|
|
|
|
preventPrintingFaithful: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preventAssembly: true,
|
|
|
|
preventExtractContent: true,
|
|
|
|
preventExtractForAccessibility: true,
|
|
|
|
preventFillInForm: true,
|
|
|
|
preventModify: true,
|
|
|
|
preventModifyAnnotations: true,
|
|
|
|
preventPrinting: true,
|
|
|
|
preventPrintingFaithful: true,
|
|
|
|
},
|
|
|
|
])('should create form data correctly', (testParameters: ChangePermissionsParameters) => {
|
|
|
|
renderHook(() => useChangePermissionsOperation());
|
|
|
|
|
|
|
|
const callArgs = getToolConfig();
|
|
|
|
const buildFormData = callArgs.buildFormData;
|
|
|
|
|
|
|
|
const testFile = new File(['test content'], 'test.pdf', { type: 'application/pdf' });
|
2025-08-27 14:51:52 +01:00
|
|
|
const formData = buildFormData(testParameters, testFile);
|
2025-08-14 14:27:23 +01:00
|
|
|
|
|
|
|
// Verify the form data contains the file
|
|
|
|
expect(formData.get('fileInput')).toBe(testFile);
|
|
|
|
|
|
|
|
(Object.keys(testParameters) as Array<keyof ChangePermissionsParameters>).forEach(key => {
|
|
|
|
expect(formData.get(key), `Parameter ${key} should be set correctly`).toBe(testParameters[key].toString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use correct translation for error messages', () => {
|
|
|
|
renderHook(() => useChangePermissionsOperation());
|
|
|
|
|
|
|
|
expect(mockT).toHaveBeenCalledWith(
|
|
|
|
'changePermissions.error.failed',
|
|
|
|
'An error occurred while changing PDF permissions.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
2025-08-27 14:51:52 +01:00
|
|
|
{ property: 'toolType' as const, expectedValue: ToolType.singleFile },
|
2025-08-14 14:27:23 +01:00
|
|
|
{ property: 'endpoint' as const, expectedValue: '/api/v1/security/add-password' },
|
|
|
|
{ property: 'filePrefix' as const, expectedValue: 'permissions_' },
|
2025-08-22 14:40:27 +01:00
|
|
|
{ property: 'operationType' as const, expectedValue: 'change-permissions' }
|
2025-08-14 14:27:23 +01:00
|
|
|
])('should configure $property correctly', ({ property, expectedValue }) => {
|
|
|
|
renderHook(() => useChangePermissionsOperation());
|
|
|
|
|
|
|
|
const callArgs = getToolConfig();
|
|
|
|
expect(callArgs[property]).toBe(expectedValue);
|
|
|
|
});
|
|
|
|
});
|