Files
ForgeBase/Max Avatar/tests/unit/skills.windowsFileOps.test.ts
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +01:00

141 lines
5.1 KiB
TypeScript

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<test>', 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()
})
})