Files
opencode-manager/backend/test/routes/settings-skills-install.test.ts
Chris Scott dacde879d6 feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)
* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
2026-09-26 13:16:45 -04:00

371 lines
13 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createStubOpenCodeClient } from '../helpers/stub-opencode-client'
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('child_process', () => ({
execSync: vi.fn(),
spawnSync: vi.fn(),
spawn: vi.fn(),
}))
vi.mock('../../src/utils/logger', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
},
}))
vi.mock('../../src/constants', () => ({
DEFAULT_AGENTS_MD: '# Test Agents MD',
}))
vi.mock('../../src/services/settings', () => ({
SettingsService: vi.fn().mockImplementation(() => ({
getSettings: vi.fn(),
updateSettings: vi.fn(),
saveLastKnownGoodConfig: vi.fn(),
})),
}))
vi.mock('../../src/services/file-operations', () => ({
writeFileContent: vi.fn(),
readFileContent: vi.fn(),
fileExists: vi.fn(),
}))
vi.mock('../../src/services/opencode-single-server', () => {
class MockConfigReloadError extends Error {
validationIssues: Array<{ path: string; message: string }> = []
constructor(message: string) {
super(message)
this.name = 'ConfigReloadError'
}
}
return {
opencodeServerManager: {
getVersion: vi.fn(),
fetchVersion: vi.fn(),
restart: vi.fn(),
clearStartupError: vi.fn(),
getLastStartupError: vi.fn(),
markRestartPending: vi.fn(),
isRestartPending: vi.fn(),
setDatabase: vi.fn(),
},
ConfigReloadError: MockConfigReloadError,
}
})
vi.mock('../../src/services/skills', () => ({
listManagedSkills: vi.fn(),
getSkill: vi.fn(),
createSkill: vi.fn(),
updateSkill: vi.fn(),
deleteSkill: vi.fn(),
installSkillFromGithubTree: vi.fn(),
installSkillFromUploadedFiles: vi.fn(),
}))
vi.mock('@opencode-manager/shared/config/env', async (importOriginal) => ({
...(await importOriginal<typeof import('@opencode-manager/shared/config/env')>()),
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', 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' },
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,
},
TIMEOUTS: {},
}))
import { createSettingsRoutes } from '../../src/routes/settings'
import { opencodeServerManager } from '../../src/services/opencode-single-server'
import {
installSkillFromGithubTree,
installSkillFromUploadedFiles,
deleteSkill,
} from '../../src/services/skills'
const mockInstallFromGithubTree = installSkillFromGithubTree as ReturnType<typeof vi.fn>
const mockInstallFromUploadedFiles = installSkillFromUploadedFiles as ReturnType<typeof vi.fn>
const mockDeleteSkill = deleteSkill as ReturnType<typeof vi.fn>
const mockRestart = opencodeServerManager.restart as ReturnType<typeof vi.fn>
const mockMarkRestartPending = opencodeServerManager.markRestartPending as ReturnType<typeof vi.fn>
const mockSuccessResponse = {
skill: {
name: 'teach',
description: 'A teaching skill',
body: '## Teach\n\nContent',
scope: 'global',
location: '/tmp/test-workspace/.config/opencode/skills/teach/SKILL.md',
},
overwritten: false,
sourceType: 'github' as const,
filesInstalled: ['teach/SKILL.md'],
}
describe('Settings Routes - Skill Install', () => {
let settingsApp: ReturnType<typeof createSettingsRoutes>
let testDb: any
beforeEach(() => {
vi.clearAllMocks()
mockRestart.mockResolvedValue(undefined)
mockInstallFromGithubTree.mockResolvedValue(mockSuccessResponse)
mockInstallFromUploadedFiles.mockResolvedValue({ ...mockSuccessResponse, sourceType: 'upload' })
testDb = {} as any
settingsApp = createSettingsRoutes(testDb, { getGitEnvironment: vi.fn().mockReturnValue({}) } as any, createStubOpenCodeClient())
})
describe('POST /skills/install', () => {
it('installs from GitHub JSON', async () => {
const res = await settingsApp.request('/skills/install', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sourceType: 'github',
url: 'https://github.com/mattpocock/skills/tree/main/skills/productivity/teach',
scope: 'global',
}),
})
expect(res.status).toBe(200)
const body = await res.json() as Record<string, unknown>
expect(body).toEqual({ ...mockSuccessResponse, restartRequired: true })
expect(mockInstallFromGithubTree).toHaveBeenCalledTimes(1)
expect(mockInstallFromGithubTree).toHaveBeenCalledWith(testDb, {
sourceType: 'github',
url: 'https://github.com/mattpocock/skills/tree/main/skills/productivity/teach',
scope: 'global',
})
expect(mockMarkRestartPending).toHaveBeenCalledTimes(1)
expect(mockRestart).not.toHaveBeenCalled()
})
it('installs from multipart upload', async () => {
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: 'teach/SKILL.md' },
]))
formData.append('file0', new File(['---\nname: teach\ndescription: A teaching skill\n---\n## Teach\n\nContent'], 'SKILL.md', { type: 'text/markdown' }))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(200)
const body = await res.json() as { sourceType: string }
expect(body.sourceType).toBe('upload')
expect(mockInstallFromUploadedFiles).toHaveBeenCalledTimes(1)
expect(mockInstallFromUploadedFiles).toHaveBeenCalledWith(
testDb,
{
sourceType: 'upload',
scope: 'global',
repoId: undefined,
overwrite: undefined,
},
[
expect.objectContaining({
relativePath: 'teach/SKILL.md',
content: expect.any(Buffer),
}),
],
)
expect(mockMarkRestartPending).toHaveBeenCalledTimes(1)
expect(mockRestart).not.toHaveBeenCalled()
})
it('returns 409 on existing skill', async () => {
mockInstallFromGithubTree.mockRejectedValue(new Error('Skill "teach" already exists in global scope'))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sourceType: 'github',
url: 'https://github.com/mattpocock/skills/tree/main/skills/productivity/teach',
scope: 'global',
}),
})
expect(res.status).toBe(409)
const body = await res.json() as { error: string }
expect(body.error).toContain('already exists')
expect(mockRestart).not.toHaveBeenCalled()
})
it('rejects project scope without repoId', async () => {
const res = await settingsApp.request('/skills/install', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sourceType: 'github',
url: 'https://github.com/mattpocock/skills/tree/main/skills/productivity/teach',
scope: 'project',
}),
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('repoId is required')
expect(mockInstallFromGithubTree).not.toHaveBeenCalled()
})
it('rejects invalid repoId in multipart upload', async () => {
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('repoId', 'abc')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: 'teach/SKILL.md' },
]))
formData.append('file0', new File(['content'], 'SKILL.md', { type: 'text/markdown' }))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('Invalid repoId')
})
it('rejects multiple uploaded skills', async () => {
mockInstallFromUploadedFiles.mockRejectedValue(new Error('Only one skill can be installed at a time'))
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: 'teach/SKILL.md' },
{ fieldName: 'file1', relativePath: 'review/SKILL.md' },
]))
formData.append('file0', new File(['---\nname: teach\n---\nBody'], 'SKILL.md', { type: 'text/markdown' }))
formData.append('file1', new File(['---\nname: review\n---\nBody'], 'SKILL.md', { type: 'text/markdown' }))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('Only one skill')
expect(mockRestart).not.toHaveBeenCalled()
})
it('rejects upload with missing SKILL.md', async () => {
mockInstallFromUploadedFiles.mockRejectedValue(new Error('Skill source must contain SKILL.md'))
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: 'teach/README.md' },
]))
formData.append('file0', new File(['# Readme'], 'README.md', { type: 'text/markdown' }))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('Skill source must contain')
expect(mockRestart).not.toHaveBeenCalled()
})
it('rejects unsafe upload path', async () => {
mockInstallFromUploadedFiles.mockRejectedValue(new Error('Path must not contain "..": "../SKILL.md"'))
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: '../SKILL.md' },
]))
formData.append('file0', new File(['---\nname: teach\n---\nBody'], 'SKILL.md', { type: 'text/markdown' }))
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('Path must not contain')
expect(mockRestart).not.toHaveBeenCalled()
})
it('rejects non-File manifest field in multipart upload', async () => {
const formData = new FormData()
formData.append('scope', 'global')
formData.append('sourceType', 'upload')
formData.append('fileManifest', JSON.stringify([
{ fieldName: 'file0', relativePath: 'teach/SKILL.md' },
]))
formData.append('file0', 'not-a-file')
const res = await settingsApp.request('/skills/install', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('not a valid file')
})
})
describe('DELETE /skills/:name', () => {
it('remains the deletion endpoint', async () => {
mockDeleteSkill.mockResolvedValue(undefined)
const res = await settingsApp.request('/skills/test-skill?scope=global', {
method: 'DELETE',
})
expect(res.status).toBe(200)
expect(mockDeleteSkill).toHaveBeenCalledWith(testDb, 'test-skill', 'global', undefined)
})
})
})