mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Reject a server address change to a new origin unless a password or pairing is supplied, so the stored password is never sent to another host. Test connection now checks the edited values and warns when an origin change needs credentials. Replay and reconcile remote session statuses on stream reconnect and browser resync so linked-server sessions no longer stay busy. Treat malformed server session URLs as not found instead of throwing.
229 lines
7.1 KiB
TypeScript
229 lines
7.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { createOpenCodeServerRoutes } from '../../src/routes/opencode-servers'
|
|
import {
|
|
OpenCodeServerCredentialsRequiredError,
|
|
OpenCodeServerNotFoundError,
|
|
OpenCodeServerPairingError,
|
|
type OpenCodeServerRegistry,
|
|
} from '../../src/services/opencode-server-registry'
|
|
|
|
const SERVER = {
|
|
id: 'srv_1',
|
|
name: 'Studio',
|
|
baseUrl: 'https://studio.example.com',
|
|
username: 'opencode',
|
|
enabled: true,
|
|
hasPassword: true,
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
}
|
|
|
|
const TEST_RESULT = { ok: true, version: '2.0.15', versionSupported: true, error: null }
|
|
|
|
describe('opencode server routes', () => {
|
|
const registry = {
|
|
list: vi.fn(),
|
|
get: vi.fn(),
|
|
create: vi.fn(),
|
|
update: vi.fn(),
|
|
delete: vi.fn(),
|
|
test: vi.fn(),
|
|
}
|
|
let app: ReturnType<typeof createOpenCodeServerRoutes>
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
registry.list.mockReturnValue([])
|
|
registry.get.mockReturnValue(null)
|
|
app = createOpenCodeServerRoutes(registry as unknown as OpenCodeServerRegistry, () => [])
|
|
})
|
|
|
|
it('lists servers', async () => {
|
|
registry.list.mockReturnValue([SERVER])
|
|
|
|
const response = await app.request('/')
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ servers: [SERVER] })
|
|
})
|
|
|
|
it('returns 404 for an unknown server id', async () => {
|
|
const response = await app.request('/srv_missing')
|
|
|
|
expect(response.status).toBe(404)
|
|
})
|
|
|
|
it('returns the server for a known id', async () => {
|
|
registry.get.mockReturnValue(SERVER)
|
|
|
|
const response = await app.request('/srv_1')
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ server: SERVER })
|
|
})
|
|
|
|
it('returns 400 for an invalid create body', async () => {
|
|
const response = await app.request('/', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'Studio' }),
|
|
})
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(registry.create).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 400 for an invalid draft test body', async () => {
|
|
const response = await app.request('/test', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ baseUrl: 'ftp://studio.example.com' }),
|
|
})
|
|
|
|
expect(response.status).toBe(400)
|
|
})
|
|
|
|
it('returns 400 for an invalid update body', async () => {
|
|
const response = await app.request('/srv_1', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: '' }),
|
|
})
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(registry.update).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('creates a server and returns its test result', async () => {
|
|
registry.create.mockResolvedValue(SERVER)
|
|
registry.test.mockResolvedValue(TEST_RESULT)
|
|
|
|
const response = await app.request('/', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'Studio', baseUrl: 'https://studio.example.com', password: 'super-secret' }),
|
|
})
|
|
|
|
expect(response.status).toBe(201)
|
|
expect(await response.json()).toEqual({ server: SERVER, test: TEST_RESULT })
|
|
expect(registry.create).toHaveBeenCalledWith({
|
|
name: 'Studio',
|
|
baseUrl: 'https://studio.example.com',
|
|
username: 'opencode',
|
|
password: 'super-secret',
|
|
enabled: true,
|
|
})
|
|
expect(registry.test).toHaveBeenCalledWith({ serverId: 'srv_1' })
|
|
})
|
|
|
|
it('returns 502 when pairing fails', async () => {
|
|
registry.create.mockRejectedValue(new OpenCodeServerPairingError('Pairing was rejected with status 403'))
|
|
|
|
const response = await app.request('/', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name: 'Studio',
|
|
baseUrl: 'https://studio.example.com',
|
|
pairing: { pairUrl: 'https://studio.example.com/pair', code: '0123456789abcdef' },
|
|
}),
|
|
})
|
|
|
|
expect(response.status).toBe(502)
|
|
})
|
|
|
|
it('updates a server', async () => {
|
|
registry.update.mockResolvedValue({ ...SERVER, name: 'Renamed' })
|
|
|
|
const response = await app.request('/srv_1', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'Renamed' }),
|
|
})
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(registry.update).toHaveBeenCalledWith('srv_1', { name: 'Renamed' })
|
|
})
|
|
|
|
it('returns 400 when an origin change would reuse the stored password', async () => {
|
|
registry.update.mockRejectedValue(new OpenCodeServerCredentialsRequiredError())
|
|
|
|
const response = await app.request('/srv_1', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ baseUrl: 'https://other-host.example.com' }),
|
|
})
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(await response.json()).toEqual({
|
|
error: 'Changing the server address requires re-entering the password or pairing again',
|
|
})
|
|
})
|
|
|
|
it('returns 404 when updating an unknown server', async () => {
|
|
registry.update.mockRejectedValue(new OpenCodeServerNotFoundError('srv_missing'))
|
|
|
|
const response = await app.request('/srv_missing', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'Renamed' }),
|
|
})
|
|
|
|
expect(response.status).toBe(404)
|
|
})
|
|
|
|
it('deletes a server', async () => {
|
|
const response = await app.request('/srv_1', { method: 'DELETE' })
|
|
|
|
expect(response.status).toBe(204)
|
|
expect(registry.delete).toHaveBeenCalledWith('srv_1')
|
|
})
|
|
|
|
it('returns 404 when deleting an unknown server', async () => {
|
|
registry.delete.mockImplementation(() => {
|
|
throw new OpenCodeServerNotFoundError('srv_missing')
|
|
})
|
|
|
|
const response = await app.request('/srv_missing', { method: 'DELETE' })
|
|
|
|
expect(response.status).toBe(404)
|
|
})
|
|
|
|
it('tests a stored server', async () => {
|
|
registry.test.mockResolvedValue(TEST_RESULT)
|
|
|
|
const response = await app.request('/srv_1/test', { method: 'POST' })
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual(TEST_RESULT)
|
|
expect(registry.test).toHaveBeenCalledWith({ serverId: 'srv_1' })
|
|
})
|
|
|
|
it('tests a draft server', async () => {
|
|
registry.test.mockResolvedValue(TEST_RESULT)
|
|
|
|
const response = await app.request('/test', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ baseUrl: 'https://studio.example.com' }),
|
|
})
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual(TEST_RESULT)
|
|
expect(registry.test).toHaveBeenCalledWith({ baseUrl: 'https://studio.example.com', username: 'opencode' })
|
|
})
|
|
|
|
it('returns the remote event statuses from the provider', async () => {
|
|
const statuses = [{ serverId: 'srv_1', connected: true, version: '2.0.15', error: null }]
|
|
const statusApp = createOpenCodeServerRoutes(
|
|
registry as unknown as OpenCodeServerRegistry,
|
|
() => statuses,
|
|
)
|
|
|
|
const response = await statusApp.request('/status')
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ statuses })
|
|
})
|
|
})
|