mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* 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
152 lines
5.4 KiB
TypeScript
152 lines
5.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs'
|
|
import { tmpdir } from 'os'
|
|
import { join } from 'path'
|
|
|
|
let home: string
|
|
let configDir: string
|
|
let savedHome: string | undefined
|
|
|
|
async function loadState() {
|
|
vi.resetModules()
|
|
return import('../src/state.js')
|
|
}
|
|
|
|
beforeEach(() => {
|
|
savedHome = process.env.HOME
|
|
home = mkdtempSync(join(tmpdir(), 'ocm-home-'))
|
|
process.env.HOME = home
|
|
configDir = join(home, '.config', 'opencode-manager')
|
|
})
|
|
|
|
afterEach(() => {
|
|
if (savedHome === undefined) delete process.env.HOME
|
|
else process.env.HOME = savedHome
|
|
rmSync(home, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('cli state file', () => {
|
|
it('returns null when no state file exists', async () => {
|
|
const { readState } = await loadState()
|
|
|
|
expect(readState()).toBeNull()
|
|
})
|
|
|
|
it('round-trips the manager url and last repo', async () => {
|
|
const { readState, writeState } = await loadState()
|
|
|
|
writeState({ managerUrl: 'https://manager.example.com', lastRepoId: 7, lastRepoName: 'repo' })
|
|
|
|
const state = readState()
|
|
expect(state?.managerUrl).toBe('https://manager.example.com')
|
|
expect(state?.lastRepoId).toBe(7)
|
|
expect(state?.updatedAt).toBeTypeOf('number')
|
|
})
|
|
|
|
it('writes the state file with owner-only permissions', async () => {
|
|
if (process.platform === 'win32') return
|
|
const { writeState, getStatePath } = await loadState()
|
|
|
|
writeState({ managerUrl: 'https://manager.example.com' })
|
|
|
|
expect(statSync(getStatePath()).mode & 0o777).toBe(0o600)
|
|
expect(statSync(configDir).mode & 0o777).toBe(0o700)
|
|
})
|
|
|
|
it('repairs loose permissions on an existing state file', async () => {
|
|
if (process.platform === 'win32') return
|
|
const { writeState, getStatePath } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
writeFileSync(getStatePath(), '{}', { mode: 0o644 })
|
|
|
|
writeState({ managerUrl: 'https://manager.example.com' })
|
|
|
|
expect(statSync(getStatePath()).mode & 0o777).toBe(0o600)
|
|
})
|
|
|
|
it('leaves no temp file behind', async () => {
|
|
const { writeState } = await loadState()
|
|
|
|
writeState({ managerUrl: 'https://manager.example.com' })
|
|
|
|
expect(readdirSync(configDir).filter((entry) => entry.endsWith('.tmp'))).toEqual([])
|
|
})
|
|
|
|
it('treats a corrupt state file as absent', async () => {
|
|
const { readState, getStatePath } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
writeFileSync(getStatePath(), 'not json')
|
|
|
|
expect(readState()).toBeNull()
|
|
})
|
|
|
|
it('treats a state file without a manager url as absent', async () => {
|
|
const { readState, getStatePath } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
writeFileSync(getStatePath(), JSON.stringify({ lastRepoId: 1 }))
|
|
|
|
expect(readState()).toBeNull()
|
|
})
|
|
|
|
it('surfaces an unreadable state file instead of reporting no manager', async () => {
|
|
if (process.platform === 'win32' || process.getuid?.() === 0) return
|
|
const { readState, writeState, getStatePath } = await loadState()
|
|
writeState({ managerUrl: 'https://manager.example.com' })
|
|
chmodSync(getStatePath(), 0o000)
|
|
|
|
expect(() => readState()).toThrow(/EACCES|EPERM/)
|
|
})
|
|
|
|
it('clearState empties an existing state file without recreating a missing one', async () => {
|
|
const { readState, writeState, clearState, getStatePath } = await loadState()
|
|
|
|
clearState()
|
|
expect(existsSync(getStatePath())).toBe(false)
|
|
|
|
writeState({ managerUrl: 'https://manager.example.com' })
|
|
clearState()
|
|
|
|
expect(readState()).toBeNull()
|
|
expect(readFileSync(getStatePath(), 'utf-8')).toBe('{}')
|
|
})
|
|
|
|
it('reads an install notice once and then removes it', async () => {
|
|
const { readInstallNotice } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
const noticeFile = join(configDir, 'install-notice.json')
|
|
writeFileSync(noticeFile, JSON.stringify({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true }))
|
|
|
|
expect(readInstallNotice()).toEqual({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true })
|
|
expect(existsSync(noticeFile)).toBe(false)
|
|
expect(readInstallNotice()).toBeNull()
|
|
})
|
|
|
|
it('writes an install notice for the next TUI launch', async () => {
|
|
const { readInstallNotice, writeInstallNotice } = await loadState()
|
|
|
|
writeInstallNotice({ link: '/home/me/.local/bin/ocm', binDir: '/home/me/.local/bin', pathMissing: false })
|
|
|
|
expect(readInstallNotice()).toEqual({ link: '/home/me/.local/bin/ocm', binDir: '/home/me/.local/bin', pathMissing: false })
|
|
})
|
|
|
|
it('keeps an unreadable install notice instead of consuming it', async () => {
|
|
if (process.platform === 'win32' || process.getuid?.() === 0) return
|
|
const { readInstallNotice } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
const noticeFile = join(configDir, 'install-notice.json')
|
|
writeFileSync(noticeFile, JSON.stringify({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true }))
|
|
chmodSync(noticeFile, 0o000)
|
|
|
|
expect(readInstallNotice()).toBeNull()
|
|
expect(existsSync(noticeFile)).toBe(true)
|
|
})
|
|
|
|
it('ignores an incomplete install notice', async () => {
|
|
const { readInstallNotice } = await loadState()
|
|
mkdirSync(configDir, { recursive: true })
|
|
writeFileSync(join(configDir, 'install-notice.json'), JSON.stringify({ binDir: '~/.local/bin' }))
|
|
|
|
expect(readInstallNotice()).toBeNull()
|
|
})
|
|
})
|