import { describe, it, expect, vi, beforeEach } from 'vitest' const openPath = vi.fn(async () => '') vi.mock('electron', () => ({ shell: { openPath }, app: { getPath: (name: string) => `C:\\Users\\test\\${name}` } })) const writeFileSyncMock = vi.fn() const existsSyncMock = vi.fn(() => false) const mkdirSyncMock = vi.fn() const statSyncMock = vi.fn(() => ({ isDirectory: () => false })) vi.mock('fs', () => ({ writeFileSync: writeFileSyncMock, existsSync: existsSyncMock, mkdirSync: mkdirSyncMock, statSync: statSyncMock })) const execFileMock = vi.fn((_cmd: string, _args: string[], cb: (err: Error | null) => void) => cb(null) ) vi.mock('child_process', () => ({ execFile: execFileMock })) const findFileByName = vi.fn(async (name: string) => name.includes('introuvable') ? null : `C:\\Users\\test\\desktop\\${name}.txt` ) vi.mock('../../src/main/skills/windows/lib/fileSearch', () => ({ findFileByName })) const { createFile } = await import('../../src/main/skills/windows/createFile') const { createFolder } = await import('../../src/main/skills/windows/createFolder') const { deleteFileOrFolder } = await import('../../src/main/skills/windows/deleteFileOrFolder') const { printFile } = await import('../../src/main/skills/windows/printFile') const { searchFile } = await import('../../src/main/skills/windows/searchFile') beforeEach(() => { vi.clearAllMocks() existsSyncMock.mockReturnValue(false) statSyncMock.mockReturnValue({ isDirectory: () => false }) findFileByName.mockImplementation(async (name: string) => name.includes('introuvable') ? null : `C:\\Users\\test\\desktop\\${name}.txt` ) }) describe('create_file', () => { it('should refuse an empty name', async () => { await expect(createFile.execute({ name: ' ' })).rejects.toThrow(/nom manquant/) }) it('should sanitize illegal characters, write the file and open it', async () => { const result = await createFile.execute({ name: 'note', content: 'hello' }) expect(result).toMatch(/Fichier cree et ouvert/) expect(writeFileSyncMock).toHaveBeenCalled() expect(openPath).toHaveBeenCalled() expect(String(writeFileSyncMock.mock.calls[0]?.[0])).not.toMatch(/[<>]/) }) it('should append .txt when the name has no extension', async () => { await createFile.execute({ name: 'sansextension' }) expect(String(writeFileSyncMock.mock.calls[0]?.[0])).toMatch(/sansextension\.txt$/) }) }) describe('create_folder', () => { it('should refuse an empty name', async () => { await expect(createFolder.execute({ name: '' })).rejects.toThrow(/nom manquant/) }) it('should report when the folder already exists without creating it again', async () => { existsSyncMock.mockReturnValue(true) const result = await createFolder.execute({ name: 'existant' }) expect(result).toMatch(/existe deja/) expect(mkdirSyncMock).not.toHaveBeenCalled() }) it('should create and open a new folder', async () => { const result = await createFolder.execute({ name: 'nouveau' }) expect(result).toMatch(/Dossier cree et ouvert/) expect(mkdirSyncMock).toHaveBeenCalled() expect(openPath).toHaveBeenCalled() }) }) describe('delete_file', () => { it('should refuse an empty name', async () => { await expect(deleteFileOrFolder.execute({ name: '' })).rejects.toThrow(/nom manquant/) }) it('should report when nothing is found', async () => { const result = await deleteFileOrFolder.execute({ name: 'introuvable' }) expect(result).toMatch(/Aucun fichier ou dossier/) }) it('should refuse to delete a protected location (drive root)', async () => { findFileByName.mockResolvedValue('C:\\') const result = await deleteFileOrFolder.execute({ name: 'quelquechose' }) expect(result).toMatch(/protege/) expect(execFileMock).not.toHaveBeenCalled() }) it('should send a found file to the recycle bin', async () => { const result = await deleteFileOrFolder.execute({ name: 'document' }) expect(result).toMatch(/corbeille/) expect(execFileMock).toHaveBeenCalled() }) }) describe('print_file', () => { it('should refuse an empty name', async () => { await expect(printFile.execute({ name: '' })).rejects.toThrow(/nom manquant/) }) it('should report when no file is found', async () => { const result = await printFile.execute({ name: 'introuvable' }) expect(result).toMatch(/Aucun fichier/) }) it('should launch printing for a found file', async () => { const result = await printFile.execute({ name: 'rapport' }) expect(result).toMatch(/Impression lancee/) expect(execFileMock).toHaveBeenCalled() }) }) describe('search_file', () => { it('should refuse an empty name', async () => { await expect(searchFile.execute({ name: '' })).rejects.toThrow(/nom manquant/) }) it('should report when nothing is found', async () => { const result = await searchFile.execute({ name: 'introuvable' }) expect(result).toMatch(/Aucun fichier ou dossier/) }) it('should find and open a matching file', async () => { const result = await searchFile.execute({ name: 'photo' }) expect(result).toMatch(/Trouve et ouvert/) expect(openPath).toHaveBeenCalled() }) })