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
227 lines
8.1 KiB
TypeScript
227 lines
8.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { createNotificationRoutes } from '../../src/routes/notifications'
|
|
import type { NotificationService } from '../../src/services/notification'
|
|
|
|
interface NotificationServiceStub {
|
|
getVapidPublicKey: ReturnType<typeof vi.fn>
|
|
saveSubscription: ReturnType<typeof vi.fn>
|
|
removeSubscription: ReturnType<typeof vi.fn>
|
|
getSubscriptions: ReturnType<typeof vi.fn>
|
|
removeSubscriptionById: ReturnType<typeof vi.fn>
|
|
isConfigured: ReturnType<typeof vi.fn>
|
|
sendTestNotification: ReturnType<typeof vi.fn>
|
|
}
|
|
|
|
describe('Notification Routes', () => {
|
|
let service: NotificationServiceStub
|
|
let app: ReturnType<typeof createNotificationRoutes>
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
service = {
|
|
getVapidPublicKey: vi.fn(),
|
|
saveSubscription: vi.fn(),
|
|
removeSubscription: vi.fn(),
|
|
getSubscriptions: vi.fn(),
|
|
removeSubscriptionById: vi.fn(),
|
|
isConfigured: vi.fn(),
|
|
sendTestNotification: vi.fn(),
|
|
}
|
|
app = createNotificationRoutes(service as unknown as NotificationService)
|
|
})
|
|
|
|
describe('GET /vapid-public-key', () => {
|
|
it('returns the public key when configured', async () => {
|
|
service.getVapidPublicKey.mockReturnValue('public-key')
|
|
|
|
const res = await app.fetch(new Request('http://localhost/vapid-public-key'))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ publicKey: 'public-key' })
|
|
})
|
|
|
|
it('returns 503 when push notifications are not configured', async () => {
|
|
service.getVapidPublicKey.mockReturnValue(null)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/vapid-public-key'))
|
|
|
|
expect(res.status).toBe(503)
|
|
await expect(res.json()).resolves.toEqual({ error: 'Push notifications are not configured' })
|
|
})
|
|
})
|
|
|
|
describe('POST /subscribe', () => {
|
|
it('saves a subscription with the default userId', async () => {
|
|
const subscription = {
|
|
id: 1,
|
|
userId: 'default',
|
|
endpoint: 'https://push.example.com/abc',
|
|
p256dh: 'p256dh-key',
|
|
auth: 'auth-key',
|
|
deviceName: 'Phone',
|
|
createdAt: 1,
|
|
lastUsedAt: null,
|
|
}
|
|
service.saveSubscription.mockReturnValue(subscription)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
endpoint: 'https://push.example.com/abc',
|
|
keys: { p256dh: 'p256dh-key', auth: 'auth-key' },
|
|
deviceName: 'Phone',
|
|
}),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ subscription })
|
|
expect(service.saveSubscription).toHaveBeenCalledWith(
|
|
'default',
|
|
'https://push.example.com/abc',
|
|
'p256dh-key',
|
|
'auth-key',
|
|
'Phone',
|
|
)
|
|
})
|
|
|
|
it('saves a subscription with an overridden userId', async () => {
|
|
const subscription = { id: 2 }
|
|
service.saveSubscription.mockReturnValue(subscription)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscribe?userId=user-9', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
endpoint: 'https://push.example.com/abc',
|
|
keys: { p256dh: 'p256dh-key', auth: 'auth-key' },
|
|
}),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ subscription })
|
|
expect(service.saveSubscription).toHaveBeenCalledWith(
|
|
'user-9',
|
|
'https://push.example.com/abc',
|
|
'p256dh-key',
|
|
'auth-key',
|
|
undefined,
|
|
)
|
|
})
|
|
|
|
it('returns 400 for invalid subscription data', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ endpoint: 'not-a-url', keys: {} }),
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json() as { error: string }
|
|
expect(json.error).toBe('Invalid subscription data')
|
|
expect(service.saveSubscription).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('DELETE /subscribe', () => {
|
|
it('removes a subscription with the default userId', async () => {
|
|
service.removeSubscription.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ endpoint: 'https://push.example.com/abc' }),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(service.removeSubscription).toHaveBeenCalledWith('https://push.example.com/abc', 'default')
|
|
})
|
|
|
|
it('returns 400 for an invalid endpoint', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ endpoint: 'not-a-url' }),
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json() as { error: string }
|
|
expect(json.error).toBe('Valid endpoint URL is required')
|
|
expect(service.removeSubscription).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('GET /subscriptions', () => {
|
|
it('lists subscriptions for the requested user', async () => {
|
|
const subscriptions = [{ id: 1 }, { id: 2 }]
|
|
service.getSubscriptions.mockReturnValue(subscriptions)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscriptions?userId=user-9'))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ subscriptions })
|
|
expect(service.getSubscriptions).toHaveBeenCalledWith('user-9')
|
|
})
|
|
})
|
|
|
|
describe('DELETE /subscriptions/:id', () => {
|
|
it('removes a subscription by numeric id', async () => {
|
|
service.removeSubscriptionById.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscriptions/12?userId=user-9', {
|
|
method: 'DELETE',
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(service.removeSubscriptionById).toHaveBeenCalledWith(12, 'user-9')
|
|
})
|
|
|
|
it('returns 400 for a non-numeric id', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/subscriptions/abc', {
|
|
method: 'DELETE',
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
await expect(res.json()).resolves.toEqual({ error: 'Invalid subscription ID' })
|
|
expect(service.removeSubscriptionById).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('POST /test', () => {
|
|
it('returns 503 when push notifications are not configured', async () => {
|
|
service.isConfigured.mockReturnValue(false)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/test', { method: 'POST' }))
|
|
|
|
expect(res.status).toBe(503)
|
|
await expect(res.json()).resolves.toEqual({ error: 'Push notifications are not configured' })
|
|
expect(service.getSubscriptions).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 when the user has no subscriptions', async () => {
|
|
service.isConfigured.mockReturnValue(true)
|
|
service.getSubscriptions.mockReturnValue([])
|
|
|
|
const res = await app.fetch(new Request('http://localhost/test', { method: 'POST' }))
|
|
|
|
expect(res.status).toBe(404)
|
|
await expect(res.json()).resolves.toEqual({ error: 'No push subscriptions registered' })
|
|
expect(service.sendTestNotification).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('sends a test notification and reports the device count', async () => {
|
|
service.isConfigured.mockReturnValue(true)
|
|
service.getSubscriptions.mockReturnValue([{ id: 1 }, { id: 2 }])
|
|
service.sendTestNotification.mockResolvedValue(undefined)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/test?userId=user-9', { method: 'POST' }))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true, devicesNotified: 2 })
|
|
expect(service.sendTestNotification).toHaveBeenCalledWith('user-9')
|
|
})
|
|
})
|
|
})
|