Repository navigation
feat(index): add a search command #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a9b639
feat(index): add a search command
KrisOei 2e4540c
refactor(index): move the endpoint to /search/gov
PhantomInTheWire 7dd360b
fix(index): exit non-zero on an unsuccessful response
PhantomInTheWire 1e3b2fc
docs: link the REST reference from the search command
PhantomInTheWire 8b1a8ab
feat(search): accept the gov category
PhantomInTheWire e159692
test: drop redundant cases
PhantomInTheWire 4f59463
test: trim remaining duplicate cases
PhantomInTheWire dc4584a
docs: rename the index
PhantomInTheWire c94e8fb
refactor: rename to gov
PhantomInTheWire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /** | ||
| * Tests for legal-regulatory command | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { handleLegalRegulatorySearchCommand } from '../../commands/legal-regulatory'; | ||
| import { getClient } from '../../utils/client'; | ||
| import { initializeConfig } from '../../utils/config'; | ||
| import { writeOutput } from '../../utils/output'; | ||
| import { setupTest, teardownTest } from '../utils/mock-client'; | ||
|
|
||
| vi.mock('../../utils/output', () => ({ writeOutput: vi.fn() })); | ||
|
|
||
| vi.mock('../../utils/client', async () => { | ||
| const actual = await vi.importActual('../../utils/client'); | ||
| return { | ||
| ...actual, | ||
| getClient: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('handleLegalRegulatorySearchCommand', () => { | ||
| let mockHttpGet: ReturnType<typeof vi.fn>; | ||
|
|
||
| // Wrap a payload in the axios envelope returned by `client.http.get`. | ||
| const mockLegalRegulatoryResponse = (web: any[]) => ({ | ||
| data: { success: true, data: { web } }, | ||
| }); | ||
|
|
||
| const sampleResult = { | ||
| url: 'https://www.ecfr.gov/current/title-21/chapter-I/subchapter-B/part-101', | ||
| title: '21 CFR Part 101 -- Food Labeling', | ||
| description: 'Food labeling requirements for packaged foods.', | ||
| position: 1, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| setupTest(); | ||
| initializeConfig({ | ||
| apiKey: 'test-api-key', | ||
| apiUrl: 'https://api.firecrawl.dev', | ||
| }); | ||
|
|
||
| mockHttpGet = vi.fn(); | ||
| vi.mocked(getClient).mockReturnValue({ | ||
| http: { get: mockHttpGet }, | ||
| } as any); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| teardownTest(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('API call generation', () => { | ||
| it('calls /v2/search/legal-regulatory with the query', async () => { | ||
| mockHttpGet.mockResolvedValue( | ||
| mockLegalRegulatoryResponse([sampleResult]) | ||
| ); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ query: 'food labeling' }); | ||
|
|
||
| expect(mockHttpGet).toHaveBeenCalledTimes(1); | ||
| expect(mockHttpGet).toHaveBeenCalledWith( | ||
| '/v2/search/legal-regulatory?query=food+labeling&integration=cli' | ||
| ); | ||
| }); | ||
|
|
||
| it('passes k when a result count is provided', async () => { | ||
| mockHttpGet.mockResolvedValue( | ||
| mockLegalRegulatoryResponse([sampleResult]) | ||
| ); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ | ||
| query: 'food labeling', | ||
| k: 5, | ||
| }); | ||
|
|
||
| expect(mockHttpGet).toHaveBeenCalledWith( | ||
| '/v2/search/legal-regulatory?query=food+labeling&k=5&integration=cli' | ||
| ); | ||
| }); | ||
|
|
||
| it('passes apiUrl and apiKey to getClient when provided', async () => { | ||
| mockHttpGet.mockResolvedValue(mockLegalRegulatoryResponse([])); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ | ||
| query: 'test', | ||
| apiKey: 'other-key', | ||
| apiUrl: 'http://localhost:3002', | ||
| }); | ||
|
|
||
| expect(getClient).toHaveBeenCalledWith({ | ||
| apiKey: 'other-key', | ||
| apiUrl: 'http://localhost:3002', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('output', () => { | ||
| it('renders numbered title, url, and description blocks', async () => { | ||
| mockHttpGet.mockResolvedValue( | ||
| mockLegalRegulatoryResponse([ | ||
| sampleResult, | ||
| { | ||
| url: 'https://www.ecfr.gov/current/title-21/part-102', | ||
| title: '21 CFR Part 102', | ||
| position: 2, | ||
| }, | ||
| ]) | ||
| ); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ query: 'food labeling' }); | ||
|
|
||
| const [content] = vi.mocked(writeOutput).mock.calls[0]; | ||
| expect(content).toBe( | ||
| [ | ||
| '## 1. 21 CFR Part 101 -- Food Labeling', | ||
| sampleResult.url, | ||
| 'Food labeling requirements for packaged foods.', | ||
| '', | ||
| '## 2. 21 CFR Part 102', | ||
| 'https://www.ecfr.gov/current/title-21/part-102', | ||
| ].join('\n') | ||
| ); | ||
| }); | ||
|
|
||
| it('prints a placeholder when there are no results', async () => { | ||
| mockHttpGet.mockResolvedValue(mockLegalRegulatoryResponse([])); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ query: 'no hits' }); | ||
|
|
||
| const [content] = vi.mocked(writeOutput).mock.calls[0]; | ||
| expect(content).toBe('(no results)'); | ||
| }); | ||
|
|
||
| it('tolerates a success response that omits data', async () => { | ||
| mockHttpGet.mockResolvedValue({ data: { success: true } }); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ query: 'no data field' }); | ||
|
|
||
| const [content] = vi.mocked(writeOutput).mock.calls[0]; | ||
| expect(content).toBe('(no results)'); | ||
| }); | ||
|
|
||
| it('outputs the raw response as JSON with --json', async () => { | ||
| mockHttpGet.mockResolvedValue( | ||
| mockLegalRegulatoryResponse([sampleResult]) | ||
| ); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ | ||
| query: 'food labeling', | ||
| json: true, | ||
| }); | ||
|
|
||
| const [content] = vi.mocked(writeOutput).mock.calls[0] as [string]; | ||
| expect(JSON.parse(content)).toEqual({ | ||
| success: true, | ||
| data: { web: [sampleResult] }, | ||
| }); | ||
| }); | ||
|
|
||
| it('writes to the output file with -o', async () => { | ||
| mockHttpGet.mockResolvedValue( | ||
| mockLegalRegulatoryResponse([sampleResult]) | ||
| ); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ | ||
| query: 'food labeling', | ||
| output: 'results.md', | ||
| }); | ||
|
|
||
| expect(writeOutput).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| 'results.md', | ||
| true | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('error handling', () => { | ||
| it('exits with code 1 when the request fails', async () => { | ||
| mockHttpGet.mockRejectedValue(new Error('boom')); | ||
| const exitSpy = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => undefined) as any); | ||
| const errorSpy = vi | ||
| .spyOn(console, 'error') | ||
| .mockImplementation(() => undefined); | ||
|
|
||
| await handleLegalRegulatorySearchCommand({ query: 'test' }); | ||
|
|
||
| expect(errorSpy).toHaveBeenCalledWith('Error:', 'boom'); | ||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
|
|
||
| exitSpy.mockRestore(); | ||
| errorSpy.mockRestore(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { getClient, isKeylessMode, keylessGet } from '../utils/client'; | ||
| import { writeOutput } from '../utils/output'; | ||
| import type { | ||
| LegalRegulatoryResult, | ||
| LegalRegulatorySearchOptions, | ||
| LegalRegulatorySearchResponse, | ||
| } from '../types/legal-regulatory'; | ||
|
|
||
| const BASE = '/v2/search/legal-regulatory'; | ||
|
|
||
| async function getLegalRegulatory<T>( | ||
| path: string, | ||
| options: LegalRegulatorySearchOptions | ||
| ): Promise<T> { | ||
| const url = `${path}${path.includes('?') ? '&' : '?'}integration=cli`; | ||
|
|
||
| if (isKeylessMode(options.apiKey, options.apiUrl)) { | ||
| return (await keylessGet(url)) as T; | ||
| } | ||
|
|
||
| const app = getClient({ apiKey: options.apiKey, apiUrl: options.apiUrl }); | ||
| const response = await (app as any).http.get(url); | ||
| return (response?.data ?? {}) as T; | ||
| } | ||
|
|
||
| function fmtResult(item: LegalRegulatoryResult, index: number): string { | ||
| const lines = [ | ||
| `## ${item.position ?? index + 1}. ${item.title ?? '(untitled)'}`, | ||
| item.url, | ||
| ]; | ||
| if (item.description) lines.push(item.description); | ||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| function fmtLegalRegulatory(data: LegalRegulatorySearchResponse): string { | ||
| const results = data.data?.web ?? []; | ||
| if (results.length === 0) return '(no results)'; | ||
| return results.map(fmtResult).join('\n\n'); | ||
| } | ||
|
|
||
| function writeLegalRegulatoryOutput( | ||
| data: LegalRegulatorySearchResponse, | ||
| readable: string, | ||
| options: LegalRegulatorySearchOptions | ||
| ): void { | ||
| const content = | ||
| options.json || options.pretty | ||
| ? options.pretty | ||
| ? JSON.stringify(data, null, 2) | ||
| : JSON.stringify(data) | ||
| : readable; | ||
| writeOutput(content, options.output, !!options.output); | ||
| } | ||
|
|
||
| function handleError(error: unknown): never { | ||
| console.error( | ||
| 'Error:', | ||
| error instanceof Error ? error.message : 'Unknown error occurred' | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| export async function handleLegalRegulatorySearchCommand( | ||
| options: LegalRegulatorySearchOptions | ||
| ): Promise<void> { | ||
| try { | ||
| const params = new URLSearchParams(); | ||
| params.append('query', options.query); | ||
| if (options.k != null) params.append('k', String(options.k)); | ||
| const data = await getLegalRegulatory<LegalRegulatorySearchResponse>( | ||
| `${BASE}?${params.toString()}`, | ||
| options | ||
| ); | ||
| writeLegalRegulatoryOutput(data, fmtLegalRegulatory(data), options); | ||
| } catch (error) { | ||
| handleError(error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.