|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { describe, it } = require("node:test"); |
| 4 | +const assert = require("node:assert/strict"); |
| 5 | +const { |
| 6 | + ANCESTRY_BEHIND_THRESHOLD, |
| 7 | + isWrongAncestry, |
| 8 | + authorHasPushPermission, |
| 9 | + assessPrDescription, |
| 10 | + collectPrQualityFailures, |
| 11 | +} = require("./pr-quality.cjs"); |
| 12 | + |
| 13 | +describe("isWrongAncestry", () => { |
| 14 | + it("flags #644-shaped compares (0 behind main, far behind base)", () => { |
| 15 | + assert.equal( |
| 16 | + isWrongAncestry({ behindMain: 0, behindBase: 44 }), |
| 17 | + true, |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it("uses threshold 20 by default", () => { |
| 22 | + assert.equal(ANCESTRY_BEHIND_THRESHOLD, 20); |
| 23 | + assert.equal(isWrongAncestry({ behindMain: 0, behindBase: 20 }), true); |
| 24 | + assert.equal(isWrongAncestry({ behindMain: 0, behindBase: 19 }), false); |
| 25 | + }); |
| 26 | + |
| 27 | + it("passes when head is behind main (not sitting on main tip)", () => { |
| 28 | + assert.equal(isWrongAncestry({ behindMain: 1, behindBase: 44 }), false); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("authorHasPushPermission", () => { |
| 33 | + it("accepts write/maintain/admin only", () => { |
| 34 | + assert.equal(authorHasPushPermission("admin"), true); |
| 35 | + assert.equal(authorHasPushPermission("maintain"), true); |
| 36 | + assert.equal(authorHasPushPermission("write"), true); |
| 37 | + assert.equal(authorHasPushPermission("triage"), false); |
| 38 | + assert.equal(authorHasPushPermission("read"), false); |
| 39 | + assert.equal(authorHasPushPermission(null), false); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("assessPrDescription", () => { |
| 44 | + it("rejects empty and comment-only bodies", () => { |
| 45 | + assert.equal(assessPrDescription("").ok, false); |
| 46 | + assert.equal(assessPrDescription(" ").ok, false); |
| 47 | + assert.equal( |
| 48 | + assessPrDescription("<!-- release notes by coderabbit.ai -->\n\n<!-- end -->").reason, |
| 49 | + "empty", |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it("rejects placeholder-only bodies", () => { |
| 54 | + assert.equal(assessPrDescription("N/A").reason, "placeholder"); |
| 55 | + assert.equal(assessPrDescription("TODO").reason, "placeholder"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("rejects literal escaped newlines like #644", () => { |
| 59 | + const body = |
| 60 | + "## What changed\\n- make the Windows tray launcher resolve Codex home\\n\\n## Validation\\n- git diff --check"; |
| 61 | + assert.equal(assessPrDescription(body).reason, "escaped_newlines"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("rejects thin real-newline bodies", () => { |
| 65 | + assert.equal(assessPrDescription("fix stuff").reason, "thin"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("accepts two rich markdown sections", () => { |
| 69 | + const body = [ |
| 70 | + "## Summary", |
| 71 | + "This change updates the Windows tray launcher so it resolves CODEX_HOME through the shared helper instead of a hardcoded path.", |
| 72 | + "", |
| 73 | + "## Test plan", |
| 74 | + "- Launch the tray app after setting CODEX_HOME", |
| 75 | + "- Confirm the listener and launcher use the same workspace root", |
| 76 | + ].join("\n"); |
| 77 | + assert.equal(assessPrDescription(body).ok, true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("accepts unstructured bodies that are long enough with multiple blocks", () => { |
| 81 | + const p1 = |
| 82 | + "Updates the Windows tray launcher to resolve the active Codex home through the shared helper so listener and launcher stay aligned."; |
| 83 | + const p2 = |
| 84 | + "Validated with git diff --check on the changed tray module; typecheck was not available in that session so CI must cover it."; |
| 85 | + assert.equal(assessPrDescription(`${p1}\n\n${p2}`).ok, true); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("collectPrQualityFailures", () => { |
| 90 | + const allowed = ["dev", "dev2-go"]; |
| 91 | + |
| 92 | + it("reports wrong_base without requiring ancestry inputs", () => { |
| 93 | + const failures = collectPrQualityFailures({ |
| 94 | + baseRef: "main", |
| 95 | + allowedBases: allowed, |
| 96 | + body: "## Summary\n" + "x".repeat(50) + "\n\n## Test plan\n" + "y".repeat(50), |
| 97 | + behindMain: 0, |
| 98 | + behindBase: 0, |
| 99 | + authorPermission: "read", |
| 100 | + }); |
| 101 | + assert.ok(failures.some((f) => f.code === "wrong_base")); |
| 102 | + assert.ok(!failures.some((f) => f.code === "wrong_ancestry")); |
| 103 | + }); |
| 104 | + |
| 105 | + it("reports wrong_ancestry for contributor on #644-shaped compare", () => { |
| 106 | + const failures = collectPrQualityFailures({ |
| 107 | + baseRef: "dev", |
| 108 | + allowedBases: allowed, |
| 109 | + body: [ |
| 110 | + "## Summary", |
| 111 | + "This change updates the Windows tray launcher so it resolves CODEX_HOME through the shared helper instead of a hardcoded path.", |
| 112 | + "", |
| 113 | + "## Test plan", |
| 114 | + "- Launch the tray app after setting CODEX_HOME", |
| 115 | + "- Confirm the listener and launcher use the same workspace root", |
| 116 | + ].join("\n"), |
| 117 | + behindMain: 0, |
| 118 | + behindBase: 44, |
| 119 | + authorPermission: "read", |
| 120 | + }); |
| 121 | + assert.deepEqual( |
| 122 | + failures.map((f) => f.code), |
| 123 | + ["wrong_ancestry"], |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it("skips ancestry for push permission but still flags bad description", () => { |
| 128 | + const failures = collectPrQualityFailures({ |
| 129 | + baseRef: "dev", |
| 130 | + allowedBases: allowed, |
| 131 | + body: "", |
| 132 | + behindMain: 0, |
| 133 | + behindBase: 44, |
| 134 | + authorPermission: "write", |
| 135 | + }); |
| 136 | + assert.ok(!failures.some((f) => f.code === "wrong_ancestry")); |
| 137 | + assert.ok(failures.some((f) => f.code === "bad_description")); |
| 138 | + }); |
| 139 | + |
| 140 | + it("applies ancestry when permission lookup failed (fail closed)", () => { |
| 141 | + const failures = collectPrQualityFailures({ |
| 142 | + baseRef: "dev", |
| 143 | + allowedBases: allowed, |
| 144 | + body: [ |
| 145 | + "## Summary", |
| 146 | + "This change updates the Windows tray launcher so it resolves CODEX_HOME through the shared helper instead of a hardcoded path.", |
| 147 | + "", |
| 148 | + "## Test plan", |
| 149 | + "- Launch the tray app after setting CODEX_HOME", |
| 150 | + "- Confirm the listener and launcher use the same workspace root", |
| 151 | + ].join("\n"), |
| 152 | + behindMain: 0, |
| 153 | + behindBase: 44, |
| 154 | + authorPermission: null, |
| 155 | + permissionLookupFailed: true, |
| 156 | + }); |
| 157 | + assert.ok(failures.some((f) => f.code === "wrong_ancestry")); |
| 158 | + }); |
| 159 | +}); |
0 commit comments