UNPKG

@factorialco/shadowdog

Version:

<img src="https://raw.githubusercontent.com/factorialco/shadowdog/refs/heads/main/logo.png" alt="drawing" width="100"/>

144 lines (143 loc) 5.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const fs_1 = __importDefault(require("fs")); const utils_1 = require("./utils"); // Mock fs module first to ensure all fs operations go through our mock vitest_1.vi.mock('fs', () => { // Create a mock implementation for statSync const statSync = vitest_1.vi.fn((filePath) => { // If it's package.json, return that it's a file if (filePath.endsWith('package.json')) { return { isFile: () => true, isDirectory: () => false, isSymbolicLink: () => false, isBlockDevice: () => false, isCharacterDevice: () => false, isFIFO: () => false, isSocket: () => false, dev: 0, ino: 0, mode: 0, nlink: 0, uid: 0, gid: 0, rdev: 0, size: 0, blksize: 0, blocks: 0, atimeMs: 0, mtimeMs: 0, ctimeMs: 0, birthtimeMs: 0, atime: new Date(), mtime: new Date(), ctime: new Date(), birthtime: new Date(), }; } // For other files, verify we're getting the expected paths (0, vitest_1.expect)(filePath).toMatch(/^\/mock\/working\/directory\/path\/to\/file.*\.txt$/); return { isFile: () => true, isDirectory: () => false, isSymbolicLink: () => false, isBlockDevice: () => false, isCharacterDevice: () => false, isFIFO: () => false, isSocket: () => false, dev: 0, ino: 0, mode: 0, nlink: 0, uid: 0, gid: 0, rdev: 0, size: 0, blksize: 0, blocks: 0, atimeMs: 0, mtimeMs: 0, ctimeMs: 0, birthtimeMs: 0, atime: new Date(), mtime: new Date(), ctime: new Date(), birthtime: new Date(), }; }); // Create a mock implementation for readFileSync const readFileSync = vitest_1.vi.fn((filePath) => { // If it's package.json, return a minimal JSON with version if (filePath.endsWith('package.json')) { return JSON.stringify({ version: '1.0.0' }); } // For other files, verify we're getting the expected paths (0, vitest_1.expect)(filePath).toMatch(/^\/mock\/working\/directory\/path\/to\/file.*\.txt$/); return 'same content'; }); // Create the mock object with a default export const mockFs = { statSync, readFileSync, // Import other fs functions we don't want to mock ...vitest_1.vi.importActual('fs'), }; // Return both the default export and the named exports return { default: mockFs, ...mockFs, }; }); // Mock glob to return the exact pattern as a single file vitest_1.vi.mock('glob', () => ({ sync: (pattern) => [pattern], })); (0, vitest_1.describe)('computeCache', () => { (0, vitest_1.beforeEach)(() => { vitest_1.vi.stubEnv('TEST_ENV', 'test_value'); // Mock process.cwd() to return a fixed path vitest_1.vi.spyOn(process, 'cwd').mockReturnValue('/mock/working/directory'); }); (0, vitest_1.afterEach)(() => { vitest_1.vi.unstubAllEnvs(); vitest_1.vi.clearAllMocks(); }); (0, vitest_1.it)('includes file paths in the cache key', () => { const filePath1 = 'path/to/file1.txt'; const filePath2 = 'path/to/file2.txt'; // First call with file1 const cacheKey1 = (0, utils_1.computeCache)([filePath1], ['TEST_ENV'], 'test command'); // Second call with file2 (same content, different path) const cacheKey2 = (0, utils_1.computeCache)([filePath2], ['TEST_ENV'], 'test command'); // The cache keys should be different because the file paths are different (0, vitest_1.expect)(cacheKey1).not.toBe(cacheKey2); // Verify that both the file path and content were used in the hash (0, vitest_1.expect)(fs_1.default.readFileSync).toHaveBeenCalledWith('/mock/working/directory/path/to/file1.txt', 'utf-8'); (0, vitest_1.expect)(fs_1.default.readFileSync).toHaveBeenCalledWith('/mock/working/directory/path/to/file2.txt', 'utf-8'); }); (0, vitest_1.it)('includes environment variables in the cache key', () => { const filePath = 'path/to/file.txt'; // First call with TEST_ENV=value1 vitest_1.vi.stubEnv('TEST_ENV', 'value1'); const cacheKey1 = (0, utils_1.computeCache)([filePath], ['TEST_ENV'], 'test command'); // Second call with TEST_ENV=value2 vitest_1.vi.stubEnv('TEST_ENV', 'value2'); const cacheKey2 = (0, utils_1.computeCache)([filePath], ['TEST_ENV'], 'test command'); // The cache keys should be different because the environment variable changed (0, vitest_1.expect)(cacheKey1).not.toBe(cacheKey2); }); (0, vitest_1.it)('includes command in the cache key', () => { const filePath = 'path/to/file.txt'; // First call with command1 const cacheKey1 = (0, utils_1.computeCache)([filePath], ['TEST_ENV'], 'command1'); // Second call with command2 const cacheKey2 = (0, utils_1.computeCache)([filePath], ['TEST_ENV'], 'command2'); // The cache keys should be different because the commands are different (0, vitest_1.expect)(cacheKey1).not.toBe(cacheKey2); }); });