chore: linter and test

This commit is contained in:
Louis 2024-10-29 14:30:21 +07:00
parent e5f5d887e3
commit 83edc1fbc7
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
2 changed files with 39 additions and 7 deletions

View File

@ -43,22 +43,55 @@ describe('LocalOAIEngine', () => {
}) })
it('should load model correctly', async () => { it('should load model correctly', async () => {
const model: Model = { engine: 'testProvider', file_path: 'path/to/model' } as any const model: ModelFile = { engine: 'testProvider', file_path: 'path/to/model' } as any
const modelFolder = 'path/to'
const systemInfo = { os: 'testOS' }
const res = { error: null }
expect(engine.loadModel(model)).toBeTruthy() ;(dirName as jest.Mock).mockResolvedValue(modelFolder)
;(systemInformation as jest.Mock).mockResolvedValue(systemInfo)
;(executeOnMain as jest.Mock).mockResolvedValue(res)
await engine.loadModel(model)
expect(dirName).toHaveBeenCalledWith(model.file_path)
expect(systemInformation).toHaveBeenCalled()
expect(executeOnMain).toHaveBeenCalledWith(
engine.nodeModule,
engine.loadModelFunctionName,
{ modelFolder, model },
systemInfo
)
expect(events.emit).toHaveBeenCalledWith(ModelEvent.OnModelReady, model)
})
it('should handle load model error', async () => {
const model: any = { engine: 'testProvider', file_path: 'path/to/model' } as any
const modelFolder = 'path/to'
const systemInfo = { os: 'testOS' }
const res = { error: 'load error' }
;(dirName as jest.Mock).mockResolvedValue(modelFolder)
;(systemInformation as jest.Mock).mockResolvedValue(systemInfo)
;(executeOnMain as jest.Mock).mockResolvedValue(res)
await expect(engine.loadModel(model)).rejects.toEqual('load error')
expect(events.emit).toHaveBeenCalledWith(ModelEvent.OnModelFail, { error: res.error })
}) })
it('should unload model correctly', async () => { it('should unload model correctly', async () => {
const model: Model = { engine: 'testProvider' } as any const model: Model = { engine: 'testProvider' } as any
expect(engine.unloadModel(model)).toBeTruthy() await engine.unloadModel(model)
expect(executeOnMain).toHaveBeenCalledWith(engine.nodeModule, engine.unloadModelFunctionName)
expect(events.emit).toHaveBeenCalledWith(ModelEvent.OnModelStopped, {})
}) })
it('should not unload model if engine does not match', async () => { it('should not unload model if engine does not match', async () => {
const model: Model = { engine: 'otherProvider' } as any const model: Model = { engine: 'otherProvider' } as any
await engine.unloadModel(model) await engine.unloadModel(model)
expect(executeOnMain).not.toHaveBeenCalled() expect(executeOnMain).not.toHaveBeenCalled()
expect(events.emit).not.toHaveBeenCalledWith(ModelEvent.OnModelStopped, {}) expect(events.emit).not.toHaveBeenCalledWith(ModelEvent.OnModelStopped, {})
}) })

View File

@ -88,8 +88,7 @@ const EventListenerWrapper = ({ children }: PropsWithChildren) => {
if (state.downloadType !== 'extension') { if (state.downloadType !== 'extension') {
state.downloadState = 'end' state.downloadState = 'end'
setDownloadState(state) setDownloadState(state)
if (state.percent !== 0) if (state.percent !== 0) removeDownloadingModel(state.modelId)
removeDownloadingModel(state.modelId)
} }
events.emit(ModelEvent.OnModelsUpdate, {}) events.emit(ModelEvent.OnModelsUpdate, {})
}, },