364 lines
14 KiB
JavaScript
364 lines
14 KiB
JavaScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { registerHooks } from 'node:module';
|
|
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import {
|
|
parseBilingualReleaseBody,
|
|
extractReleaseAssets,
|
|
classifyReleaseChannel,
|
|
isClientReleaseTag,
|
|
normalizeGitHubReleases,
|
|
sanitizeGitHubUrl,
|
|
} from '../src/lib/release-parser.ts';
|
|
import { localizedPath, switchLocalePath } from '../src/i18n/types.ts';
|
|
import worker from '../worker/index.js';
|
|
|
|
// Register lightweight hooks so Node 22 can import src/lib/releases.ts directly
|
|
// (resolving extensionless ./release-parser -> .ts and JSON imports without import attributes).
|
|
registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
if (specifier === './release-parser') {
|
|
return nextResolve('./release-parser.ts', context);
|
|
}
|
|
return nextResolve(specifier, context);
|
|
},
|
|
load(url, context, nextLoad) {
|
|
if (url.endsWith('.json')) {
|
|
const source = readFileSync(fileURLToPath(url), 'utf8');
|
|
return { format: 'json', shortCircuit: true, source };
|
|
}
|
|
return nextLoad(url, context);
|
|
},
|
|
});
|
|
|
|
const { formatStars, formatBytes, extractShaForAsset, FALLBACK_RELEASE } = await import('../src/lib/releases.ts');
|
|
|
|
const HASH_A = '1'.repeat(64);
|
|
const HASH_B = 'f'.repeat(64);
|
|
|
|
describe('src/lib/release-parser.ts — Unit Tests', () => {
|
|
it('parses bilingual cn-first and en-first release bodies and strips HTML tags in headings', () => {
|
|
const cnFirst = [
|
|
'[中文](#cn-v0.2.12) | [English](#en-v0.2.12)',
|
|
'<h3 id="cn-v0.2.12"><strong>重点</strong> <em>更新</em></h3>',
|
|
'- 支持拖放文件',
|
|
'* 剪贴板同步修复',
|
|
'### 已知限制',
|
|
'- 忽略限制项',
|
|
'### 下载',
|
|
'- 忽略下载项',
|
|
'### 构建与许可证',
|
|
'- 忽略许可证项',
|
|
'<h3 id="en-v0.2.12"><span>Key</span> Updates</h3>',
|
|
'> Ignore blockquote line',
|
|
'- Drag-and-drop file support',
|
|
'* Clipboard sync fix',
|
|
'### Known Limitations (Windows)',
|
|
'- Ignore English limitations',
|
|
'### Downloads',
|
|
'- Ignore English downloads',
|
|
'### License',
|
|
'- Ignore English license',
|
|
].join('\r\n');
|
|
|
|
const parsed = parseBilingualReleaseBody(cnFirst);
|
|
assert.deepEqual(parsed['zh-CN'], [
|
|
{ title: '重点 更新', items: ['支持拖放文件', '剪贴板同步修复'] },
|
|
]);
|
|
assert.deepEqual(parsed['en-US'], [
|
|
{ title: 'Key Updates', items: ['Drag-and-drop file support', 'Clipboard sync fix'] },
|
|
]);
|
|
|
|
const enFirst = [
|
|
'<h2 id="en-v0.2.12">Added</h2>',
|
|
'- English item',
|
|
'<h2 id="cn-v0.2.12">新增</h2>',
|
|
'- 中文项',
|
|
].join('\n');
|
|
const parsedReversed = parseBilingualReleaseBody(enFirst);
|
|
assert.deepEqual(parsedReversed['en-US'], [{ title: 'Added', items: ['English item'] }]);
|
|
assert.deepEqual(parsedReversed['zh-CN'], [{ title: '新增', items: ['中文项'] }]);
|
|
});
|
|
|
|
it('falls back single-language body and empty-half bilingual body cleanly', () => {
|
|
const single = '### Improvements\n- Faster VM startup\n- Lower memory footprint';
|
|
const resSingle = parseBilingualReleaseBody(single);
|
|
assert.deepEqual(resSingle['zh-CN'], [
|
|
{ title: 'Improvements', items: ['Faster VM startup', 'Lower memory footprint'] },
|
|
]);
|
|
assert.deepEqual(resSingle['en-US'], resSingle['zh-CN']);
|
|
|
|
// Bilingual markers present, but English half only contains a skipped section -> falls back to zh-CN
|
|
const oneEmptyHalf = [
|
|
'<h3 id="cn-v1">修复</h3>',
|
|
'- 修复启动闪烁',
|
|
'<h3 id="en-v1">Download</h3>',
|
|
'- Skipped only',
|
|
].join('\n');
|
|
const resHalf = parseBilingualReleaseBody(oneEmptyHalf);
|
|
assert.deepEqual(resHalf['zh-CN'], [{ title: '修复', items: ['修复启动闪烁'] }]);
|
|
assert.deepEqual(resHalf['en-US'], [{ title: '修复', items: ['修复启动闪烁'] }]);
|
|
});
|
|
|
|
it('handles empty, whitespace, unheaded bullets, and headings with zero bullets', () => {
|
|
assert.deepEqual(parseBilingualReleaseBody(''), { 'zh-CN': [], 'en-US': [] });
|
|
assert.deepEqual(parseBilingualReleaseBody(' \r\n '), { 'zh-CN': [], 'en-US': [] });
|
|
|
|
const mixed = [
|
|
'- Orphan bullet before heading',
|
|
'### Empty Heading',
|
|
'#### Real Section',
|
|
'- Real item',
|
|
].join('\n');
|
|
const parsed = parseBilingualReleaseBody(mixed);
|
|
assert.deepEqual(parsed['zh-CN'], [
|
|
{ title: '', items: ['Orphan bullet before heading'] },
|
|
{ title: 'Real Section', items: ['Real item'] },
|
|
]);
|
|
});
|
|
|
|
it('filters client release tags and excludes runtime-* tags', () => {
|
|
assert.equal(isClientReleaseTag('v0.2.12'), true);
|
|
assert.equal(isClientReleaseTag('v1.0.0-alpha.2'), true);
|
|
assert.equal(isClientReleaseTag('V2.10'), true);
|
|
assert.equal(isClientReleaseTag('dev'), true);
|
|
assert.equal(isClientReleaseTag('DEV'), true);
|
|
assert.equal(isClientReleaseTag('alpha'), true);
|
|
assert.equal(isClientReleaseTag('alpha-20261203'), true);
|
|
|
|
assert.equal(isClientReleaseTag(''), false);
|
|
assert.equal(isClientReleaseTag('runtime-2df30ff'), false);
|
|
assert.equal(isClientReleaseTag('RUNTIME-v1.0'), false);
|
|
assert.equal(isClientReleaseTag('docs-v1.0'), false);
|
|
assert.equal(isClientReleaseTag('v1'), false);
|
|
assert.equal(isClientReleaseTag('latest'), false);
|
|
});
|
|
|
|
it('classifies stable, alpha, and dev channels accurately', () => {
|
|
assert.equal(classifyReleaseChannel('v0.2.12', false), 'stable');
|
|
assert.equal(classifyReleaseChannel('dev', false), 'dev');
|
|
assert.equal(classifyReleaseChannel('dev', true), 'dev');
|
|
assert.equal(classifyReleaseChannel('v0.3.0-dev.1', true), 'dev');
|
|
assert.equal(classifyReleaseChannel('v0.3.0-alpha.1', false), 'alpha');
|
|
assert.equal(classifyReleaseChannel('v0.3.0-beta.2', false), 'alpha');
|
|
assert.equal(classifyReleaseChannel('v0.3.0-rc.1', false), 'alpha');
|
|
assert.equal(classifyReleaseChannel('v0.2.12', true), 'alpha');
|
|
});
|
|
|
|
it('extracts exe, zip, SHA256SUMS.txt assets, validates GitHub HTTPS URLs, and extracts valid sha256 digests', () => {
|
|
const assets = [
|
|
{
|
|
name: 'reims-go-v0.2.12-windows-amd64.exe',
|
|
size: 21023744,
|
|
browser_download_url: 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/reims.exe',
|
|
digest: `sha256:${HASH_A}`,
|
|
},
|
|
{
|
|
name: 'reims-go-v0.2.12-WINDOWS-AMD64.ZIP',
|
|
size: 8362065,
|
|
browser_download_url: 'https://objects.githubusercontent.com/reims.zip',
|
|
digest: `SHA256:${HASH_B}`,
|
|
},
|
|
{
|
|
name: 'SHA256SUMS.txt',
|
|
size: 180,
|
|
browser_download_url: 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/SHA256SUMS.txt',
|
|
},
|
|
];
|
|
|
|
const out = extractReleaseAssets(assets);
|
|
assert.deepEqual(out.exe, {
|
|
name: 'reims-go-v0.2.12-windows-amd64.exe',
|
|
size: 21023744,
|
|
url: 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/reims.exe',
|
|
});
|
|
assert.deepEqual(out.zip, {
|
|
name: 'reims-go-v0.2.12-WINDOWS-AMD64.ZIP',
|
|
size: 8362065,
|
|
url: 'https://objects.githubusercontent.com/reims.zip',
|
|
});
|
|
assert.equal(out.sha256_url, 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/SHA256SUMS.txt');
|
|
assert.deepEqual(out.digestLines, [
|
|
`${HASH_A} reims-go-v0.2.12-windows-amd64.exe`,
|
|
`${HASH_B} reims-go-v0.2.12-WINDOWS-AMD64.ZIP`,
|
|
]);
|
|
|
|
// Reject untrusted schemes & non-GitHub hosts
|
|
assert.equal(sanitizeGitHubUrl('javascript:alert(1)'), null);
|
|
assert.equal(sanitizeGitHubUrl('data:text/html,<script>alert(1)</script>'), null);
|
|
assert.equal(sanitizeGitHubUrl('http://github.com/Nwflower/Reims-Go'), null);
|
|
assert.equal(sanitizeGitHubUrl('https://evil.com/reims.exe'), null);
|
|
assert.equal(sanitizeGitHubUrl('https://github.com.evil.com/reims.exe'), null);
|
|
|
|
// Undefined / malformed assets
|
|
const empty = extractReleaseAssets(undefined);
|
|
assert.deepEqual(empty, { exe: null, zip: null, sha256_url: null, digestLines: [] });
|
|
|
|
const missingUrlsAndBadDigest = extractReleaseAssets([
|
|
{ name: 'reims-windows-amd64.exe', size: 123, digest: 'md5:1234' },
|
|
{ name: 'reims-windows-amd64.zip', size: 456, browser_download_url: 'https://evil.com/a.zip' },
|
|
]);
|
|
assert.deepEqual(missingUrlsAndBadDigest, { exe: null, zip: null, sha256_url: null, digestLines: [] });
|
|
});
|
|
|
|
it('prevents protocol-relative open redirects in localizedPath and switchLocalePath', () => {
|
|
assert.equal(localizedPath('zh-CN', '//evil.com/phish'), '/evil.com/phish');
|
|
assert.equal(localizedPath('en-US', '//evil.com/phish'), '/en/evil.com/phish');
|
|
assert.equal(switchLocalePath('/en//evil.com/phish', 'zh-CN'), '/evil.com/phish');
|
|
assert.equal(switchLocalePath('//evil.com/phish', 'en-US'), '/en/evil.com/phish');
|
|
assert.equal(switchLocalePath('/en/docs/quick-start', 'zh-CN'), '/docs/quick-start');
|
|
assert.equal(switchLocalePath('/docs/quick-start', 'en-US'), '/en/docs/quick-start');
|
|
});
|
|
|
|
it('normalizes and sorts releases (stable newest first, then prereleases newest first)', () => {
|
|
const raw = [
|
|
{ tag_name: 'runtime-abc123', published_at: '2026-12-03T10:00:00Z' },
|
|
{ tag_name: 'v0.9.9', draft: true, published_at: '2026-12-03T11:00:00Z' },
|
|
{ tag_name: 'dev', prerelease: true, published_at: '2026-12-03T09:00:00Z', body: '- Dev item' },
|
|
{ tag_name: 'v0.2.10', published_at: '2026-11-01T00:00:00Z', body: '- Older stable' },
|
|
{ tag_name: 'v0.3.0-alpha.2', prerelease: true, published_at: '2026-12-03T08:00:00Z', body: '- Alpha' },
|
|
{ tag_name: 'v0.2.12', published_at: '2026-12-02T20:00:00Z', body: '- Latest stable' },
|
|
];
|
|
|
|
const entries = normalizeGitHubReleases(raw);
|
|
assert.deepEqual(
|
|
entries.map((e) => e.tag),
|
|
['v0.2.12', 'v0.2.10', 'dev', 'v0.3.0-alpha.2'],
|
|
);
|
|
assert.equal(entries[0].date, '2026-12-02');
|
|
assert.equal(entries[0].name, 'Reims-Go v0.2.12');
|
|
assert.equal(entries[0].html_url, 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.12');
|
|
});
|
|
|
|
it('maintains 100% output parity with worker/index.js on identical GitHub fixtures', async () => {
|
|
const fixtures = [
|
|
{
|
|
tag_name: 'runtime-999999',
|
|
draft: false,
|
|
prerelease: false,
|
|
published_at: '2026-12-03T00:00:00Z',
|
|
body: '### Runtime\n- Ignored',
|
|
assets: [],
|
|
},
|
|
{
|
|
tag_name: 'v0.2.12',
|
|
name: 'Reims-Go v0.2.12',
|
|
draft: false,
|
|
prerelease: false,
|
|
published_at: '2026-12-02T20:42:58Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.12',
|
|
body: [
|
|
'[中文](#cn-v0.2.12) | [English](#en-v0.2.12)',
|
|
'<h3 id="cn-v0.2.12">重点更新</h3>',
|
|
'- 拖放支持',
|
|
'### 已知限制',
|
|
'- 跳过',
|
|
'<h3 id="en-v0.2.12">Highlights</h3>',
|
|
'- Drag and drop support',
|
|
'### Download',
|
|
'- Skip',
|
|
].join('\n'),
|
|
assets: [
|
|
{
|
|
name: 'reims-go-v0.2.12-windows-amd64.exe',
|
|
size: 21023744,
|
|
browser_download_url: 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/reims.exe',
|
|
digest: `sha256:${HASH_A}`,
|
|
},
|
|
{
|
|
name: 'SHA256SUMS.txt',
|
|
size: 128,
|
|
browser_download_url: 'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/SHA256SUMS.txt',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
tag_name: 'v0.3.0-dev.1',
|
|
name: 'Dev Preview',
|
|
draft: false,
|
|
prerelease: true,
|
|
published_at: '2026-12-03T06:00:00Z',
|
|
body: '> Warning\n### Fixes\n- Single language fix',
|
|
assets: [],
|
|
},
|
|
];
|
|
|
|
const parserEntries = normalizeGitHubReleases(fixtures);
|
|
|
|
const origFetch = globalThis.fetch;
|
|
const origCaches = globalThis.caches;
|
|
try {
|
|
globalThis.caches = {
|
|
default: {
|
|
async match() {
|
|
return undefined;
|
|
},
|
|
async put() {},
|
|
},
|
|
};
|
|
globalThis.fetch = async (url) =>
|
|
new Response(JSON.stringify(String(url).includes('/releases') ? fixtures : { stargazers_count: 1868 }), {
|
|
status: 200,
|
|
});
|
|
|
|
const res = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases'),
|
|
{},
|
|
{ waitUntil() {} },
|
|
);
|
|
const workerPayload = await res.json();
|
|
assert.deepEqual(workerPayload.entries, parserEntries);
|
|
} finally {
|
|
globalThis.fetch = origFetch;
|
|
globalThis.caches = origCaches;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('src/lib/releases.ts — Utility Helpers', () => {
|
|
it('formatStars formats star counts and invalid values cleanly', () => {
|
|
assert.equal(formatStars(1868), '1.9k');
|
|
assert.equal(formatStars(1000), '1.0k');
|
|
assert.equal(formatStars(999), '999');
|
|
assert.equal(formatStars(1), '1');
|
|
assert.equal(formatStars(0), '1.8k');
|
|
assert.equal(formatStars(-42), '1.8k');
|
|
assert.equal(formatStars(Number.NaN), '1.8k');
|
|
assert.equal(formatStars(Number.POSITIVE_INFINITY), '1.8k');
|
|
});
|
|
|
|
it('formatBytes formats bytes, KB, MB and handles empty/non-positive inputs', () => {
|
|
assert.equal(formatBytes(undefined), '');
|
|
assert.equal(formatBytes(0), '');
|
|
assert.equal(formatBytes(-100), '');
|
|
assert.equal(formatBytes(512), '512 B');
|
|
assert.equal(formatBytes(1024), '1.0 KB');
|
|
assert.equal(formatBytes(1536), '1.5 KB');
|
|
assert.equal(formatBytes(1024 * 1024), '1.0 MB');
|
|
assert.equal(formatBytes(21023744), '20.0 MB');
|
|
});
|
|
|
|
it('extractShaForAsset extracts 64-hex SHA-256 digests case-insensitively', () => {
|
|
const shaText = [
|
|
`${HASH_A} reims-go-v0.2.12-windows-amd64.exe`,
|
|
`${HASH_B} reims-go-v0.2.12-windows-amd64.zip`,
|
|
`not-a-hash broken-file.bin`,
|
|
].join('\r\n');
|
|
|
|
assert.equal(extractShaForAsset(shaText, 'reims-go-v0.2.12-windows-amd64.exe'), HASH_A);
|
|
assert.equal(extractShaForAsset(shaText, 'REIMS-GO-V0.2.12-WINDOWS-AMD64.ZIP'), HASH_B);
|
|
assert.equal(extractShaForAsset(shaText, 'broken-file.bin'), '');
|
|
assert.equal(extractShaForAsset(shaText, 'nonexistent.exe'), '');
|
|
assert.equal(extractShaForAsset('', 'reims-go-v0.2.12-windows-amd64.exe'), '');
|
|
assert.equal(extractShaForAsset(shaText, undefined), '');
|
|
|
|
// Also verifies real FALLBACK_RELEASE sha256_sums parses cleanly
|
|
if (FALLBACK_RELEASE.exe?.name && FALLBACK_RELEASE.sha256_sums) {
|
|
const realHash = extractShaForAsset(FALLBACK_RELEASE.sha256_sums, FALLBACK_RELEASE.exe.name);
|
|
assert.match(realHash, /^[a-f0-9]{64}$/i);
|
|
}
|
|
});
|
|
});
|