|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | + |
| 7 | +import { |
| 8 | + postProcessFileGithubMentions, |
| 9 | + fixMarkdownGithubMentions, |
| 10 | +} from "../src/GithubMentions.js"; |
| 11 | + |
| 12 | +test("fixMarkdownGithubMentions: replaces github profile mention links", () => { |
| 13 | + const input = "Thanks @ [Aki](https://github.com/aki).\n"; |
| 14 | + const expected = "Thanks @ [aki](https://github.com/aki).\n"; |
| 15 | + |
| 16 | + assert.equal(fixMarkdownGithubMentions(input), expected); |
| 17 | +}); |
| 18 | + |
| 19 | +test("fixMarkdownGithubMentions: leaves non-github links unchanged", () => { |
| 20 | + const input = "Thanks @ [Aki](https://example.com/aki).\n"; |
| 21 | + assert.equal(fixMarkdownGithubMentions(input), input); |
| 22 | +}); |
| 23 | + |
| 24 | +test("fixMarkdownGithubMentions: handles multiple mentions in one line", () => { |
| 25 | + const input = |
| 26 | + "Reviewers: @ [Alpha](https://github.com/alpha), @ [Beta](https://github.com/beta/)\n"; |
| 27 | + const expected = |
| 28 | + "Reviewers: @ [alpha](https://github.com/alpha), @ [beta](https://github.com/beta)\n"; |
| 29 | + assert.equal(fixMarkdownGithubMentions(input), expected); |
| 30 | +}); |
| 31 | + |
| 32 | +test("postProcessFileGithubMentions: updates file in-place", () => { |
| 33 | + const tempDir = fs.mkdtempSync( |
| 34 | + path.join(os.tmpdir(), "github-mentions-postprocess-") |
| 35 | + ); |
| 36 | + |
| 37 | + try { |
| 38 | + const filePath = path.join(tempDir, "doc.md"); |
| 39 | + fs.writeFileSync( |
| 40 | + filePath, |
| 41 | + "Reviewers: @ [Alpha](https://github.com/alpha), @ [Beta](https://github.com/beta/)\n" |
| 42 | + ); |
| 43 | + |
| 44 | + postProcessFileGithubMentions(filePath); |
| 45 | + |
| 46 | + const updated = fs.readFileSync(filePath, "utf8"); |
| 47 | + assert.equal( |
| 48 | + updated, |
| 49 | + "Reviewers: @ [alpha](https://github.com/alpha), @ [beta](https://github.com/beta)\n" |
| 50 | + ); |
| 51 | + |
| 52 | + assert.doesNotThrow(() => |
| 53 | + postProcessFileGithubMentions(path.join(tempDir, "missing.md")) |
| 54 | + ); |
| 55 | + } finally { |
| 56 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 57 | + } |
| 58 | +}); |
0 commit comments