|
@@ -15,6 +15,12 @@ import * as deepseekPlugin from '@deepseek-ai/dsh-web-search-deepseek'
|
|
|
import { citationSnippets, mapAnthropicResponse } from '../src/provider.ts'
|
|
import { citationSnippets, mapAnthropicResponse } from '../src/provider.ts'
|
|
|
import type { AnthropicResponse } from '@deepseek-ai/dsh-web-search-deepseek/src/types.ts'
|
|
import type { AnthropicResponse } from '@deepseek-ai/dsh-web-search-deepseek/src/types.ts'
|
|
|
|
|
|
|
|
|
|
+/** Construct the provider over a fixed options value; production passes a live thunk. */
|
|
|
|
|
+import type { DeepSeekSearchProviderOptions } from '@deepseek-ai/dsh-web-search-deepseek'
|
|
|
|
|
+
|
|
|
|
|
+const searchProvider = (options: DeepSeekSearchProviderOptions): DeepSeekSearchProvider =>
|
|
|
|
|
+ new DeepSeekSearchProvider(() => options)
|
|
|
|
|
+
|
|
|
const options = {
|
|
const options = {
|
|
|
apiKey: 'ds-key',
|
|
apiKey: 'ds-key',
|
|
|
baseURL: 'https://api.deepseek.test/anthropic/v1',
|
|
baseURL: 'https://api.deepseek.test/anthropic/v1',
|
|
@@ -142,21 +148,21 @@ describe('mapAnthropicResponse', () => {
|
|
|
|
|
|
|
|
describe('DeepSeekSearchProvider availability', () => {
|
|
describe('DeepSeekSearchProvider availability', () => {
|
|
|
it('is unavailable without a key', () => {
|
|
it('is unavailable without a key', () => {
|
|
|
- expect(new DeepSeekSearchProvider({ ...options, apiKey: '' }).available()).toBe(false)
|
|
|
|
|
|
|
+ expect(searchProvider({ ...options, apiKey: '' }).available()).toBe(false)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('is available with a key', () => {
|
|
it('is available with a key', () => {
|
|
|
- expect(new DeepSeekSearchProvider(options).available()).toBe(true)
|
|
|
|
|
|
|
+ expect(searchProvider(options).available()).toBe(true)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('is misconfigured when the base URL is unparseable', () => {
|
|
it('is misconfigured when the base URL is unparseable', () => {
|
|
|
- expect(new DeepSeekSearchProvider({ ...options, baseURL: 'not a url' }).available()).toBe(false)
|
|
|
|
|
|
|
+ expect(searchProvider({ ...options, baseURL: 'not a url' }).available()).toBe(false)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('is misconfigured when request limits are not positive integers', () => {
|
|
it('is misconfigured when request limits are not positive integers', () => {
|
|
|
- expect(new DeepSeekSearchProvider({ ...options, maxTokens: 0 }).available()).toBe(false)
|
|
|
|
|
- expect(new DeepSeekSearchProvider({ ...options, maxUses: 0 }).available()).toBe(false)
|
|
|
|
|
- expect(new DeepSeekSearchProvider({ ...options, maxUses: 1.5 }).available()).toBe(false)
|
|
|
|
|
|
|
+ expect(searchProvider({ ...options, maxTokens: 0 }).available()).toBe(false)
|
|
|
|
|
+ expect(searchProvider({ ...options, maxUses: 0 }).available()).toBe(false)
|
|
|
|
|
+ expect(searchProvider({ ...options, maxUses: 1.5 }).available()).toBe(false)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -165,7 +171,7 @@ describe('DeepSeekSearchProvider request mapping', () => {
|
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
|
const recordRequest = vi.fn()
|
|
const recordRequest = vi.fn()
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
- await new DeepSeekSearchProvider({ ...options, recordRequest }).search({ query: 'hello' })
|
|
|
|
|
|
|
+ await searchProvider({ ...options, recordRequest }).search({ query: 'hello' })
|
|
|
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
|
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
|
|
expect(url).toBe('https://api.deepseek.test/anthropic/v1/messages')
|
|
expect(url).toBe('https://api.deepseek.test/anthropic/v1/messages')
|
|
|
expect(init).toMatchObject({ method: 'POST', redirect: 'error' })
|
|
expect(init).toMatchObject({ method: 'POST', redirect: 'error' })
|
|
@@ -193,7 +199,7 @@ describe('DeepSeekSearchProvider request mapping', () => {
|
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
- await new DeepSeekSearchProvider(options).search({ query: 'q' }, controller.signal)
|
|
|
|
|
|
|
+ await searchProvider(options).search({ query: 'q' }, controller.signal)
|
|
|
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
|
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
|
|
expect(init.signal).toBe(controller.signal)
|
|
expect(init.signal).toBe(controller.signal)
|
|
|
})
|
|
})
|
|
@@ -207,7 +213,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
controller.abort(new Error('caller stopped'))
|
|
controller.abort(new Error('caller stopped'))
|
|
|
- await expect(new DeepSeekSearchProvider({
|
|
|
|
|
|
|
+ await expect(searchProvider({
|
|
|
...options,
|
|
...options,
|
|
|
apiKey: '',
|
|
apiKey: '',
|
|
|
resolveApiKey,
|
|
resolveApiKey,
|
|
@@ -225,7 +231,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
const fetchMock = vi.fn()
|
|
const fetchMock = vi.fn()
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
- const search = new DeepSeekSearchProvider({
|
|
|
|
|
|
|
+ const search = searchProvider({
|
|
|
...options,
|
|
...options,
|
|
|
apiKey: '',
|
|
apiKey: '',
|
|
|
resolveApiKey,
|
|
resolveApiKey,
|
|
@@ -242,7 +248,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
- await expect(new DeepSeekSearchProvider({
|
|
|
|
|
|
|
+ await expect(searchProvider({
|
|
|
...options,
|
|
...options,
|
|
|
apiKey: '',
|
|
apiKey: '',
|
|
|
resolveApiKey: async () => 'resolved-key',
|
|
resolveApiKey: async () => 'resolved-key',
|
|
@@ -253,7 +259,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
|
|
|
|
|
it('maps a credential resolver rejection under an active signal to WEB_PROVIDER_ERROR', async () => {
|
|
it('maps a credential resolver rejection under an active signal to WEB_PROVIDER_ERROR', async () => {
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
- await expect(new DeepSeekSearchProvider({
|
|
|
|
|
|
|
+ await expect(searchProvider({
|
|
|
...options,
|
|
...options,
|
|
|
apiKey: '',
|
|
apiKey: '',
|
|
|
resolveApiKey: () => Promise.reject(new Error('credential backend failed')),
|
|
resolveApiKey: () => Promise.reject(new Error('credential backend failed')),
|
|
@@ -265,7 +271,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('uses the default credential reference when no resolver is configured', async () => {
|
|
it('uses the default credential reference when no resolver is configured', async () => {
|
|
|
- await expect(new DeepSeekSearchProvider({ ...options, apiKey: '' }).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider({ ...options, apiKey: '' }).search({ query: 'q' }))
|
|
|
.rejects.toThrow('DeepSeek search has no API key for "DEEPSEEK_API_KEY"')
|
|
.rejects.toThrow('DeepSeek search has no API key for "DEEPSEEK_API_KEY"')
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -273,7 +279,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
const controller = new AbortController()
|
|
const controller = new AbortController()
|
|
|
const fetchMock = vi.fn()
|
|
const fetchMock = vi.fn()
|
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
- await expect(new DeepSeekSearchProvider({
|
|
|
|
|
|
|
+ await expect(searchProvider({
|
|
|
...options,
|
|
...options,
|
|
|
apiKey: '',
|
|
apiKey: '',
|
|
|
resolveApiKey: () => {
|
|
resolveApiKey: () => {
|
|
@@ -287,31 +293,31 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
|
|
|
|
|
it('maps an HTTP error to WEB_PROVIDER_ERROR with the provider message', async () => {
|
|
it('maps an HTTP error to WEB_PROVIDER_ERROR with the provider message', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: { message: 'rate limited' } }, { status: 429 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: { message: 'rate limited' } }, { status: 429 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR', message: 'rate limited' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR', message: 'rate limited' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('handles a string-form error body', async () => {
|
|
it('handles a string-form error body', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: 'bad request' }, { status: 400 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: 'bad request' }, { status: 400 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ message: 'bad request' }))
|
|
.rejects.toThrow(expect.objectContaining({ message: 'bad request' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('keeps a status-line message when the error body is not JSON', async () => {
|
|
it('keeps a status-line message when the error body is not JSON', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response('upstream error', { status: 503 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response('upstream error', { status: 503 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 503)' }))
|
|
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 503)' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('keeps the status-line message when the JSON error body carries no detail', async () => {
|
|
it('keeps the status-line message when the JSON error body carries no detail', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({}, { status: 500 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({}, { status: 500 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 500)' }))
|
|
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 500)' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('maps an abort to WEB_ABORTED', async () => {
|
|
it('maps an abort to WEB_ABORTED', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new DOMException('aborted', 'AbortError'))))
|
|
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new DOMException('aborted', 'AbortError'))))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -321,46 +327,46 @@ describe('DeepSeekSearchProvider error handling', () => {
|
|
|
await new Promise<Response>((_resolve, reject) => {
|
|
await new Promise<Response>((_resolve, reject) => {
|
|
|
init?.signal?.addEventListener('abort', () => { reject(new Error('custom abort reason')) }, { once: true })
|
|
init?.signal?.addEventListener('abort', () => { reject(new Error('custom abort reason')) }, { once: true })
|
|
|
})))
|
|
})))
|
|
|
- const search = new DeepSeekSearchProvider(options).search({ query: 'q' }, controller.signal)
|
|
|
|
|
|
|
+ const search = searchProvider(options).search({ query: 'q' }, controller.signal)
|
|
|
controller.abort(new Error('timeout reason'))
|
|
controller.abort(new Error('timeout reason'))
|
|
|
await expect(search).rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
await expect(search).rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('maps an unparseable success body to WEB_PROVIDER_ERROR', async () => {
|
|
it('maps an unparseable success body to WEB_PROVIDER_ERROR', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response('not json', { status: 200 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response('not json', { status: 200 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('maps a well-formed body of the wrong shape to WEB_PROVIDER_ERROR, not a raw TypeError', async () => {
|
|
it('maps a well-formed body of the wrong shape to WEB_PROVIDER_ERROR, not a raw TypeError', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: {} }, { status: 200 })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: {} }, { status: 200 })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('surfaces an abort during success-body parse as WEB_ABORTED', async () => {
|
|
it('surfaces an abort during success-body parse as WEB_ABORTED', async () => {
|
|
|
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: true, status: 200 }
|
|
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: true, status: 200 }
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
|
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('surfaces an abort during error-body parse as WEB_ABORTED', async () => {
|
|
it('surfaces an abort during error-body parse as WEB_ABORTED', async () => {
|
|
|
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: false, status: 500 }
|
|
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: false, status: 500 }
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
|
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('maps a network failure to WEB_PROVIDER_ERROR', async () => {
|
|
it('maps a network failure to WEB_PROVIDER_ERROR', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new TypeError('connection refused'))))
|
|
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new TypeError('connection refused'))))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('strict mode flows through search(): a prose-only response throws WEB_PROVIDER_ERROR', async () => {
|
|
it('strict mode flows through search(): a prose-only response throws WEB_PROVIDER_ERROR', async () => {
|
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: [{ type: 'text', text: 'no search happened' }] })))
|
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: [{ type: 'text', text: 'no search happened' }] })))
|
|
|
- await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
|
|
|
|
|
|
+ await expect(searchProvider(options).search({ query: 'q' }))
|
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|