mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-16 10:15:10 +00:00
fix(cursor): address PR #527 review feedback
- Wire cursor command into main router (ccs.ts) following copilot pattern - Centralize default port (4242) and model (gpt-4.1) as constants - Remove duplicate types already in cursor-protobuf-schema from dev - Handle daemon exit code 0 before health check succeeds - Export PID helpers and model utils for testability - Add unit tests for cursor-daemon (PID file ops, isDaemonRunning) - Add unit tests for cursor-models (detectProvider, formatModelName, defaults)
This commit is contained in:
1 parent
83894f43c6
commit
9f0ea25448
7 files changed
+250
-12
No files matched your search
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Unit tests for Cursor daemon module
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import {
|
||||
getPidFromFile,
|
||||
writePidToFile,
|
||||
removePidFile,
|
||||
isDaemonRunning,
|
||||
} from '../../../src/cursor/cursor-daemon';
|
||||
|
||||
// Test isolation
|
||||
let originalCcsHome: string | undefined;
|
||||
let tempDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
originalCcsHome = process.env.CCS_HOME;
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-daemon-test-'));
|
||||
process.env.CCS_HOME = tempDir;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalCcsHome !== undefined) {
|
||||
process.env.CCS_HOME = originalCcsHome;
|
||||
} else {
|
||||
delete process.env.CCS_HOME;
|
||||
}
|
||||
|
||||
// Cleanup temp directory
|
||||
try {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
});
|
||||
|
||||
// CCS_HOME is set to tempDir; getCcsDir() appends '.ccs' to it
|
||||
const ccsDir = () => path.join(tempDir, '.ccs');
|
||||
|
||||
describe('getPidFromFile', () => {
|
||||
it('returns null when no PID file exists', () => {
|
||||
expect(getPidFromFile()).toBeNull();
|
||||
});
|
||||
|
||||
it('returns PID when valid PID file exists', () => {
|
||||
const cursorDir = path.join(ccsDir(), 'cursor');
|
||||
fs.mkdirSync(cursorDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(cursorDir, 'daemon.pid'), '12345');
|
||||
|
||||
expect(getPidFromFile()).toBe(12345);
|
||||
});
|
||||
|
||||
it('returns null when PID file contains invalid content', () => {
|
||||
const cursorDir = path.join(ccsDir(), 'cursor');
|
||||
fs.mkdirSync(cursorDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(cursorDir, 'daemon.pid'), 'not-a-number');
|
||||
|
||||
expect(getPidFromFile()).toBeNull();
|
||||
});
|
||||
|
||||
it('trims whitespace from PID file content', () => {
|
||||
const cursorDir = path.join(ccsDir(), 'cursor');
|
||||
fs.mkdirSync(cursorDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(cursorDir, 'daemon.pid'), ' 42 \n');
|
||||
|
||||
expect(getPidFromFile()).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
describe('writePidToFile', () => {
|
||||
it('creates PID file with correct content', () => {
|
||||
writePidToFile(12345);
|
||||
|
||||
const pidFile = path.join(ccsDir(), 'cursor', 'daemon.pid');
|
||||
expect(fs.existsSync(pidFile)).toBe(true);
|
||||
expect(fs.readFileSync(pidFile, 'utf8')).toBe('12345');
|
||||
});
|
||||
|
||||
it('creates cursor directory if it does not exist', () => {
|
||||
const cursorDir = path.join(ccsDir(), 'cursor');
|
||||
expect(fs.existsSync(cursorDir)).toBe(false);
|
||||
|
||||
writePidToFile(999);
|
||||
|
||||
expect(fs.existsSync(cursorDir)).toBe(true);
|
||||
});
|
||||
|
||||
it('overwrites existing PID file', () => {
|
||||
writePidToFile(111);
|
||||
writePidToFile(222);
|
||||
|
||||
const pidFile = path.join(ccsDir(), 'cursor', 'daemon.pid');
|
||||
expect(fs.readFileSync(pidFile, 'utf8')).toBe('222');
|
||||
});
|
||||
});
|
||||
|
||||
describe('removePidFile', () => {
|
||||
it('removes existing PID file', () => {
|
||||
writePidToFile(12345);
|
||||
const pidFile = path.join(ccsDir(), 'cursor', 'daemon.pid');
|
||||
expect(fs.existsSync(pidFile)).toBe(true);
|
||||
|
||||
removePidFile();
|
||||
|
||||
expect(fs.existsSync(pidFile)).toBe(false);
|
||||
});
|
||||
|
||||
it('does not throw when PID file does not exist', () => {
|
||||
expect(() => removePidFile()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isDaemonRunning', () => {
|
||||
it('returns false when no daemon is running on port', async () => {
|
||||
// Use a port that should not have anything running
|
||||
const result = await isDaemonRunning(19999);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Unit tests for Cursor models module
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'bun:test';
|
||||
import {
|
||||
DEFAULT_CURSOR_MODELS,
|
||||
DEFAULT_CURSOR_PORT,
|
||||
DEFAULT_CURSOR_MODEL,
|
||||
getDefaultModel,
|
||||
detectProvider,
|
||||
formatModelName,
|
||||
} from '../../../src/cursor/cursor-models';
|
||||
|
||||
describe('DEFAULT_CURSOR_MODELS', () => {
|
||||
it('contains models from multiple providers', () => {
|
||||
const providers = new Set(DEFAULT_CURSOR_MODELS.map((m) => m.provider));
|
||||
expect(providers.has('anthropic')).toBe(true);
|
||||
expect(providers.has('openai')).toBe(true);
|
||||
expect(providers.has('google')).toBe(true);
|
||||
});
|
||||
|
||||
it('has exactly one default model', () => {
|
||||
const defaults = DEFAULT_CURSOR_MODELS.filter((m) => m.isDefault);
|
||||
expect(defaults).toHaveLength(1);
|
||||
expect(defaults[0].id).toBe(DEFAULT_CURSOR_MODEL);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DEFAULT_CURSOR_PORT', () => {
|
||||
it('is 4242', () => {
|
||||
expect(DEFAULT_CURSOR_PORT).toBe(4242);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DEFAULT_CURSOR_MODEL', () => {
|
||||
it('is gpt-4.1', () => {
|
||||
expect(DEFAULT_CURSOR_MODEL).toBe('gpt-4.1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDefaultModel', () => {
|
||||
it('returns the default model constant', () => {
|
||||
expect(getDefaultModel()).toBe(DEFAULT_CURSOR_MODEL);
|
||||
});
|
||||
});
|
||||
|
||||
describe('detectProvider', () => {
|
||||
it('detects anthropic models', () => {
|
||||
expect(detectProvider('claude-sonnet-4')).toBe('anthropic');
|
||||
expect(detectProvider('claude-opus-4')).toBe('anthropic');
|
||||
});
|
||||
|
||||
it('detects openai models', () => {
|
||||
expect(detectProvider('gpt-4.1')).toBe('openai');
|
||||
expect(detectProvider('gpt-5-mini')).toBe('openai');
|
||||
expect(detectProvider('o3-mini')).toBe('openai');
|
||||
});
|
||||
|
||||
it('detects google models', () => {
|
||||
expect(detectProvider('gemini-2.5-pro')).toBe('google');
|
||||
});
|
||||
|
||||
it('detects cursor models', () => {
|
||||
expect(detectProvider('cursor-small')).toBe('cursor');
|
||||
});
|
||||
|
||||
it('defaults to openai for unknown models', () => {
|
||||
expect(detectProvider('unknown-model')).toBe('openai');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatModelName', () => {
|
||||
it('returns catalog name for known models', () => {
|
||||
expect(formatModelName('claude-sonnet-4')).toBe('Claude Sonnet 4');
|
||||
expect(formatModelName('gpt-4.1')).toBe('GPT-4.1');
|
||||
});
|
||||
|
||||
it('converts kebab-case to title case for unknown models', () => {
|
||||
expect(formatModelName('my-custom-model')).toBe('My Custom Model');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user