fix: app does not relaunch on Linux - add tests

This commit is contained in:
Louis 2024-11-19 19:43:12 +07:00
parent 28add39a51
commit 52c520d2c3
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
2 changed files with 83 additions and 3 deletions

View File

@ -1,5 +1,5 @@
// useModels.test.ts
import { renderHook, act } from '@testing-library/react'
import { renderHook, act, waitFor } from '@testing-library/react'
import { events, ModelEvent, ModelManager } from '@janhq/core'
import { extensionManager } from '@/extension'
@ -36,7 +36,6 @@ describe('useModels', () => {
}),
get: () => undefined,
has: () => true,
// set: () => {}
},
})
@ -50,6 +49,85 @@ describe('useModels', () => {
expect(mockModelExtension.getModels).toHaveBeenCalled()
})
it('should return empty on error', async () => {
const mockModelExtension = {
getModels: jest.fn().mockRejectedValue(new Error('Error')),
} as any
;(ModelManager.instance as jest.Mock).mockReturnValue({
models: {
values: () => ({
toArray: () => ({
filter: () => models,
}),
}),
get: () => undefined,
has: () => true,
},
})
jest.spyOn(extensionManager, 'get').mockReturnValue(mockModelExtension)
const { result } = renderHook(() => useModels())
await act(() => {
result.current?.loadDataModel()
})
expect(mockModelExtension.getModels()).rejects.toThrow()
})
it('should update states on models update', async () => {
const mockModelExtension = {
getModels: jest.fn().mockResolvedValue(models),
} as any
;(ModelManager.instance as jest.Mock).mockReturnValue({
models: {
values: () => ({
toArray: () => ({
filter: () => models,
}),
}),
get: () => undefined,
has: () => true,
},
})
jest.spyOn(extensionManager, 'get').mockReturnValue(mockModelExtension)
jest.spyOn(events, 'on').mockImplementationOnce((event, cb) => {
cb({ fetch: false })
})
renderHook(() => useModels())
expect(mockModelExtension.getModels).not.toHaveBeenCalled()
})
it('should update states on models update', async () => {
const mockModelExtension = {
getModels: jest.fn().mockResolvedValue(models),
} as any
;(ModelManager.instance as jest.Mock).mockReturnValue({
models: {
values: () => ({
toArray: () => ({
filter: () => models,
}),
}),
get: () => undefined,
has: () => true,
},
})
jest.spyOn(extensionManager, 'get').mockReturnValue(mockModelExtension)
jest.spyOn(events, 'on').mockImplementationOnce((event, cb) => {
cb({ fetch: true })
})
renderHook(() => useModels())
expect(mockModelExtension.getModels).toHaveBeenCalled()
})
it('should remove event listener on unmount', async () => {
const removeListenerSpy = jest.spyOn(events, 'off')

View File

@ -223,7 +223,9 @@ const Advanced = () => {
}
setGpusInUse(updatedGpusInUse)
await saveSettings({ gpusInUse: updatedGpusInUse })
window.core?.api?.relaunch()
// Reload window to apply changes
// This will trigger engine servers to restart
window.location.reload()
}
const gpuSelectionPlaceHolder =