File size: 5,670 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it, vi } from "vitest";
import { GatewayClientRequestError } from "../../gateway/client.js";
import { defaultRuntime } from "../../runtime.js";
import {
  ExpectedCliError,
  formatCliFailureLines,
  formatCliJsonFailure,
} from "../failure-output.js";
import { CronCliError } from "./cron-cli-error.js";
import { handleCronCliError } from "./shared.js";

describe("handleCronCliError", () => {
  it("renders typed automation lookup misses with the cron list recovery command", () => {
    const error = new GatewayClientRequestError({
      code: "INVALID_REQUEST",
      message: "transport-neutral lookup miss",
      details: { code: "CRON_JOB_NOT_FOUND", jobId: "missing-job" },
    });
    const errorOutput = vi.spyOn(defaultRuntime, "error").mockImplementation(() => {});
    const exit = vi.spyOn(defaultRuntime, "exit").mockImplementation(((code: number) => {
      throw new Error(`exit ${code}`);
    }) as never);

    expect(() => handleCronCliError(error)).toThrow("exit 1");
    expect(errorOutput).toHaveBeenCalledWith(
      expect.stringContaining(
        "Automation not found: missing-job. Run `openclaw cron list` to see recent automation ids.",
      ),
    );
    errorOutput.mockRestore();
    exit.mockRestore();
  });

  it.each([
    {
      label: "typed lookup miss",
      error: new GatewayClientRequestError({
        code: "INVALID_REQUEST",
        message: "transport-neutral lookup miss",
        details: { code: "CRON_JOB_NOT_FOUND", jobId: "missing-job" },
      }),
      message:
        "Automation not found: missing-job. Run `openclaw cron list` to see recent automation ids.",
    },
    {
      label: "local validation failure",
      error: new CronCliError("Invalid --stagger; use e.g. 30s, 1m, 5m"),
      message: "Invalid --stagger; use e.g. 30s, 1m, 5m",
    },
  ])(
    "hands a $label to the root renderer as an expected machine-output failure",
    ({ error, message }) => {
      const argv = process.argv;
      process.argv = [...argv.slice(0, 2), "cron", "show", "missing-job", "--json"];
      try {
        let thrown: unknown;
        try {
          handleCronCliError(error);
        } catch (caught) {
          thrown = caught;
        }
        expect(thrown).toBeInstanceOf(ExpectedCliError);
        expect(formatCliJsonFailure(thrown)).toEqual({
          ok: false,
          error: { type: "cli_error", message },
        });
        const stderr = formatCliFailureLines({
          title: "Could not start the CLI.",
          error: thrown,
          argv: process.argv,
        }).join("\n");
        expect(stderr).toContain(message);
        expect(stderr).not.toContain("Could not start the CLI.");
        expect(stderr).not.toContain("openclaw doctor");
        expect(stderr).not.toContain("OPENCLAW_DEBUG");
      } finally {
        process.argv = argv;
      }
    },
  );

  // A legacy gateway without cron.get makes `cron edit <id> --exact` wrap the
  // lookup miss; the renderer only reveals such causes on explicit debug intent.
  it.each([
    {
      label: "stays terse without debug intent",
      flags: [] as string[],
      debug: "",
      causeShown: false,
    },
    { label: "keeps causes for --debug", flags: ["--debug"], debug: "", causeShown: true },
    { label: "keeps causes for OPENCLAW_DEBUG", flags: [], debug: "1", causeShown: true },
  ])("machine output for a wrapped cron failure $label", ({ flags, debug, causeShown }) => {
    const wrapped = new CronCliError("unknown automation id: missing-job", {
      cause: new Error("unknown method: cron.get"),
    });
    const argv = process.argv;
    process.argv = [
      ...argv.slice(0, 2),
      "cron",
      "edit",
      "missing-job",
      "--exact",
      "--json",
      ...flags,
    ];
    vi.stubEnv("OPENCLAW_DEBUG", debug);
    try {
      let thrown: unknown;
      try {
        handleCronCliError(wrapped);
      } catch (caught) {
        thrown = caught;
      }
      expect(thrown).toBeInstanceOf(ExpectedCliError);
      const machineMessage = formatCliJsonFailure(thrown).error.message;
      expect(machineMessage).toContain("unknown automation id: missing-job");
      expect(machineMessage.includes("unknown method: cron.get")).toBe(causeShown);
      const stderr = formatCliFailureLines({
        title: "The CLI command failed.",
        error: thrown,
      }).join("\n");
      expect(stderr).toContain("unknown automation id: missing-job");
      expect(stderr.includes("unknown method: cron.get")).toBe(causeShown);
    } finally {
      vi.unstubAllEnvs();
      process.argv = argv;
    }
  });

  it.each([false, true])("preserves unexpected machine-mode errors with debug=%s", (debug) => {
    const error = new Error("Automation runtime failed", {
      cause: new Error("Runtime load failed"),
    });
    const argv = process.argv;
    process.argv = [...argv.slice(0, 2), "automations", "status", "--json"];
    vi.stubEnv("OPENCLAW_DEBUG", debug ? "1" : "");
    try {
      let thrown: unknown;
      try {
        handleCronCliError(error);
      } catch (caught) {
        thrown = caught;
      }
      expect(thrown).toBe(error);
      const stderr = formatCliFailureLines({
        title: "The CLI command failed.",
        error: thrown,
      }).join("\n");
      expect(stderr).toContain("The CLI command failed.");
      expect(stderr).toContain("openclaw doctor");
      expect(stderr.includes("Stack:")).toBe(debug);
      expect(formatCliJsonFailure(thrown).error.message.includes("Runtime load failed")).toBe(
        debug,
      );
    } finally {
      vi.unstubAllEnvs();
      process.argv = argv;
    }
  });
});