Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

138 linesCodeBlameRaw
1import 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(
78 form([
79 ["branchPolicy", "all"],
80 ["patternName", "main"],
81 ["adminsBypass", "on"],
82 ]),
83 limits,
84 );
85 assert.ok("change" in all);
86 assert.deepEqual(all.change.branchPatterns, []);
87 assert.equal(all.change.adminsBypass, true);
88 assert.equal(all.change.waitMinutes, 0);
89});
90
91test("a form past the limits says what is wrong", () => {
92 const many = form(
93 Array.from(
94 { length: 7 },
95 (_, i) =>
96 [
97 ["reviewerType", "user"],
98 ["reviewerName", `u${i}`],
99 ] as [string, string][],
100 ).flat(),
101 );
102 assert.deepEqual(environmentFromForm(many, limits), { error: "An environment can have up to 6 reviewers." });
103 assert.ok("error" in environmentFromForm(form([["waitMinutes", "43201"]]), limits));
104 assert.ok("error" in environmentFromForm(form([["waitMinutes", "-1"]]), limits));
105 assert.ok("error" in environmentFromForm(form([["branchPolicy", "selected"]]), limits));
106});
107
108test("an environment's rules in a few words", () => {
109 assert.deepEqual(environmentSummary({ ...base, protected: false }), []);
110 assert.deepEqual(environmentSummary(base), ["No reviewers, wait timer or branch limits"]);
111 assert.deepEqual(
112 environmentSummary({
113 ...base,
114 reviewers: [
115 { type: "user", name: "alice" },
116 { type: "team", name: "platform" },
117 ],
118 waitMinutes: 90,
119 branchPolicy: "protected",
120 }),
121 ["Reviewers: alice, @platform", "Wait 1h 30m", "Protected branches only"],
122 );
123 assert.deepEqual(
124 environmentSummary({
125 ...base,
126 reviewers: [
127 { type: "user", name: "a" },
128 { type: "user", name: "b" },
129 { type: "user", name: "c" },
130 ],
131 branchPolicy: "selected",
132 branchPatterns: [{ name: "main", type: "branch" }],
133 }),
134 ["3 reviewers", "1 branch and tag pattern"],
135 );
136 assert.equal(waitWords(1440 + 60), "1d 1h");
137 assert.equal(waitWords(43_200), "30d");
138});