File size: 14,839 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { getUpdateRun } from "../../infra/update-run-ledger.js";
import type { UpdateRunRecord } from "../../infra/update-run-record.js";
import { defaultRuntime } from "../../runtime.js";
import { formatCliJsonFailure } from "../failure-output.js";
import { createUpdateProgress, printResult } from "./progress.js";

vi.mock("../../infra/update-run-ledger.js", () => ({ getUpdateRun: vi.fn() }));

const runId = "6631ecee-adbf-41e8-a0e3-1b88b28b0a59";
const context = { runId, env: { OPENCLAW_STATE_DIR: "/isolated/update-progress" } };
const step = { name: "build", command: "pnpm build", index: 0, total: 1 };
const result = { runId, status: "ok" as const, mode: "git" as const, steps: [], durationMs: 1200 };

function runRecord(): UpdateRunRecord {
  return {
    runId,
    createdAtMs: 100,
    updatedAtMs: 100,
    trigger: "cli",
    status: "running",
    phase: "requested",
    reason: null,
    before: { version: "2026.9.2" },
    after: {},
    target: { version: "2026.9.3" },
    origin: {},
    steps: [{ step: "requested", status: "in_progress", startedAtMs: 100 }],
    verification: {},
    repair: [],
    confirmedAtMs: null,
    finishedAtMs: null,
    downtimeMs: null,
  };
}

