665 lines
24 KiB
JavaScript
665 lines
24 KiB
JavaScript
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import worker from '../worker/index.js';
|
|
|
|
const VALID_EXE_HASH = 'a'.repeat(64);
|
|
const VALID_ZIP_HASH = 'b'.repeat(64);
|
|
|
|
function createMockCache() {
|
|
const store = new Map();
|
|
return {
|
|
store,
|
|
putCalls: [],
|
|
matchCalls: [],
|
|
async match(req) {
|
|
const key = typeof req === 'string' ? req : req.url;
|
|
this.matchCalls.push(key);
|
|
const hit = store.get(key);
|
|
return hit ? hit.clone() : undefined;
|
|
},
|
|
async put(req, res) {
|
|
const key = typeof req === 'string' ? req : req.url;
|
|
this.putCalls.push(key);
|
|
store.set(key, res.clone());
|
|
},
|
|
};
|
|
}
|
|
|
|
function createMockCtx() {
|
|
const promises = [];
|
|
return {
|
|
promises,
|
|
waitUntil(p) {
|
|
promises.push(Promise.resolve(p));
|
|
},
|
|
async flush() {
|
|
await Promise.all(promises);
|
|
},
|
|
};
|
|
}
|
|
|
|
function sampleGitHubRepo(overrides = {}) {
|
|
return {
|
|
stargazers_count: 1924,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function sampleGitHubReleases() {
|
|
return [
|
|
{
|
|
tag_name: 'runtime-2df30ff',
|
|
name: 'Runtime 2df30ff',
|
|
draft: false,
|
|
prerelease: false,
|
|
published_at: '2026-12-03T01:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/runtime-2df30ff',
|
|
body: '### Runtime\n- Should be filtered out',
|
|
assets: [],
|
|
},
|
|
{
|
|
tag_name: 'v0.2.13-draft',
|
|
name: 'Draft release',
|
|
draft: true,
|
|
prerelease: false,
|
|
published_at: '2026-12-03T02:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.13-draft',
|
|
body: '### Draft\n- Should not appear',
|
|
assets: [],
|
|
},
|
|
{
|
|
tag_name: 'docs-v1',
|
|
name: 'Docs Tag',
|
|
draft: false,
|
|
prerelease: false,
|
|
published_at: '2026-12-03T01:30:00Z',
|
|
body: '- Ignored non-client tag',
|
|
assets: [],
|
|
},
|
|
{
|
|
tag_name: 'dev',
|
|
name: 'Reims-Go Dev',
|
|
draft: false,
|
|
prerelease: true,
|
|
published_at: '2026-12-03T05:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/dev',
|
|
body: '> Dev warning banner\n### Changes\n- Nightly dev build fix',
|
|
assets: [],
|
|
},
|
|
{
|
|
tag_name: 'v0.3.0-alpha.1',
|
|
name: 'Reims-Go v0.3.0 Alpha 1',
|
|
draft: false,
|
|
prerelease: true,
|
|
published_at: '2026-12-03T04:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.3.0-alpha.1',
|
|
body: '### Highlights\n- Experimental GPU scheduler',
|
|
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"><strong>重点更新</strong></h3>',
|
|
'- 支持文件拖拽与剪贴板修复',
|
|
'* 支持 Npcap 无线桥接',
|
|
'',
|
|
'### 已知限制',
|
|
'- 这段应该被跳过',
|
|
'',
|
|
'### 下载',
|
|
'- 下载链接应被跳过',
|
|
'',
|
|
'### 许可证',
|
|
'- GPL-3.0 应被跳过',
|
|
'',
|
|
'<h3 id="en-v0.2.12">Highlights</h3>',
|
|
'> Note blockquote should be ignored',
|
|
'- Drag-and-drop file transfer and clipboard fix',
|
|
'* Npcap Wi-Fi bridging support',
|
|
'',
|
|
'### Known Limitations',
|
|
'- Should be skipped in English',
|
|
'',
|
|
'### Download',
|
|
'- Skipped download section',
|
|
'',
|
|
'### License',
|
|
'- Skipped license section',
|
|
].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-go-v0.2.12-windows-amd64.exe',
|
|
digest: `sha256:${VALID_EXE_HASH}`,
|
|
},
|
|
{
|
|
name: 'reims-go-v0.2.12-windows-amd64.zip',
|
|
size: 8362065,
|
|
browser_download_url:
|
|
'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/reims-go-v0.2.12-windows-amd64.zip',
|
|
digest: `sha256:${VALID_ZIP_HASH}`,
|
|
},
|
|
{
|
|
name: 'SHA256SUMS.txt',
|
|
size: 204,
|
|
browser_download_url:
|
|
'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/SHA256SUMS.txt',
|
|
},
|
|
{
|
|
name: 'extra-bad-digest.bin',
|
|
size: 100,
|
|
browser_download_url: 'https://example.com/extra.bin',
|
|
digest: 'sha256:short',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
tag_name: 'v0.2.11',
|
|
name: 'Reims-Go v0.2.11',
|
|
draft: false,
|
|
prerelease: false,
|
|
published_at: '2026-11-20T10:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.11',
|
|
body: '### Bug Fixes\n- Fixed display fallback',
|
|
assets: [],
|
|
},
|
|
];
|
|
}
|
|
|
|
describe('worker/index.js — Edge API & Caching', () => {
|
|
let origFetch;
|
|
let origCaches;
|
|
let origConsoleWarn;
|
|
let mockCache;
|
|
let loggedMessages;
|
|
|
|
beforeEach(() => {
|
|
origFetch = globalThis.fetch;
|
|
origCaches = globalThis.caches;
|
|
origConsoleWarn = console.warn;
|
|
mockCache = createMockCache();
|
|
loggedMessages = [];
|
|
globalThis.caches = { default: mockCache };
|
|
console.warn = (...args) => loggedMessages.push(args.join(' '));
|
|
});
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = origFetch;
|
|
globalThis.caches = origCaches;
|
|
console.warn = origConsoleWarn;
|
|
});
|
|
|
|
it('handles GET /api/github/releases cache MISS, fetches GitHub, sets security headers, and populates cache via waitUntil', async () => {
|
|
const fetchedUrls = [];
|
|
const fetchedHeaders = [];
|
|
globalThis.fetch = async (url, init) => {
|
|
fetchedUrls.push(String(url));
|
|
fetchedHeaders.push(init?.headers || {});
|
|
if (String(url).includes('/releases')) {
|
|
return new Response(JSON.stringify(sampleGitHubReleases()), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
return new Response(JSON.stringify(sampleGitHubRepo()), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
};
|
|
|
|
const ctx = createMockCtx();
|
|
const req = new Request('https://reims.dev/api/github/releases', { method: 'GET' });
|
|
const res = await worker.fetch(req, { GITHUB_TOKEN: 'ghp_test_token' }, ctx);
|
|
await ctx.flush();
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.headers.get('X-Reims-Cache'), 'MISS');
|
|
assert.equal(res.headers.get('X-Reims-Source'), 'edge-api');
|
|
assert.equal(res.headers.get('X-Content-Type-Options'), 'nosniff');
|
|
assert.equal(res.headers.get('X-Frame-Options'), 'DENY');
|
|
assert.equal(res.headers.get('Content-Security-Policy'), "default-src 'none'");
|
|
assert.equal(res.headers.get('Cache-Control'), 'public, max-age=120, s-maxage=3600');
|
|
assert.ok(Number(res.headers.get('X-Reims-Fetched-At')) > 0);
|
|
assert.match(res.headers.get('Content-Type') || '', /application\/json/i);
|
|
|
|
assert.equal(fetchedUrls.length, 2);
|
|
assert.ok(fetchedUrls.includes('https://api.github.com/repos/Nwflower/Reims-Go'));
|
|
assert.ok(fetchedUrls.includes('https://api.github.com/repos/Nwflower/Reims-Go/releases?per_page=25'));
|
|
for (const h of fetchedHeaders) {
|
|
assert.equal(h.Authorization, 'Bearer ghp_test_token');
|
|
assert.equal(h.Accept, 'application/vnd.github+json');
|
|
assert.match(h['User-Agent'], /Reims-Go-Landing-Worker/);
|
|
}
|
|
|
|
const body = await res.json();
|
|
assert.equal(body.source, 'edge-api');
|
|
assert.ok(Date.parse(body.synced_at) > 0);
|
|
assert.equal(body.latest.stars, 1924);
|
|
assert.equal(body.latest.version, 'v0.2.12');
|
|
assert.equal(body.latest.exe?.name, 'reims-go-v0.2.12-windows-amd64.exe');
|
|
assert.equal(body.latest.zip?.name, 'reims-go-v0.2.12-windows-amd64.zip');
|
|
assert.equal(
|
|
body.latest.sha256_url,
|
|
'https://github.com/Nwflower/Reims-Go/releases/download/v0.2.12/SHA256SUMS.txt',
|
|
);
|
|
assert.equal(
|
|
body.latest.sha256_sums,
|
|
`${VALID_EXE_HASH} reims-go-v0.2.12-windows-amd64.exe\n${VALID_ZIP_HASH} reims-go-v0.2.12-windows-amd64.zip`,
|
|
);
|
|
|
|
// Written to caches.default via ctx.waitUntil
|
|
assert.equal(ctx.promises.length, 1);
|
|
assert.deepEqual(mockCache.putCalls, ['https://reims.dev/api/github/releases']);
|
|
});
|
|
|
|
it('omits Authorization header when env.GITHUB_TOKEN is not set', async () => {
|
|
const capturedHeaders = [];
|
|
globalThis.fetch = async (url, init) => {
|
|
capturedHeaders.push(init?.headers || {});
|
|
return new Response(JSON.stringify(String(url).includes('/releases') ? [] : { stargazers_count: 100 }), {
|
|
status: 200,
|
|
});
|
|
};
|
|
|
|
const ctx = createMockCtx();
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx);
|
|
await ctx.flush();
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(capturedHeaders.length, 2);
|
|
for (const h of capturedHeaders) {
|
|
assert.equal('Authorization' in h, false);
|
|
}
|
|
});
|
|
|
|
it('returns X-Reims-Cache: HIT on subsequent request without calling fetch', async () => {
|
|
let fetchCount = 0;
|
|
globalThis.fetch = async (url) => {
|
|
fetchCount += 1;
|
|
return new Response(
|
|
JSON.stringify(String(url).includes('/releases') ? sampleGitHubReleases() : sampleGitHubRepo()),
|
|
{ status: 200 },
|
|
);
|
|
};
|
|
|
|
const ctx1 = createMockCtx();
|
|
const res1 = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx1);
|
|
await ctx1.flush();
|
|
assert.equal(res1.headers.get('X-Reims-Cache'), 'MISS');
|
|
assert.equal(fetchCount, 2);
|
|
const body1 = await res1.json();
|
|
|
|
const ctx2 = createMockCtx();
|
|
const res2 = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx2);
|
|
await ctx2.flush();
|
|
assert.equal(res2.status, 200);
|
|
assert.equal(res2.headers.get('X-Reims-Cache'), 'HIT');
|
|
assert.equal(fetchCount, 2, 'fetch must not be called on cache HIT');
|
|
const body2 = await res2.json();
|
|
assert.deepEqual(body2, body1);
|
|
});
|
|
|
|
it('normalizes query strings to the canonical /api/github/releases cache key', async () => {
|
|
let fetchCount = 0;
|
|
globalThis.fetch = async (url) => {
|
|
fetchCount += 1;
|
|
return new Response(
|
|
JSON.stringify(String(url).includes('/releases') ? sampleGitHubReleases() : sampleGitHubRepo()),
|
|
{ status: 200 },
|
|
);
|
|
};
|
|
|
|
const ctx1 = createMockCtx();
|
|
const res1 = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases?locale=en&cachebust=123'),
|
|
{},
|
|
ctx1,
|
|
);
|
|
await ctx1.flush();
|
|
assert.equal(res1.headers.get('X-Reims-Cache'), 'MISS');
|
|
assert.deepEqual(mockCache.putCalls, ['https://reims.dev/api/github/releases']);
|
|
|
|
const ctx2 = createMockCtx();
|
|
const res2 = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases?different=query'),
|
|
{},
|
|
ctx2,
|
|
);
|
|
assert.equal(res2.headers.get('X-Reims-Cache'), 'HIT');
|
|
assert.equal(fetchCount, 2);
|
|
|
|
const ctx3 = createMockCtx();
|
|
const res3 = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx3);
|
|
assert.equal(res3.headers.get('X-Reims-Cache'), 'HIT');
|
|
assert.equal(fetchCount, 2);
|
|
});
|
|
|
|
it('falls back to env.ASSETS /data/releases-bundle.json on GitHub 403 rate limit', async () => {
|
|
globalThis.fetch = async () => new Response('API rate limit exceeded', { status: 403 });
|
|
|
|
const assetRequests = [];
|
|
const fallbackPayload = { source: 'static-fallback', latest: { version: 'v0.2.12' }, entries: [] };
|
|
const env = {
|
|
ASSETS: {
|
|
async fetch(req) {
|
|
assetRequests.push(req.url);
|
|
return new Response(JSON.stringify(fallbackPayload), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
},
|
|
},
|
|
};
|
|
|
|
const ctx = createMockCtx();
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), env, ctx);
|
|
await ctx.flush();
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.headers.get('X-Reims-Source'), 'static-fallback');
|
|
assert.equal(res.headers.get('X-Reims-Cache'), 'FALLBACK');
|
|
assert.equal(res.headers.get('X-Content-Type-Options'), 'nosniff');
|
|
assert.equal(res.headers.get('Cache-Control'), 'public, max-age=60, s-maxage=60');
|
|
assert.deepEqual(assetRequests, ['https://reims.dev/data/releases-bundle.json']);
|
|
assert.deepEqual(await res.json(), fallbackPayload);
|
|
// Negative-cached for 60s to shield GitHub API during outages / rate limits
|
|
assert.deepEqual(mockCache.putCalls, ['https://reims.dev/api/github/releases']);
|
|
assert.ok(loggedMessages.some((m) => m.includes('github_releases_fallback') && m.includes('403')));
|
|
});
|
|
|
|
it('serves stale edge cache immediately and triggers background SWR refresh via waitUntil', async () => {
|
|
const stalePayload = { source: 'edge-api', latest: { version: 'v0.2.11' }, entries: [] };
|
|
mockCache.store.set(
|
|
'https://reims.dev/api/github/releases',
|
|
new Response(JSON.stringify(stalePayload), {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Reims-Source': 'edge-api',
|
|
'X-Reims-Fetched-At': String(Date.now() - 400_000),
|
|
},
|
|
}),
|
|
);
|
|
|
|
let bgFetched = 0;
|
|
globalThis.fetch = async (url) => {
|
|
bgFetched += 1;
|
|
return new Response(
|
|
JSON.stringify(String(url).includes('/releases') ? sampleGitHubReleases() : sampleGitHubRepo()),
|
|
{ status: 200 },
|
|
);
|
|
};
|
|
|
|
const ctx = createMockCtx();
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx);
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.headers.get('X-Reims-Cache'), 'STALE');
|
|
assert.equal((await res.json()).latest.version, 'v0.2.11');
|
|
|
|
await ctx.flush();
|
|
assert.equal(bgFetched, 2, 'SWR must trigger background GitHub refresh');
|
|
const refreshed = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, createMockCtx());
|
|
assert.equal(refreshed.headers.get('X-Reims-Cache'), 'HIT');
|
|
assert.equal((await refreshed.json()).latest.version, 'v0.2.12');
|
|
});
|
|
|
|
it('falls back to env.ASSETS on GitHub 500 error and network throw', async () => {
|
|
const fallbackBody = JSON.stringify({ source: 'static-fallback', latest: { version: 'v0.2.12' }, entries: [] });
|
|
const env = {
|
|
ASSETS: {
|
|
async fetch() {
|
|
return new Response(fallbackBody, { status: 200 });
|
|
},
|
|
},
|
|
};
|
|
|
|
// Case 1: HTTP 500
|
|
globalThis.fetch = async () => new Response('Internal Server Error', { status: 500 });
|
|
const res500 = await worker.fetch(new Request('https://reims.dev/api/github/releases'), env, createMockCtx());
|
|
assert.equal(res500.status, 200);
|
|
assert.equal(res500.headers.get('X-Reims-Source'), 'static-fallback');
|
|
|
|
// Case 2: Network rejection (clear negative cache between sub-cases)
|
|
mockCache.store.clear();
|
|
globalThis.fetch = async () => {
|
|
throw new TypeError('Network unreachable');
|
|
};
|
|
const resNet = await worker.fetch(new Request('https://reims.dev/api/github/releases'), env, createMockCtx());
|
|
assert.equal(resNet.status, 200);
|
|
assert.equal(resNet.headers.get('X-Reims-Source'), 'static-fallback');
|
|
assert.ok(loggedMessages.some((m) => m.includes('Network unreachable')));
|
|
});
|
|
|
|
it('returns HTTP 502 JSON error with no-store when GitHub fails and env.ASSETS fails, throws, or is missing', async () => {
|
|
globalThis.fetch = async () => new Response('Forbidden', { status: 403 });
|
|
|
|
// Missing env.ASSETS
|
|
const resNoAssets = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, createMockCtx());
|
|
assert.equal(resNoAssets.status, 502);
|
|
assert.equal(resNoAssets.headers.get('Cache-Control'), 'no-store');
|
|
assert.equal(resNoAssets.headers.get('X-Content-Type-Options'), 'nosniff');
|
|
assert.match(resNoAssets.headers.get('Content-Type') || '', /application\/json/i);
|
|
assert.deepEqual(await resNoAssets.json(), { error: 'Unable to fetch GitHub releases' });
|
|
|
|
// env.ASSETS returns 404
|
|
const envFailingAssets = {
|
|
ASSETS: {
|
|
async fetch() {
|
|
return new Response('Not Found', { status: 404 });
|
|
},
|
|
},
|
|
};
|
|
const resAssets404 = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases'),
|
|
envFailingAssets,
|
|
createMockCtx(),
|
|
);
|
|
assert.equal(resAssets404.status, 502);
|
|
assert.deepEqual(await resAssets404.json(), { error: 'Unable to fetch GitHub releases' });
|
|
|
|
// env.ASSETS throws an exception
|
|
const envThrowingAssets = {
|
|
ASSETS: {
|
|
async fetch() {
|
|
throw new Error('ASSETS binding unavailable');
|
|
},
|
|
},
|
|
};
|
|
const resAssetsThrow = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases'),
|
|
envThrowingAssets,
|
|
createMockCtx(),
|
|
);
|
|
assert.equal(resAssetsThrow.status, 502);
|
|
assert.deepEqual(await resAssetsThrow.json(), { error: 'Unable to fetch GitHub releases' });
|
|
});
|
|
|
|
it('handles HEAD on /api/github/releases, returns 405 for POST, and passes non-API paths to env.ASSETS', async () => {
|
|
let fetchCount = 0;
|
|
globalThis.fetch = async (url) => {
|
|
fetchCount += 1;
|
|
return new Response(
|
|
JSON.stringify(String(url).includes('/releases') ? sampleGitHubReleases() : sampleGitHubRepo()),
|
|
{ status: 200 },
|
|
);
|
|
};
|
|
|
|
const passthroughLog = [];
|
|
const env = {
|
|
ASSETS: {
|
|
async fetch(req) {
|
|
passthroughLog.push(`${req.method} ${new URL(req.url).pathname}`);
|
|
return new Response(`asset:${new URL(req.url).pathname}`, { status: 200 });
|
|
},
|
|
},
|
|
};
|
|
|
|
// Non-API GETs pass through to ASSETS without touching GitHub
|
|
for (const req of [
|
|
new Request('https://reims.dev/', { method: 'GET' }),
|
|
new Request('https://reims.dev/changelog', { method: 'GET' }),
|
|
new Request('https://reims.dev/_astro/foo.js', { method: 'GET' }),
|
|
]) {
|
|
const res = await worker.fetch(req, env, createMockCtx());
|
|
assert.equal(res.status, 200);
|
|
}
|
|
assert.equal(fetchCount, 0);
|
|
assert.deepEqual(passthroughLog, ['GET /', 'GET /changelog', 'GET /_astro/foo.js']);
|
|
|
|
// POST on /api/github/releases -> 405 Method Not Allowed JSON
|
|
const postRes = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases', { method: 'POST' }),
|
|
env,
|
|
createMockCtx(),
|
|
);
|
|
assert.equal(postRes.status, 405);
|
|
assert.equal(postRes.headers.get('Allow'), 'GET, HEAD');
|
|
assert.equal(postRes.headers.get('Cache-Control'), 'no-store');
|
|
assert.deepEqual(await postRes.json(), { error: 'Method Not Allowed' });
|
|
|
|
// HEAD on /api/github/releases -> 200 with empty body
|
|
const headCtx = createMockCtx();
|
|
const headRes = await worker.fetch(
|
|
new Request('https://reims.dev/api/github/releases', { method: 'HEAD' }),
|
|
env,
|
|
headCtx,
|
|
);
|
|
await headCtx.flush();
|
|
assert.equal(headRes.status, 200);
|
|
assert.equal(await headRes.text(), '');
|
|
|
|
// Non-API path when ASSETS binding is absent -> 404
|
|
const res404 = await worker.fetch(new Request('https://reims.dev/missing'), {}, createMockCtx());
|
|
assert.equal(res404.status, 404);
|
|
});
|
|
|
|
it('filters draft/runtime tags, classifies channels, sorts stable first, and parses bilingual sections accurately', async () => {
|
|
globalThis.fetch = async (url) => {
|
|
if (String(url).includes('/releases')) {
|
|
return new Response(JSON.stringify(sampleGitHubReleases()), { status: 200 });
|
|
}
|
|
return new Response(JSON.stringify(sampleGitHubRepo()), { status: 200 });
|
|
};
|
|
|
|
const ctx = createMockCtx();
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, ctx);
|
|
await ctx.flush();
|
|
const payload = await res.json();
|
|
|
|
// Expect only client non-draft releases: v0.2.12, v0.2.11 (stable sorted newest first), then dev, v0.3.0-alpha.1 (prereleases sorted newest first)
|
|
assert.deepEqual(
|
|
payload.entries.map((e) => ({ tag: e.tag, channel: e.channel })),
|
|
[
|
|
{ tag: 'v0.2.12', channel: 'stable' },
|
|
{ tag: 'v0.2.11', channel: 'stable' },
|
|
{ tag: 'dev', channel: 'dev' },
|
|
{ tag: 'v0.3.0-alpha.1', channel: 'alpha' },
|
|
],
|
|
);
|
|
|
|
const v0212 = payload.entries[0];
|
|
assert.equal(v0212.date, '2026-12-02');
|
|
assert.deepEqual(v0212.sections['zh-CN'], [
|
|
{
|
|
title: '重点更新',
|
|
items: ['支持文件拖拽与剪贴板修复', '支持 Npcap 无线桥接'],
|
|
},
|
|
]);
|
|
assert.deepEqual(v0212.sections['en-US'], [
|
|
{
|
|
title: 'Highlights',
|
|
items: ['Drag-and-drop file transfer and clipboard fix', 'Npcap Wi-Fi bridging support'],
|
|
},
|
|
]);
|
|
|
|
// Single-language dev body mirrors into both zh-CN and en-US and skips blockquote
|
|
const devEntry = payload.entries.find((e) => e.tag === 'dev');
|
|
assert.deepEqual(devEntry.sections['zh-CN'], [{ title: 'Changes', items: ['Nightly dev build fix'] }]);
|
|
assert.deepEqual(devEntry.sections['en-US'], [{ title: 'Changes', items: ['Nightly dev build fix'] }]);
|
|
});
|
|
|
|
it('handles en-first bilingual ordering and missing stargazers/stable fallbacks cleanly', async () => {
|
|
const enFirstRelease = [
|
|
{
|
|
tag_name: 'v0.4.0-rc.1',
|
|
name: '',
|
|
draft: false,
|
|
prerelease: true,
|
|
created_at: '2026-12-03T12:00:00Z',
|
|
body: [
|
|
'<h3 id="en-v0.4.0-rc.1">Features</h3>',
|
|
'- English first feature',
|
|
'<h3 id="cn-v0.4.0-rc.1">新特性</h3>',
|
|
'- 中文后置特性',
|
|
].join('\n'),
|
|
assets: [],
|
|
},
|
|
];
|
|
|
|
globalThis.fetch = async (url) => {
|
|
if (String(url).includes('/releases')) {
|
|
return new Response(JSON.stringify(enFirstRelease), { status: 200 });
|
|
}
|
|
return new Response(JSON.stringify({}), { status: 200 });
|
|
};
|
|
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, createMockCtx());
|
|
const payload = await res.json();
|
|
|
|
assert.equal(payload.latest.stars, 1868);
|
|
assert.equal(payload.latest.version, 'v0.4.0-rc.1');
|
|
assert.equal(payload.entries[0].name, 'Reims-Go v0.4.0-rc.1');
|
|
assert.equal(payload.entries[0].channel, 'alpha');
|
|
assert.deepEqual(payload.entries[0].sections['en-US'], [{ title: 'Features', items: ['English first feature'] }]);
|
|
assert.deepEqual(payload.entries[0].sections['zh-CN'], [{ title: '新特性', items: ['中文后置特性'] }]);
|
|
});
|
|
|
|
it('preserves live releases when repo metadata fails, sorts out-of-order releases before choosing latest, and strips untrusted asset URLs', async () => {
|
|
const outOfOrder = [
|
|
{
|
|
tag_name: 'v0.2.10',
|
|
published_at: '2026-11-01T00:00:00Z',
|
|
html_url: 'javascript:alert(1)',
|
|
assets: [
|
|
{
|
|
name: 'reims-go-v0.2.10-windows-amd64.exe',
|
|
size: 1000,
|
|
browser_download_url: 'https://evil.com/malware.exe',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
tag_name: 'v0.2.12',
|
|
published_at: '2026-12-02T20:00:00Z',
|
|
html_url: 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.12',
|
|
assets: [],
|
|
},
|
|
];
|
|
|
|
globalThis.fetch = async (url) => {
|
|
if (String(url).includes('/releases')) {
|
|
return new Response(JSON.stringify(outOfOrder), { status: 200 });
|
|
}
|
|
return new Response('rate limited repo', { status: 403 });
|
|
};
|
|
|
|
const res = await worker.fetch(new Request('https://reims.dev/api/github/releases'), {}, createMockCtx());
|
|
assert.equal(res.status, 200);
|
|
const payload = await res.json();
|
|
assert.equal(payload.latest.version, 'v0.2.12');
|
|
assert.equal(payload.latest.stars, 1868);
|
|
const older = payload.entries.find((e) => e.tag === 'v0.2.10');
|
|
assert.equal(older.html_url, 'https://github.com/Nwflower/Reims-Go/releases/tag/v0.2.10');
|
|
assert.equal(older.exe, null);
|
|
});
|
|
});
|