|
| 1 | +// |
| 2 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// See https://llvm.org/LICENSE.txt for license information. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +// Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com) |
| 7 | +// |
| 8 | +// Official repository: https://github.com/cppalliance/mrdocs |
| 9 | +// |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import { |
| 12 | + commitSizeWarnings, |
| 13 | + parseCommitSummary, |
| 14 | + basicChecks, |
| 15 | + summarizeScopes, |
| 16 | + validateCommits, |
| 17 | + type CommitInfo, |
| 18 | + type DangerInputs, |
| 19 | +} from "./logic"; |
| 20 | + |
| 21 | +describe("parseCommitSummary", () => { |
| 22 | + // Ensures we correctly extract type, scope, and subject when format is valid. |
| 23 | + it("parses valid commit summaries", () => { |
| 24 | + const parsed = parseCommitSummary("fix(core): handle edge case"); |
| 25 | + expect(parsed?.type).toBe("fix"); |
| 26 | + expect(parsed?.scope).toBe("core"); |
| 27 | + expect(parsed?.subject).toBe("handle edge case"); |
| 28 | + }); |
| 29 | + |
| 30 | + // Guards against accepting malformed commit first lines. |
| 31 | + it("rejects invalid format", () => { |
| 32 | + expect(parseCommitSummary("invalid summary")).toBeNull(); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("summarizeScopes", () => { |
| 37 | + // Verifies file paths are bucketed into the correct scopes and totals are tallied. |
| 38 | + it("aggregates by scope", () => { |
| 39 | + const report = summarizeScopes([ |
| 40 | + { filename: "src/lib/file.cpp", additions: 10, deletions: 2 }, |
| 41 | + { filename: "src/test/file.cpp", additions: 5, deletions: 1 }, |
| 42 | + { filename: "test-files/golden-tests/out.xml", additions: 100, deletions: 0 }, |
| 43 | + { filename: "docs/index.adoc", additions: 4, deletions: 0 }, |
| 44 | + ]); |
| 45 | + |
| 46 | + expect(report.totals.source.files).toBe(1); |
| 47 | + expect(report.totals.tests.files).toBe(1); |
| 48 | + expect(report.totals["golden-tests"].files).toBe(1); |
| 49 | + expect(report.totals.docs.files).toBe(1); |
| 50 | + expect(report.overall.files).toBe(4); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("commitSizeWarnings", () => { |
| 55 | + // Confirms that large non-test churn triggers a warning while ignoring test fixtures. |
| 56 | + it("flags large non-test commits", () => { |
| 57 | + const commits: CommitInfo[] = [ |
| 58 | + { |
| 59 | + sha: "abc", |
| 60 | + message: "feat: huge change", |
| 61 | + files: [ |
| 62 | + { filename: "src/lib/large.cpp", additions: 900, deletions: 200 }, |
| 63 | + { filename: "test-files/golden-tests/out.xml", additions: 1000, deletions: 0 }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + ]; |
| 67 | + const warnings = commitSizeWarnings(commits); |
| 68 | + expect(warnings.length).toBe(1); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("starterChecks", () => { |
| 73 | + // Checks that source changes without accompanying tests produce a warning. |
| 74 | + it("requests tests when source changes without coverage", () => { |
| 75 | + const inputs: DangerInputs = { |
| 76 | + files: [], |
| 77 | + commits: [], |
| 78 | + prBody: "Summary\n\nTesting: pending", |
| 79 | + prTitle: "Test PR", |
| 80 | + labels: [], |
| 81 | + }; |
| 82 | + const summary = summarizeScopes([{ filename: "src/lib/file.cpp", additions: 1, deletions: 0 }]); |
| 83 | + const parsed = validateCommits([{ sha: "1", message: "fix: change" }]).parsed; |
| 84 | + const warnings = basicChecks(inputs, summary, parsed); |
| 85 | + expect(warnings.some((message) => message.includes("Source changed"))).toBe(true); |
| 86 | + }); |
| 87 | +}); |
0 commit comments