File size: 2,599 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { request } from "../request";
import { checkpointsApi } from "./checkpoints";

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

describe("checkpointsApi", () => {
  beforeEach(() => vi.mocked(request).mockReset());

  it("previews a restore without changing its pinned commit", async () => {
    vi.mocked(request).mockResolvedValue({});
    const body = {
      commit: "a".repeat(40),
      session_id: "session",
      user_id: "user",
      channel: "console",
      include_memory: true,
      include_files: false,
    };

    await checkpointsApi.previewRestore(body);

    expect(request).toHaveBeenCalledWith(
      "/workspace/checkpoints/restore/preview",
      expect.objectContaining({
        method: "POST",
        body: JSON.stringify(body),
      }),
    );
  });

  it("sends selected files only to the confirm endpoint", async () => {
    vi.mocked(request).mockResolvedValue({});
    const body = {
      commit: "b".repeat(40),
      session_id: "session",
      user_id: "user",
      channel: "console",
      include_memory: false,
      include_files: true,
      files: ["src/app.ts"],
    };

    await checkpointsApi.restore(body);

    expect(request).toHaveBeenCalledWith(
      "/workspace/checkpoints/restore",
      expect.objectContaining({ body: JSON.stringify(body) }),
    );
  });

  it("uses retention GC by default and compact GC only when explicit", async () => {
    vi.mocked(request).mockResolvedValue({});

    await checkpointsApi.previewGc();
    await checkpointsApi.gc({ compact: true });

    expect(request).toHaveBeenNthCalledWith(
      1,
      "/workspace/checkpoints/gc/preview",
      expect.objectContaining({ body: "{}" }),
    );
    expect(request).toHaveBeenNthCalledWith(
      2,
      "/workspace/checkpoints/gc",
      expect.objectContaining({ body: JSON.stringify({ compact: true }) }),
    );
  });

  it("reads and updates automatic cleanup settings", async () => {
    vi.mocked(request).mockResolvedValue({});
    const settings = {
      gc_keep_count: 30,
      gc_keep_days: 14,
      pre_restore_retention_days: 5,
    };

    await checkpointsApi.getGcSettings();
    await checkpointsApi.updateGcSettings(settings);

    expect(request).toHaveBeenNthCalledWith(
      1,
      "/workspace/checkpoints/gc/settings",
    );
    expect(request).toHaveBeenNthCalledWith(
      2,
      "/workspace/checkpoints/gc/settings",
      expect.objectContaining({
        method: "PATCH",
        body: JSON.stringify(settings),
      }),
    );
  });
});