"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const path_1 = require("path");
const fs_1 = require("fs");
const file_ops_js_1 = require("./file-ops.js");
const TEST_DIR = (0, path_1.resolve)(process.cwd(), 'data', '.test-file-ops');
(0, vitest_1.beforeEach)(() => {
    (0, fs_1.mkdirSync)(TEST_DIR, { recursive: true });
});
(0, vitest_1.afterEach)(() => {
    (0, fs_1.rmSync)(TEST_DIR, { recursive: true, force: true });
});
(0, vitest_1.describe)('atomicWriteJSON', () => {
    (0, vitest_1.it)('should write JSON data to a file', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'test.json');
        const data = { name: 'test', value: 42 };
        await (0, file_ops_js_1.atomicWriteJSON)(filePath, data);
        (0, vitest_1.expect)((0, fs_1.existsSync)(filePath)).toBe(true);
        const content = JSON.parse((0, fs_1.readFileSync)(filePath, 'utf-8'));
        (0, vitest_1.expect)(content).toEqual(data);
    });
    (0, vitest_1.it)('should create parent directories if they do not exist', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'nested', 'deep', 'test.json');
        await (0, file_ops_js_1.atomicWriteJSON)(filePath, { ok: true });
        (0, vitest_1.expect)((0, fs_1.existsSync)(filePath)).toBe(true);
    });
    (0, vitest_1.it)('should overwrite existing file', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'overwrite.json');
        await (0, file_ops_js_1.atomicWriteJSON)(filePath, { v: 1 });
        await (0, file_ops_js_1.atomicWriteJSON)(filePath, { v: 2 });
        const content = JSON.parse((0, fs_1.readFileSync)(filePath, 'utf-8'));
        (0, vitest_1.expect)(content).toEqual({ v: 2 });
    });
    (0, vitest_1.it)('should not leave temp files on success', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'clean.json');
        await (0, file_ops_js_1.atomicWriteJSON)(filePath, { clean: true });
        const files = require('fs').readdirSync(TEST_DIR);
        const tmpFiles = files.filter((f) => f.endsWith('.tmp'));
        (0, vitest_1.expect)(tmpFiles).toHaveLength(0);
    });
});
(0, vitest_1.describe)('readJSON', () => {
    (0, vitest_1.it)('should read and parse a valid JSON file', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'read.json');
        (0, fs_1.writeFileSync)(filePath, JSON.stringify({ hello: 'world' }), 'utf-8');
        const result = await (0, file_ops_js_1.readJSON)(filePath);
        (0, vitest_1.expect)(result).toEqual({ hello: 'world' });
    });
    (0, vitest_1.it)('should return null for non-existent file', async () => {
        const result = await (0, file_ops_js_1.readJSON)((0, path_1.join)(TEST_DIR, 'nope.json'));
        (0, vitest_1.expect)(result).toBeNull();
    });
    (0, vitest_1.it)('should return null for invalid JSON', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'bad.json');
        (0, fs_1.writeFileSync)(filePath, 'not valid json {{{', 'utf-8');
        const result = await (0, file_ops_js_1.readJSON)(filePath);
        (0, vitest_1.expect)(result).toBeNull();
    });
});
(0, vitest_1.describe)('deleteFile', () => {
    (0, vitest_1.it)('should delete an existing file and return true', async () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'delete-me.json');
        (0, fs_1.writeFileSync)(filePath, '{}', 'utf-8');
        const result = await (0, file_ops_js_1.deleteFile)(filePath);
        (0, vitest_1.expect)(result).toBe(true);
        (0, vitest_1.expect)((0, fs_1.existsSync)(filePath)).toBe(false);
    });
    (0, vitest_1.it)('should return false for non-existent file', async () => {
        const result = await (0, file_ops_js_1.deleteFile)((0, path_1.join)(TEST_DIR, 'ghost.json'));
        (0, vitest_1.expect)(result).toBe(false);
    });
});
(0, vitest_1.describe)('ensureDir', () => {
    (0, vitest_1.it)('should create a directory recursively', async () => {
        const dirPath = (0, path_1.join)(TEST_DIR, 'a', 'b', 'c');
        await (0, file_ops_js_1.ensureDir)(dirPath);
        (0, vitest_1.expect)((0, fs_1.existsSync)(dirPath)).toBe(true);
    });
    (0, vitest_1.it)('should not throw if directory already exists', async () => {
        await (0, file_ops_js_1.ensureDir)(TEST_DIR);
        await (0, vitest_1.expect)((0, file_ops_js_1.ensureDir)(TEST_DIR)).resolves.toBeUndefined();
    });
});
(0, vitest_1.describe)('fileExists', () => {
    (0, vitest_1.it)('should return true for existing file', () => {
        const filePath = (0, path_1.join)(TEST_DIR, 'exists.txt');
        (0, fs_1.writeFileSync)(filePath, 'hi', 'utf-8');
        (0, vitest_1.expect)((0, file_ops_js_1.fileExists)(filePath)).toBe(true);
    });
    (0, vitest_1.it)('should return false for non-existent file', () => {
        (0, vitest_1.expect)((0, file_ops_js_1.fileExists)((0, path_1.join)(TEST_DIR, 'nope.txt'))).toBe(false);
    });
});
(0, vitest_1.describe)('listFiles', () => {
    (0, vitest_1.it)('should list all files in a directory', async () => {
        (0, fs_1.writeFileSync)((0, path_1.join)(TEST_DIR, 'a.json'), '{}');
        (0, fs_1.writeFileSync)((0, path_1.join)(TEST_DIR, 'b.txt'), 'hi');
        const files = await (0, file_ops_js_1.listFiles)(TEST_DIR);
        (0, vitest_1.expect)(files).toContain('a.json');
        (0, vitest_1.expect)(files).toContain('b.txt');
    });
    (0, vitest_1.it)('should filter by extension', async () => {
        const subDir = (0, path_1.join)(TEST_DIR, 'filter-test');
        (0, fs_1.mkdirSync)(subDir, { recursive: true });
        (0, fs_1.writeFileSync)((0, path_1.join)(subDir, 'a.json'), '{}');
        (0, fs_1.writeFileSync)((0, path_1.join)(subDir, 'b.txt'), 'hi');
        const files = await (0, file_ops_js_1.listFiles)(subDir, '.json');
        (0, vitest_1.expect)(files).toEqual(['a.json']);
    });
    (0, vitest_1.it)('should return empty array for non-existent directory', async () => {
        const files = await (0, file_ops_js_1.listFiles)((0, path_1.join)(TEST_DIR, 'nope'));
        (0, vitest_1.expect)(files).toEqual([]);
    });
});
(0, vitest_1.describe)('listDirectories', () => {
    (0, vitest_1.it)('should list only subdirectories', async () => {
        const subDir = (0, path_1.join)(TEST_DIR, 'dirs-test');
        (0, fs_1.mkdirSync)(subDir, { recursive: true });
        (0, fs_1.mkdirSync)((0, path_1.join)(subDir, 'subdir1'), { recursive: true });
        (0, fs_1.mkdirSync)((0, path_1.join)(subDir, 'subdir2'), { recursive: true });
        (0, fs_1.writeFileSync)((0, path_1.join)(subDir, 'file.txt'), 'hi');
        const dirs = await (0, file_ops_js_1.listDirectories)(subDir);
        (0, vitest_1.expect)(dirs).toContain('subdir1');
        (0, vitest_1.expect)(dirs).toContain('subdir2');
        (0, vitest_1.expect)(dirs).not.toContain('file.txt');
    });
    (0, vitest_1.it)('should return empty array for non-existent directory', async () => {
        const dirs = await (0, file_ops_js_1.listDirectories)((0, path_1.join)(TEST_DIR, 'nope'));
        (0, vitest_1.expect)(dirs).toEqual([]);
    });
});
//# sourceMappingURL=file-ops.test.js.map