File size: 4,216 Bytes
a070805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from "vitest";
import type { Automation } from "#/types/automation";
import {
  AutomationFileValidationError,
  parseAutomationFile,
  serializeAutomation,
} from "./automation-export";

const cronAutomation: Automation = {
  id: "automation-1",
  name: "Daily review",
  trigger: {
    type: "cron",
    schedule: "0 9 * * *",
    schedule_human: "Daily at 09:00",
  },
  enabled: true,
  repository: "openhands/agent-canvas",
  branch: "main",
  model: "fast",
  prompt: "Review open pull requests.",
  plugins: ["github:openhands/extensions"],
  notification: "Post a summary",
  timezone: "America/Los_Angeles",
  created_at: "2026-07-01T00:00:00Z",
  updated_at: "2026-07-02T00:00:00Z",
  last_triggered_at: "2026-07-03T00:00:00Z",
};

describe("automation export files", () => {
  it("round-trips a cron automation without server-assigned fields", () => {
    const exported = serializeAutomation(cronAutomation);

    expect(exported).toEqual({
      version: 1,
      kind: "automation",
      spec: {
        name: cronAutomation.name,
        trigger: cronAutomation.trigger,
        enabled: true,
        repository: cronAutomation.repository,
        model: cronAutomation.model,
        prompt: cronAutomation.prompt,
        branch: cronAutomation.branch,
        plugins: cronAutomation.plugins,
        notification: cronAutomation.notification,
        timezone: cronAutomation.timezone,
      },
    });
    expect(parseAutomationFile(exported)).toEqual(exported.spec);
    expect(exported.spec).not.toHaveProperty("id");
    expect(exported.spec).not.toHaveProperty("created_at");
    expect(exported.spec).not.toHaveProperty("updated_at");
    expect(exported.spec).not.toHaveProperty("last_triggered_at");
  });

  it("round-trips event trigger fields", () => {
    const exported = serializeAutomation({
      ...cronAutomation,
      trigger: {
        type: "event",
        source: "github",
        on: ["pull_request.opened", "pull_request.synchronize"],
        filter: "repository.full_name == 'openhands/agent-canvas'",
      },
      timezone: undefined,
    });

    expect(parseAutomationFile(exported).trigger).toEqual(
      exported.spec.trigger,
    );
  });

  it("preserves a positive timeout through an export/import round-trip", () => {
    const withTimeout: Automation = { ...cronAutomation, timeout: 900 };

    const exported = serializeAutomation(withTimeout);

    expect(parseAutomationFile(exported).timeout).toBe(900);
  });

  it("reports every malformed field with its path", () => {
    const malformed = {
      version: 2,
      kind: "workflow",
      spec: {
        name: "",
        prompt: null,
        enabled: "yes",
        trigger: { type: "event", source: 42, on: [] },
        plugins: ["github:openhands/extensions", 42],
        model: false,
        timeout: -5,
      },
    };

    expect(() => parseAutomationFile(malformed)).toThrow(
      AutomationFileValidationError,
    );

    try {
      parseAutomationFile(malformed);
    } catch (error) {
      expect(error).toBeInstanceOf(AutomationFileValidationError);
      expect((error as AutomationFileValidationError).issues).toEqual(
        expect.arrayContaining([
          "version: expected 1",
          'kind: expected "automation"',
          "spec.name: expected a non-empty string",
          "spec.prompt: expected a non-empty string",
          "spec.enabled: expected a boolean",
          "spec.trigger.source: expected a string",
          "spec.trigger.source: required for an event trigger",
          "spec.trigger.on: expected a string or a non-empty array of strings",
          "spec.trigger.on: required for an event trigger",
          "spec.plugins: expected an array of non-empty strings",
          "spec.model: expected a non-empty string or null",
          "spec.timeout: expected a positive integer",
        ]),
      );
    }
  });

  it("rejects non-object files and missing specs", () => {
    expect(() => parseAutomationFile(null)).toThrow(
      "file: expected a JSON object",
    );
    expect(() =>
      parseAutomationFile({ version: 1, kind: "automation" }),
    ).toThrow("spec: expected an object");
  });
});