|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { mkdtemp, readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | +import test from 'node:test'; |
| 8 | + |
| 9 | +import { |
| 10 | + discoverReleaseNoteAssets, |
| 11 | + inspectReleaseNoteArchive, |
| 12 | + materializeReleaseNotes, |
| 13 | + normalizeSynchronizedReleaseNotes, |
| 14 | + resolveReleaseNotesConfig, |
| 15 | +} from '../scripts/release-notes-sync-lib.mjs'; |
| 16 | + |
| 17 | +async function writeFixtureFile(filePath, contents) { |
| 18 | + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); |
| 19 | + await writeFile(filePath, contents, 'utf8'); |
| 20 | +} |
| 21 | + |
| 22 | +async function createArchiveFixture({ |
| 23 | + tag = 'v1.0.0', |
| 24 | + payloadTag = tag, |
| 25 | + includeJson = true, |
| 26 | + includeZh = true, |
| 27 | + includeEn = true, |
| 28 | + repositories = { |
| 29 | + web: { |
| 30 | + path: 'repos/web', |
| 31 | + group: { |
| 32 | + range: 'v0.9.0..v1.0.0', |
| 33 | + commits: [ |
| 34 | + { |
| 35 | + hash: 'abcdef', |
| 36 | + shortHash: 'abcdef', |
| 37 | + date: '2026-04-13', |
| 38 | + author: 'tester', |
| 39 | + subject: 'feat: ship release notes', |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | +} = {}) { |
| 46 | + const root = await mkdtemp(path.join(os.tmpdir(), 'docs-release-notes-archive-')); |
| 47 | + const sourceRoot = path.join(root, 'source'); |
| 48 | + const zipPath = path.join(root, `${tag}.zip`); |
| 49 | + |
| 50 | + if (includeJson) { |
| 51 | + await writeFixtureFile( |
| 52 | + path.join(sourceRoot, 'artifacts', 'tags', tag, `${tag}.json`), |
| 53 | + `${JSON.stringify( |
| 54 | + { |
| 55 | + generatedAt: '2026-04-13T14:14:52.517Z', |
| 56 | + formatVersion: 1, |
| 57 | + tag: payloadTag, |
| 58 | + repositories, |
| 59 | + }, |
| 60 | + null, |
| 61 | + 2, |
| 62 | + )}\n`, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (includeZh) { |
| 67 | + await writeFixtureFile( |
| 68 | + path.join(sourceRoot, 'published', `${tag}.zh-CN.md`), |
| 69 | + '# HagiCode\n\n- 统一了入口流程。\n', |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (includeEn) { |
| 74 | + await writeFixtureFile( |
| 75 | + path.join(sourceRoot, 'published', `${tag}.en.md`), |
| 76 | + '# HagiCode\n\n- Unified the entry flow.\n', |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const result = spawnSync('zip', ['-qr', zipPath, '.'], { |
| 81 | + cwd: sourceRoot, |
| 82 | + encoding: 'utf8', |
| 83 | + }); |
| 84 | + |
| 85 | + if ((result.status ?? 0) !== 0) { |
| 86 | + throw new Error(`zip failed: ${result.stderr || result.stdout}`); |
| 87 | + } |
| 88 | + |
| 89 | + return { root, zipPath }; |
| 90 | +} |
| 91 | + |
| 92 | +function createReleasePayload() { |
| 93 | + return [ |
| 94 | + { |
| 95 | + id: 1, |
| 96 | + tag_name: 'v1.0.1', |
| 97 | + name: 'v1.0.1', |
| 98 | + published_at: '2026-04-14T08:00:00.000Z', |
| 99 | + html_url: 'https://example.test/releases/v1.0.1', |
| 100 | + assets: [ |
| 101 | + { |
| 102 | + id: 10, |
| 103 | + name: 'release-notes-v1.0.1-history.zip', |
| 104 | + browser_download_url: 'https://example.test/assets/v1.0.1.zip', |
| 105 | + updated_at: '2026-04-14T08:10:00.000Z', |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 11, |
| 109 | + name: 'notes.txt', |
| 110 | + browser_download_url: 'https://example.test/assets/notes.txt', |
| 111 | + updated_at: '2026-04-14T08:10:00.000Z', |
| 112 | + }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + { |
| 116 | + id: 2, |
| 117 | + tag_name: '1.0.0', |
| 118 | + name: '1.0.0', |
| 119 | + published_at: '2026-04-13T08:00:00.000Z', |
| 120 | + html_url: 'https://example.test/releases/1.0.0', |
| 121 | + assets: [ |
| 122 | + { |
| 123 | + id: 20, |
| 124 | + name: 'release-notes-1.0.0-history.zip', |
| 125 | + browser_download_url: 'https://example.test/assets/1.0.0.zip', |
| 126 | + updated_at: '2026-04-13T08:10:00.000Z', |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + ]; |
| 131 | +} |
| 132 | + |
| 133 | +test('release discovery keeps only matching history bundle assets', () => { |
| 134 | + const discovered = discoverReleaseNoteAssets(createReleasePayload()); |
| 135 | + |
| 136 | + assert.deepEqual( |
| 137 | + discovered.map((entry) => entry.tag), |
| 138 | + ['v1.0.1', '1.0.0'], |
| 139 | + ); |
| 140 | + assert.equal(discovered[0].assetName, 'release-notes-v1.0.1-history.zip'); |
| 141 | +}); |
| 142 | + |
| 143 | +test('config prefers explicit cross-repository release-notes tokens', () => { |
| 144 | + const config = resolveReleaseNotesConfig({ |
| 145 | + repoRoot: '/tmp/docs', |
| 146 | + env: { |
| 147 | + DOCS_RELEASE_NOTES_TOKEN: 'release-notes-token', |
| 148 | + DOCS_GITHUB_TOKEN: 'docs-token', |
| 149 | + GITHUB_TOKEN: 'github-token', |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + assert.equal(config.token, 'release-notes-token'); |
| 154 | +}); |
| 155 | + |
| 156 | +test('archive validation rejects missing required tag json', async () => { |
| 157 | + const fixture = await createArchiveFixture({ includeJson: false }); |
| 158 | + const inspection = inspectReleaseNoteArchive({ |
| 159 | + zipPath: fixture.zipPath, |
| 160 | + candidate: { tag: 'v1.0.0' }, |
| 161 | + }); |
| 162 | + |
| 163 | + assert.equal(inspection.accepted, false); |
| 164 | + assert.equal(inspection.reason, 'missing_required_payload'); |
| 165 | +}); |
| 166 | + |
| 167 | +test('archive validation rejects tag mismatches between asset and payload', async () => { |
| 168 | + const fixture = await createArchiveFixture({ payloadTag: 'v9.9.9' }); |
| 169 | + const inspection = inspectReleaseNoteArchive({ |
| 170 | + zipPath: fixture.zipPath, |
| 171 | + candidate: { tag: 'v1.0.0' }, |
| 172 | + }); |
| 173 | + |
| 174 | + assert.equal(inspection.accepted, false); |
| 175 | + assert.equal(inspection.reason, 'tag_mismatch'); |
| 176 | +}); |
| 177 | + |
| 178 | +test('archive validation requires both published locales', async () => { |
| 179 | + const fixture = await createArchiveFixture({ includeEn: false }); |
| 180 | + const inspection = inspectReleaseNoteArchive({ |
| 181 | + zipPath: fixture.zipPath, |
| 182 | + candidate: { tag: 'v1.0.0' }, |
| 183 | + }); |
| 184 | + |
| 185 | + assert.equal(inspection.accepted, false); |
| 186 | + assert.equal(inspection.reason, 'missing_locale_body'); |
| 187 | + assert.equal(inspection.locale, 'en'); |
| 188 | +}); |
| 189 | + |
| 190 | +test('normalization preserves display tags, sorts semver consistently, and materialization is idempotent', async () => { |
| 191 | + const firstFixture = await createArchiveFixture({ tag: '1.0.0' }); |
| 192 | + const secondFixture = await createArchiveFixture({ tag: 'v1.0.1' }); |
| 193 | + const firstAccepted = inspectReleaseNoteArchive({ |
| 194 | + zipPath: firstFixture.zipPath, |
| 195 | + candidate: { |
| 196 | + tag: '1.0.0', |
| 197 | + assetName: 'release-notes-1.0.0-history.zip', |
| 198 | + assetDownloadUrl: 'https://example.test/assets/1.0.0.zip', |
| 199 | + releasePublishedAt: '2026-04-13T08:00:00.000Z', |
| 200 | + releaseHtmlUrl: 'https://example.test/releases/1.0.0', |
| 201 | + releaseName: '1.0.0', |
| 202 | + }, |
| 203 | + }); |
| 204 | + const secondAccepted = inspectReleaseNoteArchive({ |
| 205 | + zipPath: secondFixture.zipPath, |
| 206 | + candidate: { |
| 207 | + tag: 'v1.0.1', |
| 208 | + assetName: 'release-notes-v1.0.1-history.zip', |
| 209 | + assetDownloadUrl: 'https://example.test/assets/v1.0.1.zip', |
| 210 | + releasePublishedAt: '2026-04-14T08:00:00.000Z', |
| 211 | + releaseHtmlUrl: 'https://example.test/releases/v1.0.1', |
| 212 | + releaseName: 'v1.0.1', |
| 213 | + }, |
| 214 | + }); |
| 215 | + |
| 216 | + assert.equal(firstAccepted.accepted, true); |
| 217 | + assert.equal(secondAccepted.accepted, true); |
| 218 | + |
| 219 | + const repoRoot = await mkdtemp(path.join(os.tmpdir(), 'docs-release-notes-materialize-')); |
| 220 | + const config = resolveReleaseNotesConfig({ repoRoot }); |
| 221 | + const snapshot = { |
| 222 | + ...normalizeSynchronizedReleaseNotes({ |
| 223 | + acceptedEntries: [firstAccepted, secondAccepted], |
| 224 | + config, |
| 225 | + synchronizedAt: '2026-04-14T10:00:00.000Z', |
| 226 | + }), |
| 227 | + skipped: [], |
| 228 | + counts: { |
| 229 | + releases: 2, |
| 230 | + discovered: 2, |
| 231 | + synchronized: 2, |
| 232 | + skipped: 0, |
| 233 | + }, |
| 234 | + }; |
| 235 | + |
| 236 | + assert.deepEqual( |
| 237 | + snapshot.entries.map((entry) => entry.tag), |
| 238 | + ['v1.0.1', '1.0.0'], |
| 239 | + ); |
| 240 | + |
| 241 | + const firstRun = await materializeReleaseNotes({ snapshot, config }); |
| 242 | + const firstIndex = await readFile(config.outputPaths.indexJson, 'utf8'); |
| 243 | + const firstZh = await readFile(path.join(config.outputPaths.zhDir, 'v1.0.1.md'), 'utf8'); |
| 244 | + |
| 245 | + const secondRun = await materializeReleaseNotes({ snapshot, config }); |
| 246 | + const secondIndex = await readFile(config.outputPaths.indexJson, 'utf8'); |
| 247 | + const secondZh = await readFile(path.join(config.outputPaths.zhDir, 'v1.0.1.md'), 'utf8'); |
| 248 | + |
| 249 | + assert.equal(firstIndex, secondIndex); |
| 250 | + assert.equal(firstZh, secondZh); |
| 251 | + assert.deepEqual(firstRun.writtenFiles, secondRun.writtenFiles); |
| 252 | +}); |
0 commit comments