File size: 2,394 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { afterEach, describe, expect, it } from "vitest";

import WorkspacesService from "#/api/workspaces-service/workspaces-service.api";
import { resetMockWorkspaces } from "#/mocks/handlers";

describe("mock workspaces handlers", () => {
  afterEach(() => {
    resetMockWorkspaces();
  });

  it("starts with an empty workspaces list", async () => {
    const response = await WorkspacesService.listWorkspaces();
    expect(response.workspaces).toEqual([]);
    expect(response.workspaceParents).toEqual([]);
  });

  it("persists added workspaces across list calls", async () => {
    await WorkspacesService.addWorkspaces([
      { id: "w1", name: "Project", path: "/workspace/project" },
    ]);

    const response = await WorkspacesService.listWorkspaces();
    expect(response.workspaces).toEqual([
      { id: "w1", name: "Project", path: "/workspace/project" },
    ]);
  });

  it("upserts a workspace when path already exists", async () => {
    await WorkspacesService.addWorkspaces([
      { id: "w1", name: "Old", path: "/workspace/project" },
    ]);
    await WorkspacesService.addWorkspaces([
      { id: "w2", name: "New", path: "/workspace/project" },
    ]);
    const response = await WorkspacesService.listWorkspaces();
    expect(response.workspaces).toHaveLength(1);
    expect(response.workspaces[0].name).toBe("New");
  });

  it("removes a workspace by path", async () => {
    await WorkspacesService.addWorkspaces([
      { id: "w1", name: "Project", path: "/workspace/project" },
      { id: "w2", name: "Other", path: "/workspace/other" },
    ]);

    await WorkspacesService.removeWorkspace("/workspace/project");

    const response = await WorkspacesService.listWorkspaces();
    expect(response.workspaces.map((w) => w.path)).toEqual([
      "/workspace/other",
    ]);
  });

  it("persists workspace parents and removes them by path", async () => {
    await WorkspacesService.addWorkspaceParents([
      { id: "p1", name: "Repos", path: "/workspace/repos" },
    ]);

    const afterAdd = await WorkspacesService.listWorkspaces();
    expect(afterAdd.workspaceParents).toEqual([
      { id: "p1", name: "Repos", path: "/workspace/repos" },
    ]);

    await WorkspacesService.removeWorkspaceParent("/workspace/repos");

    const afterRemove = await WorkspacesService.listWorkspaces();
    expect(afterRemove.workspaceParents).toEqual([]);
  });
});