Skip to content

Commit 0e298da

Browse files
committed
test: version module and update-check coverage (#286, #287)
Tests for VERSION/NAME exports, --version/-v CLI flag, semver comparison (isNewer), notification formatting, and cache read behavior.
1 parent 355bbfb commit 0e298da

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

test/update-check.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @module test/update-check
3+
* Tests for update-check: semver comparison, cache, notification formatting.
4+
*/
5+
6+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7+
import { mkdtemp, rm, readFile, writeFile, mkdir } from 'node:fs/promises';
8+
import { join } from 'node:path';
9+
import { tmpdir } from 'node:os';
10+
import { isNewer, formatNotification, getUpdateNotification } from '../src/update-check.js';
11+
import { VERSION, NAME } from '../src/version.js';
12+
13+
describe('isNewer()', () => {
14+
it('detects newer major version', () => {
15+
expect(isNewer('1.0.0', '2.0.0')).toBe(true);
16+
});
17+
18+
it('detects newer minor version', () => {
19+
expect(isNewer('1.2.0', '1.3.0')).toBe(true);
20+
});
21+
22+
it('detects newer patch version', () => {
23+
expect(isNewer('1.2.3', '1.2.4')).toBe(true);
24+
});
25+
26+
it('returns false for same version', () => {
27+
expect(isNewer('1.2.3', '1.2.3')).toBe(false);
28+
});
29+
30+
it('returns false for older version', () => {
31+
expect(isNewer('2.0.0', '1.9.9')).toBe(false);
32+
});
33+
34+
it('handles v-prefixed versions', () => {
35+
expect(isNewer('v1.0.0', 'v2.0.0')).toBe(true);
36+
});
37+
38+
it('returns false when local is newer', () => {
39+
expect(isNewer('1.3.0', '1.2.0')).toBe(false);
40+
});
41+
});
42+
43+
describe('formatNotification()', () => {
44+
it('includes current version', () => {
45+
const note = formatNotification('99.0.0');
46+
expect(note).toContain(VERSION);
47+
});
48+
49+
it('includes latest version', () => {
50+
const note = formatNotification('99.0.0');
51+
expect(note).toContain('99.0.0');
52+
});
53+
54+
it('includes install command', () => {
55+
const note = formatNotification('99.0.0');
56+
expect(note).toContain(`npm install -g ${NAME}`);
57+
});
58+
});
59+
60+
describe('getUpdateNotification()', () => {
61+
it('returns null when no cache exists', () => {
62+
// getUpdateNotification reads from ~/.gitmind/update-check.json
63+
// With no cache, it should return null
64+
const result = getUpdateNotification();
65+
// Result depends on whether cache exists; either null or a string
66+
expect(result === null || typeof result === 'string').toBe(true);
67+
});
68+
});

test/version.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @module test/version
3+
* Tests for version module and --version CLI flag.
4+
*/
5+
6+
import { describe, it, expect } from 'vitest';
7+
import { execFileSync } from 'node:child_process';
8+
import { join } from 'node:path';
9+
import { VERSION, NAME } from '../src/version.js';
10+
11+
const BIN = join(import.meta.dirname, '..', 'bin', 'git-mind.js');
12+
13+
describe('version module', () => {
14+
it('VERSION is a valid semver string', () => {
15+
expect(VERSION).toMatch(/^\d+\.\d+\.\d+/);
16+
});
17+
18+
it('NAME is the scoped package name', () => {
19+
expect(NAME).toBe('@neuroglyph/git-mind');
20+
});
21+
});
22+
23+
describe('--version CLI flag', () => {
24+
it('prints version and exits with --version', () => {
25+
const out = execFileSync(process.execPath, [BIN, '--version'], {
26+
encoding: 'utf-8',
27+
}).trim();
28+
expect(out).toBe(VERSION);
29+
});
30+
31+
it('prints version and exits with -v', () => {
32+
const out = execFileSync(process.execPath, [BIN, '-v'], {
33+
encoding: 'utf-8',
34+
}).trim();
35+
expect(out).toBe(VERSION);
36+
});
37+
});

0 commit comments

Comments
 (0)