Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/genie-commands/shortcuts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, test } from 'bun:test';
import { shortcutsShowCommand } from './shortcuts.js';

describe('shortcutsShowCommand', () => {
test('does not throw', async () => {
// shortcutsShowCommand prints to console — just verify it doesn't crash
await expect(shortcutsShowCommand()).resolves.toBeUndefined();
});
});
42 changes: 42 additions & 0 deletions src/genie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,46 @@ describe('handleEntrypointArgs', () => {
expect(calls.ensureInboxWatcherDaemon).toBe(1);
expect(calls.error).toBe(1);
});

test('--reset flag triggers sessionCommand with reset: true', async () => {
let resetValue = false;
const { deps } = makeDeps({
sessionCommand: async (options) => {
resetValue = options.reset;
},
});

await expect(handleEntrypointArgs(['--reset'], deps)).rejects.toMatchObject({ code: 0 });
expect(resetValue).toBe(true);
});

test('--session at end of args without value falls through to parseProgram', async () => {
const { deps, calls } = makeDeps();

await handleEntrypointArgs(['--session'], deps);
expect(calls.parseProgram).toBe(1);
expect(calls.startNamedSession).toBe(0);
});

test('--session with only flag args (no subcommand) starts session', async () => {
const { deps, calls } = makeDeps();

await expect(handleEntrypointArgs(['--session', 'beta', '--verbose'], deps)).rejects.toMatchObject({ code: 0 });
expect(calls.startNamedSession).toBe(1);
});

test('reports non-Error session failures correctly', async () => {
let errorMessage = '';
const { deps } = makeDeps({
startNamedSession: async () => {
throw 'string error';
},
error: (msg) => {
errorMessage = msg;
},
});

await expect(handleEntrypointArgs(['--session', 'gamma'], deps)).rejects.toMatchObject({ code: 1 });
expect(errorMessage).toContain('string error');
});
});
58 changes: 58 additions & 0 deletions src/lib/claude-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,64 @@ describe('parseLogEntry', () => {
});
});

// ============================================================================
// readLogFile Tests
// ============================================================================

describe('readLogFile', () => {
const { readLogFile } = require('./claude-logs.js');

test('reads entries from a JSONL file', async () => {
const tempDir = join(TEST_DIR, 'readlogfile-test');
await mkdir(tempDir, { recursive: true });
const logPath = join(tempDir, 'test.jsonl');
const lines = sampleLogEntries.map((e) => JSON.stringify(e)).join('\n');
const { writeFile: wf } = await import('node:fs/promises');
await wf(logPath, lines);

const entries = await readLogFile(logPath);
expect(entries.length).toBe(3);
expect(entries[0].type).toBe('user');
expect(entries[1].type).toBe('assistant');
expect(entries[2].type).toBe('progress');
});

test('returns empty array for non-existent file', async () => {
const entries = await readLogFile('/tmp/nonexistent-log-12345.jsonl');
expect(entries).toEqual([]);
});
});

// ============================================================================
// getLogsForPane Tests
// ============================================================================

describe('getLogsForPane', () => {
const { getLogsForPane } = require('./claude-logs.js');

beforeAll(async () => {
await setupTestStructure();
});

afterAll(async () => {
await cleanupTestStructure();
});

test('returns null for non-existent workspace', async () => {
const result = await getLogsForPane('/nonexistent/path/12345', CLAUDE_DIR);
expect(result).toBeNull();
});

test('returns log info for known workspace', async () => {
const result = await getLogsForPane('/home/genie/workspace/guga', CLAUDE_DIR);
expect(result).not.toBeNull();
if (result) {
expect(result.logPath).toContain('test-session-123.jsonl');
expect(result.session.sessionId).toBe('test-session-123');
}
});
});

// ============================================================================
// Integration Tests (using real ~/.claude if available)
// ============================================================================
Expand Down
42 changes: 42 additions & 0 deletions src/lib/claude-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, test } from 'bun:test';
import { homedir } from 'node:os';
import { contractClaudePath, getClaudeSettingsPath, hookScriptExists, removeHookScript } from './claude-settings.js';

describe('getClaudeSettingsPath', () => {
test('returns path under ~/.claude', () => {
const path = getClaudeSettingsPath();
expect(path).toContain('.claude');
expect(path).toEndWith('settings.json');
});
});

describe('hookScriptExists', () => {
test('returns a boolean', () => {
const result = hookScriptExists();
expect(typeof result).toBe('boolean');
});
});

describe('removeHookScript', () => {
test('does not throw when hook script does not exist', () => {
// In a clean test env, the hook script likely doesn't exist
// This just verifies it doesn't crash
expect(() => removeHookScript()).not.toThrow();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid deleting real Claude hook in unit test

This test calls removeHookScript() against the default path under the current user's home directory, so running the suite on a machine where Genie/Claude hooks are installed will delete ~/.claude/hooks/genie-bash-hook.sh as a side effect. That makes the test destructive and can silently break a developer’s local setup; it should run against an isolated temp HOME (or a mocked filesystem path) instead.

Useful? React with 👍 / 👎.

});
});

