-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move getLocalModels to ModelsManager
- Loading branch information
Showing
7 changed files
with
132 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { type MockInstance, beforeEach, expect, test, vi } from 'vitest'; | ||
import os from 'os'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { ModelsManager } from './modelsManager'; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
test('getLocalModels should return models in local directory', () => { | ||
vi.spyOn(os, 'homedir').mockReturnValue('/home/user'); | ||
const readdirSyncMock = vi.spyOn(fs, 'readdirSync') as unknown as MockInstance< | ||
[path: string], | ||
string[] | fs.Dirent[] | ||
>; | ||
readdirSyncMock.mockImplementation((dir: string) => { | ||
if (dir.endsWith('model-id-1') || dir.endsWith('model-id-2')) { | ||
const base = path.basename(dir); | ||
return [base + '-model']; | ||
} else { | ||
return [ | ||
{ | ||
isDirectory: () => true, | ||
path: '/home/user/appstudio-dir', | ||
name: 'model-id-1', | ||
}, | ||
{ | ||
isDirectory: () => true, | ||
path: '/home/user/appstudio-dir', | ||
name: 'model-id-2', | ||
}, | ||
{ | ||
isDirectory: () => false, | ||
path: '/home/user/appstudio-dir', | ||
name: 'other-file-should-be-ignored.txt', | ||
}, | ||
] as fs.Dirent[]; | ||
} | ||
}); | ||
const manager = new ModelsManager('/home/user/aistudio'); | ||
const models = manager.getLocalModels(); | ||
expect(models).toEqual([ | ||
{ | ||
id: 'model-id-1', | ||
file: 'model-id-1-model', | ||
}, | ||
{ | ||
id: 'model-id-2', | ||
file: 'model-id-2-model', | ||
}, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { LocalModelInfo } from '@shared/src/models/ILocalModelInfo'; | ||
import fs from 'fs'; | ||
import * as path from 'node:path'; | ||
|
||
export class ModelsManager { | ||
constructor(private appUserDirectory: string) {} | ||
|
||
getLocalModels(): LocalModelInfo[] { | ||
const result: LocalModelInfo[] = []; | ||
const modelsDir = path.join(this.appUserDirectory, 'models'); | ||
const entries = fs.readdirSync(modelsDir, { withFileTypes: true }); | ||
const dirs = entries.filter(dir => dir.isDirectory()); | ||
for (const d of dirs) { | ||
const modelEntries = fs.readdirSync(path.resolve(d.path, d.name)); | ||
if (modelEntries.length !== 1) { | ||
// we support models with one file only for now | ||
continue; | ||
} | ||
result.push({ | ||
id: d.name, | ||
file: modelEntries[0], | ||
}); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.