File size: 9,715 Bytes
fd2c364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from "vitest";
import { validateInterfaceManifest } from "#/manifests/interface-validation";
import type { InterfaceValidationContext } from "#/manifests/interface-validation";
import type {
  InterfaceIconSlug,
  InterfaceManifest,
  InterfaceSubPageId,
  OverviewMetric,
} from "#/manifests/types";
import {
  createInterfaceManifest,
  createInterfaceManifestWith,
  createInterfaceManifestWithSubPages,
} from "./manifest-test-data";

const CONTEXT: InterfaceValidationContext = {
  catalogIds: new Set([
    "github-pr-reviewer",
    "github-repo-monitor",
    "slack-channel-monitor",
  ]),
  mountedRoutes: {
    list: "/automations",
    setup: "/automations/new/:automationId",
    detail: "/automations/:automationId",
  },
};

/** The context of a host that also mounts the templates sub-page. */
const SUB_PAGE_CONTEXT: InterfaceValidationContext = {
  ...CONTEXT,
  mountedRoutes: {
    ...CONTEXT.mountedRoutes,
    templates: "/automations/templates",
  },
};

describe("validateInterfaceManifest", () => {
  it("admits a well-formed manifest", () => {
    // Arrange
    const manifest = createInterfaceManifest();

    // Act
    const result = validateInterfaceManifest(manifest, CONTEXT);

    // Assert
    expect(result).toEqual({ valid: true, errors: [] });
  });

  // Each case is a separate invariant the host enforces on data authored in
  // another repository. A manifest that trips any of them reverts the whole
  // interface to the host's defaults.
  it.each([
    ["a version this host cannot interpret", { version: "2.0" }],
    [
      // The host serves what it has registrations for; a manifest cannot remap
      // the router table, only own link construction against it.
      "a route the host has no registration for",
      {
        routes: {
          list: "/automations",
          setup: "/workflows/new/:automationId",
          detail: "/automations/:automationId",
        },
      },
    ],
    [
      "markup inside user-visible copy",
      {
        navigation: {
          sidebar: { label: "<img src=x onerror=alert(1)>" },
          commandMenu: {
            title: "Automations",
            description: "Review them.",
            keywords: "automate",
          },
        },
      },
    ],
    [
      "a documentation link outside the product documentation",
      { docsUrl: "https://evil.example/phishing" },
    ],
    [
      "an endpoint that is not a rooted service-relative path",
      {
        endpoints: {
          ...createInterfaceManifest().endpoints,
          list: "https://evil.example/v1",
        },
      },
    ],
    [
      "an id endpoint without its {id} substitution",
      {
        endpoints: {
          ...createInterfaceManifest().endpoints,
          detail: "/v1/latest",
        },
      },
    ],
    [
      "a substitution on an endpoint the host calls without an id",
      {
        endpoints: {
          ...createInterfaceManifest().endpoints,
          list: "/v1/{id}",
        },
      },
    ],
    [
      "a featured automation the catalog does not publish",
      { featuredAutomationIds: ["github-pr-reviewer", "unpublished-entry"] },
    ],
    [
      "an attribute the host cannot set",
      {
        attributes: {
          tarballPath: { type: "text", label: "Tarball", required: false },
        },
      },
    ],
    [
      "constraints on a non-number attribute",
      {
        attributes: {
          name: {
            type: "text",
            label: "Name",
            required: true,
            constraints: { max: 50 },
          },
        },
      },
    ],
    // The next three pin an admitted manifest to the semantics the edit
    // dialog implements, so it can never promise a control, a requiredness,
    // or a minimum the form would silently ignore.
    [
      "a control the host does not render for that attribute",
      {
        attributes: {
          name: { type: "textarea", label: "Name", required: true },
        },
      },
    ],
    [
      "a requiredness the host does not enforce",
      {
        attributes: {
          prompt: { type: "textarea", label: "Prompt", required: true },
        },
      },
    ],
    [
      "a minimum the host does not enforce",
      {
        attributes: {
          timeout: {
            type: "number",
            label: "Timeout",
            required: false,
            constraints: { min: 60, max: 900 },
          },
        },
      },
    ],
    [
      "a responder integration id that is not a lowercase slug",
      { responderIntegrationIds: ["GitHub!"] },
    ],
    ["a key this host does not read", { dashboards: [] }],
  ])("refuses %s", (_case, overrides) => {
    // Arrange
    const candidate = createInterfaceManifestWith(overrides);

    // Act
    const result = validateInterfaceManifest(candidate, CONTEXT);

    // Assert
    expect(result.valid).toBe(false);
  });

  it("admits a manifest declaring the complete sub-page surface", () => {
    // Arrange
    const manifest = createInterfaceManifestWithSubPages();

    // Act
    const result = validateInterfaceManifest(manifest, SUB_PAGE_CONTEXT);

    // Assert
    expect(result).toEqual({ valid: true, errors: [] });
  });

  // Each case breaks the sub-page surface in one specific way. As with the
  // base cases, one bad field reverts the whole manifest to host defaults.
  it.each<[string, (manifest: InterfaceManifest) => InterfaceManifest]>([
    [
      // Navigation, routes, and the dashboard sections describe one surface;
      // a partial declaration would render navigation to a missing page.
      "a sub-page surface declared only in part",
      (manifest) => ({
        ...manifest,
        pages: {
          list: manifest.pages.list,
          detail: manifest.pages.detail,
          edit: manifest.pages.edit,
        },
      }),
    ],
    [
      "a templates route the host does not mount",
      (manifest) => ({
        ...manifest,
        routes: { ...manifest.routes, templates: "/automations/library" },
      }),
    ],
    [
      "a sub-page this host does not serve",
      (manifest) => ({
        ...manifest,
        navigation: {
          ...manifest.navigation,
          subPages: [
            {
              page: "workflows" as InterfaceSubPageId,
              label: "Widget flows",
              icon: "sparkles",
            },
          ],
        },
      }),
    ],
    [
      "an icon outside the host's icon map",
      (manifest) => ({
        ...manifest,
        navigation: {
          ...manifest.navigation,
          subPages: [
            {
              page: "list",
              label: "Widget dashboard",
              icon: "rocket" as InterfaceIconSlug,
            },
          ],
        },
      }),
    ],
    [
      "a tile metric this host does not compute",
      (manifest) => ({
        ...manifest,
        pages: {
          ...manifest.pages,
          list: {
            ...manifest.pages.list,
            overview: {
              label: "Widget overview",
              tiles: [
                {
                  metric: "mean-time-between-failures" as OverviewMetric,
                  label: "Widget MTBF",
                  detail: "Recent widgets",
                  icon: "timer",
                },
              ],
            },
          },
        },
      }),
    ],
    [
      "tile copy using a placeholder its metric does not expose",
      (manifest) => ({
        ...manifest,
        pages: {
          ...manifest.pages,
          list: {
            ...manifest.pages.list,
            overview: {
              label: "Widget overview",
              tiles: [
                {
                  metric: "total-runs",
                  label: "Widget runs",
                  detail: "{{active}} runs",
                  icon: "activity",
                },
              ],
            },
          },
        },
      }),
    ],
    [
      // "all" is the host's initial selection and reset target; without it a
      // filter could never be neutral.
      "a filter without the all option",
      (manifest) => ({
        ...manifest,
        pages: {
          ...manifest.pages,
          list: {
            ...manifest.pages.list,
            filters: [
              {
                id: "status",
                label: "Filter widgets by state",
                options: [
                  { value: "active", label: "Live" },
                  { value: "disabled", label: "Off" },
                ],
              },
            ],
          },
        },
      }),
    ],
    [
      "a sort default the options do not offer",
      (manifest) => ({
        ...manifest,
        pages: {
          ...manifest.pages,
          list: {
            ...manifest.pages.list,
            sort: {
              label: "Order widgets",
              default: "name",
              options: [{ value: "last-run", label: "Latest" }],
            },
          },
        },
      }),
    ],
  ])("refuses %s", (_case, breakManifest) => {
    // Arrange
    const candidate = breakManifest(createInterfaceManifestWithSubPages());

    // Act
    const result = validateInterfaceManifest(candidate, SUB_PAGE_CONTEXT);

    // Assert
    expect(result.valid).toBe(false);
  });

  it("reports every problem at once so an author sees the whole picture", () => {
    // Arrange
    const candidate = createInterfaceManifestWith({
      docsUrl: "https://evil.example/",
      featuredAutomationIds: ["unpublished-entry"],
    });

    // Act
    const { errors } = validateInterfaceManifest(candidate, CONTEXT);

    // Assert
    expect(errors).toHaveLength(2);
  });
});