Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/tui/workspace/server/agent-actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect, it, vi } from 'vitest'
import type { ActionContext } from '../action-types.js'
import { createWorkspace, type WorkspaceState } from '../state.js'
import type { WorkspaceController } from '../controller.js'
import { agentAction } from './agent-actions.js'

function context(s: WorkspaceState, models: () => Promise<string[]>) {
const ctx: ActionContext = {
get: () => s,
update: (fn) => {
s = fn(s)
},
controller: { models: vi.fn(models) } as unknown as WorkspaceController,
} as ActionContext
return { ctx, state: () => s }
}

/** A finished setup with the API serving but no model loaded yet. */
function servingWithoutModel() {
const s = createWorkspace('server')
s.setupComplete = true
s.firstRun = null
s.base = {
...s.base,
daemon: {
kind: 'up',
record: undefined,
pending: [],
snapshot: { server: { running: true, host: '127.0.0.1', port: 8080, prefix: '/v1' } },
} as unknown as WorkspaceState['base']['daemon'],
}
return s
}

// An empty but successful model list is a precondition: the refusal stays a message in the workspace.
it('refuses to open an agent when no model is loaded without throwing', async () => {
const s = servingWithoutModel()
const { ctx, state } = context(s, async () => [])
await expect(agentAction('agent:claude', ctx)).resolves.toBe(true)
expect(state().message).toBe('Load a text model in Models before opening an agent.')
expect(state().busy).toBe(false)
expect(state().overlay).toBeNull()
})

// A real failure of the lookup still propagates to the reporting boundary.
it('keeps a failed model lookup reported instead of turning it into a refusal', async () => {
const s = servingWithoutModel()
const { ctx, state } = context(s, async () => {
throw new Error('Model list returned HTTP 503. Check the endpoint and API key.')
})
await expect(agentAction('agent:claude', ctx)).rejects.toThrow('Model list returned HTTP 503')
expect(state().message).not.toBe('Load a text model in Models before opening an agent.')
expect(state().busy).toBe(false)
})
5 changes: 4 additions & 1 deletion src/tui/workspace/server/agent-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export async function agentAction(id: string, ctx: ActionContext): Promise<boole
patch({ busy: true, message: 'Finding a model for this agent...' })
try {
const models = await controller.models(serverEndpoint(s), true)
if (!models.length) throw new Error('Load a text model in Models before opening an agent.')
if (!models.length) {
patch({ message: 'Load a text model in Models before opening an agent.' })
return true
}
const agent = id.slice(6)
const preferred =
s.connection?.step === 'done' && s.connection.agent === agent ? s.connection.model : s.model
Expand Down
Loading