/** * Tests for the safe-delete command (src/cli/commands/safe-delete.ts). * * Tests operate against the pure helpers and status detection logic. * All tests use HOME/SHELL overrides and tmp directories — no mutation of * the real developer shell profile. */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import * as os from 'os'; import * as path from 'path'; import * as fsSync from 'fs'; import { promises as fs } from 'fs'; import { getSafeDeleteStatus, type SafeDeleteStatus, } from '../src/cli/commands/safe-delete.js'; import { getInstalledVersion, installToProfile, removeFromProfile, generateSafeDeleteBlock, SAFE_DELETE_BLOCK_VERSION, } from '../src/core/safe-delete-install.js'; import { detectShell, getProfilePath, hasSafeDelete, } from '../src/core/safe-delete.js'; // ─── helpers ───────────────────────────────────────────────────────────────── function makeTmpDir(): string { return fsSync.mkdtempSync(path.join(os.tmpdir(), 'devflow-sd-test-')); } async function cleanup(dir: string): Promise { await fs.rm(dir, { recursive: true, force: true }); } // ─── getSafeDeleteStatus ───────────────────────────────────────────────────── describe('getSafeDeleteStatus', () => { beforeEach(() => { vi.unstubAllEnvs(); vi.restoreAllMocks(); }); afterEach(() => { vi.unstubAllEnvs(); vi.restoreAllMocks(); }); it('returns unknown when $SHELL is unset', async () => { vi.stubEnv('SHELL', ''); vi.stubEnv('PSModulePath', ''); const { status, profilePath } = await getSafeDeleteStatus(); expect(status).toBe('unknown'); expect(profilePath).toBeNull(); }); it('returns absent when profile has no block installed', async () => { const tmpDir = makeTmpDir(); try { vi.stubEnv('HOME', tmpDir); vi.stubEnv('SHELL', '/bin/zsh'); vi.stubEnv('PSModulePath', ''); // Profile file doesn't exist → absent const { status } = await getSafeDeleteStatus(); expect(status).toBe('absent'); } finally { await cleanup(tmpDir); } }); it('returns installed when block is at current version', async () => { const tmpDir = makeTmpDir(); try { vi.stubEnv('HOME', tmpDir); vi.stubEnv('SHELL', '/bin/zsh'); vi.stubEnv('PSModulePath', ''); const profilePath = path.join(tmpDir, '.zshrc'); const block = generateSafeDeleteBlock('zsh', 'darwin', 'trash'); expect(block).not.toBeNull(); await installToProfile(profilePath, block!); const { status } = await getSafeDeleteStatus(); expect(status).toBe('installed'); } finally { await cleanup(tmpDir); } }); it('returns outdated when block is at an older version', async () => { const tmpDir = makeTmpDir(); try { vi.stubEnv('HOME', tmpDir); vi.stubEnv('SHELL', '/bin/zsh'); vi.stubEnv('PSModulePath', ''); const profilePath = path.join(tmpDir, '.zshrc'); // Write a v1 block manually (no version stamp = v1 per getInstalledVersion) const legacyBlock = '# >>> Devflow safe-delete >>>\nrm() { trash "$@"; }\n# <<< Devflow safe-delete <<<'; await fs.writeFile(profilePath, legacyBlock, 'utf-8'); const version = await getInstalledVersion(profilePath); expect(version).toBe(1); // sanity-check: reads as v1 const { status } = await getSafeDeleteStatus(); expect(status).toBe('outdated'); } finally { await cleanup(tmpDir); } }); it('never crashes — returns unknown on internal error', async () => { // Stub getProfilePath to throw vi.stubEnv('SHELL', '/bin/zsh'); vi.stubEnv('PSModulePath', ''); vi.stubEnv('HOME', '/nonexistent/__devflow_test__/'); // forces file read error, still returns absent or unknown const { status } = await getSafeDeleteStatus(); // Either absent (ENOENT is swallowed by getInstalledVersion) or unknown expect(['absent', 'unknown']).toContain(status); }); }); // ─── enable — no-trash-CLI guard ───────────────────────────────────────────── describe('hasSafeDelete (no trash CLI)', () => { beforeEach(() => { vi.restoreAllMocks(); }); afterEach(() => { vi.restoreAllMocks(); }); it('returns true for windows (always has Recycle Bin, no CLI check)', () => { // Windows fast-path never checks for a trash CLI binary. const result = hasSafeDelete('windows'); expect(result).toBe(true); }); }); // ─── enable — upgrade-no-duplicate ─────────────────────────────────────────── describe('upgrade-no-duplicate', () => { it('does not stack blocks — remove then reinstall leaves exactly one block', async () => { const tmpDir = makeTmpDir(); try { const profilePath = path.join(tmpDir, '.zshrc'); // Write a v1 legacy block const legacyBlock = '# >>> Devflow safe-delete >>>\nrm() { trash "$@"; }\n# <<< Devflow safe-delete <<<'; await fs.writeFile(profilePath, legacyBlock + '\n', 'utf-8'); // Simulate upgrade: remove old, install new await removeFromProfile(profilePath); const newBlock = generateSafeDeleteBlock('zsh', 'darwin', 'trash'); expect(newBlock).not.toBeNull(); await installToProfile(profilePath, newBlock!); // Verify only one block marker pair exists const content = await fs.readFile(profilePath, 'utf-8'); const openCount = (content.match(/# >>> Devflow safe-delete >>>/g) ?? []).length; const closeCount = (content.match(/# <<< Devflow safe-delete << { const tmpDir = makeTmpDir(); try { const profilePath = path.join(tmpDir, '.zshrc'); const block = generateSafeDeleteBlock('zsh', 'darwin', 'trash')!; // Install once await installToProfile(profilePath, block); // The command's enable path checks version and no-ops if current — simulate by checking version const v1 = await getInstalledVersion(profilePath); expect(v1).toBe(SAFE_DELETE_BLOCK_VERSION); // Attempting a second install via installToProfile would stack, but the command // guards against this via the version check in the action handler. // Here we only verify the helper itself — the command is tested via getSafeDeleteStatus. const content = await fs.readFile(profilePath, 'utf-8'); const openCount = (content.match(/# >>> Devflow safe-delete >>>/g) ?? []).length; expect(openCount).toBe(1); } finally { await cleanup(tmpDir); } }); }); // ─── disable ───────────────────────────────────────────────────────────────── describe('disable (removeFromProfile)', () => { it('removes only the marker block, preserving other content', async () => { const tmpDir = makeTmpDir(); try { const profilePath = path.join(tmpDir, '.zshrc'); const before = 'export PATH="$PATH:/usr/local/bin"'; const block = generateSafeDeleteBlock('zsh', 'darwin', 'trash')!; await fs.writeFile(profilePath, before + '\n', 'utf-8'); await installToProfile(profilePath, block); await removeFromProfile(profilePath); const content = await fs.readFile(profilePath, 'utf-8'); expect(content).not.toContain('Devflow safe-delete'); expect(content).toContain('export PATH'); } finally { await cleanup(tmpDir); } }); it('returns false and does not crash when block not installed', async () => { const tmpDir = makeTmpDir(); try { const profilePath = path.join(tmpDir, '.zshrc'); await fs.writeFile(profilePath, 'export FOO=bar\n', 'utf-8'); const removed = await removeFromProfile(profilePath); expect(removed).toBe(false); } finally { await cleanup(tmpDir); } }); it('deletes the fish function file when it becomes empty', async () => { const tmpDir = makeTmpDir(); try { const fishDir = path.join(tmpDir, '.config', 'fish', 'functions'); await fs.mkdir(fishDir, { recursive: true }); const profilePath = path.join(fishDir, 'rm.fish'); const block = generateSafeDeleteBlock('fish', 'linux', 'trash-put')!; // Write only the block (no other content → file becomes empty after remove) await fs.writeFile(profilePath, block + '\n', 'utf-8'); await removeFromProfile(profilePath); // File should be deleted when empty const exists = await fs.access(profilePath).then(() => true).catch(() => false); expect(exists).toBe(false); } finally { await cleanup(tmpDir); } }); }); // ─── $SHELL unset → status unknown ─────────────────────────────────────────── describe('$SHELL unset handling', () => { beforeEach(() => { vi.unstubAllEnvs(); }); afterEach(() => { vi.unstubAllEnvs(); }); it('detectShell returns unknown when SHELL is not set', () => { vi.stubEnv('SHELL', ''); vi.stubEnv('PSModulePath', ''); expect(detectShell()).toBe('unknown'); }); it('getProfilePath returns null for unknown shell', () => { expect(getProfilePath('unknown')).toBeNull(); }); it('getSafeDeleteStatus returns unknown when SHELL unset', async () => { vi.stubEnv('SHELL', ''); vi.stubEnv('PSModulePath', ''); const { status, profilePath } = await getSafeDeleteStatus(); expect(status).toBe('unknown'); expect(profilePath).toBeNull(); }); });