mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Switch the @opencode-manager/workspace-plugin adaptor from type local to type remote so opencode TUIs running on machines without filesystem access can use the manager's managed opencode server as their workspace backend. Phase 1 - Plugin adaptor: - Update target() to return type remote with manager URL and bearer auth - Export makeAdaptor for testing - Add tests for remote URL construction, slug encoding, and trailing slash Phase 2 - HTTP proxy route: - Add ALL /opencode/:slug/* catch-all proxy in workspace-plugin routes - Reuses existing proxyRequest helper with slug-to-directory translation - Rewrites inbound URL to /api/opencode/* shape for shared proxy logic - Includes bearer token scope validation and project service Phase 3 - WebSocket proxy: - Extract verifyPluginBearerAuth as named function for reuse - Attach second WebSocket proxy mount for /api/workspace-plugin/opencode prefix - Both /api/opencode and /api/workspace-plugin/opencode prefixes supported Phase 4 - Tests: - Add 8 backend route tests covering auth, proxy forwarding, and POST body - Add 3 plugin adaptor tests for remote target construction
110 lines
4.2 KiB
TypeScript
110 lines
4.2 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { PluginOptionsSchema } from '../src/types'
|
|
import { resolveConfig } from '../src/index'
|
|
import type { WorkspaceInfo } from '@opencode-ai/plugin'
|
|
|
|
describe('PluginOptionsSchema', () => {
|
|
it('accepts url from options', () => {
|
|
const parsed = PluginOptionsSchema.parse({ url: 'http://test.com' })
|
|
expect(parsed.url).toBe('http://test.com')
|
|
})
|
|
|
|
it('accepts token from options', () => {
|
|
const parsed = PluginOptionsSchema.parse({ token: 'test-token' })
|
|
expect(parsed.token).toBe('test-token')
|
|
})
|
|
|
|
it('requires token to be non-empty', () => {
|
|
expect(() => PluginOptionsSchema.parse({ token: '' })).toThrow()
|
|
})
|
|
|
|
it('rejects malformed url', () => {
|
|
expect(() => PluginOptionsSchema.parse({ url: 'not-a-url' })).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('resolveConfig', () => {
|
|
const ORIGINAL_URL = process.env.OPENCODE_MANAGER_URL
|
|
const ORIGINAL_TOKEN = process.env.OPENCODE_MANAGER_TOKEN
|
|
|
|
beforeEach(() => {
|
|
delete process.env.OPENCODE_MANAGER_URL
|
|
delete process.env.OPENCODE_MANAGER_TOKEN
|
|
})
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_URL === undefined) delete process.env.OPENCODE_MANAGER_URL
|
|
else process.env.OPENCODE_MANAGER_URL = ORIGINAL_URL
|
|
if (ORIGINAL_TOKEN === undefined) delete process.env.OPENCODE_MANAGER_TOKEN
|
|
else process.env.OPENCODE_MANAGER_TOKEN = ORIGINAL_TOKEN
|
|
})
|
|
|
|
it('uses options when provided', () => {
|
|
const cfg = resolveConfig({ url: 'http://options.com', token: 'options-token' })
|
|
expect(cfg).toEqual({ url: 'http://options.com', token: 'options-token' })
|
|
})
|
|
|
|
it('falls back to env vars when options omit fields', () => {
|
|
process.env.OPENCODE_MANAGER_URL = 'http://env.com'
|
|
process.env.OPENCODE_MANAGER_TOKEN = 'env-token'
|
|
|
|
const cfg = resolveConfig({})
|
|
expect(cfg).toEqual({ url: 'http://env.com', token: 'env-token' })
|
|
})
|
|
|
|
it('prefers options over env vars when both present', () => {
|
|
process.env.OPENCODE_MANAGER_URL = 'http://env.com'
|
|
process.env.OPENCODE_MANAGER_TOKEN = 'env-token'
|
|
|
|
const cfg = resolveConfig({ url: 'http://options.com', token: 'options-token' })
|
|
expect(cfg).toEqual({ url: 'http://options.com', token: 'options-token' })
|
|
})
|
|
|
|
it('throws when url missing from both options and env', () => {
|
|
process.env.OPENCODE_MANAGER_TOKEN = 'env-token'
|
|
expect(() => resolveConfig({})).toThrow(/missing `url`/)
|
|
})
|
|
|
|
it('throws when token missing from both options and env', () => {
|
|
process.env.OPENCODE_MANAGER_URL = 'http://env.com'
|
|
expect(() => resolveConfig({})).toThrow(/missing `token`/)
|
|
})
|
|
})
|
|
|
|
describe('adaptor.target', () => {
|
|
const sampleInfo: WorkspaceInfo = {
|
|
name: 'Test Workspace',
|
|
directory: '/test/dir',
|
|
extra: {},
|
|
}
|
|
|
|
it('returns { type: "remote", url, headers } with bearer auth when target is invoked', async () => {
|
|
const { makeAdaptor } = await import('../src/index.js')
|
|
const cfg = { url: 'http://manager.test:5003', token: 'tok-abc' }
|
|
const project = { slug: '42', name: 'Test Project', directory: '/workspace/repos/test' }
|
|
const adaptor = makeAdaptor(cfg, project)
|
|
const result = adaptor.target(sampleInfo)
|
|
expect(result.type).toBe('remote')
|
|
expect(result.url).toBe('http://manager.test:5003/api/workspace-plugin/opencode/42')
|
|
expect(result.headers).toEqual({ Authorization: 'Bearer tok-abc' })
|
|
})
|
|
|
|
it('url-encodes special slug characters', async () => {
|
|
const { makeAdaptor } = await import('../src/index.js')
|
|
const cfg = { url: 'http://manager.test:5003', token: 'tok-abc' }
|
|
const project = { slug: 'a/b c', name: 'Test Project', directory: '/workspace/repos/test' }
|
|
const adaptor = makeAdaptor(cfg, project)
|
|
const result = adaptor.target(sampleInfo)
|
|
expect(result.url).toContain('a%2Fb%20c')
|
|
})
|
|
|
|
it('strips trailing slash from manager url', async () => {
|
|
const { makeAdaptor } = await import('../src/index.js')
|
|
const cfg = { url: 'http://manager.test:5003/', token: 'tok-abc' }
|
|
const project = { slug: '42', name: 'Test Project', directory: '/workspace/repos/test' }
|
|
const adaptor = makeAdaptor(cfg, project)
|
|
const result = adaptor.target(sampleInfo)
|
|
expect(result.url).toBe('http://manager.test:5003/api/workspace-plugin/opencode/42')
|
|
})
|
|
})
|