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
124 changes: 124 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Normalize line endings for all text files
* text=auto

# Source code
*.py text diff=python
*.js text
*.ts text
*.jsx text
*.tsx text
*.json text
*.yaml text
*.yml text
*.toml text
*.ini text
*.cfg text

# Shell scripts (must use LF)
*.sh text eol=lf
quickstart.sh text eol=lf

# PowerShell scripts (Windows-friendly)
*.ps1 text eol=lf
*.psm1 text eol=lf

# Windows batch files (must use CRLF)
*.bat text eol=crlf
*.cmd text eol=crlf

# Documentation
*.md text
*.txt text
*.rst text
*.tex text

# Configuration files
.gitignore text
.gitattributes text
.editorconfig text
Dockerfile text
docker-compose.yml text
requirements*.txt text
pyproject.toml text
setup.py text
setup.cfg text
MANIFEST.in text
LICENSE text
README* text
CHANGELOG* text
CONTRIBUTING* text
CODE_OF_CONDUCT* text

# Web files
*.html text
*.css text
*.scss text
*.sass text

# Data files
*.xml text
*.csv text
*.sql text

# Graphics (binary)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.svg text
*.eps binary
*.bmp binary
*.tif binary
*.tiff binary

# Archives (binary)
*.zip binary
*.tar binary
*.gz binary
*.bz2 binary
*.7z binary
*.rar binary

# Python compiled (binary)
*.pyc binary
*.pyo binary
*.pyd binary
*.whl binary
*.egg binary

# System libraries (binary)
*.so binary
*.dll binary
*.dylib binary
*.lib binary
*.a binary

# Documents (binary)
*.pdf binary
*.doc binary
*.docx binary
*.ppt binary
*.pptx binary
*.xls binary
*.xlsx binary

# Fonts (binary)
*.ttf binary
*.otf binary
*.woff binary
*.woff2 binary
*.eot binary

# Audio/Video (binary)
*.mp3 binary
*.mp4 binary
*.wav binary
*.avi binary
*.mov binary
*.flv binary

# Database files (binary)
*.db binary
*.sqlite binary
*.sqlite3 binary
178 changes: 89 additions & 89 deletions src/tools/WebSearchTool/providers/brave.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
} from '../../../test/sharedMutationLock.js'

import { braveProvider } from './brave.ts'
const originalEnv = {
BRAVE_API_KEY: process.env.BRAVE_API_KEY,
}

const originalEnv = {
BRAVE_API_KEY: process.env.BRAVE_API_KEY,
}

const originalFetch = globalThis.fetch

