test: add tests to legacy model-json utilities

This commit is contained in:
Louis 2024-10-21 18:31:16 +07:00
parent 895c3d4246
commit 5edf121d96
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
2 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,80 @@
import { scanModelsFolder, getModelJsonPath } from './model-json'
// Mock the @janhq/core module
jest.mock('@janhq/core', () => ({
fs: {
existsSync: jest.fn(),
readdirSync: jest.fn(),
fileStat: jest.fn(),
readFileSync: jest.fn(),
},
joinPath: jest.fn((paths) => paths.join('/')),
}))
// Import the mocked fs and joinPath after the mock is set up
const { fs } = jest.requireMock('@janhq/core')
describe('model-json', () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe('scanModelsFolder', () => {
it('should return an empty array when models folder does not exist', async () => {
fs.existsSync.mockReturnValue(false)
const result = await scanModelsFolder()
expect(result).toEqual([])
})
it('should return an array of models when valid model folders exist', async () => {
const mockModelJson = {
id: 'test-model',
sources: [
{
filename: 'test-model',
url: 'file://models/test-model/test-model.gguf',
},
],
}
fs.existsSync.mockReturnValue(true)
fs.readdirSync.mockReturnValueOnce(['test-model'])
fs.fileStat.mockResolvedValue({ isDirectory: () => true })
fs.readFileSync.mockReturnValue(JSON.stringify(mockModelJson))
fs.readdirSync.mockReturnValueOnce(['test-model.gguf', 'model.json'])
const result = await scanModelsFolder()
expect(result).toHaveLength(1)
expect(result[0]).toMatchObject(mockModelJson)
})
})
describe('getModelJsonPath', () => {
it('should return undefined when folder does not exist', async () => {
fs.existsSync.mockReturnValue(false)
const result = await getModelJsonPath('non-existent-folder')
expect(result).toBeUndefined()
})
it('should return the path when model.json exists in the root folder', async () => {
fs.existsSync.mockReturnValue(true)
fs.readdirSync.mockReturnValue(['model.json'])
const result = await getModelJsonPath('test-folder')
expect(result).toBe('test-folder/model.json')
})
it('should return the path when model.json exists in a subfolder', async () => {
fs.existsSync.mockReturnValue(true)
fs.readdirSync
.mockReturnValueOnce(['subfolder'])
.mockReturnValueOnce(['model.json'])
fs.fileStat.mockResolvedValue({ isDirectory: () => true })
const result = await getModelJsonPath('test-folder')
expect(result).toBe('test-folder/subfolder/model.json')
})
})
})

View File

@ -57,7 +57,7 @@ export const scanModelsFolder = async (): Promise<Model[]> => {
!source.url.startsWith(`https://`)
)
)
if (existFiles.every((exist) => exist)) return true
if (existFiles.every((exist) => exist)) return model
const result = await fs
.readdirSync(await joinPath([_homeDir, dirName]))