-
Notifications
You must be signed in to change notification settings - Fork 938
navy meadow 16 #126
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
Closed
Closed
navy meadow 16 #126
Changes from all commits
Commits
Show all changes
2 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
190 changes: 190 additions & 0 deletions
190
apps/desktop/src/lib/trpc/routers/workspaces/workspaces.test.ts
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,190 @@ | ||
| import { describe, expect, it, mock } from "bun:test"; | ||
| import { createWorkspacesRouter } from "./workspaces"; | ||
| import * as gitUtils from "./utils/git"; | ||
|
|
||
| // Mock the git utilities | ||
| mock.module("./utils/git", () => ({ | ||
| createWorktree: mock(() => Promise.resolve()), | ||
| removeWorktree: mock(() => Promise.resolve()), | ||
| generateBranchName: mock(() => "test-branch-123"), | ||
| })); | ||
|
|
||
| // Mock the database | ||
| const mockDb = { | ||
| data: { | ||
| workspaces: [ | ||
| { | ||
| id: "workspace-1", | ||
| projectId: "project-1", | ||
| worktreeId: "worktree-1", | ||
| name: "Test Workspace", | ||
| tabOrder: 0, | ||
| createdAt: Date.now(), | ||
| updatedAt: Date.now(), | ||
| lastOpenedAt: Date.now(), | ||
| }, | ||
| ], | ||
| worktrees: [ | ||
| { | ||
| id: "worktree-1", | ||
| projectId: "project-1", | ||
| path: "/path/to/worktree", | ||
| branch: "test-branch", | ||
| createdAt: Date.now(), | ||
| }, | ||
| ], | ||
| projects: [ | ||
| { | ||
| id: "project-1", | ||
| name: "Test Project", | ||
| mainRepoPath: "/path/to/repo", | ||
| color: "#ff0000", | ||
| tabOrder: 0, | ||
| createdAt: Date.now(), | ||
| lastOpenedAt: Date.now(), | ||
| }, | ||
| ], | ||
| settings: { | ||
| lastActiveWorkspaceId: "workspace-1", | ||
| }, | ||
| }, | ||
| update: mock(async (fn: (data: typeof mockDb.data) => void) => { | ||
| fn(mockDb.data); | ||
| }), | ||
| }; | ||
|
|
||
| describe("workspaces router - delete", () => { | ||
| it("should successfully delete workspace and remove worktree", async () => { | ||
| const router = createWorkspacesRouter(); | ||
|
|
||
| // Mock removeWorktree to succeed | ||
| const removeWorktreeMock = mock(() => Promise.resolve()); | ||
| mock.module("./utils/git", () => ({ | ||
| ...gitUtils, | ||
| removeWorktree: removeWorktreeMock, | ||
| })); | ||
|
|
||
| const caller = router.createCaller({ db: mockDb as any }); | ||
|
|
||
| const result = await caller.delete({ id: "workspace-1" }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(removeWorktreeMock).toHaveBeenCalledWith( | ||
| "/path/to/repo", | ||
| "/path/to/worktree", | ||
| ); | ||
| expect(mockDb.data.workspaces).toHaveLength(0); | ||
| expect(mockDb.data.worktrees).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should fail deletion if worktree removal fails", async () => { | ||
| // Reset mock data | ||
| mockDb.data.workspaces = [ | ||
| { | ||
| id: "workspace-1", | ||
| projectId: "project-1", | ||
| worktreeId: "worktree-1", | ||
| name: "Test Workspace", | ||
| tabOrder: 0, | ||
| createdAt: Date.now(), | ||
| updatedAt: Date.now(), | ||
| lastOpenedAt: Date.now(), | ||
| }, | ||
| ]; | ||
| mockDb.data.worktrees = [ | ||
| { | ||
| id: "worktree-1", | ||
| projectId: "project-1", | ||
| path: "/path/to/worktree", | ||
| branch: "test-branch", | ||
| createdAt: Date.now(), | ||
| }, | ||
| ]; | ||
|
|
||
| const router = createWorkspacesRouter(); | ||
|
|
||
| // Mock removeWorktree to fail | ||
| const removeWorktreeMock = mock(() => | ||
| Promise.reject(new Error("Failed to remove worktree")), | ||
| ); | ||
| mock.module("./utils/git", () => ({ | ||
| ...gitUtils, | ||
| removeWorktree: removeWorktreeMock, | ||
| })); | ||
|
|
||
| const caller = router.createCaller({ db: mockDb as any }); | ||
|
|
||
| const result = await caller.delete({ id: "workspace-1" }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| expect(result.error).toContain("Failed to remove worktree"); | ||
| // Workspace should NOT be removed from DB if worktree removal fails | ||
| expect(mockDb.data.workspaces).toHaveLength(1); | ||
| expect(mockDb.data.worktrees).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("workspaces router - canDelete", () => { | ||
| it("should return true when worktree can be deleted", async () => { | ||
| const router = createWorkspacesRouter(); | ||
|
|
||
| // Mock git to return worktree list | ||
| const mockGit = { | ||
| raw: mock(() => | ||
| Promise.resolve("/path/to/worktree\n/path/to/other-worktree"), | ||
| ), | ||
| }; | ||
| const mockSimpleGit = mock(() => mockGit); | ||
| mock.module("simple-git", () => ({ | ||
| default: mockSimpleGit, | ||
| })); | ||
|
|
||
| const caller = router.createCaller({ db: mockDb as any }); | ||
|
|
||
| const result = await caller.canDelete({ id: "workspace-1" }); | ||
|
|
||
| expect(result.canDelete).toBe(true); | ||
| expect(result.reason).toBeNull(); | ||
| expect(result.warning).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return warning when worktree doesn't exist in git", async () => { | ||
| const router = createWorkspacesRouter(); | ||
|
|
||
| // Mock git to return worktree list without our worktree | ||
| const mockGit = { | ||
| raw: mock(() => Promise.resolve("/path/to/other-worktree")), | ||
| }; | ||
| const mockSimpleGit = mock(() => mockGit); | ||
| mock.module("simple-git", () => ({ | ||
| default: mockSimpleGit, | ||
| })); | ||
|
|
||
| const caller = router.createCaller({ db: mockDb as any }); | ||
|
|
||
| const result = await caller.canDelete({ id: "workspace-1" }); | ||
|
|
||
| expect(result.canDelete).toBe(true); | ||
| expect(result.warning).toContain("not found in git"); | ||
| }); | ||
|
|
||
| it("should return false when git check fails", async () => { | ||
| const router = createWorkspacesRouter(); | ||
|
|
||
| // Mock git to throw error | ||
| const mockGit = { | ||
| raw: mock(() => Promise.reject(new Error("Git error"))), | ||
| }; | ||
| const mockSimpleGit = mock(() => mockGit); | ||
| mock.module("simple-git", () => ({ | ||
| default: mockSimpleGit, | ||
| })); | ||
|
|
||
| const caller = router.createCaller({ db: mockDb as any }); | ||
|
|
||
| const result = await caller.canDelete({ id: "workspace-1" }); | ||
|
|
||
| expect(result.canDelete).toBe(false); | ||
| expect(result.reason).toContain("Failed to check worktree status"); | ||
| }); | ||
| }); | ||
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
97 changes: 97 additions & 0 deletions
97
...sktop/src/renderer/screens/main/components/TopBar/WorkspaceTabs/DeleteWorkspaceDialog.tsx
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,97 @@ | ||
| import { | ||
| AlertDialog, | ||
| AlertDialogAction, | ||
| AlertDialogCancel, | ||
| AlertDialogContent, | ||
| AlertDialogDescription, | ||
| AlertDialogFooter, | ||
| AlertDialogHeader, | ||
| AlertDialogTitle, | ||
| } from "@superset/ui/alert-dialog"; | ||
| import { useState } from "react"; | ||
| import { trpc } from "renderer/lib/trpc"; | ||
| import { useDeleteWorkspace } from "renderer/react-query/workspaces"; | ||
|
|
||
| interface DeleteWorkspaceDialogProps { | ||
| workspaceId: string; | ||
| workspaceName: string; | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| } | ||
|
|
||
| export function DeleteWorkspaceDialog({ | ||
| workspaceId, | ||
| workspaceName, | ||
| open, | ||
| onOpenChange, | ||
| }: DeleteWorkspaceDialogProps) { | ||
| const [isDeleting, setIsDeleting] = useState(false); | ||
| const deleteWorkspace = useDeleteWorkspace(); | ||
|
|
||
| // Query to check if workspace can be deleted | ||
| const { data: canDeleteData, isLoading } = trpc.workspaces.canDelete.useQuery( | ||
| { id: workspaceId }, | ||
| { enabled: open }, // Only run when dialog is open | ||
| ); | ||
|
|
||
| const handleDelete = async () => { | ||
| setIsDeleting(true); | ||
| try { | ||
| await deleteWorkspace.mutateAsync({ id: workspaceId }); | ||
| onOpenChange(false); | ||
| } catch (error) { | ||
| console.error("Failed to delete workspace:", error); | ||
| } finally { | ||
| setIsDeleting(false); | ||
| } | ||
| }; | ||
|
|
||
| const canDelete = canDeleteData?.canDelete ?? true; | ||
| const reason = canDeleteData?.reason; | ||
| const warning = canDeleteData?.warning; | ||
|
|
||
| return ( | ||
| <AlertDialog open={open} onOpenChange={onOpenChange}> | ||
| <AlertDialogContent> | ||
| <AlertDialogHeader> | ||
| <AlertDialogTitle>Delete Workspace</AlertDialogTitle> | ||
| <AlertDialogDescription> | ||
| {isLoading ? ( | ||
| <span>Checking workspace status...</span> | ||
| ) : !canDelete ? ( | ||
| <span className="text-destructive"> | ||
| Cannot delete workspace: {reason} | ||
| </span> | ||
| ) : ( | ||
| <> | ||
| Are you sure you want to delete "{workspaceName}"? | ||
| {warning && ( | ||
| <span className="block mt-2 text-yellow-600 dark:text-yellow-400"> | ||
| Warning: {warning} | ||
| </span> | ||
| )} | ||
| <span className="block mt-2"> | ||
| This will remove the workspace and its associated git | ||
| worktree. This action cannot be undone. | ||
| </span> | ||
| </> | ||
| )} | ||
| </AlertDialogDescription> | ||
| </AlertDialogHeader> | ||
| <AlertDialogFooter> | ||
| <AlertDialogCancel disabled={isDeleting}>Cancel</AlertDialogCancel> | ||
| <AlertDialogAction | ||
| onClick={(e: React.MouseEvent) => { | ||
| e.preventDefault(); | ||
| handleDelete(); | ||
| }} | ||
| disabled={!canDelete || isDeleting || isLoading} | ||
| className="bg-destructive text-white hover:bg-destructive/90" | ||
| > | ||
| {isDeleting ? "Deleting..." : "Delete"} | ||
| </AlertDialogAction> | ||
| </AlertDialogFooter> | ||
| </AlertDialogContent> | ||
| </AlertDialog> | ||
| ); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Router under test doesn’t actually use
mockDbIn these tests you construct
mockDband pass it viarouter.createCaller({ db: mockDb as any }), then assert againstmockDb.data. However,createWorkspacesRouterimports and uses thedbsingleton from"main/lib/db"directly (e.g.,db.data.workspaces,db.update(...)) rather thanctx.db.Unless
"main/lib/db"is being mocked or initialized elsewhere to point atmockDb, these tests will not exercisemockDbat all and may either:mockDb.datameaningless), or_dbisn’t set.I’d recommend either:
ctx.dbinstead of the importeddb, so the context you pass tocreateCalleris actually used; or"main/lib/db"in this test to export{ db: mockDb }so the router’s importeddbis your mock.I need to verify the concerns raised in the review comment by examining the actual test file and router implementation. Let me gather the necessary information:
🏁 Script executed:
Length of output: 65
🏁 Script executed:
Length of output: 116
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 39
🏁 Script executed:
Length of output: 513
🏁 Script executed:
Length of output: 5075
🏁 Script executed:
Length of output: 108
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 81
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 5009
🏁 Script executed:
Length of output: 5009
🏁 Script executed:
Length of output: 2229
🏁 Script executed:
Length of output: 355
🏁 Script executed:
Length of output: 402
🏁 Script executed:
Length of output: 5027
🏁 Script executed:
Length of output: 198
🏁 Script executed:
Length of output: 174
🏁 Script executed:
Length of output: 242
🏁 Script executed:
Length of output: 9955
🏁 Script executed:
Length of output: 726
🏁 Script executed:
Length of output: 1298
Router uses imported
dbsingleton, notctx.db—mockDb is unusedThe review comment is correct. The router definitively imports and uses the
dbsingleton from"main/lib/db"(line 2 of workspaces.ts), accessing it directly viadb.data.*anddb.update()throughout all resolvers. The router does not accept or usectx.dbat all.Since
mockDbis passed tocreateCaller({ db: mockDb as any })but the router never reads it, the tests do not exercise the mock. Instead:_dbis uninitialized, resolvers will throw "Database not initialized. Call initDb() first."_dbhappens to be set, tests will operate against the real singleton, making assertions onmockDb.datameaningless.To fix this:
"main/lib/db"at the test top-level to export{ db: mockDb }, so the router's import resolves to your mock; orctx.dbinstead of the imported singleton.🤖 Prompt for AI Agents