describe("update progress", () => {
  let run: UpdateRunRecord;
  let presentation: ReturnType<typeof createUpdateProgress> | undefined;
  const tty = Object.getOwnPropertyDescriptor(process.stdout, "isTTY");

  beforeEach(() => {
    run = runRecord();
    vi.mocked(getUpdateRun).mockImplementation(() => run);
    Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: false });
  });

  afterEach(() => {
    presentation?.dispose();
    presentation = undefined;
    if (tty) {
      Object.defineProperty(process.stdout, "isTTY", tty);
    } else {
      Reflect.deleteProperty(process.stdout, "isTTY");
    }
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  it("replays rapid recorded phases once and preserves redirected step failures", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true, context);
    run.phase = "validating";
    run.steps.push(
      { step: "staging", status: "completed" },
      { step: "validating", status: "in_progress" },
    );
    presentation.progress.onStepStart?.(step);
    expect(log).toHaveBeenCalledWith("validating — build...");
    presentation.progress.onStepComplete?.({
      ...step,
      durationMs: 1200,
      exitCode: 1,
      stdoutTail: "Build type error",
    });
    const lines = log.mock.calls.flat();
    expect(lines.filter((line) => typeof line === "string" && line.startsWith("Phase:"))).toEqual([
      "Phase: requested",
      "Phase: staging",
      "Phase: validating",
    ]);
    expect(lines.join("\n")).toContain("Build type error");
  });

  it("does not leave a phase observer after initial observation fails", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    vi.mocked(getUpdateRun).mockImplementationOnce(() => {
      throw new Error("initial ledger read failed");
    });
    try {
      expect(() => createUpdateProgress(true, context)).toThrow("initial ledger read failed");
      run.phase = "verifying";
      run.steps = [
        { step: "requested", status: "completed" },
        { step: "verifying", status: "in_progress" },
      ];
      printResult(result, { run: context });
      const lines = log.mock.calls.flat();
      expect(lines.join("\n")).toContain("OpenClaw update in progress: verifying.");
      expect(lines.filter((line) => typeof line === "string" && line.startsWith("Phase:"))).toEqual(
        [],
      );
    } finally {
      // Replace and dispose a leaked observer when this regression runs on old code.
      vi.mocked(getUpdateRun).mockImplementation(() => run);
      presentation = createUpdateProgress(true, context);
      presentation.dispose();
      presentation = undefined;
    }
  });

  it("releases the terminal spinner when its final ledger read fails", () => {
    vi.useFakeTimers();
    vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    vi.spyOn(process.stdout, "write").mockImplementation(() => true);
    Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: true });
    const timerCount = vi.getTimerCount();
    const signals = ["SIGINT", "SIGTERM"] as const;
    const listenerCounts = signals.map((signal) => process.listenerCount(signal));
    presentation = createUpdateProgress(true, context);
    presentation.progress.onStepStart?.(step);
    expect(vi.getTimerCount()).toBeGreaterThan(timerCount);
    const failure = new Error("final ledger read failed");
    vi.mocked(getUpdateRun).mockImplementationOnce(() => {
      throw failure;
    });

    expect(() => presentation?.dispose()).toThrow(failure);

    expect(vi.getTimerCount()).toBe(timerCount);
    expect(signals.map((signal) => process.listenerCount(signal))).toEqual(listenerCounts);
  });

  it("keeps unbound step presentation independent of ledger records", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true);
    run.phase = "validating";
    run.steps.push({ step: "validating", status: "in_progress" });
    vi.mocked(getUpdateRun).mockImplementation(() => {
      throw new Error("unbound presentation must not read the ledger");
    });
    try {
      presentation.progress.onStepStart?.(step, run);
      presentation.progress.onStepComplete?.(
        { ...step, durationMs: 1200, exitCode: 1, stdoutTail: "Build type error" },
        run,
      );
      expect(log).toHaveBeenCalledWith("build...");
      expect(log.mock.calls.flat().join("\n")).toContain("Build type error");
      expect(
        log.mock.calls
          .flat()
          .filter((line) => typeof line === "string" && line.startsWith("Phase:")),
      ).toEqual([]);
    } finally {
      vi.mocked(getUpdateRun).mockImplementation(() => run);
    }
  });

  it.each([true, false])(
    "renders the report and phases from one snapshot (present: %s)",
    (present) => {
      const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
      presentation = createUpdateProgress(true, context);
      const captured: UpdateRunRecord = {
        ...run,
        phase: "verifying",
        steps: [
          { step: "requested", status: "completed" },
          { step: "verifying", status: "in_progress" },
        ],
      };
      const later: UpdateRunRecord = {
        ...captured,
        phase: "repairing",
        steps: [
          { step: "requested", status: "completed" },
          { step: "verifying", status: "completed" },
          { step: "repairing", status: "in_progress" },
        ],
      };
      vi.mocked(getUpdateRun)
        .mockReturnValueOnce(present ? captured : undefined)
        .mockReturnValue(later);
      try {
        printResult(result, { run: context });
        const lines = log.mock.calls.flat();
        expect(
          lines.filter((line) => typeof line === "string" && line.startsWith("Phase:")),
        ).toEqual(present ? ["Phase: requested", "Phase: verifying"] : ["Phase: requested"]);
        expect(lines.join("\n")).toContain(
          present ? "OpenClaw update in progress: verifying." : "OpenClaw updated.",
        );
        expect(log).not.toHaveBeenCalledWith("Phase: repairing");
      } finally {
        vi.mocked(getUpdateRun).mockImplementation(() => run);
      }
    },
  );

  it("prints the exact repair command from a recoverable step", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true, context);
    const message =
      "Skipped temporary cleanup. Run: rm -rf -- '/opt/update fixture/candidate'. Reason: permission denied";
    presentation.progress.onStepComplete?.({
      ...step,
      durationMs: 1,
      exitCode: 1,
      stderrTail: "permission denied",
      advisory: { kind: "recoverable-maintenance", message },
    });
    expect(log.mock.calls.flat().join("\n")).toContain(message);
  });

  it("shows recorded failure facts without replaying the child error envelope", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true, context);
    const envelope = formatCliJsonFailure(new Error("Unable to load plugin"), {
      argv: [],
      env: {},
    });
    const failed = {
      ...step,
      durationMs: 1,
      exitCode: 1,
      stdoutTail: JSON.stringify(envelope),
      stderrTail:
        "[openclaw] The CLI command failed.\n[openclaw] Reason: Unable to load plugin\n[openclaw] Help: openclaw --help",
      failureFacts: [{ check: "doctor", code: "doctor-failed", message: "Unable to load plugin" }],
    };
    presentation.progress.onStepComplete?.(failed);
    const progress = log.mock.calls.flat().join("\n");
    expect(progress.match(/Unable to load plugin/gu)).toHaveLength(1);
    expect(progress).not.toContain("The CLI command failed");
    log.mockClear();
    printResult(
      { ...result, runId: undefined, status: "error", steps: [{ ...failed, cwd: "/fixture" }] },
      {},
    );
    const report = log.mock.calls.flat().join("\n");
    expect(report.match(/Unable to load plugin/gu)).toHaveLength(1);
    expect(report).not.toContain("Help: openclaw --help");
    for (const stdoutTail of [
      "Additional diagnostic",
      JSON.stringify({ ...envelope, details: "Additional diagnostic" }),
    ]) {
      log.mockClear();
      const detailed = { ...failed, stdoutTail, cwd: "/fixture" };
      presentation.progress.onStepComplete?.(detailed);
      expect(log.mock.calls.flat().join("\n")).toContain("Additional diagnostic");
      log.mockClear();
      printResult({ ...result, runId: undefined, status: "error", steps: [detailed] }, {});
      expect(log.mock.calls.flat().join("\n")).toContain("Additional diagnostic");
    }
    log.mockClear();
    printResult(
      {
        ...result,
        runId: undefined,
        status: "error",
        steps: [
          {
            ...failed,
            cwd: "/fixture",
            stdoutTail: "x".repeat(160),
            stderrTail: `[openclaw] Reason: Unable to load plugin\nDistinct detail ${"y".repeat(160)}\n[openclaw] Help: openclaw --help\ndoctor: Candidate doctor failed (deadline exceeded) (1000ms)`,
          },
        ],
      },
      {},
    );
    expect(log.mock.calls.flat().join("\n")).toContain("deadline exceeded");
    log.mockClear();
    presentation.progress.onStepComplete?.({
      ...failed,
      stdoutTail: undefined,
      stderrTail: undefined,
    });
    expect(
      log.mock.calls
        .flat()
        .join("\n")
        .match(/Unable to load plugin/gu),
    ).toHaveLength(1);
  });

  it("follows restart verification after step progress stops and flushes before the final report", async () => {
    vi.useFakeTimers();
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true, context);
    presentation.stop();
    // The restarted gateway writes these phases while the CLI has no active step.
    run.phase = "verifying";
    run.steps.push(
      { step: "restarting", status: "completed" },
      { step: "verifying", status: "in_progress" },
    );
    await vi.waitFor(() => expect(log).toHaveBeenCalledWith("Phase: verifying"));
    expect(log).toHaveBeenCalledWith("Phase: restarting");
    expect(log).not.toHaveBeenCalledWith("Phase: repairing");
    run.phase = "finished";
    run.status = "succeeded";
    run.after = { version: "2026.9.3" };
    run.verification = { serviceRunning: true, versionMatch: true };
    printResult(result, { run: context });
    presentation.dispose();
    const lines = log.mock.calls.flat();
    const finalPhase = lines.indexOf("Phase: finished");
    const report = lines.findIndex(
      (line) => typeof line === "string" && line.includes("OpenClaw updated to 2026.9.3"),
    );
    expect(finalPhase).toBeGreaterThan(-1);
    expect(report).toBeGreaterThan(finalPhase);
    expect(lines.filter((line) => line === "Phase: verifying")).toHaveLength(1);
    expect(lines.filter((line) => line === "Phase: finished")).toHaveLength(1);
    expect(lines.join("\n")).toContain("service running; version verified");
  });

  it("keeps JSON stdout silent until one result containing the durable row", () => {
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    const writeJson = vi.spyOn(defaultRuntime, "writeJson").mockImplementation(() => {});
    presentation = createUpdateProgress(false, context);
    presentation.suspend();
    presentation.resume();
    presentation.progress.onStepStart?.(step);
    presentation.progress.onStepComplete?.({ ...step, durationMs: 1, exitCode: 0 });
    presentation.stop();
    run.phase = "finished";
    run.status = "succeeded";
    printResult(result, { json: true, run: context });
    expect(log).not.toHaveBeenCalled();
    expect(writeJson).toHaveBeenCalledExactlyOnceWith({ ...result, run });
  });

  it("suspends every ledger reader through activation and resumes the recorded timeline", () => {
    vi.useFakeTimers();
    const log = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
    presentation = createUpdateProgress(true, context);
    presentation.suspend();
    const read = vi
      .mocked(getUpdateRun)
      .mockClear()
      .mockImplementation(() => {
        throw new Error("candidate owns the migrated ledger");
      });
    presentation.progress.onStepStart?.(step);
    presentation.progress.onStepComplete?.({ ...step, durationMs: 10, exitCode: 0 });
    vi.advanceTimersByTime(500);
    expect(read).not.toHaveBeenCalled();
    expect(log).toHaveBeenCalledWith("build...");

    run.phase = "verifying";
    run.steps.push(
      { step: "activating", status: "completed" },
      { step: "restarting", status: "completed" },
      { step: "verifying", status: "in_progress" },
    );
    read.mockImplementation(() => run);
    presentation.resume();
    vi.advanceTimersByTime(500);
    expect(read).toHaveBeenCalled();
    expect(
      log.mock.calls.flat().filter((line) => typeof line === "string" && line.startsWith("Phase:")),
    ).toEqual(["Phase: requested", "Phase: activating", "Phase: restarting", "Phase: verifying"]);

    presentation.suspend();
    read.mockClear().mockImplementation(() => {
      throw new Error("candidate owns the migrated ledger");
    });
    presentation.dispose();
    presentation.dispose();
    presentation.resume();
    vi.advanceTimersByTime(500);
    expect(read).not.toHaveBeenCalled();
  });
});