Skip to content

Commit

Permalink
fix: getLocalModels returns an empty array if the models folder does …
Browse files Browse the repository at this point in the history
…not exist (#117)

* fix: getLocalModels returns an empty array if the models folder does not exist

* remove duplicate code

* fix windows path
  • Loading branch information
feloy authored Jan 23, 2024
1 parent 8b63ac9 commit 984fe48
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ beforeEach(() => {

test('getLocalModels should return models in local directory', () => {
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
const existsSyncSpy = vi.spyOn(fs, 'existsSync');
existsSyncSpy.mockImplementation((path: string) => {
if (process.platform === 'win32') {
expect(path).toBe('\\home\\user\\aistudio\\models');
} else {
expect(path).toBe('/home/user/aistudio/models');
}
return true;
});
const readdirSyncMock = vi.spyOn(fs, 'readdirSync') as unknown as MockInstance<
[path: string],
string[] | fs.Dirent[]
Expand Down Expand Up @@ -51,3 +60,17 @@ test('getLocalModels should return models in local directory', () => {
},
]);
});

test('getLocalModels should return an empty array if the models folder does not exist', () => {
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
const existsSyncSpy = vi.spyOn(fs, 'existsSync');
existsSyncSpy.mockReturnValue(false);
const manager = new ModelsManager('/home/user/aistudio');
const models = manager.getLocalModels();
expect(models).toEqual([]);
if (process.platform === 'win32') {
expect(existsSyncSpy).toHaveBeenCalledWith('\\home\\user\\aistudio\\models');
} else {
expect(existsSyncSpy).toHaveBeenCalledWith('/home/user/aistudio/models');
}
});
3 changes: 3 additions & 0 deletions packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class ModelsManager {
getLocalModels(): LocalModelInfo[] {
const result: LocalModelInfo[] = [];
const modelsDir = path.join(this.appUserDirectory, 'models');
if (!fs.existsSync(modelsDir)) {
return [];
}
const entries = fs.readdirSync(modelsDir, { withFileTypes: true });
const dirs = entries.filter(dir => dir.isDirectory());
for (const d of dirs) {
Expand Down

0 comments on commit 984fe48

Please sign in to comment.