@@ -6,6 +6,7 @@ const { describe, it } = require("node:test");
66const assert = require ( "node:assert/strict" ) ;
77const {
88 detectTypeLabelFromTitle,
9+ detectTypeLabelFromCommits,
910 hasHumanTypeLabelOverride,
1011 planTypeLabelSync,
1112 TYPE_LABELS ,
@@ -145,6 +146,115 @@ describe("planTypeLabelSync", () => {
145146 } ) ;
146147} ) ;
147148
149+ describe ( "detectTypeLabelFromCommits" , ( ) => {
150+ it ( "reads the type from unanimous commits" , ( ) => {
151+ assert . equal (
152+ detectTypeLabelFromCommits ( [
153+ "fix(usage): price long-context requests at the published long rate" ,
154+ ] ) ,
155+ "bug" ,
156+ ) ;
157+ } ) ;
158+
159+ it ( "treats chore as supporting, not competing (PR #955 shape)" , ( ) => {
160+ // Four `fix(codex):` commits plus one `test(codex):`. Requiring unanimity
161+ // would abstain here, and on almost every real PR — nearly every
162+ // substantial change carries a test or chore commit alongside its fix.
163+ assert . equal (
164+ detectTypeLabelFromCommits ( [
165+ "fix(codex): probe reset-derived cooldowns without waiting to be selected" ,
166+ "fix(codex): fail closed on an unrecognized plan" ,
167+ "fix(codex): classify prolite as a weekly plan" ,
168+ "fix(codex): share one window rule instead of a plan allowlist" ,
169+ "test(codex): assert the window rule in literals" ,
170+ ] ) ,
171+ "bug" ,
172+ ) ;
173+ } ) ;
174+
175+ it ( "keeps chore when nothing else competes" , ( ) => {
176+ assert . equal (
177+ detectTypeLabelFromCommits ( [ "ci: pin an action" , "test: add a case" ] ) ,
178+ "chore" ,
179+ ) ;
180+ } ) ;
181+
182+ it ( "abstains on a genuine mix of fix and feat" , ( ) => {
183+ assert . equal (
184+ detectTypeLabelFromCommits ( [ "fix(a): repair x" , "feat(b): add y" ] ) ,
185+ null ,
186+ ) ;
187+ } ) ;
188+
189+ it ( "reads only the first line of a multi-line commit message" , ( ) => {
190+ // A body line starting with `feat:` must not vote.
191+ assert . equal (
192+ detectTypeLabelFromCommits ( [
193+ "fix(a): repair x\n\nfeat: this is prose in the body, not a type" ,
194+ ] ) ,
195+ "bug" ,
196+ ) ;
197+ } ) ;
198+
199+ it ( "returns null for absent or unusable input" , ( ) => {
200+ assert . equal ( detectTypeLabelFromCommits ( [ ] ) , null ) ;
201+ assert . equal ( detectTypeLabelFromCommits ( undefined ) , null ) ;
202+ assert . equal ( detectTypeLabelFromCommits ( [ "" , null ] ) , null ) ;
203+ } ) ;
204+ } ) ;
205+
206+ describe ( "planTypeLabelSync commit fallback" , ( ) => {
207+ it ( "labels a stack PR whose title carries no type" , ( ) => {
208+ // `stack 3/5:` fails the conventional regex (the `3/5` sits between the
209+ // word and the colon), reaches the sentence-case fallback, which extracts
210+ // `stack` — a word with no PREFIX_TO_LABEL entry. The sync used to skip
211+ // here, and a skip is not a failure, so the `label` check stayed green
212+ // while all four stack PRs carried no type label.
213+ const plan = planTypeLabelSync ( {
214+ title : "stack 3/5: carry six contributor bug fixes with authorship intact" ,
215+ currentLabels : [ ] ,
216+ events : [ ] ,
217+ commitMessages : [
218+ "fix(kiro): round-trip the redactedContent reasoning blob" ,
219+ "fix(responses): close passthrough streams at terminal events" ,
220+ ] ,
221+ } ) ;
222+ assert . deepEqual ( plan , { skip : false , detected : "bug" , add : "bug" , remove : [ ] } ) ;
223+ } ) ;
224+
225+ it ( "does not let commits override a title that already classifies" , ( ) => {
226+ const plan = planTypeLabelSync ( {
227+ title : "feat(providers): add a preset" ,
228+ currentLabels : [ ] ,
229+ events : [ ] ,
230+ commitMessages : [ "fix(a): repair x" , "fix(b): repair y" ] ,
231+ } ) ;
232+ assert . equal ( plan . detected , "enhancement" ) ;
233+ } ) ;
234+
235+ it ( "still skips when neither the title nor the commits classify" , ( ) => {
236+ const plan = planTypeLabelSync ( {
237+ title : "stack 1/5: triage the open issue surface" ,
238+ currentLabels : [ ] ,
239+ events : [ ] ,
240+ commitMessages : [ "wip" , "more wip" ] ,
241+ } ) ;
242+ assert . deepEqual ( plan , { skip : true , reason : "no-prefix" } ) ;
243+ } ) ;
244+
245+ it ( "still honours a human override before consulting commits" , ( ) => {
246+ const plan = planTypeLabelSync ( {
247+ title : "stack 2/5: price long-context requests" ,
248+ currentLabels : [ "enhancement" ] ,
249+ events : [
250+ { event : "labeled" , label : { name : "enhancement" } , actor : { login : "a-human" } } ,
251+ ] ,
252+ commitMessages : [ "fix(usage): price long-context requests" ] ,
253+ } ) ;
254+ assert . deepEqual ( plan , { skip : true , reason : "human-override" } ) ;
255+ } ) ;
256+ } ) ;
257+
148258describe ( "pr-labeler workflow" , ( ) => {
149259 const workflowPath = path . join ( __dirname , "../workflows/pr-labeler.yml" ) ;
150260 const workflow = fs . readFileSync ( workflowPath , "utf8" ) ;
0 commit comments