Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

scheduledTask.test.ts

60 lines2.2 KB

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​
27it.each(["literal", " ", "\t\n", "{{body.bad", "{{unknown.field}}"])(
28 "rejects invalid dedupe template %s",
29 (replayKey) => {
30 expect(() => decodeUpsertSchedule({ type: "webhook", dedupe: { replayKey } })).toThrow();
31 expect(() =>
32 decodeUpsertSchedule({ type: "webhook", dedupe: { coalesceKey: replayKey } }),
33 ).toThrow();
34 },
35);
36describe("webhook filters", () => {
37 const accepts = (filters: unknown) => {
38 try {
39 decodeUpsertSchedule({ type: "webhook", filters });
40 return true;
41 } catch {
42 return false;
43 }
44 };
45​
46 it("rejects empty or unknown paths, unknown ops, and values that do not fit the op", () => {
47 expect(accepts([{ path: "", op: "exists" }])).toBe(false);
48 expect(accepts([{ path: " ", op: "exists" }])).toBe(false);
49 expect(accepts([{ path: "request", op: "exists" }])).toBe(false);
50 expect(accepts([{ path: "header.x-github-event", op: "exists" }])).toBe(false);
51 expect(accepts([{ path: "body.action", op: "in", value: "opened" }])).toBe(false);
52 expect(accepts([{ path: "body.action", op: "in", value: [] }])).toBe(false);
53 for (const value of ["", " opened", "opened ", "a,b"]) {
54 expect(accepts([{ path: "body.action", op: "in", value: [value] }])).toBe(false);
55 }
56 expect(accepts([{ path: "body.label.name", op: "in", value: ["needs review"] }])).toBe(true);
57 expect(accepts([{ path: "body.action", op: "equals" }])).toBe(false);
58 expect(accepts([{ path: "body.action", op: "matches", value: "^opened$" }])).toBe(false);
59 });
60});