Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import {
5 MIN_SCHEDULED_TASK_INTERVAL_MS,
6 ScheduledTaskSchedule,
7 ScheduledTaskUpsertSchedule,
8} from "./scheduledTask.ts";
9
10const decodeSchedule = Schema.decodeUnknownSync(ScheduledTaskSchedule);
11const decodeUpsertSchedule = Schema.decodeUnknownSync(ScheduledTaskUpsertSchedule);
12
13describe("ScheduledTaskSchedule", () => {
14 it("keeps legacy sub-minute persisted schedules readable", () => {
15 expect(
16 decodeSchedule({
17 type: "interval",
18 everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1,
19 }),
20 ).toEqual({
21 type: "interval",
22 everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1,
23 });
24 });
25
26 it("accepts a one-time run only with an offset", () => {
27 for (const runAt of ["2026-11-03T09:00Z", "2026-11-03T09:00:00.000-08:00"]) {
28 expect(decodeUpsertSchedule({ type: "once", runAt })).toEqual({ type: "once", runAt });
29 }
30 for (const runAt of [
31 "2026-11-03T09:00",
32 "2026-13-03T09:00Z",
33 "2026-02-30T09:00Z",
34 "9999-12-31T23:30-01:00",
35 "0000-01-01T00:00+00:01",
36 "tomorrow",
37 ]) {
38 expect(() => decodeUpsertSchedule({ type: "once", runAt })).toThrow();
39 }
40 });
41});
42
43it.each([
44 "literal",
45 " ",
46 "\t\n",
47 "{{body.bad",
48 "{{unknown.field}}",
49 "{{body.issue..id}}",
50 "{{body.issue. .id}}",
51 "{{query.foo.}}",
52 "{{query. }}",
53])("rejects invalid dedupe template %s", (replayKey) => {
54 expect(() => decodeUpsertSchedule({ type: "webhook", dedupe: { replayKey } })).toThrow();
55 expect(() =>
56 decodeUpsertSchedule({ type: "webhook", dedupe: { coalesceKey: replayKey } }),
57 ).toThrow();
58});
59it.each(["replayKey", "coalesceKey"])(
60 "explains a %s without a placeholder instead of 'Expected <filter>'",
61 (field) => {
62 const decode = () => decodeUpsertSchedule({ type: "webhook", dedupe: { [field]: "links" } });
63 expect(decode).toThrow(/placeholder/);
64 expect(decode).toThrow(/\{\{body\.id\}\}/);
65 expect(decode).not.toThrow(/Expected <filter>/);
66 },
67);
68it("accepts dedupe templates whose key names contain spaces", () => {
69 const dedupe = {
70 replayKey: "{{query.pull request}}",
71 coalesceKey: "pr-{{ body.pull request.id }}",
72 };
73 expect(decodeUpsertSchedule({ type: "webhook", dedupe })).toEqual({ type: "webhook", dedupe });
74});
75describe("webhook filters", () => {
76 const accepts = (filters: unknown) => {
77 try {
78 decodeUpsertSchedule({ type: "webhook", filters });
79 return true;
80 } catch {
81 return false;
82 }
83 };
84
85 it("rejects empty or unknown paths, unknown ops, and values that do not fit the op", () => {
86 expect(accepts([{ path: "", op: "exists" }])).toBe(false);
87 expect(accepts([{ path: " ", op: "exists" }])).toBe(false);
88 expect(accepts([{ path: "request", op: "exists" }])).toBe(false);
89 expect(accepts([{ path: "header.x-github-event", op: "exists" }])).toBe(false);
90 expect(accepts([{ path: "body.action", op: "in", value: "opened" }])).toBe(false);
91 expect(accepts([{ path: "body.action", op: "in", value: [] }])).toBe(false);
92 for (const value of ["", " opened", "opened ", "a,b"]) {
93 expect(accepts([{ path: "body.action", op: "in", value: [value] }])).toBe(false);
94 }
95 expect(accepts([{ path: "body.label.name", op: "in", value: ["needs review"] }])).toBe(true);
96 expect(accepts([{ path: "body.action", op: "equals" }])).toBe(false);
97 expect(accepts([{ path: "body.action", op: "matches", value: "^opened$" }])).toBe(false);
98 });
99});