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
|
import { describe, it, expect } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
AUTOMATION_DEFAULTS,
getAutomationSettings,
isAutomationOn,
normalizeMode,
resolveEffectiveMode,
settingsFromRow,
type AutomationSettings,
type AutomationSettingsLoader,
} from "../lib/automation-settings";
import { evaluateAutoMerge } from "../lib/auto-merge";
const REPO_ID = "11111111-2222-3333-4444-555555555555";
function makeSettings(
overrides: Partial<AutomationSettings> = {}
): AutomationSettings {
return { ...AUTOMATION_DEFAULTS, ...overrides };
}
function makeLoader(settings: AutomationSettings): {
loader: AutomationSettingsLoader;
calls: string[];
} {
const calls: string[] = [];
return {
loader: async (repositoryId: string) => {
calls.push(repositoryId);
return settings;
},
calls,
};
}
describe("AUTOMATION_DEFAULTS — match pre-0106 behavior", () => {
it("advisory features default to 'suggest'", () => {
expect(AUTOMATION_DEFAULTS.aiReviewMode).toBe("suggest");
expect(AUTOMATION_DEFAULTS.prTriageMode).toBe("suggest");
expect(AUTOMATION_DEFAULTS.issueTriageMode).toBe("suggest");
expect(AUTOMATION_DEFAULTS.ciAutofixMode).toBe("suggest");
});
it("auto-merge defaults to 'off' — developers opt in, not opt out", () => {
expect(AUTOMATION_DEFAULTS.autoMergeMode).toBe("off");
});
it("auto-repair defaults to 'off' — bot never commits to branches unless owner enables it", () => {
expect(AUTOMATION_DEFAULTS.autoRepairMode).toBe("off");
});
it("auto-issues defaults to 'off' — off by default, owner opts in", () => {
expect(AUTOMATION_DEFAULTS.autoIssuesMode).toBe("off");
});
it("doc-drift defaults to 'off' — off by default, owner opts in", () => {
expect(AUTOMATION_DEFAULTS.docDriftMode).toBe("off");
});
});
describe("getAutomationSettings — fail-open to defaults", () => {
it("returns the defaults when the DB is unavailable (no row, no env)", async () => {
const settings = await getAutomationSettings(REPO_ID);
expect(settings).toEqual({ ...AUTOMATION_DEFAULTS });
});
});
describe("normalizeMode", () => {
it("passes through the three valid modes", () => {
expect(normalizeMode("off", "suggest")).toBe("off");
expect(normalizeMode("suggest", "off")).toBe("suggest");
expect(normalizeMode("auto", "off")).toBe("auto");
});
it("falls back on garbage, undefined, and non-strings", () => {
expect(normalizeMode("ON", "suggest")).toBe("suggest");
expect(normalizeMode(undefined, "auto")).toBe("auto");
expect(normalizeMode(null, "off")).toBe("off");
expect(normalizeMode(1, "suggest")).toBe("suggest");
expect(normalizeMode("", "suggest")).toBe("suggest");
});
});
describe("resolveEffectiveMode — env kill-switches stay supreme", () => {
it("env off forces 'off' regardless of the repo mode", () => {
expect(resolveEffectiveMode("auto", false)).toBe("off");
expect(resolveEffectiveMode("suggest", false)).toBe("off");
expect(resolveEffectiveMode("off", false)).toBe("off");
});
it("env on yields the repo mode unchanged (repo can only narrow)", () => {
expect(resolveEffectiveMode("auto", true)).toBe("auto");
expect(resolveEffectiveMode("suggest", true)).toBe("suggest");
expect(resolveEffectiveMode("off", true)).toBe("off");
});
});
describe("isAutomationOn — 'suggest' and 'auto' both count as on", () => {
it("only 'off' is off", () => {
expect(isAutomationOn("off")).toBe(false);
expect(isAutomationOn("suggest")).toBe(true);
expect(isAutomationOn("auto")).toBe(true);
});
});
describe("settingsFromRow", () => {
it("null/undefined row → defaults", () => {
expect(settingsFromRow(null)).toEqual({ ...AUTOMATION_DEFAULTS });
expect(settingsFromRow(undefined)).toEqual({ ...AUTOMATION_DEFAULTS });
});
it("valid row values pass through", () => {
const out = settingsFromRow({
aiReviewMode: "off",
prTriageMode: "off",
issueTriageMode: "suggest",
autoMergeMode: "suggest",
ciAutofixMode: "auto",
});
expect(out.aiReviewMode).toBe("off");
expect(out.prTriageMode).toBe("off");
expect(out.issueTriageMode).toBe("suggest");
expect(out.autoMergeMode).toBe("suggest");
expect(out.ciAutofixMode).toBe("auto");
});
it("corrupt per-field values fall back per-field to the defaults", () => {
const out = settingsFromRow({
aiReviewMode: "banana",
autoMergeMode: "off",
});
expect(out.aiReviewMode).toBe("suggest");
expect(out.autoMergeMode).toBe("off");
expect(out.ciAutofixMode).toBe("suggest");
});
});
describe("evaluateAutoMerge — per-repo automation gate", () => {
const ctx = {
pullRequestId: "pr-1",
repositoryId: REPO_ID,
baseBranch: "main",
isDraft: false,
authorUserId: "user-1",
};
it("'off' blocks the merge before any DB work", async () => {
const { loader, calls } = makeLoader(makeSettings({ autoMergeMode: "off" }));
const decision = await evaluateAutoMerge(ctx, {
loadAutomationSettings: loader,
});
expect(decision.merge).toBe(false);
expect(decision.reason).toContain("turned off");
expect(decision.blocking?.length).toBe(1);
expect(calls).toEqual([REPO_ID]);
});
it("'suggest' evaluates to merge:false with a human-handoff reason", async () => {
const { loader } = makeLoader(makeSettings({ autoMergeMode: "suggest" }));
const decision = await evaluateAutoMerge(ctx, {
loadAutomationSettings: loader,
});
expect(decision.merge).toBe(false);
expect(decision.reason).toContain("suggest");
expect(decision.reason).toContain("merge left to a human");
});
it("'auto' proceeds past the gate into the K2 evaluation", async () => {
const { loader, calls } = makeLoader(makeSettings({ autoMergeMode: "auto" }));
let decision: { merge: boolean; reason: string } | null = null;
try {
decision = await evaluateAutoMerge(ctx, {
loadAutomationSettings: loader,
});
} catch {
}
expect(calls).toEqual([REPO_ID]);
if (decision) {
expect(decision.reason).not.toContain("automation settings");
}
});
});
describe("dispatch-site source wiring", () => {
const read = (rel: string) =>
readFileSync(join(import.meta.dir, rel), "utf8");
it("ai-review: triggerAiReview consults the per-repo setting and skips on 'off'", () => {
const src = read("../lib/ai-review.ts");
expect(src).toContain('from "./automation-settings"');
expect(src).toContain("options.loadSettings ?? getAutomationSettings");
expect(src).toContain('if (automation.aiReviewMode === "off") return;');
});
it("pr-triage: triggerPrTriage consults the per-repo setting and skips on 'off'", () => {
const src = read("../lib/pr-triage.ts");
expect(src).toContain('from "./automation-settings"');
expect(src).toContain('if (automation.prTriageMode === "off") return;');
});
it("issue-triage: triggerIssueTriage consults the per-repo setting and skips on 'off'", () => {
const src = read("../lib/issue-triage.ts");
expect(src).toContain('from "./automation-settings"');
expect(src).toContain('if (automation.issueTriageMode === "off") return;');
});
it("ci-autofix: _runAutofix skips on 'off' and auto-applies on 'auto'", () => {
const src = read("../lib/ci-autofix.ts");
expect(src).toContain('from "./automation-settings"');
expect(src).toContain('if (automation.ciAutofixMode === "off") return;');
expect(src).toContain('automation.ciAutofixMode === "auto"');
expect(src).toContain("applyAutofix(posted.id, repoRow.ownerId, deps)");
});
it("env guards stay supreme at the triage/review dispatch sites", () => {
for (const rel of ["../lib/pr-triage.ts", "../lib/issue-triage.ts"]) {
const src = read(rel);
const envIdx = src.indexOf("if (!isAiAvailable()) return;");
const gateIdx = src.indexOf("options.loadSettings ?? getAutomationSettings");
expect(envIdx).toBeGreaterThan(-1);
expect(gateIdx).toBeGreaterThan(envIdx);
}
const review = read("../lib/ai-review.ts");
const envIdx = review.indexOf("if (!isAiReviewEnabled()) return;");
const gateIdx = review.indexOf("options.loadSettings ?? getAutomationSettings");
expect(envIdx).toBeGreaterThan(-1);
expect(gateIdx).toBeGreaterThan(envIdx);
});
});
|