Files
opencode-manager/backend/test/services/prompt-templates.test.ts
Chris Scott 295d24ced7 refactor(config): make on-disk opencode.json the single source of truth (#354)
* 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
2026-09-19 00:28:34 -04:00

90 lines
2.7 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { Database } from 'bun:sqlite'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
import { PromptTemplateService, PromptTemplateServiceError } from '../../src/services/prompt-templates'
function createTemplateInput(overrides: Record<string, unknown> = {}) {
return {
title: 'Daily standup',
category: 'standup',
cadenceHint: 'daily',
suggestedName: 'daily-standup',
suggestedDescription: 'Summarize yesterday',
description: 'A standup prompt',
prompt: 'Summarize my work',
...overrides,
}
}
describe('PromptTemplateService', () => {
let db: Database
let service: PromptTemplateService
beforeEach(() => {
db = new Database(':memory:')
migrate(db, allMigrations)
service = new PromptTemplateService(db)
})
afterEach(() => {
db.close()
})
it('creates, lists, and gets a template', () => {
const created = service.create(createTemplateInput())
expect(service.list().map(template => template.id)).toContain(created.id)
expect(service.getById(created.id)).toEqual(created)
})
it('throws a 404 service error when a template is missing', () => {
expect(() => service.getById(999)).toThrow(PromptTemplateServiceError)
try {
service.getById(999)
} catch (error) {
expect(error).toBeInstanceOf(PromptTemplateServiceError)
expect((error as PromptTemplateServiceError).statusCode).toBe(404)
expect((error as Error).message).toBe('Template not found')
}
})
it('updates an existing template', () => {
const created = service.create(createTemplateInput())
const updated = service.update(created.id, { title: 'Renamed' })
expect(updated.title).toBe('Renamed')
expect(service.getById(created.id).title).toBe('Renamed')
})
it('throws a 404 service error when updating a missing template', () => {
try {
service.update(999, { title: 'Missing' })
throw new Error('expected update to throw')
} catch (error) {
expect(error).toBeInstanceOf(PromptTemplateServiceError)
expect((error as PromptTemplateServiceError).statusCode).toBe(404)
}
})
it('deletes an existing template', () => {
const created = service.create(createTemplateInput())
service.delete(created.id)
expect(service.list().map(template => template.id)).not.toContain(created.id)
})
it('throws a 404 service error when deleting a missing template', () => {
try {
service.delete(999)
throw new Error('expected delete to throw')
} catch (error) {
expect(error).toBeInstanceOf(PromptTemplateServiceError)
expect((error as PromptTemplateServiceError).statusCode).toBe(404)
}
})
})