-
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.
Merge branch 'main' into fix/move-modules
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./useAssistantList"; | ||
export * from "./useAssistant"; | ||
export * from "./useMessageList"; | ||
export * from "./useThreadList"; |
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,48 @@ | ||
import { useCallback } from "react"; | ||
import { useState } from "react"; | ||
import { | ||
AssistantSchema, | ||
djangoAiAssistantViewsGetAssistant, | ||
} from "../client"; | ||
|
||
/** | ||
* React hook to manage an Assistant. | ||
*/ | ||
export function useAssistant({ assistantId }: { | ||
assistantId: string; | ||
}) { | ||
const [assistant, setAssistant] = useState<AssistantSchema | null>(null); | ||
const [loadingFetchAssistant, setLoadingFetchAssistant] = | ||
useState<boolean>(false); | ||
|
||
/** | ||
* Fetches an AI assistant. | ||
* | ||
* @returns A promise that resolves with the fetched AI assistant. | ||
*/ | ||
const fetchAssistant = useCallback(async (): Promise<AssistantSchema> => { | ||
try { | ||
setLoadingFetchAssistant(true); | ||
const fetchedAssistant = await djangoAiAssistantViewsGetAssistant({ assistantId }); | ||
setAssistant(fetchedAssistant); | ||
return fetchedAssistant; | ||
} finally { | ||
setLoadingFetchAssistant(false); | ||
} | ||
}, [assistantId]); | ||
|
||
return { | ||
/** | ||
* Function to fetch an AI assistant from the server. | ||
*/ | ||
fetchAssistant, | ||
/** | ||
* Fetched AI assistant. | ||
*/ | ||
assistant, | ||
/** | ||
* Loading state of the fetch operation. | ||
*/ | ||
loadingFetchAssistant, | ||
}; | ||
} |
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,66 @@ | ||
import { act, renderHook } from "@testing-library/react"; | ||
import { useAssistant } from "../src/hooks"; | ||
import { djangoAiAssistantViewsGetAssistant } from "../src/client"; | ||
|
||
jest.mock("../src/client", () => ({ | ||
djangoAiAssistantViewsGetAssistant: jest | ||
.fn() | ||
.mockImplementation(() => Promise.resolve()), | ||
})); | ||
|
||
describe("useAssistant", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should initialize with no assistant and loading false", () => { | ||
const { result } = renderHook(() => useAssistant({ assistantId: 'weather_assistant' })); | ||
|
||
expect(result.current.assistant).toBeNull(); | ||
expect(result.current.loadingFetchAssistant).toBe(false); | ||
}); | ||
|
||
describe("fetchAssistant", () => { | ||
it("should fetch assistant and update state correctly", async () => { | ||
const mockAssistants = [ | ||
{ id: 'weather_assistant', name: "Assistant 1" }, | ||
{ id: 'movies_assistant', name: "Assistant 2" }, | ||
]; | ||
(djangoAiAssistantViewsGetAssistant as jest.Mock).mockResolvedValue( | ||
mockAssistants | ||
); | ||
|
||
const { result } = renderHook(() => useAssistant({ assistantId: 'weather_assistant' })); | ||
|
||
expect(result.current.assistant).toBeNull(); | ||
expect(result.current.loadingFetchAssistant).toBe(false); | ||
|
||
await act(async () => { | ||
await result.current.fetchAssistant(); | ||
}); | ||
|
||
expect(result.current.assistant).toEqual(mockAssistants); | ||
expect(result.current.loadingFetchAssistant).toBe(false); | ||
}); | ||
|
||
it("should set loading to false if fetch fails", async () => { | ||
(djangoAiAssistantViewsGetAssistant as jest.Mock).mockRejectedValue( | ||
new Error("Failed to fetch") | ||
); | ||
|
||
const { result } = renderHook(() => useAssistant({ assistantId: 'non_existent_assistant' })); | ||
|
||
expect(result.current.assistant).toBeNull(); | ||
expect(result.current.loadingFetchAssistant).toBe(false); | ||
|
||
await expect(async () => { | ||
await act(async () => { | ||
await result.current.fetchAssistant(); | ||
}); | ||
}).rejects.toThrow("Failed to fetch"); | ||
|
||
expect(result.current.assistant).toBeNull(); | ||
expect(result.current.loadingFetchAssistant).toBe(false); | ||
}); | ||
}); | ||
}); |