mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-31 04:07:52 +00:00
The repo publishes zero GitHub releases, so releases/latest is a permanent 404 and fetchLatestRelease() could never succeed — the entire upgrade notification subsystem was a silent no-op, and refreshUpdateCache() cached a fabricated up_to_date marker on every failure. - Resolve the latest version from raw.githubusercontent.com/.../master/VERSION (same trusted host fetchChangelog already uses). Bounded, shape-gated parse; handles legacy 3-segment and -suffix channel forms. - Discriminate network_error from no_releases in the --json error field and human output. - Never write up_to_date on a failed check: preserve the last-known-good marker (mtime bump keeps the TTL throttle) or write nothing. - Rejected the issue's proposed npm fallback: the gbrain npm package is an unrelated GPU library (#505) and would produce false upgrade prompts. Co-authored-by: Garry Tan <garrytan@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
110 lines
4.2 KiB
TypeScript
110 lines
4.2 KiB
TypeScript
/**
|
|
* Serial (stubs globalThis.fetch): `gbrain self-upgrade --check-only --json`
|
|
* surfaces the changelog so the notify prompt can tell the operator WHAT they'll
|
|
* get, not just a version number. Network stubbed; the JSON shape + changelog
|
|
* extraction are real.
|
|
*/
|
|
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
import { mkdtempSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { VERSION } from '../src/version.ts';
|
|
import { parseSemver } from '../src/core/semver.ts';
|
|
import { runSelfUpgrade } from '../src/commands/self-upgrade.ts';
|
|
|
|
const realFetch = globalThis.fetch;
|
|
const realLog = console.log;
|
|
let home: string;
|
|
let priorHome: string | undefined;
|
|
let captured: string[];
|
|
|
|
function minorBump(): string {
|
|
const v = parseSemver(VERSION)!;
|
|
return `${v[0]}.${v[1] + 1}.0.0`;
|
|
}
|
|
|
|
function microBump(): string {
|
|
const v = parseSemver(VERSION)!;
|
|
return `${v[0]}.${v[1]}.${v[2]}.${v[3] + 1}`;
|
|
}
|
|
|
|
function stub(tag: string | null, changelog: string): void {
|
|
globalThis.fetch = (async (url: any) => {
|
|
const u = String(url);
|
|
if (u.includes('/gbrain/master/VERSION')) {
|
|
if (tag === null) throw new Error('network down');
|
|
return new Response(tag + '\n', { status: 200 });
|
|
}
|
|
if (u.includes('CHANGELOG.md')) return new Response(changelog, { status: 200 });
|
|
return new Response('', { status: 200 });
|
|
}) as typeof fetch;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
priorHome = process.env.GBRAIN_HOME;
|
|
home = mkdtempSync(join(tmpdir(), 'gbrain-checkonly-'));
|
|
process.env.GBRAIN_HOME = home;
|
|
captured = [];
|
|
console.log = (...a: unknown[]) => { captured.push(a.join(' ')); };
|
|
});
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = realFetch;
|
|
console.log = realLog;
|
|
if (priorHome === undefined) delete process.env.GBRAIN_HOME;
|
|
else process.env.GBRAIN_HOME = priorHome;
|
|
rmSync(home, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('self-upgrade --check-only surfaces what you get', () => {
|
|
test('behind → JSON includes changelog_diff + release_url + update_available', async () => {
|
|
const latest = minorBump();
|
|
const changelog = `# Changelog\n\n## [${latest}] - 2026-01-01\n\n- Shiny new thing\n- Another fix\n\n## [${VERSION}] - 2025-12-01\n\n- old\n`;
|
|
stub(`v${latest}`, changelog);
|
|
await runSelfUpgrade(['--check-only', '--json']);
|
|
const out = JSON.parse(captured.join('\n'));
|
|
expect(out.update_available).toBe(true);
|
|
expect(out.latest_version).toBe(latest);
|
|
expect(out.release_url).toBe('https://github.com/garrytan/gbrain/blob/master/CHANGELOG.md');
|
|
expect(out.changelog_diff).toContain('Shiny new thing');
|
|
});
|
|
|
|
test('behind, human output prints What changed', async () => {
|
|
const latest = minorBump();
|
|
const changelog = `# Changelog\n\n## [${latest}] - 2026-01-01\n\n- Headline feature\n\n## [${VERSION}] - 2025-12-01\n\n- old\n`;
|
|
stub(`v${latest}`, changelog);
|
|
await runSelfUpgrade(['--check-only']);
|
|
const text = captured.join('\n');
|
|
expect(text).toContain('What changed');
|
|
expect(text).toContain('Headline feature');
|
|
});
|
|
|
|
test('up to date → no changelog fetched, empty diff', async () => {
|
|
// Stub returns the SAME version → not behind → no changelog.
|
|
stub(`v${VERSION}`, 'should-not-be-read');
|
|
await runSelfUpgrade(['--check-only', '--json']);
|
|
const out = JSON.parse(captured.join('\n'));
|
|
expect(out.update_available).toBe(false);
|
|
expect(out.changelog_diff).toBe('');
|
|
});
|
|
|
|
test('micro release → reports update available', async () => {
|
|
const latest = microBump();
|
|
const changelog = `# Changelog\n\n## [${latest}] - 2026-01-01\n\n- Follow-up fix\n\n## [${VERSION}] - 2025-12-01\n\n- old\n`;
|
|
stub(`v${latest}`, changelog);
|
|
await runSelfUpgrade(['--check-only', '--json']);
|
|
const out = JSON.parse(captured.join('\n'));
|
|
expect(out.update_available).toBe(true);
|
|
expect(out.latest_version).toBe(latest);
|
|
expect(out.changelog_diff).toContain('Follow-up fix');
|
|
});
|
|
|
|
test('network failure → up to date, no crash', async () => {
|
|
stub(null, '');
|
|
await runSelfUpgrade(['--check-only', '--json']);
|
|
const out = JSON.parse(captured.join('\n'));
|
|
expect(out.update_available).toBe(false);
|
|
expect(out.changelog_diff).toBe('');
|
|
});
|
|
});
|