fix: tests
This commit is contained in:
parent
5fe4cc6bab
commit
af8404d627
@ -3,96 +3,134 @@
|
|||||||
*/
|
*/
|
||||||
import { LocalOAIEngine } from './LocalOAIEngine'
|
import { LocalOAIEngine } from './LocalOAIEngine'
|
||||||
import { events } from '../../events'
|
import { events } from '../../events'
|
||||||
import { ModelEvent, Model } from '../../../types'
|
import { Model, ModelEvent } from '../../../types'
|
||||||
import { executeOnMain, systemInformation, dirName } from '../../core'
|
|
||||||
|
|
||||||
jest.mock('../../core', () => ({
|
jest.mock('../../events')
|
||||||
executeOnMain: jest.fn(),
|
|
||||||
systemInformation: jest.fn(),
|
|
||||||
dirName: jest.fn(),
|
|
||||||
}))
|
|
||||||
|
|
||||||
jest.mock('../../events', () => ({
|
|
||||||
events: {
|
|
||||||
on: jest.fn(),
|
|
||||||
emit: jest.fn(),
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
class TestLocalOAIEngine extends LocalOAIEngine {
|
class TestLocalOAIEngine extends LocalOAIEngine {
|
||||||
inferenceUrl = ''
|
inferenceUrl = 'http://test-local-inference-url'
|
||||||
nodeModule = 'testNodeModule'
|
provider = 'test-local-provider'
|
||||||
provider = 'testProvider'
|
nodeModule = 'test-node-module'
|
||||||
|
|
||||||
|
async headers() {
|
||||||
|
return { Authorization: 'Bearer test-token' }
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadModel(model: Model & { file_path?: string }): Promise<void> {
|
||||||
|
this.loadedModel = model
|
||||||
|
}
|
||||||
|
|
||||||
|
async unloadModel(model?: Model) {
|
||||||
|
this.loadedModel = undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('LocalOAIEngine', () => {
|
describe('LocalOAIEngine', () => {
|
||||||
let engine: TestLocalOAIEngine
|
let engine: TestLocalOAIEngine
|
||||||
|
const mockModel: Model & { file_path?: string } = {
|
||||||
|
object: 'model',
|
||||||
|
version: '1.0.0',
|
||||||
|
format: 'gguf',
|
||||||
|
sources: [],
|
||||||
|
id: 'test-model',
|
||||||
|
name: 'Test Model',
|
||||||
|
description: 'A test model',
|
||||||
|
settings: {},
|
||||||
|
parameters: {},
|
||||||
|
metadata: {},
|
||||||
|
file_path: '/path/to/model.gguf'
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
engine = new TestLocalOAIEngine('', '')
|
engine = new TestLocalOAIEngine('', '')
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should subscribe to events on load', () => {
|
describe('onLoad', () => {
|
||||||
engine.onLoad()
|
it('should call super.onLoad and subscribe to model events', () => {
|
||||||
expect(events.on).toHaveBeenCalledWith(ModelEvent.OnModelInit, expect.any(Function))
|
const superOnLoadSpy = jest.spyOn(Object.getPrototypeOf(Object.getPrototypeOf(engine)), 'onLoad')
|
||||||
expect(events.on).toHaveBeenCalledWith(ModelEvent.OnModelStop, expect.any(Function))
|
|
||||||
|
engine.onLoad()
|
||||||
|
|
||||||
|
expect(superOnLoadSpy).toHaveBeenCalled()
|
||||||
|
expect(events.on).toHaveBeenCalledWith(
|
||||||
|
ModelEvent.OnModelInit,
|
||||||
|
expect.any(Function)
|
||||||
|
)
|
||||||
|
expect(events.on).toHaveBeenCalledWith(
|
||||||
|
ModelEvent.OnModelStop,
|
||||||
|
expect.any(Function)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should load model when OnModelInit event is triggered', () => {
|
||||||
|
const loadModelSpy = jest.spyOn(engine, 'loadModel')
|
||||||
|
engine.onLoad()
|
||||||
|
|
||||||
|
// Get the event handler for OnModelInit
|
||||||
|
const onModelInitCall = (events.on as jest.Mock).mock.calls.find(
|
||||||
|
call => call[0] === ModelEvent.OnModelInit
|
||||||
|
)
|
||||||
|
const onModelInitHandler = onModelInitCall[1]
|
||||||
|
|
||||||
|
// Trigger the event handler
|
||||||
|
onModelInitHandler(mockModel)
|
||||||
|
|
||||||
|
expect(loadModelSpy).toHaveBeenCalledWith(mockModel)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should unload model when OnModelStop event is triggered', () => {
|
||||||
|
const unloadModelSpy = jest.spyOn(engine, 'unloadModel')
|
||||||
|
engine.onLoad()
|
||||||
|
|
||||||
|
// Get the event handler for OnModelStop
|
||||||
|
const onModelStopCall = (events.on as jest.Mock).mock.calls.find(
|
||||||
|
call => call[0] === ModelEvent.OnModelStop
|
||||||
|
)
|
||||||
|
const onModelStopHandler = onModelStopCall[1]
|
||||||
|
|
||||||
|
// Trigger the event handler
|
||||||
|
onModelStopHandler(mockModel)
|
||||||
|
|
||||||
|
expect(unloadModelSpy).toHaveBeenCalledWith(mockModel)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should load model correctly', async () => {
|
describe('properties', () => {
|
||||||
const model: any = { engine: 'testProvider', file_path: 'path/to/model' } as any
|
it('should have correct default function names', () => {
|
||||||
const modelFolder = 'path/to'
|
expect(engine.loadModelFunctionName).toBe('loadModel')
|
||||||
const systemInfo = { os: 'testOS' }
|
expect(engine.unloadModelFunctionName).toBe('unloadModel')
|
||||||
const res = { error: null }
|
})
|
||||||
|
|
||||||
;(dirName as jest.Mock).mockResolvedValue(modelFolder)
|
it('should have abstract nodeModule property implemented', () => {
|
||||||
;(systemInformation as jest.Mock).mockResolvedValue(systemInfo)
|
expect(engine.nodeModule).toBe('test-node-module')
|
||||||
;(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 () => {
|
describe('loadModel', () => {
|
||||||
const model: any = { engine: 'testProvider', file_path: 'path/to/model' } as any
|
it('should load the model and set loadedModel', async () => {
|
||||||
const modelFolder = 'path/to'
|
await engine.loadModel(mockModel)
|
||||||
const systemInfo = { os: 'testOS' }
|
expect(engine.loadedModel).toBe(mockModel)
|
||||||
const res = { error: 'load error' }
|
})
|
||||||
|
|
||||||
;(dirName as jest.Mock).mockResolvedValue(modelFolder)
|
it('should handle model with file_path', async () => {
|
||||||
;(systemInformation as jest.Mock).mockResolvedValue(systemInfo)
|
const modelWithPath = { ...mockModel, file_path: '/custom/path/model.gguf' }
|
||||||
;(executeOnMain as jest.Mock).mockResolvedValue(res)
|
await engine.loadModel(modelWithPath)
|
||||||
|
expect(engine.loadedModel).toBe(modelWithPath)
|
||||||
await expect(engine.loadModel(model)).rejects.toEqual('load error')
|
})
|
||||||
|
|
||||||
expect(events.emit).toHaveBeenCalledWith(ModelEvent.OnModelFail, { error: res.error })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should unload model correctly', async () => {
|
describe('unloadModel', () => {
|
||||||
const model: Model = { engine: 'testProvider' } as any
|
it('should unload the model and clear loadedModel', async () => {
|
||||||
|
engine.loadedModel = mockModel
|
||||||
|
await engine.unloadModel(mockModel)
|
||||||
|
expect(engine.loadedModel).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
await engine.unloadModel(model)
|
it('should handle unload without passing a model', async () => {
|
||||||
|
engine.loadedModel = mockModel
|
||||||
expect(executeOnMain).toHaveBeenCalledWith(engine.nodeModule, engine.unloadModelFunctionName)
|
await engine.unloadModel()
|
||||||
expect(events.emit).toHaveBeenCalledWith(ModelEvent.OnModelStopped, {})
|
expect(engine.loadedModel).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not unload model if engine does not match', async () => {
|
|
||||||
const model: Model = { engine: 'otherProvider' } as any
|
|
||||||
await engine.unloadModel(model)
|
|
||||||
expect(executeOnMain).not.toHaveBeenCalled()
|
|
||||||
expect(events.emit).not.toHaveBeenCalledWith(ModelEvent.OnModelStopped, {})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user