forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add core modules test cases (janhq#3498)
* chore: add core module test cases * chore: fix tests * chore: add code coverage report * chore: split coverage step * chore: split coverage step * Update jan-electron-linter-and-test.yml * Update jan-electron-linter-and-test.yml * Update jan-electron-linter-and-test.yml * chore: update tests * chore: add web utils test cases * chore: add restful and helper tests * chore: add tests
- Loading branch information
Showing
46 changed files
with
1,194 additions
and
19 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
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ module.exports = { | |
moduleNameMapper: { | ||
'@/(.*)': '<rootDir>/src/$1', | ||
}, | ||
runner: './testRunner.js', | ||
} |
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,98 @@ | ||
import { openExternalUrl } from './core'; | ||
import { joinPath } from './core'; | ||
import { openFileExplorer } from './core'; | ||
import { getJanDataFolderPath } from './core'; | ||
import { abortDownload } from './core'; | ||
import { getFileSize } from './core'; | ||
import { executeOnMain } from './core'; | ||
|
||
it('should open external url', async () => { | ||
const url = 'http://example.com'; | ||
globalThis.core = { | ||
api: { | ||
openExternalUrl: jest.fn().mockResolvedValue('opened') | ||
} | ||
}; | ||
const result = await openExternalUrl(url); | ||
expect(globalThis.core.api.openExternalUrl).toHaveBeenCalledWith(url); | ||
expect(result).toBe('opened'); | ||
}); | ||
|
||
|
||
it('should join paths', async () => { | ||
const paths = ['/path/one', '/path/two']; | ||
globalThis.core = { | ||
api: { | ||
joinPath: jest.fn().mockResolvedValue('/path/one/path/two') | ||
} | ||
}; | ||
const result = await joinPath(paths); | ||
expect(globalThis.core.api.joinPath).toHaveBeenCalledWith(paths); | ||
expect(result).toBe('/path/one/path/two'); | ||
}); | ||
|
||
|
||
it('should open file explorer', async () => { | ||
const path = '/path/to/open'; | ||
globalThis.core = { | ||
api: { | ||
openFileExplorer: jest.fn().mockResolvedValue('opened') | ||
} | ||
}; | ||
const result = await openFileExplorer(path); | ||
expect(globalThis.core.api.openFileExplorer).toHaveBeenCalledWith(path); | ||
expect(result).toBe('opened'); | ||
}); | ||
|
||
|
||
it('should get jan data folder path', async () => { | ||
globalThis.core = { | ||
api: { | ||
getJanDataFolderPath: jest.fn().mockResolvedValue('/path/to/jan/data') | ||
} | ||
}; | ||
const result = await getJanDataFolderPath(); | ||
expect(globalThis.core.api.getJanDataFolderPath).toHaveBeenCalled(); | ||
expect(result).toBe('/path/to/jan/data'); | ||
}); | ||
|
||
|
||
it('should abort download', async () => { | ||
const fileName = 'testFile'; | ||
globalThis.core = { | ||
api: { | ||
abortDownload: jest.fn().mockResolvedValue('aborted') | ||
} | ||
}; | ||
const result = await abortDownload(fileName); | ||
expect(globalThis.core.api.abortDownload).toHaveBeenCalledWith(fileName); | ||
expect(result).toBe('aborted'); | ||
}); | ||
|
||
|
||
it('should get file size', async () => { | ||
const url = 'http://example.com/file'; | ||
globalThis.core = { | ||
api: { | ||
getFileSize: jest.fn().mockResolvedValue(1024) | ||
} | ||
}; | ||
const result = await getFileSize(url); | ||
expect(globalThis.core.api.getFileSize).toHaveBeenCalledWith(url); | ||
expect(result).toBe(1024); | ||
}); | ||
|
||
|
||
it('should execute function on main process', async () => { | ||
const extension = 'testExtension'; | ||
const method = 'testMethod'; | ||
const args = ['arg1', 'arg2']; | ||
globalThis.core = { | ||
api: { | ||
invokeExtensionFunc: jest.fn().mockResolvedValue('result') | ||
} | ||
}; | ||
const result = await executeOnMain(extension, method, ...args); | ||
expect(globalThis.core.api.invokeExtensionFunc).toHaveBeenCalledWith(extension, method, ...args); | ||
expect(result).toBe('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { events } from './events'; | ||
import { jest } from '@jest/globals'; | ||
|
||
it('should emit an event', () => { | ||
const mockObject = { key: 'value' }; | ||
globalThis.core = { | ||
events: { | ||
emit: jest.fn() | ||
} | ||
}; | ||
events.emit('testEvent', mockObject); | ||
expect(globalThis.core.events.emit).toHaveBeenCalledWith('testEvent', mockObject); | ||
}); | ||
|
||
|
||
it('should remove an observer for an event', () => { | ||
const mockHandler = jest.fn(); | ||
globalThis.core = { | ||
events: { | ||
off: jest.fn() | ||
} | ||
}; | ||
events.off('testEvent', mockHandler); | ||
expect(globalThis.core.events.off).toHaveBeenCalledWith('testEvent', mockHandler); | ||
}); | ||
|
||
|
||
it('should add an observer for an event', () => { | ||
const mockHandler = jest.fn(); | ||
globalThis.core = { | ||
events: { | ||
on: jest.fn() | ||
} | ||
}; | ||
events.on('testEvent', mockHandler); | ||
expect(globalThis.core.events.on).toHaveBeenCalledWith('testEvent', mockHandler); | ||
}); |
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,46 @@ | ||
import { BaseExtension } from './extension' | ||
|
||
class TestBaseExtension extends BaseExtension { | ||
onLoad(): void {} | ||
onUnload(): void {} | ||
} | ||
|
||
describe('BaseExtension', () => { | ||
let baseExtension: TestBaseExtension | ||
|
||
beforeEach(() => { | ||
baseExtension = new TestBaseExtension('https://example.com', 'TestExtension') | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should have the correct properties', () => { | ||
expect(baseExtension.name).toBe('TestExtension') | ||
expect(baseExtension.productName).toBeUndefined() | ||
expect(baseExtension.url).toBe('https://example.com') | ||
expect(baseExtension.active).toBeUndefined() | ||
expect(baseExtension.description).toBeUndefined() | ||
expect(baseExtension.version).toBeUndefined() | ||
}) | ||
|
||
it('should return undefined for type()', () => { | ||
expect(baseExtension.type()).toBeUndefined() | ||
}) | ||
|
||
it('should have abstract methods onLoad() and onUnload()', () => { | ||
expect(baseExtension.onLoad).toBeDefined() | ||
expect(baseExtension.onUnload).toBeDefined() | ||
}) | ||
|
||
it('should have installationState() return "NotRequired"', async () => { | ||
const installationState = await baseExtension.installationState() | ||
expect(installationState).toBe('NotRequired') | ||
}) | ||
|
||
it('should install the extension', async () => { | ||
await baseExtension.install() | ||
// Add your assertions here | ||
}) | ||
}) |
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,60 @@ | ||
import { lastValueFrom, Observable } from 'rxjs' | ||
import { requestInference } from './sse' | ||
|
||
describe('requestInference', () => { | ||
it('should send a request to the inference server and return an Observable', () => { | ||
// Mock the fetch function | ||
const mockFetch: any = jest.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ choices: [{ message: { content: 'Generated response' } }] }), | ||
headers: new Headers(), | ||
redirected: false, | ||
status: 200, | ||
statusText: 'OK', | ||
// Add other required properties here | ||
}) | ||
) | ||
jest.spyOn(global, 'fetch').mockImplementation(mockFetch) | ||
|
||
// Define the test inputs | ||
const inferenceUrl = 'https://inference-server.com' | ||
const requestBody = { message: 'Hello' } | ||
const model = { id: 'model-id', parameters: { stream: false } } | ||
|
||
// Call the function | ||
const result = requestInference(inferenceUrl, requestBody, model) | ||
|
||
// Assert the expected behavior | ||
expect(result).toBeInstanceOf(Observable) | ||
expect(lastValueFrom(result)).resolves.toEqual('Generated response') | ||
}) | ||
|
||
it('returns 401 error', () => { | ||
// Mock the fetch function | ||
const mockFetch: any = jest.fn(() => | ||
Promise.resolve({ | ||
ok: false, | ||
json: () => Promise.resolve({ error: { message: 'Wrong API Key', code: 'invalid_api_key' } }), | ||
headers: new Headers(), | ||
redirected: false, | ||
status: 401, | ||
statusText: 'invalid_api_key', | ||
// Add other required properties here | ||
}) | ||
) | ||
jest.spyOn(global, 'fetch').mockImplementation(mockFetch) | ||
|
||
// Define the test inputs | ||
const inferenceUrl = 'https://inference-server.com' | ||
const requestBody = { message: 'Hello' } | ||
const model = { id: 'model-id', parameters: { stream: false } } | ||
|
||
// Call the function | ||
const result = requestInference(inferenceUrl, requestBody, model) | ||
|
||
// Assert the expected behavior | ||
expect(result).toBeInstanceOf(Observable) | ||
expect(lastValueFrom(result)).rejects.toEqual({ message: 'Wrong API Key', code: 'invalid_api_key' }) | ||
}) | ||
}) |
Oops, something went wrong.