beforeEach(async () => {
Expand All @@ -27,87 +27,87 @@ afterEach(() => {
releaseSharedMutationLock()
}
})
describe('braveProvider isConfigured', () => {
test('true when BRAVE_API_KEY is set', () => {
process.env.BRAVE_API_KEY = 'brv-test-key'
expect(braveProvider.isConfigured()).toBe(true)
})
test('false when BRAVE_API_KEY is missing', () => {
delete process.env.BRAVE_API_KEY
expect(braveProvider.isConfigured()).toBe(false)
})
})
describe('braveProvider search', () => {
beforeEach(() => {
process.env.BRAVE_API_KEY = 'brv-test-key'
})
test('sends bare token in X-Subscription-Token (no Bearer prefix)', async () => {
let capturedHeaders: Record<string, string> = {}
let capturedUrl = ''
globalThis.fetch = (async (input: any, init: any) => {
capturedUrl = typeof input === 'string' ? input : input.toString()
capturedHeaders = (init?.headers ?? {}) as Record<string, string>
return new Response(JSON.stringify({ web: { results: [] } }), { status: 200 })
}) as typeof fetch
await braveProvider.search({ query: 'hello' })
expect(capturedHeaders['X-Subscription-Token']).toBe('brv-test-key')
expect(capturedUrl).toContain('https://api.search.brave.com/res/v1/web/search')
expect(capturedUrl).toContain('q=hello')
})
test('maps web.results into SearchHit shape', async () => {
globalThis.fetch = (async (_input: any, _init: any) => new Response(JSON.stringify({
web: {
results: [
{ title: 'Example', url: 'https://example.com/a', description: 'snippet a' },
{ title: 'Other', url: 'https://other.com/b', description: 'snippet b' },
],
},
}), { status: 200 })) as typeof fetch
const out = await braveProvider.search({ query: 'hello' })
expect(out.providerName).toBe('brave')
expect(out.hits).toHaveLength(2)
expect(out.hits[0]).toEqual({
title: 'Example',
url: 'https://example.com/a',
description: 'snippet a',
source: 'example.com',
})
})
test('applies blocked_domains client-side', async () => {
globalThis.fetch = (async (_input: any, _init: any) => new Response(JSON.stringify({
web: {
results: [
{ title: 'Keep', url: 'https://keep.com/a', description: 'k' },
{ title: 'Drop', url: 'https://drop.com/b', description: 'd' },
],
},
}), { status: 200 })) as typeof fetch
const out = await braveProvider.search({ query: 'q', blocked_domains: ['drop.com'] })
expect(out.hits).toHaveLength(1)
expect(out.hits[0].url).toBe('https://keep.com/a')
})
test('throws on non-2xx response with status code', async () => {
globalThis.fetch = (async (_input: any, _init: any) =>
new Response('rate limited', { status: 429 })) as typeof fetch
await expect(braveProvider.search({ query: 'q' })).rejects.toThrow(/429/)
})
test('returns empty hits when web.results is missing', async () => {
globalThis.fetch = (async (_input: any, _init: any) =>
new Response(JSON.stringify({}), { status: 200 })) as typeof fetch
const out = await braveProvider.search({ query: 'q' })
expect(out.hits).toHaveLength(0)
})
})

describe('braveProvider isConfigured', () => {
test('true when BRAVE_API_KEY is set', () => {
process.env.BRAVE_API_KEY = 'brv-test-key'
expect(braveProvider.isConfigured()).toBe(true)
})

test('false when BRAVE_API_KEY is missing', () => {
delete process.env.BRAVE_API_KEY
expect(braveProvider.isConfigured()).toBe(false)
})
})

describe('braveProvider search', () => {
beforeEach(() => {
process.env.BRAVE_API_KEY = 'brv-test-key'
})

test('sends bare token in X-Subscription-Token (no Bearer prefix)', async () => {
let capturedHeaders: Record<string, string> = {}
let capturedUrl = ''
globalThis.fetch = (async (input: any, init: any) => {
capturedUrl = typeof input === 'string' ? input : input.toString()
capturedHeaders = (init?.headers ?? {}) as Record<string, string>
return new Response(JSON.stringify({ web: { results: [] } }), { status: 200 })
}) as typeof fetch

await braveProvider.search({ query: 'hello' })

expect(capturedHeaders['X-Subscription-Token']).toBe('brv-test-key')
expect(capturedUrl).toContain('https://api.search.brave.com/res/v1/web/search')
expect(capturedUrl).toContain('q=hello')
})

test('maps web.results into SearchHit shape', async () => {
globalThis.fetch = (async (_input: any, _init: any) => new Response(JSON.stringify({
web: {
results: [
{ title: 'Example', url: 'https://example.com/a', description: 'snippet a' },
{ title: 'Other', url: 'https://other.com/b', description: 'snippet b' },
],
},
}), { status: 200 })) as typeof fetch

const out = await braveProvider.search({ query: 'hello' })

expect(out.providerName).toBe('brave')
expect(out.hits).toHaveLength(2)
expect(out.hits[0]).toEqual({
title: 'Example',
url: 'https://example.com/a',
description: 'snippet a',
source: 'example.com',
})
})

test('applies blocked_domains client-side', async () => {
globalThis.fetch = (async (_input: any, _init: any) => new Response(JSON.stringify({
web: {
results: [
{ title: 'Keep', url: 'https://keep.com/a', description: 'k' },
{ title: 'Drop', url: 'https://drop.com/b', description: 'd' },
],
},
}), { status: 200 })) as typeof fetch

const out = await braveProvider.search({ query: 'q', blocked_domains: ['drop.com'] })
expect(out.hits).toHaveLength(1)
expect(out.hits[0].url).toBe('https://keep.com/a')
})

test('throws on non-2xx response with status code', async () => {
globalThis.fetch = (async (_input: any, _init: any) =>
new Response('rate limited', { status: 429 })) as typeof fetch
await expect(braveProvider.search({ query: 'q' })).rejects.toThrow(/429/)
})

test('returns empty hits when web.results is missing', async () => {
globalThis.fetch = (async (_input: any, _init: any) =>
new Response(JSON.stringify({}), { status: 200 })) as typeof fetch
const out = await braveProvider.search({ query: 'q' })
expect(out.hits).toHaveLength(0)
})
})
Loading