diff --git a/backend/src/services/opencode-server-registry.test.ts b/backend/src/services/opencode-server-registry.test.ts index 8eaede39..168dcb81 100644 --- a/backend/src/services/opencode-server-registry.test.ts +++ b/backend/src/services/opencode-server-registry.test.ts @@ -487,6 +487,76 @@ describe('OpenCodeServerRegistry', () => { expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:super-secret')}`) }) + it('reuses the stored password for a draft on the same origin', async () => { + const server = await createServer({ + name: 'Studio', + baseUrl: 'https://studio.example.com', + password: 'super-secret', + }) + fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' })) + + const result = await registry.test({ + baseUrl: 'https://studio.example.com/opencode', + username: 'alice', + serverId: server.id, + }) + + expect(result.ok).toBe(true) + expect(fetchFn.mock.calls[0]?.[0]).toBe('https://studio.example.com/opencode/api/info') + const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers) + expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:super-secret')}`) + }) + + it('does not send the stored password to a draft on a different origin', async () => { + const server = await createServer({ + name: 'Studio', + baseUrl: 'https://studio.example.com', + password: 'super-secret', + }) + fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' })) + + const result = await registry.test({ + baseUrl: 'https://relocated.example.com', + username: 'alice', + serverId: server.id, + }) + + expect(result.ok).toBe(true) + expect(fetchFn.mock.calls[0]?.[0]).toBe('https://relocated.example.com/api/info') + const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers) + expect(headers.get('authorization')).toBeNull() + }) + + it('prefers an explicit draft password over the stored one', async () => { + const server = await createServer({ + name: 'Studio', + baseUrl: 'https://studio.example.com', + password: 'super-secret', + }) + fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' })) + + await registry.test({ + baseUrl: 'https://studio.example.com', + username: 'alice', + password: 'draft-secret', + serverId: server.id, + }) + + const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers) + expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:draft-secret')}`) + }) + + it('throws a not-found error for an unknown server id on a draft', async () => { + await expect( + registry.test({ + baseUrl: 'https://studio.example.com', + username: 'opencode', + serverId: 'srv_missing', + }), + ).rejects.toBeInstanceOf(OpenCodeServerNotFoundError) + expect(fetchFn).not.toHaveBeenCalled() + }) + it('reports a failed request without throwing', async () => { fetchFn.mockResolvedValueOnce(new Response(null, { status: 500 })) diff --git a/backend/src/services/opencode-server-registry.ts b/backend/src/services/opencode-server-registry.ts index 5e4f4026..9c6f27f2 100644 --- a/backend/src/services/opencode-server-registry.ts +++ b/backend/src/services/opencode-server-registry.ts @@ -249,7 +249,7 @@ export class OpenCodeServerRegistry { } private resolveTestTarget(input: TestServerSource): TestTarget { - if ('serverId' in input) { + if (!('baseUrl' in input)) { const server = this.requireServer(input.serverId) return { baseUrl: server.baseUrl, @@ -258,10 +258,22 @@ export class OpenCodeServerRegistry { } } + const baseUrl = trimUrlTrailingSlashes(input.baseUrl) + if (input.password !== undefined || input.serverId === undefined) { + return { + baseUrl, + username: input.username, + password: input.password ?? null, + } + } + + const server = this.requireServer(input.serverId) return { - baseUrl: trimUrlTrailingSlashes(input.baseUrl), + baseUrl, username: input.username, - password: input.password ?? null, + password: haveSameUrlOrigin(server.baseUrl, baseUrl) + ? readOpenCodeServerPassword(this.db, input.serverId) + : null, } } diff --git a/backend/test/routes/opencode-servers.test.ts b/backend/test/routes/opencode-servers.test.ts index f2beac30..7a2d2899 100644 --- a/backend/test/routes/opencode-servers.test.ts +++ b/backend/test/routes/opencode-servers.test.ts @@ -213,6 +213,36 @@ describe('opencode server routes', () => { expect(registry.test).toHaveBeenCalledWith({ baseUrl: 'https://studio.example.com', username: 'opencode' }) }) + it('tests a draft server against a saved server id', 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/opencode', serverId: 'srv_1' }), + }) + + expect(response.status).toBe(200) + expect(registry.test).toHaveBeenCalledWith({ + baseUrl: 'https://studio.example.com/opencode', + username: 'opencode', + serverId: 'srv_1', + }) + }) + + it('returns 404 when testing a draft against an unknown server id', async () => { + registry.test.mockRejectedValue(new OpenCodeServerNotFoundError('srv_missing')) + + const response = await app.request('/test', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ baseUrl: 'https://studio.example.com', serverId: 'srv_missing' }), + }) + + expect(response.status).toBe(404) + expect(await response.json()).toEqual({ error: 'OpenCode server not found: srv_missing' }) + }) + 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( diff --git a/frontend/src/components/settings/OpenCodeServersSettings.test.tsx b/frontend/src/components/settings/OpenCodeServersSettings.test.tsx index 3a67adf3..4cf0d3f9 100644 --- a/frontend/src/components/settings/OpenCodeServersSettings.test.tsx +++ b/frontend/src/components/settings/OpenCodeServersSettings.test.tsx @@ -67,16 +67,21 @@ describe('OpenCodeServersSettings', () => { mocks.testByIdAsync.mockResolvedValue(testResult) }) - it('tests the saved server when the edited target and credentials are unchanged', async () => { + it('tests the draft with the saved server id when the password is left blank', async () => { const user = await openEditDialog() await user.click(screen.getByRole('button', { name: 'Test connection' })) - expect(mocks.testByIdAsync).toHaveBeenCalledWith('srv_1') - expect(mocks.testAsync).not.toHaveBeenCalled() + expect(mocks.testAsync).toHaveBeenCalledWith({ + baseUrl: 'https://office.example.com', + username: 'opencode', + password: undefined, + serverId: 'srv_1', + }) + expect(mocks.testByIdAsync).not.toHaveBeenCalled() }) - it('tests the edited address when the base URL changes', async () => { + it('tests the edited address with the saved server id', async () => { const user = await openEditDialog() await user.clear(screen.getByLabelText('URL')) @@ -87,11 +92,12 @@ describe('OpenCodeServersSettings', () => { baseUrl: 'https://relocated.example.com', username: 'opencode', password: undefined, + serverId: 'srv_1', }) expect(mocks.testByIdAsync).not.toHaveBeenCalled() }) - it('tests the edited address when the stored password is being cleared', async () => { + it('tests the edited address without the saved server id when the stored password is being cleared', async () => { const user = await openEditDialog() await user.click(screen.getByLabelText('Clear stored password')) @@ -101,6 +107,7 @@ describe('OpenCodeServersSettings', () => { baseUrl: 'https://office.example.com', username: 'opencode', password: undefined, + serverId: undefined, }) expect(mocks.testByIdAsync).not.toHaveBeenCalled() }) diff --git a/frontend/src/components/settings/OpenCodeServersSettings.tsx b/frontend/src/components/settings/OpenCodeServersSettings.tsx index c0ef74cf..e0084ab3 100644 --- a/frontend/src/components/settings/OpenCodeServersSettings.tsx +++ b/frontend/src/components/settings/OpenCodeServersSettings.tsx @@ -6,7 +6,6 @@ import type { OpenCodeServerTestResult, UpdateOpenCodeServerRequest, } from '@opencode-manager/shared/schemas' -import { trimUrlTrailingSlashes } from '@opencode-manager/shared/utils' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Switch } from '@/components/ui/switch' @@ -108,23 +107,13 @@ export function OpenCodeServersSettings() { } } - const handleTest = async (values: OpenCodeServerFormValues) => { - const testsSavedServer = - editingServer !== null && - trimUrlTrailingSlashes(values.baseUrl) === editingServer.baseUrl && - values.username === editingServer.username && - !values.password && - !values.clearPassword && - !values.pairing - if (editingServer && testsSavedServer) { - return testByIdAsync(editingServer.id) - } - return testAsync({ + const handleTest = async (values: OpenCodeServerFormValues) => + testAsync({ baseUrl: values.baseUrl, username: values.username, password: values.password || undefined, + serverId: editingServer && !values.password && !values.clearPassword ? editingServer.id : undefined, }) - } const toggleEnabled = async (server: OpenCodeServer, enabled: boolean) => { try { diff --git a/shared/src/schemas/opencode-servers.ts b/shared/src/schemas/opencode-servers.ts index c058d7bc..5072e0dd 100644 --- a/shared/src/schemas/opencode-servers.ts +++ b/shared/src/schemas/opencode-servers.ts @@ -53,6 +53,7 @@ export const TestOpenCodeServerRequestSchema = z.object({ baseUrl: HttpUrlSchema, username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_USERNAME), password: z.string().max(1024).optional(), + serverId: z.string().trim().min(1).optional(), }); export const OpenCodeServerTestResultSchema = z.object({