-
Notifications
You must be signed in to change notification settings - Fork 14
🤖 fix: Prevent unhandled promise rejection in web force delete #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,135 @@ | ||
| /** | ||
| * Tests for browser API client | ||
| * Tests the invokeIPC function to ensure it behaves consistently with Electron's ipcRenderer.invoke() | ||
| */ | ||
|
|
||
| import { describe, test, expect } from "bun:test"; | ||
|
|
||
| // Helper to create a mock fetch that returns a specific response | ||
| function createMockFetch(responseData: unknown) { | ||
| return () => { | ||
| return Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.resolve(responseData), | ||
| } as Response); | ||
| }; | ||
| } | ||
|
|
||
| interface InvokeResponse<T> { | ||
| success: boolean; | ||
| data?: T; | ||
| error?: unknown; | ||
| } | ||
|
|
||
| // Helper to create invokeIPC function with mocked fetch | ||
| function createInvokeIPC( | ||
| mockFetch: (url: string, init?: RequestInit) => Promise<Response> | ||
| ): <T>(channel: string, ...args: unknown[]) => Promise<T> { | ||
| const API_BASE = "http://localhost:3000"; | ||
|
|
||
| async function invokeIPC<T>(channel: string, ...args: unknown[]): Promise<T> { | ||
| const response = await mockFetch(`${API_BASE}/ipc/${encodeURIComponent(channel)}`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ args }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
|
|
||
| const result = (await response.json()) as InvokeResponse<T>; | ||
|
|
||
| // Return the result as-is - let the caller handle success/failure | ||
| // This matches the behavior of Electron's ipcRenderer.invoke() which doesn't throw on error | ||
| if (!result.success) { | ||
| return result as T; | ||
| } | ||
|
|
||
| // Success - unwrap and return the data | ||
| return result.data as T; | ||
| } | ||
|
|
||
| return invokeIPC; | ||
| } | ||
|
|
||
| describe("Browser API invokeIPC", () => { | ||
| test("should return error object on failure (matches Electron behavior)", async () => { | ||
| const mockFetch = createMockFetch({ | ||
| success: false, | ||
| error: "fatal: contains modified or untracked files", | ||
| }); | ||
|
|
||
| const invokeIPC = createInvokeIPC(mockFetch); | ||
|
|
||
| // Fixed behavior: invokeIPC returns error object instead of throwing | ||
| // This matches Electron's ipcRenderer.invoke() which never throws on error | ||
| const result = await invokeIPC<{ success: boolean; error?: string }>( | ||
| "WORKSPACE_REMOVE", | ||
| "test-workspace", | ||
| { force: false } | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: "fatal: contains modified or untracked files", | ||
| }); | ||
| }); | ||
|
|
||
| test("should return success data on success", async () => { | ||
| const mockFetch = createMockFetch({ | ||
| success: true, | ||
| data: { someData: "value" }, | ||
| }); | ||
|
|
||
| const invokeIPC = createInvokeIPC(mockFetch); | ||
|
|
||
| const result = await invokeIPC("WORKSPACE_REMOVE", "test-workspace", { force: true }); | ||
|
|
||
| expect(result).toEqual({ someData: "value" }); | ||
| }); | ||
|
|
||
| test("should throw on HTTP errors", async () => { | ||
| const mockFetch = () => { | ||
| return Promise.resolve({ | ||
| ok: false, | ||
| status: 500, | ||
| } as Response); | ||
| }; | ||
|
|
||
| const invokeIPC = createInvokeIPC(mockFetch); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/await-thenable | ||
| await expect(invokeIPC("WORKSPACE_REMOVE", "test-workspace", { force: false })).rejects.toThrow( | ||
| "HTTP error! status: 500" | ||
| ); | ||
| }); | ||
|
|
||
| test("should return structured error objects as-is", async () => { | ||
| const structuredError = { | ||
| type: "STREAMING_IN_PROGRESS", | ||
| message: "Cannot send message while streaming", | ||
| workspaceId: "test-workspace", | ||
| }; | ||
|
|
||
| const mockFetch = createMockFetch({ | ||
| success: false, | ||
| error: structuredError, | ||
| }); | ||
|
|
||
| const invokeIPC = createInvokeIPC(mockFetch); | ||
|
|
||
| const result = await invokeIPC("WORKSPACE_SEND_MESSAGE", "test-workspace", { | ||
| role: "user", | ||
| content: [{ type: "text", text: "test" }], | ||
| }); | ||
|
|
||
| // Structured errors should be returned as-is | ||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: structuredError, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.