|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { describe, it } from "node:test"; |
| 6 | + |
| 7 | +import { |
| 8 | + isValidChangenoteContent, |
| 9 | + isValidChangenoteFile, |
| 10 | + isValidChangenoteFilename, |
| 11 | + hasValidChangenoteCategory, |
| 12 | + VALID_CHANGE_NOTE_CATEGORIES, |
| 13 | +} from "./validate.ts"; |
| 14 | + |
| 15 | +async function withTmpFile<T>( |
| 16 | + baseFileName: string, |
| 17 | + contents: string, |
| 18 | + body: (filePath: string) => Promise<T>, |
| 19 | +): Promise<T> { |
| 20 | + const tmpDir = fs.mkdtempSync( |
| 21 | + path.join(os.tmpdir(), "changetool-validate-test-"), |
| 22 | + ); |
| 23 | + try { |
| 24 | + const filePath = path.join(tmpDir, baseFileName); |
| 25 | + fs.writeFileSync(filePath, contents); |
| 26 | + return await body(filePath); |
| 27 | + } finally { |
| 28 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +await describe("isValidChangenoteContent", async () => { |
| 33 | + await it("recognizes an unordered Markdown list", async () => { |
| 34 | + const inputs = [ |
| 35 | + "- One changenote entry", |
| 36 | + "- First item\n- Second item", |
| 37 | + "\n\n\n\n- Fixed a bug\n- Added a feature", |
| 38 | + ]; |
| 39 | + |
| 40 | + for (const input of inputs) { |
| 41 | + assert.equal(isValidChangenoteContent(input), true); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + await it("does not recognize non-Markdown text", async () => { |
| 46 | + const inputs = [ |
| 47 | + "This is not a list.", |
| 48 | + '["this", "is", "JSON"]', |
| 49 | + "---", |
| 50 | + "***", |
| 51 | + "___", |
| 52 | + "paragraph", |
| 53 | + ]; |
| 54 | + |
| 55 | + for (const input of inputs) { |
| 56 | + assert.equal(isValidChangenoteContent(input), false); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + await it("does not recognize ordered Markdown lists", async () => { |
| 61 | + const inputs = [ |
| 62 | + "1. First item\n2. Second item", |
| 63 | + "\n\n\n1. First item\n1. Second item", |
| 64 | + ]; |
| 65 | + |
| 66 | + for (const input of inputs) { |
| 67 | + assert.equal(isValidChangenoteContent(input), false); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + await it("requires all list items to use a hyphen bullet", async () => { |
| 72 | + const inputs = [ |
| 73 | + "* Fixed a bug\n* Added feature", |
| 74 | + "+ Fixed a bug\n+ Added feature", |
| 75 | + "- Fixed a bug\n* Added feature", |
| 76 | + "- Fixed a bug\n+ Added feature", |
| 77 | + "\n\n\n* Fixed a bug", |
| 78 | + "\n\n\n+ Fixed a bug", |
| 79 | + "---\n* Fixed a bug\n* Added feature", |
| 80 | + ] as const; |
| 81 | + |
| 82 | + for (const input of inputs) { |
| 83 | + assert.equal(isValidChangenoteContent(input), false); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + await it("does not contain other Markdown elements", async () => { |
| 88 | + const inputs = [ |
| 89 | + "- Fixed a bug\n\nParagraph of text", |
| 90 | + "- Fixed a bug\n\n* Added a feature", |
| 91 | + "# Header\n- Fixed a bug", |
| 92 | + "- Fixed a bug\n## Subheader", |
| 93 | + ]; |
| 94 | + |
| 95 | + for (const input of inputs) { |
| 96 | + assert.equal(isValidChangenoteContent(input), false); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +await describe("isValidChangenoteFilename", async () => { |
| 102 | + await it("accepts valid filenames", async () => { |
| 103 | + const inputs = [ |
| 104 | + "2023-01-01-fix-bug.md", |
| 105 | + "2023-12-31-add-feature.md", |
| 106 | + "2023-06-15-update-docs.md", |
| 107 | + ]; |
| 108 | + |
| 109 | + for (const input of inputs) { |
| 110 | + assert.equal(isValidChangenoteFilename(input), true); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + await it("rejects invalid filenames", async () => { |
| 115 | + const inputs = [ |
| 116 | + "missing-date-from-filename.md", |
| 117 | + "2021-01-01.md", |
| 118 | + "2026-12-19-wrong-file-name-extension.txt", |
| 119 | + ]; |
| 120 | + |
| 121 | + for (const input of inputs) { |
| 122 | + assert.equal(isValidChangenoteFilename(input), false); |
| 123 | + } |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +await describe("hasValidChangenoteCategory", async () => { |
| 128 | + await it("accepts valid categories", async () => { |
| 129 | + for (const category of Object.keys(VALID_CHANGE_NOTE_CATEGORIES)) { |
| 130 | + const frontmatter = { category }; |
| 131 | + assert.equal(hasValidChangenoteCategory(frontmatter), true); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + await it("rejects invalid categories", async () => { |
| 136 | + const inputs = [ |
| 137 | + "", |
| 138 | + "invalid-category", |
| 139 | + "bug-fix", |
| 140 | + "new-feature", |
| 141 | + "security-patch", |
| 142 | + "miscellaneous", |
| 143 | + "documentation", |
| 144 | + ]; |
| 145 | + |
| 146 | + for (const category of inputs) { |
| 147 | + const frontmatter = { category }; |
| 148 | + assert.equal(hasValidChangenoteCategory(frontmatter), false); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + await it("reject missing category", async () => { |
| 153 | + assert.equal(hasValidChangenoteCategory({}), false); |
| 154 | + assert.equal(hasValidChangenoteCategory({ category: null }), false); |
| 155 | + assert.equal(hasValidChangenoteCategory({ category: undefined }), false); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +await describe("isValidChangenoteFile", async () => { |
| 160 | + await it("accepts a valid change-note file", async () => { |
| 161 | + await withTmpFile( |
| 162 | + "2026-01-01-fix-bug.md", |
| 163 | + "---\ncategory: fix\n---\n- Fixed a bug\n", |
| 164 | + async (filePath) => { |
| 165 | + assert.equal(isValidChangenoteFile(filePath), true); |
| 166 | + }, |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + await it("rejects invalid filename", async () => { |
| 171 | + await withTmpFile( |
| 172 | + "fix-bug.md", |
| 173 | + "---\ncategory: fix\n---\n- Fixed a bug\n", |
| 174 | + async (filePath) => { |
| 175 | + assert.equal(isValidChangenoteFile(filePath), false); |
| 176 | + }, |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + await it("rejects missing frontmatter", async () => { |
| 181 | + await withTmpFile( |
| 182 | + "2026-01-01-fix-bug.md", |
| 183 | + "- Fixed a bug\n", |
| 184 | + async (filePath) => { |
| 185 | + assert.equal(isValidChangenoteFile(filePath), false); |
| 186 | + }, |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + await it("rejects invalid Markdown", async () => { |
| 191 | + await withTmpFile( |
| 192 | + "2026-01-01-fix-bug.md", |
| 193 | + "---\ncategory: fix\n---\n* Fixed a bug\n", |
| 194 | + async (filePath) => { |
| 195 | + assert.equal(isValidChangenoteFile(filePath), false); |
| 196 | + }, |
| 197 | + ); |
| 198 | + }); |
| 199 | +}); |
0 commit comments