mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +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
231 lines
7.0 KiB
TypeScript
231 lines
7.0 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
type Listener = (event: unknown) => void
|
|
|
|
const listeners: Record<string, Listener[]> = {}
|
|
|
|
function makeWorkerGlobalScope() {
|
|
const scope = {
|
|
addEventListener(type: string, listener: Listener) {
|
|
listeners[type] = listeners[type] ?? []
|
|
listeners[type].push(listener)
|
|
},
|
|
skipWaiting: vi.fn(async () => {}),
|
|
location: { origin: 'http://localhost' },
|
|
registration: {
|
|
showNotification: vi.fn(async () => {}),
|
|
},
|
|
clients: {
|
|
matchAll: vi.fn(async () => []),
|
|
openWindow: vi.fn(async () => ({})),
|
|
claim: vi.fn(async () => {}),
|
|
},
|
|
}
|
|
;(globalThis as Record<string, unknown>).self = scope
|
|
;(globalThis as Record<string, unknown>).caches = {
|
|
open: vi.fn(async () => ({ addAll: vi.fn(async () => {}), put: vi.fn(async () => {}) })),
|
|
keys: vi.fn(async () => []),
|
|
match: vi.fn(async () => undefined),
|
|
delete: vi.fn(async () => true),
|
|
}
|
|
return scope
|
|
}
|
|
|
|
function dispatch(type: string, event: unknown) {
|
|
const typeListeners = listeners[type] ?? []
|
|
expect(typeListeners.length).toBeGreaterThan(0)
|
|
for (const listener of typeListeners) listener(event)
|
|
}
|
|
|
|
async function dispatchAndWait(type: string, event: unknown) {
|
|
const promises: Promise<void>[] = []
|
|
const enriched = {
|
|
...event,
|
|
waitUntil: (promise: Promise<void>) => {
|
|
promises.push(promise)
|
|
},
|
|
}
|
|
dispatch(type, enriched)
|
|
await Promise.all(promises)
|
|
}
|
|
|
|
function makeClient(overrides: Partial<WindowClient> & { url: string }) {
|
|
return {
|
|
focused: false,
|
|
visibilityState: 'visible',
|
|
postMessage: vi.fn(),
|
|
focus: vi.fn(async () => overrides),
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('service worker notifications', () => {
|
|
let scope: ReturnType<typeof makeWorkerGlobalScope>
|
|
|
|
beforeEach(async () => {
|
|
vi.resetModules()
|
|
for (const key of Object.keys(listeners)) delete listeners[key]
|
|
scope = makeWorkerGlobalScope()
|
|
await import('./sw')
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
describe('push', () => {
|
|
it('shows high-priority notification with defaults from payload', async () => {
|
|
const payload = {
|
|
title: 'Permission requested',
|
|
body: 'A session needs approval',
|
|
tag: 'repo-1-session-1',
|
|
renotify: true,
|
|
timestamp: 123,
|
|
data: {
|
|
eventType: 'permission.asked',
|
|
url: '/repos/1/sessions/s',
|
|
},
|
|
}
|
|
|
|
await dispatchAndWait('push', {
|
|
data: { json: () => payload, text: () => '' },
|
|
})
|
|
|
|
expect(scope.registration.showNotification).toHaveBeenCalledWith(
|
|
'Permission requested',
|
|
expect.objectContaining({
|
|
badge: '/icons/badge-96x96.png',
|
|
icon: '/icons/icon-512x512.png',
|
|
renotify: true,
|
|
timestamp: 123,
|
|
requireInteraction: true,
|
|
tag: 'repo-1-session-1',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('does not require interaction for low-priority events', async () => {
|
|
const payload = {
|
|
title: 'Session idle',
|
|
body: 'Done',
|
|
tag: 'repo-1-session-1',
|
|
data: { eventType: 'session.idle' },
|
|
}
|
|
|
|
await dispatchAndWait('push', {
|
|
data: { json: () => payload, text: () => '' },
|
|
})
|
|
|
|
expect(scope.registration.showNotification).toHaveBeenCalledWith(
|
|
'Session idle',
|
|
expect.objectContaining({ requireInteraction: false, renotify: false })
|
|
)
|
|
})
|
|
|
|
it('requires interaction for form requests', async () => {
|
|
const payload = {
|
|
title: 'Question',
|
|
body: 'A session needs your answer',
|
|
tag: 'repo-1-session-1',
|
|
data: { eventType: 'form.created', url: '/repos/1/sessions/s' },
|
|
}
|
|
|
|
await dispatchAndWait('push', {
|
|
data: { json: () => payload, text: () => '' },
|
|
})
|
|
|
|
expect(scope.registration.showNotification).toHaveBeenCalledWith(
|
|
'Question',
|
|
expect.objectContaining({ requireInteraction: true })
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('notificationclick', () => {
|
|
it('posts message to the client already on the target pathname and focuses it', async () => {
|
|
const matchClient = makeClient({ url: 'http://localhost/repos/1/sessions/s' })
|
|
const focusedClient = makeClient({ url: 'http://localhost/repos/1', focused: true })
|
|
scope.clients.matchAll.mockResolvedValue([focusedClient, matchClient])
|
|
|
|
await dispatchAndWait('notificationclick', {
|
|
notification: {
|
|
close: vi.fn(),
|
|
data: { url: '/repos/1/sessions/s' },
|
|
},
|
|
})
|
|
|
|
expect(matchClient.postMessage).toHaveBeenCalledWith({
|
|
type: 'NOTIFICATION_CLICK',
|
|
url: '/repos/1/sessions/s',
|
|
})
|
|
expect(matchClient.focus).toHaveBeenCalled()
|
|
expect(focusedClient.postMessage).not.toHaveBeenCalled()
|
|
expect(focusedClient.focus).not.toHaveBeenCalled()
|
|
expect(scope.clients.openWindow).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('posts message to the focused client when no client matches the pathname', async () => {
|
|
const focusedClient = makeClient({ url: 'http://localhost/repos/2', focused: true })
|
|
const otherClient = makeClient({ url: 'http://localhost/repos/3' })
|
|
scope.clients.matchAll.mockResolvedValue([otherClient, focusedClient])
|
|
|
|
await dispatchAndWait('notificationclick', {
|
|
notification: {
|
|
close: vi.fn(),
|
|
data: { url: '/repos/1/sessions/s' },
|
|
},
|
|
})
|
|
|
|
expect(focusedClient.postMessage).toHaveBeenCalledWith({
|
|
type: 'NOTIFICATION_CLICK',
|
|
url: '/repos/1/sessions/s',
|
|
})
|
|
expect(focusedClient.focus).toHaveBeenCalled()
|
|
expect(otherClient.postMessage).not.toHaveBeenCalled()
|
|
expect(scope.clients.openWindow).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('opens a new window when there are no clients', async () => {
|
|
scope.clients.matchAll.mockResolvedValue([])
|
|
|
|
await dispatchAndWait('notificationclick', {
|
|
notification: {
|
|
close: vi.fn(),
|
|
data: { url: '/repos/1/sessions/s' },
|
|
},
|
|
})
|
|
|
|
expect(scope.clients.openWindow).toHaveBeenCalledWith('/repos/1/sessions/s')
|
|
})
|
|
|
|
it('ignores clients from other origins', async () => {
|
|
const foreignClient = makeClient({ url: 'https://evil.example/repos/1/sessions/s' })
|
|
foreignClient.postMessage = vi.fn()
|
|
scope.clients.matchAll.mockResolvedValue([foreignClient])
|
|
|
|
await dispatchAndWait('notificationclick', {
|
|
notification: {
|
|
close: vi.fn(),
|
|
data: { url: '/repos/1/sessions/s' },
|
|
},
|
|
})
|
|
|
|
expect(foreignClient.postMessage).not.toHaveBeenCalled()
|
|
expect(scope.clients.openWindow).toHaveBeenCalledWith('/repos/1/sessions/s')
|
|
})
|
|
|
|
it('falls back to "/" when notification data url is missing', async () => {
|
|
scope.clients.matchAll.mockResolvedValue([])
|
|
|
|
await dispatchAndWait('notificationclick', {
|
|
notification: {
|
|
close: vi.fn(),
|
|
data: {},
|
|
},
|
|
})
|
|
|
|
expect(scope.clients.openWindow).toHaveBeenCalledWith('/')
|
|
})
|
|
})
|
|
})
|