File size: 6,261 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("../request", () => ({
  request: vi.fn(),
}));

import { agentsApi } from "./agents";
import { request } from "../request";

describe("agentsApi", () => {
  beforeEach(() => {
    vi.mocked(request).mockResolvedValue(undefined);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  it("listAgents calls GET /agents", async () => {
    const data = { agents: [] };
    vi.mocked(request).mockResolvedValue(data);
    const result = await agentsApi.listAgents();
    expect(request).toHaveBeenCalledWith("/agents");
    expect(result).toEqual(data);
  });

  it("getAgent calls GET /agents/${id}", async () => {
    const data = { name: "a1" } as any;
    vi.mocked(request).mockResolvedValue(data);
    const result = await agentsApi.getAgent("a1");
    expect(request).toHaveBeenCalledWith("/agents/a1");
    expect(result).toEqual(data);
  });

  it("createAgent sends POST /agents with JSON body", async () => {
    const agent = { name: "new" } as any;
    const ref = { agent_id: "x" } as any;
    vi.mocked(request).mockResolvedValue(ref);
    const result = await agentsApi.createAgent(agent);
    expect(request).toHaveBeenCalledWith("/agents", {
      method: "POST",
      body: JSON.stringify(agent),
    });
    expect(result).toEqual(ref);
  });

  it("updateAgent sends PUT /agents/${id} with JSON body", async () => {
    const agent = { name: "updated" } as any;
    vi.mocked(request).mockResolvedValue(agent);
    const result = await agentsApi.updateAgent("a1", agent);
    expect(request).toHaveBeenCalledWith("/agents/a1", {
      method: "PUT",
      body: JSON.stringify(agent),
    });
    expect(result).toEqual(agent);
  });

  it("updateModelSettings sends a narrow PATCH request", async () => {
    const settings = {
      fallback_models: [{ provider_id: "openai", model: "fallback" }],
      subagent_model: null,
    };
    vi.mocked(request).mockResolvedValue(settings);

    const result = await agentsApi.updateModelSettings("a1", settings);

    expect(request).toHaveBeenCalledWith("/agents/a1/model-settings", {
      method: "PATCH",
      body: JSON.stringify(settings),
    });
    expect(result).toEqual(settings);
  });

  it("updates third-party model settings from Chat", async () => {
    await agentsApi.updateBackendSettings("a1", {
      model: "gpt-test-codex",
      reasoning_effort: "high",
    });
    expect(request).toHaveBeenCalledWith("/agents/a1/backend-settings", {
      method: "PATCH",
      body: JSON.stringify({
        model: "gpt-test-codex",
        reasoning_effort: "high",
      }),
    });
  });

  it("rebuildMemoryIndex sends POST with an extended timeout", async () => {
    const resp = { status: "completed" } as const;
    vi.mocked(request).mockResolvedValue(resp);
    const result = await agentsApi.rebuildMemoryIndex("a1");
    expect(request).toHaveBeenCalledWith("/agents/a1/memory/reindex", {
      method: "POST",
      timeout: 10 * 60 * 1000,
    });
    expect(result).toEqual(resp);
  });

  it("getMemoryStatus fetches structured ReMe status", async () => {
    const status = {
      components: {},
      components_total: "0 B",
      process_rss: "1.00 KiB",
    };
    vi.mocked(request).mockResolvedValue(status);

    const result = await agentsApi.getMemoryStatus("a1");

    expect(request).toHaveBeenCalledWith("/agents/a1/memory/status");
    expect(result).toEqual(status);
  });

  it("getMemoryStatus forwards a cancellation signal", async () => {
    const controller = new AbortController();

    await agentsApi.getMemoryStatus("a1", controller.signal);

    expect(request).toHaveBeenCalledWith("/agents/a1/memory/status", {
      signal: controller.signal,
    });
  });

  it("getMemoryRuntimeStatus fetches lightweight runtime state", async () => {
    const runtime = { reindexing: true } as any;
    const controller = new AbortController();
    vi.mocked(request).mockResolvedValue(runtime);

    const result = await agentsApi.getMemoryRuntimeStatus(
      "a1",
      controller.signal,
    );

    expect(request).toHaveBeenCalledWith("/agents/a1/memory/runtime-status", {
      signal: controller.signal,
    });
    expect(result).toEqual(runtime);
  });

  it("getMemoryGraph loads the indexed wikilink graph", async () => {
    const graph = { version: 1, nodes: [], edges: [] } as const;
    vi.mocked(request).mockResolvedValue(graph);
    const result = await agentsApi.getMemoryGraph("a1");
    expect(request).toHaveBeenCalledWith("/agents/a1/memory/graph");
    expect(result).toEqual(graph);
  });

  it("deleteAgent sends DELETE /agents/${id}", async () => {
    const resp = { success: true, agent_id: "a1" };
    vi.mocked(request).mockResolvedValue(resp);
    const result = await agentsApi.deleteAgent("a1");
    expect(request).toHaveBeenCalledWith("/agents/a1", {
      method: "DELETE",
    });
    expect(result).toEqual(resp);
  });

  it("reorderAgents sends PUT /agents/order with agent_ids", async () => {
    const resp = { success: true, agent_ids: ["a", "b"] } as any;
    vi.mocked(request).mockResolvedValue(resp);
    const result = await agentsApi.reorderAgents(["a", "b"]);
    expect(request).toHaveBeenCalledWith("/agents/order", {
      method: "PUT",
      body: JSON.stringify({ agent_ids: ["a", "b"] }),
    });
    expect(result).toEqual(resp);
  });

  it("toggleAgentEnabled sends PATCH with enabled flag", async () => {
    const resp = { success: true, agent_id: "a1", enabled: true };
    vi.mocked(request).mockResolvedValue(resp);
    const result = await agentsApi.toggleAgentEnabled("a1", true);
    expect(request).toHaveBeenCalledWith("/agents/a1/toggle", {
      method: "PATCH",
      body: JSON.stringify({ enabled: true }),
    });
    expect(result).toEqual(resp);
  });

  it("setAgentPinned sends PATCH with pinned flag", async () => {
    const resp = { success: true, agent_id: "a1", pinned: true };
    vi.mocked(request).mockResolvedValue(resp);
    const result = await agentsApi.setAgentPinned("a1", true);
    expect(request).toHaveBeenCalledWith("/agents/a1/pin", {
      method: "PATCH",
      body: JSON.stringify({ pinned: true }),
    });
    expect(result).toEqual(resp);
  });
});