don't return empty profile, instead throw

This commit is contained in:
DaKheera47 2026-01-21 16:14:30 +00:00
parent 6bd1593e13
commit cae54c39fb
2 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,32 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { readFile } from 'fs/promises';
import { getProfile } from './profile.js';
vi.mock('fs/promises', async () => {
const fn = vi.fn();
return {
readFile: fn,
default: {
readFile: fn
}
};
});
describe('getProfile failure', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('should throw an error if the profile file does not exist', async () => {
vi.mocked(readFile).mockRejectedValue(new Error('ENOENT: no such file or directory'));
await expect(getProfile('/non/existent/path.json', true)).rejects.toThrow('ENOENT: no such file or directory');
});
it('should throw an error if the profile file is invalid JSON', async () => {
vi.mocked(readFile).mockResolvedValue('invalid json');
await expect(getProfile('/invalid/json.json', true)).rejects.toThrow();
});
});

View File

@ -28,7 +28,7 @@ export async function getProfile(profilePath?: string, forceRefresh = false): Pr
return cachedProfile;
} catch (error) {
console.error(`❌ Failed to load profile from ${targetPath}:`, error);
return {};
throw error;
}
}