From 2d75a15e369a3abd78f27999bcfa590d3205eab1 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 23 Jan 2024 11:28:14 +0100 Subject: [PATCH 1/3] fix: getLocalModels returns an empty array if the models folder does not exist --- .../backend/src/managers/modelsManager.spec.ts | 18 ++++++++++++++++++ packages/backend/src/managers/modelsManager.ts | 3 +++ 2 files changed, 21 insertions(+) diff --git a/packages/backend/src/managers/modelsManager.spec.ts b/packages/backend/src/managers/modelsManager.spec.ts index 7b5778ddc..c92b833c6 100644 --- a/packages/backend/src/managers/modelsManager.spec.ts +++ b/packages/backend/src/managers/modelsManager.spec.ts @@ -10,6 +10,11 @@ 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) => { + expect(path).toBe('/home/user/aistudio/models'); + return true; + }); const readdirSyncMock = vi.spyOn(fs, 'readdirSync') as unknown as MockInstance< [path: string], string[] | fs.Dirent[] @@ -51,3 +56,16 @@ 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.mockImplementation((path: string) => { + expect(path).toBe('/home/user/aistudio/models'); + return false; + }); + const manager = new ModelsManager('/home/user/aistudio'); + const models = manager.getLocalModels(); + expect(models).toEqual([]); + expect(existsSyncSpy).toHaveBeenCalledWith('/home/user/aistudio/models'); +}); diff --git a/packages/backend/src/managers/modelsManager.ts b/packages/backend/src/managers/modelsManager.ts index c69e3d97e..32ac7d3bf 100644 --- a/packages/backend/src/managers/modelsManager.ts +++ b/packages/backend/src/managers/modelsManager.ts @@ -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) { From 515bf198dc30c0ff6765162cc963797dfc68c05c Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 23 Jan 2024 12:01:47 +0100 Subject: [PATCH 2/3] remove duplicate code --- packages/backend/src/managers/modelsManager.spec.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/backend/src/managers/modelsManager.spec.ts b/packages/backend/src/managers/modelsManager.spec.ts index c92b833c6..17ecfb74e 100644 --- a/packages/backend/src/managers/modelsManager.spec.ts +++ b/packages/backend/src/managers/modelsManager.spec.ts @@ -60,10 +60,7 @@ 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.mockImplementation((path: string) => { - expect(path).toBe('/home/user/aistudio/models'); - return false; - }); + existsSyncSpy.mockReturnValue(false); const manager = new ModelsManager('/home/user/aistudio'); const models = manager.getLocalModels(); expect(models).toEqual([]); From 6dc378f16d01badb933c0d4f5d1ba3b5bd0296b3 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 23 Jan 2024 12:03:32 +0100 Subject: [PATCH 3/3] fix windows path --- packages/backend/src/managers/modelsManager.spec.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/managers/modelsManager.spec.ts b/packages/backend/src/managers/modelsManager.spec.ts index 17ecfb74e..fbf0ea0a6 100644 --- a/packages/backend/src/managers/modelsManager.spec.ts +++ b/packages/backend/src/managers/modelsManager.spec.ts @@ -12,7 +12,11 @@ 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) => { - expect(path).toBe('/home/user/aistudio/models'); + 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< @@ -64,5 +68,9 @@ test('getLocalModels should return an empty array if the models folder does not const manager = new ModelsManager('/home/user/aistudio'); const models = manager.getLocalModels(); expect(models).toEqual([]); - expect(existsSyncSpy).toHaveBeenCalledWith('/home/user/aistudio/models'); + if (process.platform === 'win32') { + expect(existsSyncSpy).toHaveBeenCalledWith('\\home\\user\\aistudio\\models'); + } else { + expect(existsSyncSpy).toHaveBeenCalledWith('/home/user/aistudio/models'); + } });