diff --git a/backend/src/utils/ssh-key-manager.ts b/backend/src/utils/ssh-key-manager.ts index e2da3bff..7707bcbb 100644 --- a/backend/src/utils/ssh-key-manager.ts +++ b/backend/src/utils/ssh-key-manager.ts @@ -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') diff --git a/backend/test/integration/ssh-integration.test.ts b/backend/test/integration/ssh-integration.test.ts index a9584efb..5730a3c1 100644 --- a/backend/test/integration/ssh-integration.test.ts +++ b/backend/test/integration/ssh-integration.test.ts @@ -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', () => ({ diff --git a/backend/test/routes/settings.test.ts b/backend/test/routes/settings.test.ts index ca46a0e7..747d0648 100644 --- a/backend/test/routes/settings.test.ts +++ b/backend/test/routes/settings.test.ts @@ -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 const mockGetVersion = opencodeServerManager.getVersion as ReturnType diff --git a/backend/test/services/git-auth.test.ts b/backend/test/services/git-auth.test.ts index 5f099f0f..2131d119 100644 --- a/backend/test/services/git-auth.test.ts +++ b/backend/test/services/git-auth.test.ts @@ -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' diff --git a/backend/test/utils/ssh-key-manager.test.ts b/backend/test/utils/ssh-key-manager.test.ts index 803bb356..277e639a 100644 --- a/backend/test/utils/ssh-key-manager.test.ts +++ b/backend/test/utils/ssh-key-manager.test.ts @@ -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')