Skip to content
122 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Actions: keep workflow runs safe1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { Environment } from "@g1t/contracts";
5
6import { environmentFromForm, environmentName, environmentSummary, waitWords } from "./environments.ts";
7
8const limits = { reviewers: 6, waitMinutes: 43_200 };
9
10function form(entries: [string, string][]): FormData {
11 const data = new FormData();
12 for (const [key, value] of entries) data.append(key, value);
13 return data;
14}
15
16const base: Environment = {
17 name: "production",
18 reviewers: [],
19 preventSelfReview: false,
20 waitMinutes: 0,
21 branchPolicy: "all",
22 branchPatterns: [],
23 adminsBypass: true,
24 protected: true,
25 updatedAt: null,
26 updatedBy: null,
27};
28
29test("an environment's name is lowercased, and anything else is refused", () => {
30 assert.equal(environmentName(" Production "), "production");
31 assert.equal(environmentName("eu-west_2"), "eu-west_2");
32 assert.equal(environmentName("prod env"), null);
33 assert.equal(environmentName("a".repeat(41)), null);
34 assert.equal(environmentName(""), null);
35});
36
37test("a form's rules: reviewers in order without blanks or repeats, patterns only for selected branches", () => {
38 const read = environmentFromForm(
39 form([
40 ["reviewerType", "user"],
41 ["reviewerName", "@alice"],
42 ["reviewerType", "team"],
43 ["reviewerName", "platform"],
44 ["reviewerType", "user"],
45 ["reviewerName", " "],
46 ["reviewerType", "user"],
47 ["reviewerName", "Alice"],
48 ["preventSelfReview", "on"],
49 ["waitMinutes", "30"],
50 ["branchPolicy", "selected"],
51 ["patternType", "branch"],
52 ["patternName", "release/*"],
53 ["patternType", "tag"],
54 ["patternName", "v*"],
55 ["patternType", "branch"],
56 ["patternName", ""],
57 ]),
58 limits,
59 );
60 assert.deepEqual(read, {
61 change: {
62 reviewers: [
63 { type: "user", name: "alice" },
64 { type: "team", name: "platform" },
65 ],
66 preventSelfReview: true,
67 waitMinutes: 30,
68 branchPolicy: "selected",
69 branchPatterns: [
70 { name: "release/*", type: "branch" },
71 { name: "v*", type: "tag" },
72 ],
73 adminsBypass: false,
74 },
75 });
76
77 const all = environmentFromForm(form([["branchPolicy", "all"], ["patternName", "main"], ["adminsBypass", "on"]]), limits);
78 assert.ok("change" in all);
79 assert.deepEqual(all.change.branchPatterns, []);
80 assert.equal(all.change.adminsBypass, true);
81 assert.equal(all.change.waitMinutes, 0);
82});
83
84test("a form past the limits says what is wrong", () => {
85 const many = form(Array.from({ length: 7 }, (_, i) => [["reviewerType", "user"], ["reviewerName", `u${i}`]] as [string, string][]).flat());
86 assert.deepEqual(environmentFromForm(many, limits), { error: "An environment can have up to 6 reviewers." });
87 assert.ok("error" in environmentFromForm(form([["waitMinutes", "43201"]]), limits));
88 assert.ok("error" in environmentFromForm(form([["waitMinutes", "-1"]]), limits));
89 assert.ok("error" in environmentFromForm(form([["branchPolicy", "selected"]]), limits));
90});
91
92test("an environment's rules in a few words", () => {
93 assert.deepEqual(environmentSummary({ ...base, protected: false }), []);
94 assert.deepEqual(environmentSummary(base), ["No reviewers, wait timer or branch limits"]);
95 assert.deepEqual(
96 environmentSummary({
97 ...base,
98 reviewers: [
99 { type: "user", name: "alice" },
100 { type: "team", name: "platform" },
101 ],
102 waitMinutes: 90,
103 branchPolicy: "protected",
104 }),
105 ["Reviewers: alice, @platform", "Wait 1h 30m", "Protected branches only"],
106 );
107 assert.deepEqual(
108 environmentSummary({
109 ...base,
110 reviewers: [
111 { type: "user", name: "a" },
112 { type: "user", name: "b" },
113 { type: "user", name: "c" },
114 ],
115 branchPolicy: "selected",
116 branchPatterns: [{ name: "main", type: "branch" }],
117 }),
118 ["3 reviewers", "1 branch and tag pattern"],
119 );
120 assert.equal(waitWords(1440 + 60), "1d 1h");
121 assert.equal(waitWords(43_200), "30d");
122});