Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion apps/desktop/src/lib/trpc/routers/workspaces/workspaces.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { homedir } from "node:os";
import { join } from "node:path";
import { beforeEach, describe, expect, it, mock } from "bun:test";
import { createWorkspacesRouter } from "./workspaces";

Expand Down Expand Up @@ -54,7 +56,10 @@ mock.module("main/lib/db", () => ({
let mockRemoveWorktree = mock((_mainRepoPath: string, _worktreePath: string) =>
Promise.resolve(),
);
const mockCreateWorktree = mock(() => Promise.resolve());
const mockCreateWorktree = mock(
(_mainRepoPath: string, _branch: string, _worktreePath: string) =>
Promise.resolve(),
);
const mockGenerateBranchName = mock(() => "test-branch-123");

mock.module("./utils/git", () => ({
Expand Down Expand Up @@ -108,6 +113,50 @@ beforeEach(() => {
};
});

describe("workspaces router - create", () => {
it("creates worktree under home superset worktrees path", async () => {
const router = createWorkspacesRouter();
const caller = router.createCaller({});

const result = await caller.create({
projectId: "project-1",
name: "New Workspace",
});

const expectedPath = join(
homedir(),
".superset",
"worktrees",
"test-branch-123",
);

expect(mockCreateWorktree).toHaveBeenCalledWith(
"/path/to/repo",
"test-branch-123",
expectedPath,
);
expect(result.worktreePath).toBe(expectedPath);
expect(result.workspace.name).toBe("New Workspace");
expect(result.workspace.tabOrder).toBe(1);

const createdWorktree = mockDb.data.worktrees.find(
(worktree) =>
worktree.branch === "test-branch-123" &&
worktree.path === expectedPath &&
worktree.projectId === "project-1",
);
expect(createdWorktree).toBeTruthy();

const createdWorkspace = mockDb.data.workspaces.find(
(workspace) => workspace.id === result.workspace.id,
);
expect(createdWorkspace?.worktreeId).toBe(createdWorktree?.id);
expect(mockDb.data.settings.lastActiveWorkspaceId).toBe(
result.workspace.id,
);
});
});

describe("workspaces router - delete", () => {
it("should successfully delete workspace and remove worktree", async () => {
const router = createWorkspacesRouter();
Expand Down
9 changes: 8 additions & 1 deletion apps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { homedir } from "node:os";
import { join } from "node:path";
import { db } from "main/lib/db";
import { nanoid } from "nanoid";
import { SUPERSET_DIR_NAME, WORKTREES_DIR_NAME } from "shared/constants";
import simpleGit from "simple-git";
import { z } from "zod";
import { publicProcedure, router } from "../..";
Expand Down Expand Up @@ -28,7 +30,12 @@ export const createWorkspacesRouter = () => {

const branch = generateBranchName();

const worktreePath = join(project.mainRepoPath, ".superset", branch);
const worktreePath = join(
homedir(),
SUPERSET_DIR_NAME,
WORKTREES_DIR_NAME,
branch,
);

await createWorktree(project.mainRepoPath, branch, worktreePath);

Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export const PLATFORM = {
};

export const NOTIFICATIONS_PORT = 31415;

export const SUPERSET_DIR_NAME = ".superset";
export const WORKTREES_DIR_NAME = "worktrees";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Loading