File size: 6,387 Bytes
eb3f11e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { registerFleetCli } from "../fleet-cli.js";

const mocks = await vi.hoisted(async () => {
  const { createCliRuntimeMock } = await import("../test-runtime-mock.js");
  return {
    ...createCliRuntimeMock(vi),
    runFleetCreateCommand: vi.fn(),
    runFleetListCommand: vi.fn(),
    runFleetStatusCommand: vi.fn(),
    runFleetLogsCommand: vi.fn(),
    runFleetLifecycleCommand: vi.fn(),
    runFleetUpgradeCommand: vi.fn(),
    runFleetRemoveCommand: vi.fn(),
    runFleetBackupCommand: vi.fn(),
    runFleetRestoreCommand: vi.fn(),
    runFleetDoctorCommand: vi.fn(),
  };
});

vi.mock("../../runtime.js", () => ({
  defaultRuntime: mocks.defaultRuntime,
}));

vi.mock("./commands.runtime.js", () => ({
  runFleetCreateCommand: mocks.runFleetCreateCommand,
  runFleetListCommand: mocks.runFleetListCommand,
  runFleetStatusCommand: mocks.runFleetStatusCommand,
  runFleetLogsCommand: mocks.runFleetLogsCommand,
  runFleetLifecycleCommand: mocks.runFleetLifecycleCommand,
  runFleetUpgradeCommand: mocks.runFleetUpgradeCommand,
  runFleetRemoveCommand: mocks.runFleetRemoveCommand,
  runFleetBackupCommand: mocks.runFleetBackupCommand,
  runFleetRestoreCommand: mocks.runFleetRestoreCommand,
  runFleetDoctorCommand: mocks.runFleetDoctorCommand,
}));

function createProgram(): Command {
  const program = new Command();
  program.exitOverride();
  program.configureOutput({
    writeErr: () => undefined,
    writeOut: () => undefined,
  });
  registerFleetCli(program);
  return program;
}

async function runFleetCli(argv: string[]): Promise<void> {
  await createProgram().parseAsync(["fleet", ...argv], { from: "user" });
}

describe("fleet cli", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.runtimeLogs.length = 0;
    mocks.runtimeErrors.length = 0;
  });

  it("normalizes create options", async () => {
    await runFleetCli([
      "create",
      "tenant-a",
      "--image",
      "registry.example/openclaw:v1",
      "--runtime",
      "podman",
      "--port",
      "19123",
      "--memory",
      "3g",
      "--cpus",
      "1.5",
      "--disk",
      "10GB",
      "--network",
      "internal",
      "--pids-limit",
      "256",
      "--env",
      "CHANNEL_ID=alpha",
      "--env",
      "FEATURE_FLAG=1",
      "--gateway-token",
      "test-token",
      "--no-start",
      "--json",
    ]);

    expect(mocks.runFleetCreateCommand).toHaveBeenCalledOnce();
    expect(mocks.runFleetCreateCommand).toHaveBeenCalledWith({
      tenant: "tenant-a",
      image: "registry.example/openclaw:v1",
      runtime: "podman",
      port: 19_123,
      memory: "3g",
      cpus: "1.5",
      disk: "10GB",
      network: "internal",
      pidsLimit: 256,
      env: ["CHANNEL_ID=alpha", "FEATURE_FLAG=1"],
      gatewayToken: "test-token",
      start: false,
      json: true,
    });
  });

  it("applies create defaults", async () => {
    await runFleetCli(["create", "tenant-b"]);

    expect(mocks.runFleetCreateCommand).toHaveBeenCalledWith({
      tenant: "tenant-b",
      image: "ghcr.io/openclaw/openclaw:latest",
      runtime: "docker",
      port: undefined,
      memory: "2g",
      cpus: "2",
      disk: undefined,
      network: "bridge",
      pidsLimit: 512,
      env: [],
      gatewayToken: undefined,
      start: true,
      json: false,
    });
  });

  it.each(["list", "ls"])("routes fleet %s JSON output", async (command) => {
    await runFleetCli([command, "--json"]);

    expect(mocks.runFleetListCommand).toHaveBeenCalledOnce();
    expect(mocks.runFleetListCommand).toHaveBeenCalledWith({ json: true });
  });

  it("normalizes logs options", async () => {
    await runFleetCli([
      "logs",
      "acme",
      "--follow",
      "--timestamps",
      "--tail",
      "100",
      "--since",
      "10m",
    ]);

    expect(mocks.runFleetLogsCommand).toHaveBeenCalledWith({
      tenant: "acme",
      follow: true,
      timestamps: true,
      tail: 100,
      since: "10m",
    });
  });

  it("normalizes remove safety flags", async () => {
    await runFleetCli(["rm", "tenant-a", "--purge-data", "--force"]);

    expect(mocks.runFleetRemoveCommand).toHaveBeenCalledOnce();
    expect(mocks.runFleetRemoveCommand).toHaveBeenCalledWith({
      tenant: "tenant-a",
      purgeData: true,
      force: true,
    });
  });

  it("registers backup, restore, logs, and doctor options", async () => {
    await runFleetCli([
      "backup",
      "tenant-a",
      "--out",
      "/tmp/cell.tgz",
      "--max-bytes",
      "42",
      "--json",
    ]);
    await runFleetCli(["restore", "tenant-a", "--from", "/tmp/cell.tgz", "--force", "--json"]);
    await runFleetCli(["logs", "tenant-a", "--tail", "500", "--since", "10m"]);
    await runFleetCli(["doctor", "tenant-a", "--json"]);
    expect(mocks.runFleetBackupCommand).toHaveBeenCalledWith({
      tenant: "tenant-a",
      out: "/tmp/cell.tgz",
      maxBytes: 42,
      json: true,
    });
    expect(mocks.runFleetRestoreCommand).toHaveBeenCalledWith({
      tenant: "tenant-a",
      from: "/tmp/cell.tgz",
      force: true,
      json: true,
    });
    expect(mocks.runFleetLogsCommand).toHaveBeenCalledWith({
      tenant: "tenant-a",
      follow: false,
      timestamps: false,
      tail: 500,
      since: "10m",
    });
    expect(mocks.runFleetDoctorCommand).toHaveBeenCalledWith({ tenant: "tenant-a", json: true });
  });

  it.each([
    [["create", "tenant-a", "--runtime", "containerd"], /--runtime must be docker or podman/],
    [["create", "tenant-a", "--port", "65536"], /--port must be between 1 and 65535/],
    [["create", "tenant-a", "--cpus", "0"], /--cpus must be a positive number/],
    [["create", "tenant-a", "--cpus", "0x10"], /--cpus must be a positive number/],
    [["logs", "tenant-a", "--tail", "1.5"], /--tail must be a positive integer/],
    [["create", "tenant-a", "--disk", "10 g"], /--disk/],
    [["create", "tenant-a", "--network", "none"], /--network must be bridge or internal/],
    [["restore", "tenant-a"], /required option '--from/],
  ])("rejects invalid options: %s", async (argv, error) => {
    await expect(runFleetCli(argv)).rejects.toThrow(error);
    expect(mocks.runFleetCreateCommand).not.toHaveBeenCalled();
    expect(mocks.runFleetLogsCommand).not.toHaveBeenCalled();
  });
});