diff --git a/src/commands/thread/ask.ts b/src/commands/thread/ask.ts index 2eb97e9..c0109bd 100644 --- a/src/commands/thread/ask.ts +++ b/src/commands/thread/ask.ts @@ -59,7 +59,6 @@ export const threadAskCommand: Command = { ...(name ? { name } : {}), ...(context.length > 0 ? { context } : {}), }); - await api.messagesPost({ workspaceId, threadId: thread.id, prompt }); const url = await threadConsoleUrl(config, api, thread); if (noWait) { diff --git a/test/thread-ask.test.ts b/test/thread-ask.test.ts new file mode 100644 index 0000000..8de2d53 --- /dev/null +++ b/test/thread-ask.test.ts @@ -0,0 +1,53 @@ +import { after, before, beforeEach, describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { threadAskCommand } from '../src/commands/thread/ask'; +import { mockConfig } from './helpers/config'; + +type Sent = { method: string; url: string; body: unknown }; + +const thread = { + id: 'thrd_1', + workspaceId: 'ws_1', + name: '', + _html_url: 'https://console.example.test/acme/threads/thrd_1', +}; + +describe('thread ask', () => { + const originalFetch = globalThis.fetch; + const originalWrite = process.stdout.write; + let sent: Sent[] = []; + + before(() => { + process.stdout.write = ((): boolean => true) as typeof process.stdout.write; + }); + + beforeEach(() => { + delete process.env.POLYLANE_API_KEY; + sent = []; + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + sent.push({ method: String(init?.method ?? 'GET'), url, body: init?.body ? JSON.parse(String(init.body)) : undefined }); + if (url.endsWith('/v1/threads')) { + return Response.json({ success: true, error: null, result: thread }, { status: 201 }); + } + return Response.json({ success: true, error: null, result: {} }, { status: 202 }); + }) as typeof fetch; + }); + + after(() => { + globalThis.fetch = originalFetch; + process.stdout.write = originalWrite; + }); + + it('sends the prompt once, inside the thread create call', async () => { + const config = mockConfig({ apiKey: 'test-key', apiKeySource: 'flag', workspaceId: 'ws_1' }); + await threadAskCommand.execute(config, {}, { _: ['hello', 'there'], noWait: true }); + assert.deepEqual(sent, [ + { + method: 'POST', + url: 'https://api.example.test/v1/threads', + body: { workspaceId: 'ws_1', prompt: 'hello there', visibility: 'workspace' }, + }, + ]); + }); +});