import { describe, expect, test } from 'bun:test'; import { auditCliVersion, compareSemver } from './cli-version'; describe('compareSemver', () => { test.each([ ['1.0.0', '1.0.0', 0], ['1.0.0', '1.0.1', -1], ['1.0.1', '1.0.0', 1], ['1.0.0', '2.0.0', -1], ['2.0.0', '1.9.9', 1], ['v1.0.0', '1.0.0', 0], ['1.0.0-beta.1', '1.0.0', 0], // pre-release stripped ['1.0.0+build.42', '1.0.0', 0], // build metadata stripped ])('compareSemver(%s, %s) = %s', (a, b, expected) => { expect(compareSemver(a, b)).toBe(expected); }); }); describe('auditCliVersion', () => { test('no finding when running version equals latest', async () => { const result = await auditCliVersion({ installedVersion: '0.1.5', fetcher: async () => '0.1.5', }); expect(result).toEqual([]); }); test('no finding when running ahead of latest (dev build)', async () => { const result = await auditCliVersion({ installedVersion: '0.2.0-dev', fetcher: async () => '0.1.5', }); expect(result).toEqual([]); }); test('drift finding when published is newer', async () => { const result = await auditCliVersion({ installedVersion: '0.1.5', fetcher: async () => '0.1.7', }); expect(result).toHaveLength(1); expect(result[0]).toMatchObject({ category: 'cli_version', severity: 'drift', code: 'cli_version_drift', subject: 'system', }); expect(result[0].message).toContain('0.1.5'); expect(result[0].message).toContain('0.1.7'); }); test('a registry we could not reach is unmeasured, not up-to-date', async () => { // Was `expect(result).toEqual([])`. "I could not ask npm" and "you are on // the latest" are different statements and rendered identically. const result = await auditCliVersion({ installedVersion: '0.1.5', fetcher: async () => null, }); expect(result).toHaveLength(1); expect(result[0]?.severity).toBe('unmeasured'); expect(result[0]?.code).toBe('cli_version_unmeasured'); }); });