@hop-top/kit 0.5.0-alpha.10 → 0.5.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/README.md +4 -4
- package/dist/alias.js +3 -3
- package/dist/{chunk-T46YPHKN.js → chunk-4FMALWYB.js} +45 -45
- package/dist/{chunk-JZ22ZUFA.js → chunk-65VPWMPV.js} +4 -1
- package/dist/{chunk-YGYQUFGY.js → chunk-BXJ2J27B.js} +10 -10
- package/dist/chunk-CXBZTONY.js +65 -0
- package/dist/{chunk-K7VCKJ27.js → chunk-EE4M2UOF.js} +8 -4
- package/dist/{chunk-72XZWQX7.js → chunk-GFRAYYRD.js} +475 -162
- package/dist/{chunk-2JV3A7OP.js → chunk-IVQHLI56.js} +6 -10
- package/dist/{chunk-NLMS7HGP.js → chunk-IZ6Q4SE3.js} +3 -3
- package/dist/chunk-MFVUENZR.js +1549 -0
- package/dist/{chunk-BLS2OBIQ.js → chunk-ML7D5DWY.js} +2 -2
- package/dist/chunk-P3Y46PYF.js +138 -0
- package/dist/{chunk-GGWJRWWA.js → chunk-RYKLVOVK.js} +7 -7
- package/dist/{chunk-QPUCXQXT.js → chunk-T6OZMPPV.js} +2 -1
- package/dist/{chunk-MUIQVU6E.js → chunk-VGOA5Y7S.js} +11 -11
- package/dist/{chunk-ABQSE7AI.js → chunk-VWGP4NVJ.js} +38 -38
- package/dist/chunk-X4E5QGEB.js +171 -0
- package/dist/cli-RSRk1M36.d.ts +707 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +60 -5
- package/dist/completion.js +2 -2
- package/dist/id/index.d.ts +1 -1
- package/dist/id/index.js +4 -2
- package/dist/{index-DCe1boBz.d.ts → index-CGE5yLBj.d.ts} +1 -1
- package/dist/{index-VLDcI5NF.d.ts → index-D9zpUrpE.d.ts} +18 -6
- package/dist/index.d.ts +4 -4
- package/dist/index.js +23 -23
- package/dist/llm-anthropic.js +5 -5
- package/dist/llm-openai.js +5 -5
- package/dist/llm.js +3 -3
- package/dist/log.js +3 -3
- package/dist/mcp/index.js +3 -2
- package/dist/output.js +2 -4
- package/dist/routellm.js +6 -6
- package/dist/toolspec.js +4 -4
- package/dist/tui/index.d.ts +2 -2
- package/dist/tui/index.js +3 -3
- package/dist/{upgrade-p_zh3pvV.d.ts → upgrade-BJ2z6Oqf.d.ts} +58 -4
- package/dist/upgrade.d.ts +1 -1
- package/dist/upgrade.js +4 -2
- package/package.json +9 -9
- package/src/cli.root.conformance.test.ts +166 -0
- package/src/cli.ts +39 -0
- package/src/completion.conformance.test.ts +3 -2
- package/src/completion.test.ts +12 -0
- package/src/completion.ts +8 -3
- package/src/delegation.test.ts +299 -0
- package/src/delegation.ts +807 -0
- package/src/id/README.md +2 -2
- package/src/id/contract.test.ts +11 -8
- package/src/id/differential.test.ts +54 -0
- package/src/id/index.ts +123 -17
- package/src/mcp/bridge.test.ts +28 -0
- package/src/mcp/bridge.ts +5 -10
- package/src/plan.ts +89 -0
- package/src/policy.ts +472 -0
- package/src/sideeffect.ts +105 -0
- package/src/toolspec-manifest.ts +7 -2
- package/src/upgrade.test.ts +393 -1
- package/src/upgrade.ts +183 -25
- package/dist/chunk-GHPJJV5L.js +0 -613
- package/dist/chunk-IXVQ62NZ.js +0 -334
- package/dist/chunk-L23O2FFE.js +0 -51
- package/dist/chunk-WWUKGJB5.js +0 -90
- package/dist/cli-C5B5tnpA.d.ts +0 -400
- package/dist/{chunk-PSFYJGDG.js → chunk-K7E34Q3A.js} +2 -2
package/src/upgrade.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
|
2
2
|
import * as os from 'os';
|
|
3
3
|
import * as fs from 'fs';
|
|
4
4
|
import * as path from 'path';
|
|
5
|
-
import { createChecker } from './upgrade';
|
|
5
|
+
import { createChecker, UpgradeCheckError } from './upgrade';
|
|
6
6
|
|
|
7
7
|
// ─── helpers ─────────────────────────────────────────────────────────────────
|
|
8
8
|
|
|
@@ -296,3 +296,395 @@ describe('createChecker', () => {
|
|
|
296
296
|
expect(out.value()).toContain('v2.0.0');
|
|
297
297
|
});
|
|
298
298
|
});
|
|
299
|
+
|
|
300
|
+
// ─── component tags and prereleases ──────────────────────────────────────────
|
|
301
|
+
|
|
302
|
+
interface Rel {
|
|
303
|
+
tag_name: string;
|
|
304
|
+
prerelease?: boolean;
|
|
305
|
+
draft?: boolean;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const API = 'https://api.github.com/repos/acme/mono/releases';
|
|
309
|
+
|
|
310
|
+
/** Serve `pages[n-1]` for `?page=n` on the list endpoint; 404 elsewhere. */
|
|
311
|
+
function mockListing(pages: Rel[][]) {
|
|
312
|
+
return vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
|
|
313
|
+
const url = new URL(String(input));
|
|
314
|
+
if (url.origin + url.pathname !== API) return jsonResponse({}, 404);
|
|
315
|
+
const page = Number(url.searchParams.get('page') ?? '1');
|
|
316
|
+
return jsonResponse(pages[page - 1] ?? []);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
describe('createChecker: default mode', () => {
|
|
321
|
+
let tmpDir: string;
|
|
322
|
+
beforeEach(() => {
|
|
323
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-test-'));
|
|
324
|
+
vi.restoreAllMocks();
|
|
325
|
+
});
|
|
326
|
+
afterEach(() => {
|
|
327
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
328
|
+
vi.restoreAllMocks();
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('reads releases/latest only, with the latest-release link and bare cache', async () => {
|
|
332
|
+
const spy = vi
|
|
333
|
+
.spyOn(globalThis, 'fetch')
|
|
334
|
+
.mockResolvedValue(jsonResponse({ tag_name: 'v2.0.0' }));
|
|
335
|
+
const out = new StringStream();
|
|
336
|
+
await createChecker({
|
|
337
|
+
name: 'my-tool',
|
|
338
|
+
currentVersion: '1.0.0',
|
|
339
|
+
owner: 'acme',
|
|
340
|
+
repo: 'mono',
|
|
341
|
+
stateDir: tmpDir,
|
|
342
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
343
|
+
|
|
344
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
345
|
+
expect(String(spy.mock.calls[0][0])).toBe(`${API}/latest`);
|
|
346
|
+
expect(out.value()).toBe(
|
|
347
|
+
'\nA new release of my-tool is available: v1.0.0 → v2.0.0\n' +
|
|
348
|
+
'https://github.com/acme/mono/releases/latest\n',
|
|
349
|
+
);
|
|
350
|
+
const cache = JSON.parse(
|
|
351
|
+
fs.readFileSync(path.join(tmpDir, '.upgrade-my-tool-cache.json'), 'utf8'),
|
|
352
|
+
);
|
|
353
|
+
expect(Object.keys(cache)).toEqual(['version', 'checkedAt']);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('stays silent for a component tag when no tagPrefix is set', async () => {
|
|
357
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
358
|
+
jsonResponse({ tag_name: 'xray/v0.2.0' }),
|
|
359
|
+
);
|
|
360
|
+
const out = new StringStream();
|
|
361
|
+
await createChecker({
|
|
362
|
+
name: 'xray',
|
|
363
|
+
currentVersion: '0.1.0',
|
|
364
|
+
owner: 'acme',
|
|
365
|
+
repo: 'mono',
|
|
366
|
+
stateDir: tmpDir,
|
|
367
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
368
|
+
expect(out.value()).toBe('');
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
describe('createChecker: tagPrefix and includePrereleases', () => {
|
|
373
|
+
let tmpDir: string;
|
|
374
|
+
beforeEach(() => {
|
|
375
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-test-'));
|
|
376
|
+
vi.restoreAllMocks();
|
|
377
|
+
});
|
|
378
|
+
afterEach(() => {
|
|
379
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
380
|
+
vi.restoreAllMocks();
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
async function run(
|
|
384
|
+
currentVersion: string,
|
|
385
|
+
extra: { tagPrefix?: string; includePrereleases?: boolean },
|
|
386
|
+
): Promise<string> {
|
|
387
|
+
const out = new StringStream();
|
|
388
|
+
await createChecker({
|
|
389
|
+
name: 'xray',
|
|
390
|
+
currentVersion,
|
|
391
|
+
owner: 'acme',
|
|
392
|
+
repo: 'mono',
|
|
393
|
+
stateDir: tmpDir,
|
|
394
|
+
...extra,
|
|
395
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
396
|
+
return out.value();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
it('notifies for a prefixed tag and links the exact release', async () => {
|
|
400
|
+
mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
401
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe(
|
|
402
|
+
'\nA new release of xray is available: v0.1.0 → v0.2.0\n' +
|
|
403
|
+
'https://github.com/acme/mono/releases/tag/xray/v0.2.0\n',
|
|
404
|
+
);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('ignores other components even when their version is higher', async () => {
|
|
408
|
+
mockListing([[{ tag_name: 'kit/v9.0.0' }, { tag_name: 'xray/v0.2.0' }]]);
|
|
409
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('v0.1.0 → v0.2.0');
|
|
410
|
+
|
|
411
|
+
fs.rmSync(path.join(tmpDir, '.upgrade-xray-cache.json'));
|
|
412
|
+
mockListing([[{ tag_name: 'kit/v9.0.0' }, { tag_name: 'xray-cli/v9.0.0' }]]);
|
|
413
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('picks the highest semver, not the first listed', async () => {
|
|
417
|
+
mockListing([
|
|
418
|
+
[{ tag_name: 'xray/v0.2.1' }, { tag_name: 'xray/v0.10.0' }, { tag_name: 'xray/v0.9.0' }],
|
|
419
|
+
]);
|
|
420
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.10.0');
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it('skips drafts', async () => {
|
|
424
|
+
mockListing([[{ tag_name: 'xray/v0.3.0', draft: true }, { tag_name: 'xray/v0.2.0' }]]);
|
|
425
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.0');
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('does not nudge a stable user onto a prerelease without includePrereleases', async () => {
|
|
429
|
+
mockListing([
|
|
430
|
+
[
|
|
431
|
+
{ tag_name: 'xray/v0.3.0-rc.1', prerelease: true },
|
|
432
|
+
{ tag_name: 'xray/v0.3.0-rc.0' }, // not flagged by GitHub; semver says prerelease
|
|
433
|
+
],
|
|
434
|
+
]);
|
|
435
|
+
expect(await run('0.2.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('offers a newer prerelease to a stable user when includePrereleases is on', async () => {
|
|
439
|
+
mockListing([[{ tag_name: 'xray/v0.3.0-rc.1', prerelease: true }]]);
|
|
440
|
+
expect(await run('0.2.0', { tagPrefix: 'xray/v', includePrereleases: true })).toContain(
|
|
441
|
+
'v0.2.0 → v0.3.0-rc.1',
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it('shows a prerelease user the newer prerelease', async () => {
|
|
446
|
+
mockListing([
|
|
447
|
+
[
|
|
448
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
449
|
+
{ tag_name: 'xray/v0.1.0-rc.0', prerelease: true },
|
|
450
|
+
],
|
|
451
|
+
]);
|
|
452
|
+
const o = { tagPrefix: 'xray/v', includePrereleases: true };
|
|
453
|
+
expect(await run('0.1.0-rc.0', o)).toContain('v0.1.0-rc.0 → v0.1.0-rc.1');
|
|
454
|
+
fs.rmSync(path.join(tmpDir, '.upgrade-xray-cache.json'));
|
|
455
|
+
expect(await run('0.1.0-rc.1', o)).toBe('');
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('shows a prerelease user the final stable over its own prereleases', async () => {
|
|
459
|
+
mockListing([
|
|
460
|
+
[
|
|
461
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
462
|
+
{ tag_name: 'xray/v0.1.0' },
|
|
463
|
+
],
|
|
464
|
+
]);
|
|
465
|
+
expect(
|
|
466
|
+
await run('0.1.0-rc.0', { tagPrefix: 'xray/v', includePrereleases: true }),
|
|
467
|
+
).toContain('v0.1.0-rc.0 → v0.1.0\n');
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('shows a prerelease user the final stable without includePrereleases', async () => {
|
|
471
|
+
mockListing([
|
|
472
|
+
[
|
|
473
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
474
|
+
{ tag_name: 'xray/v0.1.0' },
|
|
475
|
+
],
|
|
476
|
+
]);
|
|
477
|
+
expect(await run('0.1.0-rc.0', { tagPrefix: 'xray/v' })).toContain('v0.1.0-rc.0 → v0.1.0\n');
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('includePrereleases alone lists releases and links the tag as published', async () => {
|
|
481
|
+
mockListing([[{ tag_name: 'v1.0.0-rc.1', prerelease: true }, { tag_name: 'v0.9.0' }]]);
|
|
482
|
+
expect(await run('0.9.0', { includePrereleases: true })).toBe(
|
|
483
|
+
'\nA new release of xray is available: v0.9.0 → v1.0.0-rc.1\n' +
|
|
484
|
+
'https://github.com/acme/mono/releases/tag/v1.0.0-rc.1\n',
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it('never calls releases/latest in list mode', async () => {
|
|
489
|
+
const spy = mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
490
|
+
await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
491
|
+
for (const call of spy.mock.calls) {
|
|
492
|
+
expect(String(call[0])).not.toContain('/releases/latest');
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('pages until a short page', async () => {
|
|
497
|
+
const filler = (n: number): Rel[] =>
|
|
498
|
+
Array.from({ length: 100 }, (_, i) => ({ tag_name: `kit/v1.${n}.${i}` }));
|
|
499
|
+
const spy = mockListing([filler(0), [{ tag_name: 'xray/v0.2.0' }]]);
|
|
500
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.0');
|
|
501
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it('fetches at most 3 pages', async () => {
|
|
505
|
+
const filler = (n: number): Rel[] =>
|
|
506
|
+
Array.from({ length: 100 }, (_, i) => ({ tag_name: `kit/v1.${n}.${i}` }));
|
|
507
|
+
const spy = mockListing([filler(0), filler(1), filler(2), [{ tag_name: 'xray/v0.2.0' }]]);
|
|
508
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
509
|
+
expect(spy).toHaveBeenCalledTimes(3);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it('keeps earlier pages when a later page fails', async () => {
|
|
513
|
+
const full = Array.from({ length: 100 }, (_, i) => ({ tag_name: `xray/v0.2.${i}` }));
|
|
514
|
+
let n = 0;
|
|
515
|
+
vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
|
|
516
|
+
++n === 1 ? jsonResponse(full) : jsonResponse({}, 500),
|
|
517
|
+
);
|
|
518
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.99');
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it('is silent when listing fails, returns junk, or throws', async () => {
|
|
522
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({}, 403));
|
|
523
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
524
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({ message: 'nope' }));
|
|
525
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
526
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
527
|
+
jsonResponse([null, { tag_name: 7 }, { tag_name: 'xray/vbogus' }]),
|
|
528
|
+
);
|
|
529
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
530
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('offline'));
|
|
531
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('keeps the tag link when the result comes from a fresh cache', async () => {
|
|
535
|
+
const spy = mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
536
|
+
await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
537
|
+
const second = await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
538
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
539
|
+
expect(second).toContain('releases/tag/xray/v0.2.0');
|
|
540
|
+
});
|
|
541
|
+
it.each([
|
|
542
|
+
['path traversal', 'xray/v/../../../../evil/repo/releases/tag/x'],
|
|
543
|
+
['another version', 'xray/v9.9.9'],
|
|
544
|
+
['another component', 'kit/v2.0.0'],
|
|
545
|
+
['not a string', 42],
|
|
546
|
+
['missing', undefined],
|
|
547
|
+
])('treats a cached tag that does not name the version as a miss: %s', async (_label, tag) => {
|
|
548
|
+
fs.writeFileSync(
|
|
549
|
+
path.join(tmpDir, '.upgrade-xray-cache.json'),
|
|
550
|
+
JSON.stringify({ version: '2.0.0', checkedAt: Date.now(), tag }),
|
|
551
|
+
);
|
|
552
|
+
const spy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
553
|
+
jsonResponse([{ tag_name: 'xray/v1.5.0', draft: false, prerelease: false }]),
|
|
554
|
+
);
|
|
555
|
+
const out = new StringStream();
|
|
556
|
+
await createChecker({
|
|
557
|
+
name: 'xray',
|
|
558
|
+
currentVersion: '1.0.0',
|
|
559
|
+
owner: 'acme',
|
|
560
|
+
repo: 'mono',
|
|
561
|
+
stateDir: tmpDir,
|
|
562
|
+
tagPrefix: 'xray/v',
|
|
563
|
+
cacheTTL: 60_000,
|
|
564
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
565
|
+
expect(spy).toHaveBeenCalled();
|
|
566
|
+
expect(out.value()).toContain('https://github.com/acme/mono/releases/tag/xray/v1.5.0');
|
|
567
|
+
expect(out.value()).not.toContain('evil');
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
it('uses a cached tag that names the version, without fetching', async () => {
|
|
571
|
+
fs.writeFileSync(
|
|
572
|
+
path.join(tmpDir, '.upgrade-xray-cache.json'),
|
|
573
|
+
JSON.stringify({ version: '2.0.0', checkedAt: Date.now(), tag: 'xray/v2.0.0' }),
|
|
574
|
+
);
|
|
575
|
+
const spy = vi.spyOn(globalThis, 'fetch');
|
|
576
|
+
const out = new StringStream();
|
|
577
|
+
await createChecker({
|
|
578
|
+
name: 'xray',
|
|
579
|
+
currentVersion: '1.0.0',
|
|
580
|
+
owner: 'acme',
|
|
581
|
+
repo: 'mono',
|
|
582
|
+
stateDir: tmpDir,
|
|
583
|
+
tagPrefix: 'xray/v',
|
|
584
|
+
cacheTTL: 60_000,
|
|
585
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
586
|
+
expect(spy).not.toHaveBeenCalled();
|
|
587
|
+
expect(out.value()).toContain('https://github.com/acme/mono/releases/tag/xray/v2.0.0');
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
describe('createChecker: check()', () => {
|
|
592
|
+
let tmpDir: string;
|
|
593
|
+
|
|
594
|
+
beforeEach(() => {
|
|
595
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-check-'));
|
|
596
|
+
vi.restoreAllMocks();
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
afterEach(() => {
|
|
600
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
601
|
+
vi.restoreAllMocks();
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
const mk = (extra: Record<string, unknown> = {}) =>
|
|
605
|
+
createChecker({
|
|
606
|
+
name: 'my-tool',
|
|
607
|
+
currentVersion: '1.0.0',
|
|
608
|
+
owner: 'acme',
|
|
609
|
+
repo: 'my-tool',
|
|
610
|
+
stateDir: tmpDir,
|
|
611
|
+
...extra,
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it('resolves update info when newer', async () => {
|
|
615
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({ tag_name: 'v2.0.0' }));
|
|
616
|
+
expect(await mk().check()).toEqual({
|
|
617
|
+
current: '1.0.0',
|
|
618
|
+
latest: '2.0.0',
|
|
619
|
+
updateAvailable: true,
|
|
620
|
+
url: 'https://github.com/acme/my-tool/releases/latest',
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
it('resolves updateAvailable false when up to date', async () => {
|
|
625
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({ tag_name: 'v1.0.0' }));
|
|
626
|
+
const r = await mk().check();
|
|
627
|
+
expect(r.updateAvailable).toBe(false);
|
|
628
|
+
expect(r.latest).toBe('1.0.0');
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it('rejects with UpgradeCheckError on network failure', async () => {
|
|
632
|
+
const boom = new Error('offline');
|
|
633
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValue(boom);
|
|
634
|
+
const err = await mk().check().catch((e) => e);
|
|
635
|
+
expect(err).toBeInstanceOf(UpgradeCheckError);
|
|
636
|
+
expect(err.cause).toBe(boom);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it('rejects on HTTP error status', async () => {
|
|
640
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({}, 500));
|
|
641
|
+
await expect(mk().check()).rejects.toBeInstanceOf(UpgradeCheckError);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it('rejects on malformed body', async () => {
|
|
645
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
646
|
+
new Response('not json', { status: 200 }),
|
|
647
|
+
);
|
|
648
|
+
await expect(mk().check()).rejects.toBeInstanceOf(UpgradeCheckError);
|
|
649
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({ tag_name: 5 }));
|
|
650
|
+
await expect(mk().check()).rejects.toBeInstanceOf(UpgradeCheckError);
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
it('rejects when no release matches tagPrefix', async () => {
|
|
654
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
655
|
+
jsonResponse([{ tag_name: 'other/v9.0.0' }]),
|
|
656
|
+
);
|
|
657
|
+
await expect(mk({ tagPrefix: 'xray/v' }).check()).rejects.toBeInstanceOf(
|
|
658
|
+
UpgradeCheckError,
|
|
659
|
+
);
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
it('honours tagPrefix and links the exact release', async () => {
|
|
663
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
664
|
+
jsonResponse([{ tag_name: 'xray/v1.5.0' }, { tag_name: 'other/v9.0.0' }]),
|
|
665
|
+
);
|
|
666
|
+
const r = await mk({ tagPrefix: 'xray/v' }).check();
|
|
667
|
+
expect(r.latest).toBe('1.5.0');
|
|
668
|
+
expect(r.url).toBe('https://github.com/acme/my-tool/releases/tag/xray/v1.5.0');
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it('serves from cache within TTL; cacheTTL 0 bypasses it', async () => {
|
|
672
|
+
const spy = vi
|
|
673
|
+
.spyOn(globalThis, 'fetch')
|
|
674
|
+
.mockImplementation(async () => jsonResponse({ tag_name: 'v2.0.0' }));
|
|
675
|
+
await mk().check();
|
|
676
|
+
await mk().check();
|
|
677
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
678
|
+
await mk({ cacheTTL: 0 }).check();
|
|
679
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('notifyIfAvailable still swallows what check() rejects', async () => {
|
|
683
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('offline'));
|
|
684
|
+
const out = new StringStream();
|
|
685
|
+
await expect(
|
|
686
|
+
mk().notifyIfAvailable(out as unknown as NodeJS.WritableStream),
|
|
687
|
+
).resolves.toBeUndefined();
|
|
688
|
+
expect(out.value()).toBe('');
|
|
689
|
+
});
|
|
690
|
+
});
|