File size: 3,996 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import {
  __resetActiveStoreForTests,
  setActiveSelection,
  setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
import WorkspacesService from "#/api/workspaces-service/workspaces-service.api";

const {
  mockListWorkspaces,
  mockAddWorkspaces,
  mockDeleteWorkspace,
  mockAddWorkspaceParents,
  mockDeleteWorkspaceParent,
} = vi.hoisted(() => ({
  mockListWorkspaces: vi.fn(),
  mockAddWorkspaces: vi.fn(),
  mockDeleteWorkspace: vi.fn(),
  mockAddWorkspaceParents: vi.fn(),
  mockDeleteWorkspaceParent: vi.fn(),
}));

vi.mock("@openhands/typescript-client/clients", () => ({
  WorkspacesClient: vi.fn(function WorkspacesClientMock() {
    return {
      listWorkspaces: mockListWorkspaces,
      addWorkspaces: mockAddWorkspaces,
      deleteWorkspace: mockDeleteWorkspace,
      addWorkspaceParents: mockAddWorkspaceParents,
      deleteWorkspaceParent: mockDeleteWorkspaceParent,
    };
  }),
}));

const localBackend: Backend = {
  id: "local",
  name: "Local",
  host: "http://127.0.0.1:8000",
  apiKey: "",
  kind: "local",
};

beforeEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
  setRegisteredBackends([localBackend]);
  setActiveSelection({ backendId: localBackend.id });
  mockListWorkspaces.mockReset();
  mockAddWorkspaces.mockReset();
  mockDeleteWorkspace.mockReset();
  mockAddWorkspaceParents.mockReset();
  mockDeleteWorkspaceParent.mockReset();
});

afterEach(() => {
  __resetActiveStoreForTests();
});

describe("WorkspacesService", () => {
  it("listWorkspaces delegates to WorkspacesClient", async () => {
    // Arrange
    const body = {
      workspaces: [{ id: "/a", name: "a", path: "/a" }],
      workspaceParents: [{ id: "/p", name: "p", path: "/p" }],
    };
    mockListWorkspaces.mockResolvedValue(body);

    // Act
    const result = await WorkspacesService.listWorkspaces();

    // Assert
    expect(mockListWorkspaces).toHaveBeenCalledWith();
    expect(result).toEqual(body);
  });

  it("propagates the typed old-server error from WorkspacesClient", async () => {
    // Arrange
    const error = {
      code: "AGENT_SERVER_VERSION_TOO_OLD",
      feature: "workspaces",
      requiredVersion: "1.23.0",
      actualVersion: "1.22.1",
    };
    mockListWorkspaces.mockRejectedValue(error);

    // Act + Assert
    await expect(WorkspacesService.listWorkspaces()).rejects.toBe(error);
  });

  it("addWorkspaces delegates to WorkspacesClient", async () => {
    // Arrange
    mockAddWorkspaces.mockResolvedValue({
      workspaces: [],
      workspaceParents: [],
    });
    const items = [{ id: "/a", name: "a", path: "/a", parentPath: "/p" }];

    // Act
    await WorkspacesService.addWorkspaces(items);

    // Assert
    expect(mockAddWorkspaces).toHaveBeenCalledWith(items);
  });

  it("addWorkspaceParents delegates to WorkspacesClient", async () => {
    // Arrange
    mockAddWorkspaceParents.mockResolvedValue({
      workspaces: [],
      workspaceParents: [],
    });
    const parents = [{ id: "/p", name: "p", path: "/p" }];

    // Act
    await WorkspacesService.addWorkspaceParents(parents);

    // Assert
    expect(mockAddWorkspaceParents).toHaveBeenCalledWith(parents);
  });

  it("removeWorkspace delegates to WorkspacesClient", async () => {
    // Arrange
    mockDeleteWorkspace.mockResolvedValue({ deleted: true });

    // Act
    await WorkspacesService.removeWorkspace("/Users/me/dev/repo 1");

    // Assert
    expect(mockDeleteWorkspace).toHaveBeenCalledWith("/Users/me/dev/repo 1");
  });

  it("removeWorkspaceParent delegates to WorkspacesClient", async () => {
    // Arrange
    mockDeleteWorkspaceParent.mockResolvedValue({ deleted: true });

    // Act
    await WorkspacesService.removeWorkspaceParent("/parents/root");

    // Assert
    expect(mockDeleteWorkspaceParent).toHaveBeenCalledWith("/parents/root");
  });
});