describe('contractClaudePath', () => {
test('contracts home directory to ~', () => {
const home = homedir();
expect(contractClaudePath(`${home}/foo/bar`)).toBe('~/foo/bar');
});

test('contracts exact home to ~', () => {
const home = homedir();
expect(contractClaudePath(home)).toBe('~');
});

test('returns non-home paths unchanged', () => {
expect(contractClaudePath('/tmp/foo')).toBe('/tmp/foo');
});
});
23 changes: 23 additions & 0 deletions src/lib/codex-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from 'bun:test';
import { OTEL_RELAY_PORT, getCodexConfigPath, isCodexConfigured } from './codex-config.js';

describe('OTEL_RELAY_PORT', () => {
test('is a valid port number', () => {
expect(OTEL_RELAY_PORT).toBe(14318);
});
});

describe('getCodexConfigPath', () => {
test('returns a path ending with config.toml', () => {
const path = getCodexConfigPath();
expect(path).toEndWith('config.toml');
expect(path).toContain('.codex');
});
});

describe('isCodexConfigured', () => {
test('returns a boolean', () => {
const result = isCodexConfigured();
expect(typeof result).toBe('boolean');
});
});
89 changes: 89 additions & 0 deletions src/lib/genie-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expect, test } from 'bun:test';

// genie-config.ts uses module-level constants that read homedir() at import time.
// We can't override GENIE_DIR/GENIE_CONFIG_FILE. Instead we test the pure functions
// and verify the ones that use the filesystem work without crashing.

import {
contractPath,
genieConfigExists,
getGenieConfigPath,
getGenieDir,
getTerminalConfig,
isSetupComplete,
loadGenieConfig,
loadGenieConfigSync,
} from './genie-config.js';

describe('getGenieDir', () => {
test('returns a path containing .genie', () => {
const dir = getGenieDir();
expect(dir).toContain('.genie');
});
});

describe('getGenieConfigPath', () => {
test('returns a path ending with config.json', () => {
const path = getGenieConfigPath();
expect(path).toEndWith('config.json');
});
});

describe('genieConfigExists', () => {
test('returns a boolean', () => {
const result = genieConfigExists();
expect(typeof result).toBe('boolean');
});
});

describe('contractPath', () => {
test('contracts home directory to ~', () => {
const { homedir } = require('node:os');
const home = homedir();
expect(contractPath(`${home}/projects/test`)).toBe('~/projects/test');
});

test('contracts exact home to ~', () => {
const { homedir } = require('node:os');
expect(contractPath(homedir())).toBe('~');
});
Comment on lines +40 to +49

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and best practice, it's better to use a top-level import { homedir } from 'node:os'; instead of require() inside each test. This avoids repeated module loading and keeps imports organized at the top of the file.

  test('contracts home directory to ~', () => {
    const home = homedir();
    expect(contractPath(`${home}/projects/test`)).toBe('~/projects/test');
  });

  test('contracts exact home to ~', () => {
    expect(contractPath(homedir())).toBe('~');
  });


test('returns non-home paths unchanged', () => {
expect(contractPath('/tmp/foo')).toBe('/tmp/foo');
});

test('returns relative paths unchanged', () => {
expect(contractPath('relative/path')).toBe('relative/path');
});
});

describe('loadGenieConfig', () => {
test('returns a valid config object', async () => {
const config = await loadGenieConfig();
expect(config).toBeDefined();
expect(typeof config).toBe('object');
});
});

describe('loadGenieConfigSync', () => {
test('returns a valid config object', () => {
const config = loadGenieConfigSync();
expect(config).toBeDefined();
expect(typeof config).toBe('object');
});
});

describe('getTerminalConfig', () => {
test('returns terminal config with expected shape', () => {
const config = getTerminalConfig();
expect(config).toBeDefined();
expect(typeof config).toBe('object');
});
});

describe('isSetupComplete', () => {
test('returns a boolean', () => {
const result = isSetupComplete();
expect(typeof result).toBe('boolean');
});
});
14 changes: 14 additions & 0 deletions src/lib/genie-dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, test } from 'bun:test';
import { getRepoGenieDir } from './genie-dir.js';

describe('getRepoGenieDir', () => {
test('returns .genie path under the given repo path', () => {
const dir = getRepoGenieDir('/tmp/some-repo');
expect(dir).toEndWith('.genie');
});

test('returns path for current repo', () => {
const dir = getRepoGenieDir(process.cwd());
expect(dir).toContain('.genie');
});
});
Loading
Loading