File size: 3,371 Bytes
63522a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { AUTOMATION_CATALOG } from "@openhands/extensions/automations";
import customAutomationFixture from "@openhands/extensions/testing/automations/custom-automation.json";
import { describe, expect, it } from "vitest";
import { buildCreatePayload, buildPreflightBody } from "./automation-setup";
import type { SetupEntry, SetupFormValues, SetupRequestBody } from "./types";
import { validateSetupEntry } from "./manifest-validation";

const customAutomation = AUTOMATION_CATALOG.find(
  (entry) => entry.id === "custom-automation",
) as SetupEntry | undefined;

describe("custom automation setup actions", () => {
  it("admits the custom automation action manifest", () => {
    expect(customAutomation).toBeDefined();
    expect(validateSetupEntry(customAutomation).errors).toEqual([]);
  });

  it.each(customAutomationFixture.scenarios)(
    "derives the $id preflight and create payloads",
    (scenario) => {
      expect(customAutomation).toBeDefined();
      const formValues = scenario.formValues as unknown as SetupFormValues;
      const createBody = scenario.create.request
        .body as unknown as SetupRequestBody;
      const tarballPath =
        typeof createBody.tarball_path === "string"
          ? createBody.tarball_path
          : undefined;

      expect(
        buildPreflightBody(
          customAutomation!,
          formValues,
          scenario.selectedTrigger,
          scenario.selectedAction,
        ),
      ).toEqual(scenario.preflight.request.body);
      expect(
        buildCreatePayload(
          customAutomation!,
          formValues,
          tarballPath,
          scenario.selectedTrigger,
          scenario.selectedAction,
        ),
      ).toEqual(createBody);
    },
  );

  it("keeps event filters rendered from non-filter form fields", () => {
    const entry: SetupEntry = {
      id: "mention-responder",
      name: "Mention responder",
      description: "Respond to matching comments.",
      requires: { integrations: {} },
      setup: {
        version: "1.0",
        mode: "direct",
        form: {
          triggers: {
            event: {
              source: {
                type: "event-source",
                label: "Source",
                help: "Where events come from.",
                required: true,
              },
              on: {
                type: "event-type",
                label: "Event",
                help: "Which event to watch.",
                required: true,
              },
              mention: {
                type: "text",
                label: "Mention",
                help: "Text that must appear in the comment.",
                required: true,
              },
            },
          },
          args: {},
        },
        prompt: "Handle {{form.mention}}.",
        filter: "icontains(comment.body, '{{form.mention}}')",
      },
    };

    expect(
      buildCreatePayload(
        entry,
        {
          source: "github",
          on: "issue_comment.created",
          mention: "@openhands",
        },
        undefined,
        "event",
      ),
    ).toEqual({
      name: "Mention responder",
      prompt: "Handle @openhands.",
      trigger: {
        type: "event",
        source: "github",
        on: "issue_comment.created",
        filter: "icontains(comment.body, '@openhands')",
      },
    });
  });
});