Test fix branch (#157)

* fix(lsp): Fixes pyright LSP initialization timeout

* test(lsp): Add unit tests for bin directory initialization

- Add reinitializeBinDirectory() public method for testing
- Add 7 comprehensive test cases
- Cover success scenarios, error handling, and edge cases
- Achieve 100% code and branch coverage

* Refactor OpenCode server for testability and fix bin directory tests

* Standardize test ENV mocks and modernize imports

---------

Co-authored-by: Niu Shuai <guisu2010@gmail.com>
This commit is contained in:
Chris ScottandNiu Shuai authored and GitHub committed 2026-03-08 21:40:37 -04:00
1 parent 62343366f6
commit 5746033fde
5 files changed
+131 -48

No files matched your search

+4 -4
View File
@@ -1,8 +1,8 @@
/* eslint-disable no-empty */
import { promises as fs } from 'fs'
import { execFile } from 'child_process'
import { randomBytes } from 'crypto'
import { join } from 'path'
import fs from 'node:fs/promises'
import { execFile } from 'node:child_process'
import { randomBytes } from 'node:crypto'
import { join } from 'node:path'
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
const SSH_KEYS_DIR = join(getWorkspacePath(), '.ssh-keys')
@@ -5,19 +5,27 @@ import * as fs from 'fs/promises'
import * as path from 'path'
vi.mock('@opencode-manager/shared/config/env', () => ({
ENV: {
AUTH: {
SECRET: 'test-secret-for-encryption-key-32c'
},
OPENCODE: {
PORT: 5551
},
SERVER: {
PORT: 5003
}
},
getWorkspacePath: vi.fn(() => '/tmp/test-workspace'),
getReposPath: vi.fn(() => '/tmp/test-repos')
getReposPath: vi.fn(() => '/tmp/test-repos'),
getOpenCodeConfigFilePath: vi.fn(() => '/tmp/test-workspace/.config/opencode.json'),
getAgentsMdPath: vi.fn(() => '/tmp/test-workspace/AGENTS.md'),
getDatabasePath: vi.fn(() => ':memory:'),
getConfigPath: vi.fn(() => '/tmp/test-workspace/config'),
ENV: {
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
WORKSPACE: { BASE_PATH: '/tmp/test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
DATABASE: { PATH: ':memory:' },
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
},
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
}))
vi.mock('../../src/utils/logger', () => ({
+45 -19
View File
@@ -1,18 +1,25 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { execSync } from 'child_process'
vi.mock('child_process', () => ({
execSync: vi.fn(),
vi.mock('fs', () => ({
existsSync: vi.fn(() => false),
promises: {
mkdir: vi.fn(),
access: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
stat: vi.fn(),
chmod: vi.fn(),
unlink: vi.fn(),
rm: vi.fn(),
readdir: vi.fn(),
},
}))
vi.mock('../../src/services/opencode-single-server', () => ({
opencodeServerManager: {
getVersion: vi.fn(),
fetchVersion: vi.fn(),
reloadConfig: vi.fn(),
restart: vi.fn(),
clearStartupError: vi.fn(),
},
vi.mock('child_process', () => ({
execSync: vi.fn(),
spawnSync: vi.fn(),
spawn: vi.fn(),
}))
vi.mock('../../src/utils/logger', () => ({
@@ -45,17 +52,36 @@ vi.mock('../../src/services/proxy', () => ({
proxyToOpenCodeWithDirectory: vi.fn(),
}))
vi.mock('../../src/services/opencode-single-server', () => ({
opencodeServerManager: {
getVersion: vi.fn(),
fetchVersion: vi.fn(),
reloadConfig: vi.fn(),
restart: vi.fn(),
clearStartupError: vi.fn(),
getLastStartupError: vi.fn(),
setDatabase: vi.fn(),
reinitializeBinDirectory: vi.fn(),
},
}))
vi.mock('@opencode-manager/shared/config/env', () => ({
getOpenCodeConfigFilePath: vi.fn(() => '/test/config.json'),
getAgentsMdPath: vi.fn(() => '/test/AGENTS.md'),
getWorkspacePath: vi.fn(() => '/test/workspace'),
getReposPath: vi.fn(() => '/test/repos'),
getDatabasePath: vi.fn(() => '/test/db.sqlite'),
getWorkspacePath: vi.fn(() => '/tmp/test-workspace'),
getReposPath: vi.fn(() => '/tmp/test-repos'),
getOpenCodeConfigFilePath: vi.fn(() => '/tmp/test-workspace/.config/opencode.json'),
getAgentsMdPath: vi.fn(() => '/tmp/test-workspace/AGENTS.md'),
getDatabasePath: vi.fn(() => ':memory:'),
getConfigPath: vi.fn(() => '/tmp/test-workspace/config'),
ENV: {
SERVER: { PORT: 5003, HOST: '0.0.0.0' },
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173' },
WORKSPACE: { BASE_PATH: '/test/workspace' },
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
WORKSPACE: { BASE_PATH: '/tmp/test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
DATABASE: { PATH: ':memory:' },
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
},
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
@@ -63,8 +89,8 @@ vi.mock('@opencode-manager/shared/config/env', () => ({
},
}))
import { opencodeServerManager } from '../../src/services/opencode-single-server'
import { createSettingsRoutes } from '../../src/routes/settings'
import { opencodeServerManager } from '../../src/services/opencode-single-server'
const mockExecSync = execSync as ReturnType<typeof vi.fn>
const mockGetVersion = opencodeServerManager.getVersion as ReturnType<typeof vi.fn>
+28 -12
View File
@@ -8,19 +8,27 @@ vi.mock('bun:sqlite', () => ({
}))
vi.mock('@opencode-manager/shared/config/env', () => ({
ENV: {
AUTH: {
SECRET: 'test-secret-for-encryption'
},
OPENCODE: {
PORT: 5551
},
SERVER: {
PORT: 5003
}
},
getWorkspacePath: vi.fn(() => '/tmp/test-workspace'),
getReposPath: vi.fn(() => '/tmp/test-repos'),
getWorkspacePath: vi.fn(() => '/tmp/test-workspace')
getOpenCodeConfigFilePath: vi.fn(() => '/tmp/test-workspace/.config/opencode.json'),
getAgentsMdPath: vi.fn(() => '/tmp/test-workspace/AGENTS.md'),
getDatabasePath: vi.fn(() => ':memory:'),
getConfigPath: vi.fn(() => '/tmp/test-workspace/config'),
ENV: {
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
WORKSPACE: { BASE_PATH: '/tmp/test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
DATABASE: { PATH: ':memory:' },
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
},
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
}))
vi.mock('@opencode-manager/shared/config', () => ({
@@ -58,6 +66,14 @@ vi.mock('../../src/utils/ssh-key-manager', () => ({
),
}))
vi.mock('../../src/utils/crypto', () => ({
encryptSecret: vi.fn((s: string) => `encrypted:${s}`),
decryptSecret: vi.fn((s: string) => {
if (!s.startsWith('encrypted:')) throw new Error('Decryption failed')
return s.slice(10)
}),
}))
import { GitAuthService } from '../../src/services/git-auth'
import { encryptSecret } from '../../src/utils/crypto'
+34 -1
View File
@@ -1,7 +1,40 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
import * as fs from 'fs/promises'
vi.mock('@opencode-manager/shared/config/env', () => ({
getWorkspacePath: vi.fn(() => '/tmp/ssh-key-test-workspace'),
getReposPath: vi.fn(() => '/tmp/ssh-key-test-workspace/repos'),
getOpenCodeConfigFilePath: vi.fn(() => '/tmp/ssh-key-test-workspace/.config/opencode.json'),
getAgentsMdPath: vi.fn(() => '/tmp/ssh-key-test-workspace/AGENTS.md'),
getDatabasePath: vi.fn(() => ':memory:'),
getConfigPath: vi.fn(() => '/tmp/ssh-key-test-workspace/config'),
ENV: {
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
WORKSPACE: { BASE_PATH: '/tmp/ssh-key-test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
DATABASE: { PATH: ':memory:' },
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
},
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024,
},
}))
import { parseSSHHost, normalizeHostPort, parseHostPort, writeTemporarySSHKey, cleanupSSHKey, cleanupAllSSHKeys } from '../../src/utils/ssh-key-manager'
beforeAll(async () => {
await fs.mkdir('/tmp/ssh-key-test-workspace', { recursive: true })
})
afterAll(async () => {
await fs.rm('/tmp/ssh-key-test-workspace', { recursive: true, force: true })
})
describe('SSH Host Parsing', () => {
it('should parse git@host:path format', () => {
const result = parseSSHHost('git@github.com:user/repo.git')