-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* playing around * update * fix recursiveness * add tests and refactor * fix tests * unify message component, rearrange files and setup jest * remove jest from shinkai ui --------- Co-authored-by: paulclindo <[email protected]>
- Loading branch information
1 parent
ac28f16
commit b4b2270
Showing
30 changed files
with
1,618 additions
and
313 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default { | ||
displayName: 'shinkai-desktop', | ||
preset: '../../jest.preset.js', | ||
transform: { | ||
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../coverage/apps/shinkai-desktop', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
testEnvironment: 'jsdom', | ||
}; |
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
File renamed without changes.
122 changes: 122 additions & 0 deletions
122
...esktop/src/components/chat/python-code-runner/hooks/__tests__/usePyodideInstance.test.tsx
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,122 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { loadPyodide, PyodideInterface } from 'pyodide'; | ||
|
||
import { usePyodideInstance } from '../usePyodideInstance'; | ||
|
||
// Mock pyodide | ||
jest.mock('pyodide', () => ({ | ||
loadPyodide: jest.fn(), | ||
})); | ||
|
||
describe('usePyodideInstance', () => { | ||
let mockPyodide: jest.Mocked<PyodideInterface>; | ||
|
||
beforeEach(() => { | ||
// Reset mocks | ||
jest.clearAllMocks(); | ||
|
||
// Create mock Pyodide instance | ||
mockPyodide = { | ||
FS: { | ||
mount: jest.fn(), | ||
readdir: jest.fn(), | ||
stat: jest.fn(), | ||
isDir: jest.fn(), | ||
readFile: jest.fn(), | ||
writeFile: jest.fn(), | ||
unlink: jest.fn(), | ||
mkdir: jest.fn(), | ||
rmdir: jest.fn(), | ||
syncfs: jest.fn(), | ||
filesystems: { | ||
IDBFS: 'IDBFS', | ||
}, | ||
}, | ||
} as unknown as jest.Mocked<PyodideInterface>; | ||
|
||
// Mock loadPyodide to return our mock instance | ||
(loadPyodide as jest.Mock).mockResolvedValue(mockPyodide); | ||
}); | ||
|
||
it('should initialize Pyodide and file system service', async () => { | ||
mockPyodide.FS.syncfs.mockImplementation( | ||
// @ts-expect-error populate | ||
(populate: boolean, callback: (err: Error | null) => void) => | ||
callback(null), | ||
); | ||
|
||
const { result } = renderHook(() => usePyodideInstance()); | ||
|
||
// Initially, both pyodide and fileSystemService should be null | ||
expect(result.current.pyodide).toBeNull(); | ||
expect(result.current.fileSystemService).toBeNull(); | ||
|
||
// Initialize | ||
const { pyodide, fileSystemService } = | ||
await result.current.initializePyodide(); | ||
|
||
// After initialization | ||
expect(pyodide).toBe(mockPyodide); | ||
expect(fileSystemService).toBeDefined(); | ||
expect(loadPyodide).toHaveBeenCalledWith({ | ||
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.26.2/full/', | ||
stdout: console.log, | ||
stderr: console.error, | ||
}); | ||
expect(mockPyodide.FS.mount).toHaveBeenCalledWith( | ||
'IDBFS', | ||
{}, | ||
'/home/pyodide', | ||
); | ||
expect(mockPyodide.FS.syncfs).toHaveBeenCalledWith( | ||
true, | ||
expect.any(Function), | ||
); | ||
}); | ||
|
||
it('should reuse existing Pyodide instance', async () => { | ||
mockPyodide.FS.syncfs.mockImplementation( | ||
// @ts-expect-error populate | ||
(populate: boolean, callback: (err: Error | null) => void) => | ||
callback(null), | ||
); | ||
|
||
const { result } = renderHook(() => usePyodideInstance()); | ||
|
||
// First initialization | ||
const first = await result.current.initializePyodide(); | ||
expect(loadPyodide).toHaveBeenCalledTimes(1); | ||
|
||
// Second initialization | ||
const second = await result.current.initializePyodide(); | ||
expect(loadPyodide).toHaveBeenCalledTimes(1); // Should not be called again | ||
expect(second.pyodide).toBe(first.pyodide); | ||
expect(second.fileSystemService).toBe(first.fileSystemService); | ||
}); | ||
|
||
it('should handle initialization errors', async () => { | ||
(loadPyodide as jest.Mock).mockRejectedValue( | ||
new Error('Failed to load Pyodide'), | ||
); | ||
|
||
const { result } = renderHook(() => usePyodideInstance()); | ||
|
||
await expect(result.current.initializePyodide()).rejects.toThrow( | ||
'Failed to load Pyodide', | ||
); | ||
}); | ||
|
||
it('should handle file system initialization errors', async () => { | ||
mockPyodide.FS.syncfs.mockImplementation( | ||
// @ts-expect-error populate | ||
(populate: boolean, callback: (err: Error | null) => void) => | ||
callback(new Error('Sync failed')), | ||
); | ||
|
||
const { result } = renderHook(() => usePyodideInstance()); | ||
|
||
await expect(result.current.initializePyodide()).rejects.toThrow( | ||
'Sync failed', | ||
); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
apps/shinkai-desktop/src/components/chat/python-code-runner/hooks/usePyodideInstance.ts
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,43 @@ | ||
import { loadPyodide, PyodideInterface } from 'pyodide'; | ||
import { useCallback, useRef } from 'react'; | ||
|
||
import { IFileSystemService, PyodideFileSystemService } from '../services/file-system-service'; | ||
|
||
export function usePyodideInstance() { | ||
const pyodideRef = useRef<PyodideInterface | null>(null); | ||
const fileSystemServiceRef = useRef<IFileSystemService | null>(null); | ||
|
||
const initializePyodide = useCallback(async () => { | ||
if (pyodideRef.current) { | ||
console.log('Pyodide is already initialized.'); | ||
return { pyodide: pyodideRef.current, fileSystemService: fileSystemServiceRef.current! }; | ||
} | ||
|
||
console.time('initialize pyodide'); | ||
const pyodide = await loadPyodide({ | ||
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.26.2/full/', | ||
stdout: console.log, | ||
stderr: console.error, | ||
}); | ||
console.log('Pyodide initialized'); | ||
|
||
pyodideRef.current = pyodide; | ||
fileSystemServiceRef.current = new PyodideFileSystemService(pyodide); | ||
|
||
try { | ||
await fileSystemServiceRef.current.initialize(); | ||
} catch (error) { | ||
console.error('Failed to initialize file system:', error); | ||
throw error; | ||
} | ||
|
||
console.timeEnd('initialize pyodide'); | ||
return { pyodide, fileSystemService: fileSystemServiceRef.current }; | ||
}, []); | ||
|
||
return { | ||
pyodide: pyodideRef.current, | ||
fileSystemService: fileSystemServiceRef.current, | ||
initializePyodide, | ||
}; | ||
} |
File renamed without changes.
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
Oops, something went wrong.