mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* refactor(config): make on-disk opencode.json the single source of truth Make <workspace>/.config/opencode/opencode.json the only store for the active OpenCode configuration. One owner (opencode-config-file.ts) reads, validates, writes, archives, and seeds the file; one owner (opencode-config-apply.ts) decides how an edit is applied: restart pending, or a live patch with recovery. Settings routes, the internal assistant API, the health-watch supervisor, boot, and host import all go through them, so an edit that lands on the file without going through the database is visible immediately instead of at the next Manager boot. Remove the named-config profiles. Migration 019 archives every opencode_configs row to .config/opencode-configs-archive/<name>.json, restores the default row to opencode.json when no file exists, then drops the table, its indexes, and repos.opencode_config_name. The /opencode-configs* and config/switch routes and the CreateConfigDialog/SwitchConfigDialog dialogs are gone, and the ocm tool gains GET/PUT /opencode-config backed by the same apply owner. Harden the write path: serialize and atomically write config updates while preserving the file mode, keep withFileLock usable after a rejection, route reloadConfig's cleaned-config write through the file owner, stop boot from rewriting an existing valid config when importing state, prune the health-watch directory to its newest 20 entries, resolve archive paths with realpath, and share one frontend config query across dialogs. * fix(mirror): canonicalize worktree paths before branch ownership check * refactor(config): unify opencode config apply and rollback paths - share one config route factory between public and internal mounts - capture last-known-good before every config write and restore via one helper - stop auto-restarting on save; flag restart-required fields and live-patch the rest - add CONFIG_PATCH_TIMEOUT_MS for config patch requests - extract canonicalPath helpers, health-watch artifact writer, and useProvidersWithModels hook * refactor(model-state): use OpenCode state file as model state source of truth
274 lines
7.8 KiB
TypeScript
274 lines
7.8 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import * as db from '../../src/db/queries'
|
|
|
|
const mockDb = {
|
|
prepare: vi.fn(),
|
|
exec: vi.fn(),
|
|
close: vi.fn(),
|
|
transaction: vi.fn()
|
|
} as any
|
|
|
|
vi.mock('bun:sqlite', () => ({
|
|
Database: vi.fn(() => mockDb)
|
|
}))
|
|
|
|
describe('Database Queries', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockDb.prepare.mockReset()
|
|
})
|
|
|
|
describe('createRepo', () => {
|
|
it('should insert new repo record', () => {
|
|
const clonedAt = Date.now()
|
|
const repo = {
|
|
repoUrl: 'https://github.com/test/repo',
|
|
localPath: 'repos/test-repo',
|
|
sourcePath: '/Users/test/repos/test-repo',
|
|
branch: 'main',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready' as const,
|
|
clonedAt,
|
|
isWorktree: false,
|
|
isLocal: true,
|
|
}
|
|
|
|
const existingCheckSourceStmt = {
|
|
get: vi.fn().mockReturnValue(undefined)
|
|
}
|
|
|
|
const existingCheckLocalStmt = {
|
|
get: vi.fn().mockReturnValue(undefined)
|
|
}
|
|
|
|
const insertStmt = {
|
|
run: vi.fn().mockReturnValue({ changes: 1, lastInsertRowid: 1 })
|
|
}
|
|
|
|
const selectStmt = {
|
|
get: vi.fn().mockReturnValue({
|
|
id: 1,
|
|
repo_url: repo.repoUrl,
|
|
local_path: repo.localPath,
|
|
source_path: repo.sourcePath,
|
|
branch: repo.branch,
|
|
default_branch: repo.defaultBranch,
|
|
clone_status: repo.cloneStatus,
|
|
cloned_at: repo.clonedAt,
|
|
last_accessed_at: clonedAt,
|
|
is_worktree: 0
|
|
})
|
|
}
|
|
|
|
mockDb.prepare
|
|
.mockReturnValueOnce(existingCheckSourceStmt)
|
|
.mockReturnValueOnce(existingCheckLocalStmt)
|
|
.mockReturnValueOnce(insertStmt)
|
|
.mockReturnValueOnce(selectStmt)
|
|
|
|
const result = db.createRepo(mockDb, repo)
|
|
|
|
expect(mockDb.prepare).toHaveBeenCalled()
|
|
expect(insertStmt.run).toHaveBeenCalledWith(
|
|
repo.repoUrl,
|
|
repo.localPath,
|
|
repo.sourcePath,
|
|
repo.branch || null,
|
|
repo.defaultBranch,
|
|
repo.cloneStatus,
|
|
repo.clonedAt,
|
|
clonedAt,
|
|
repo.isWorktree ? 1 : 0,
|
|
1
|
|
)
|
|
expect(result.id).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('getRepoById', () => {
|
|
it('should retrieve repo by ID', () => {
|
|
const clonedAt = Date.now()
|
|
const lastAccessedAt = Date.now()
|
|
const repoRow = {
|
|
id: 1,
|
|
repo_url: 'https://github.com/test/repo',
|
|
local_path: 'repos/test-repo',
|
|
source_path: '/Users/test/repos/test-repo',
|
|
branch: 'main',
|
|
default_branch: 'main',
|
|
clone_status: 'ready',
|
|
cloned_at: clonedAt,
|
|
last_pulled: null,
|
|
last_accessed_at: lastAccessedAt,
|
|
is_worktree: 0,
|
|
is_local: 0
|
|
}
|
|
|
|
const stmt = {
|
|
get: vi.fn().mockReturnValue(repoRow)
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
const result = db.getRepoById(mockDb, 1)
|
|
|
|
expect(result).toEqual({
|
|
id: 1,
|
|
repoUrl: 'https://github.com/test/repo',
|
|
localPath: 'repos/test-repo',
|
|
fullPath: '/Users/test/repos/test-repo',
|
|
sourcePath: '/Users/test/repos/test-repo',
|
|
branch: 'main',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: clonedAt,
|
|
lastPulled: null,
|
|
lastAccessedAt: lastAccessedAt,
|
|
isWorktree: undefined,
|
|
isLocal: undefined
|
|
})
|
|
})
|
|
|
|
it('should return null for non-existent repo', () => {
|
|
const stmt = {
|
|
get: vi.fn().mockReturnValue(undefined)
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
const result = db.getRepoById(mockDb, 999)
|
|
|
|
expect(result).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('listRepos', () => {
|
|
it('should return all repos', () => {
|
|
const repoRows = [
|
|
{
|
|
id: 1,
|
|
repo_url: 'https://github.com/test/repo1',
|
|
local_path: 'repos/test-repo1',
|
|
branch: 'main',
|
|
default_branch: 'main',
|
|
clone_status: 'ready',
|
|
cloned_at: Date.now(),
|
|
is_worktree: 0
|
|
},
|
|
{
|
|
id: 2,
|
|
repo_url: 'https://github.com/test/repo2',
|
|
local_path: 'repos/test-repo2',
|
|
branch: 'main',
|
|
default_branch: 'main',
|
|
clone_status: 'ready',
|
|
cloned_at: Date.now(),
|
|
is_worktree: 0
|
|
}
|
|
]
|
|
|
|
const stmt = {
|
|
all: vi.fn().mockReturnValue(repoRows)
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
const result = db.listRepos(mockDb)
|
|
|
|
expect(mockDb.prepare).toHaveBeenCalledWith(
|
|
expect.stringContaining('SELECT * FROM repos ORDER BY cloned_at DESC')
|
|
)
|
|
expect(stmt.all).toHaveBeenCalled()
|
|
expect(result).toHaveLength(2)
|
|
expect(result[0]?.repoUrl).toBe('https://github.com/test/repo1')
|
|
})
|
|
})
|
|
|
|
describe('updateRepoStatus', () => {
|
|
it('should update repo clone status', () => {
|
|
const stmt = {
|
|
run: vi.fn().mockReturnValue({ changes: 1 })
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
db.updateRepoStatus(mockDb, 1, 'ready')
|
|
|
|
expect(mockDb.prepare).toHaveBeenCalledWith(
|
|
'UPDATE repos SET clone_status = ? WHERE id = ?'
|
|
)
|
|
expect(stmt.run).toHaveBeenCalledWith('ready', 1)
|
|
})
|
|
})
|
|
|
|
describe('updateLastPulled', () => {
|
|
it('should update repo last pulled timestamp', () => {
|
|
const stmt = {
|
|
run: vi.fn().mockReturnValue({ changes: 1 })
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
db.updateLastPulled(mockDb, 1)
|
|
|
|
expect(mockDb.prepare).toHaveBeenCalledWith(
|
|
'UPDATE repos SET last_pulled = ? WHERE id = ?'
|
|
)
|
|
expect(stmt.run).toHaveBeenCalledWith(expect.any(Number), 1)
|
|
})
|
|
})
|
|
|
|
describe('updateLastAccessed', () => {
|
|
it('should update repo last accessed timestamp', () => {
|
|
const stmt = {
|
|
run: vi.fn().mockReturnValue({ changes: 1 })
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
db.updateLastAccessed(mockDb, 1)
|
|
|
|
expect(mockDb.prepare).toHaveBeenCalledWith(
|
|
'UPDATE repos SET last_accessed_at = ? WHERE id = ?'
|
|
)
|
|
expect(stmt.run).toHaveBeenCalledWith(expect.any(Number), 1)
|
|
})
|
|
|
|
it('should throw error when repo not found', () => {
|
|
const stmt = {
|
|
run: vi.fn().mockReturnValue({ changes: 0 })
|
|
}
|
|
mockDb.prepare.mockReturnValue(stmt)
|
|
|
|
expect(() => db.updateLastAccessed(mockDb, 999)).toThrow('Repository with id 999 not found')
|
|
})
|
|
})
|
|
|
|
describe('deleteRepo', () => {
|
|
it('should delete repo schedules before deleting repo by ID', () => {
|
|
const deleteRunsStmt = { run: vi.fn().mockReturnValue({ changes: 2 }) }
|
|
const deleteJobsStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) }
|
|
const deleteSettingsStmt = { run: vi.fn().mockReturnValue({ changes: 0 }) }
|
|
const deleteRepoStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) }
|
|
mockDb.prepare
|
|
.mockReturnValueOnce(deleteRunsStmt)
|
|
.mockReturnValueOnce(deleteJobsStmt)
|
|
.mockReturnValueOnce(deleteSettingsStmt)
|
|
.mockReturnValueOnce(deleteRepoStmt)
|
|
|
|
db.deleteRepo(mockDb, 1)
|
|
|
|
expect(mockDb.prepare).toHaveBeenNthCalledWith(1, 'DELETE FROM schedule_jobs WHERE repo_id = ?')
|
|
expect(deleteJobsStmt.run).toHaveBeenCalledWith(1)
|
|
expect(mockDb.prepare).toHaveBeenNthCalledWith(2, 'DELETE FROM schedule_runs WHERE repo_id = ?')
|
|
expect(deleteRunsStmt.run).toHaveBeenCalledWith(1)
|
|
expect(mockDb.prepare).toHaveBeenNthCalledWith(3, 'DELETE FROM repo_settings WHERE repo_id = ?')
|
|
expect(deleteSettingsStmt.run).toHaveBeenCalledWith(1)
|
|
expect(mockDb.prepare).toHaveBeenNthCalledWith(4,
|
|
'DELETE FROM repos WHERE id = ?'
|
|
)
|
|
expect(deleteRepoStmt.run).toHaveBeenCalledWith(1)
|
|
})
|
|
|
|
it('should not delete the assistant repo', () => {
|
|
db.deleteRepo(mockDb, 0)
|
|
|
|
expect(mockDb.prepare).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
})
|