|
| 1 | +import {describe, it, expect, beforeEach, vi} from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(()=>({ |
| 4 | + post: vi.fn(), |
| 5 | + get: vi.fn(), |
| 6 | + ensure_authenticated: vi.fn(), |
| 7 | + stop: vi.fn(), |
| 8 | + start: vi.fn(), |
| 9 | + print: vi.fn(), |
| 10 | + print_table: vi.fn(), |
| 11 | + fail: vi.fn((msg: string)=>{ throw new Error(`fail:${msg}`); }), |
| 12 | + dim: vi.fn((msg: string)=>msg), |
| 13 | + parse_timeout: vi.fn(), |
| 14 | + poll_until: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../../utils/client', ()=>({ |
| 18 | + post: mocks.post, |
| 19 | + get: mocks.get, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock('../../utils/auth', ()=>({ |
| 23 | + ensure_authenticated: mocks.ensure_authenticated, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('../../utils/spinner', ()=>({ |
| 27 | + start: mocks.start, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('../../utils/output', ()=>({ |
| 31 | + print: mocks.print, |
| 32 | + print_table: mocks.print_table, |
| 33 | + fail: mocks.fail, |
| 34 | + dim: mocks.dim, |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('../../utils/polling', ()=>({ |
| 38 | + parse_timeout: mocks.parse_timeout, |
| 39 | + poll_until: mocks.poll_until, |
| 40 | +})); |
| 41 | + |
| 42 | +import { |
| 43 | + handle_discover, |
| 44 | + build_request, |
| 45 | + extract_status, |
| 46 | + format_markdown, |
| 47 | + print_discover_table, |
| 48 | +} from '../../commands/discover'; |
| 49 | + |
| 50 | +describe('commands/discover', ()=>{ |
| 51 | + beforeEach(()=>{ |
| 52 | + vi.clearAllMocks(); |
| 53 | + mocks.ensure_authenticated.mockReturnValue('api_key'); |
| 54 | + mocks.parse_timeout.mockReturnValue(600); |
| 55 | + mocks.start.mockReturnValue({stop: mocks.stop}); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('build_request', ()=>{ |
| 59 | + it('builds minimal request with only query', ()=>{ |
| 60 | + const req = build_request('AI trends', {}); |
| 61 | + expect(req).toEqual({query: 'AI trends'}); |
| 62 | + }); |
| 63 | + |
| 64 | + it('includes all optional params', ()=>{ |
| 65 | + const req = build_request('AI trends', { |
| 66 | + intent: 'find research papers', |
| 67 | + city: 'New York', |
| 68 | + country: 'US', |
| 69 | + language: 'en', |
| 70 | + numResults: '10', |
| 71 | + filterKeywords: 'AI, machine learning', |
| 72 | + includeContent: true, |
| 73 | + startDate: '2025-01-01', |
| 74 | + endDate: '2025-12-31', |
| 75 | + }); |
| 76 | + expect(req).toEqual({ |
| 77 | + query: 'AI trends', |
| 78 | + intent: 'find research papers', |
| 79 | + city: 'New York', |
| 80 | + country: 'US', |
| 81 | + language: 'en', |
| 82 | + num_results: 10, |
| 83 | + filter_keywords: ['AI', 'machine learning'], |
| 84 | + include_content: true, |
| 85 | + start_date: '2025-01-01', |
| 86 | + end_date: '2025-12-31', |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('parses comma-separated filter keywords with whitespace', ()=>{ |
| 91 | + const req = build_request('q', {filterKeywords: ' a , b , c '}); |
| 92 | + expect(req.filter_keywords).toEqual(['a', 'b', 'c']); |
| 93 | + }); |
| 94 | + |
| 95 | + it('does not set format by default (API returns JSON)', ()=>{ |
| 96 | + const req = build_request('test', {}); |
| 97 | + expect(req.format).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('does not set format when include-content is used', ()=>{ |
| 101 | + const req = build_request('test', {includeContent: true}); |
| 102 | + expect(req.format).toBeUndefined(); |
| 103 | + expect(req.include_content).toBe(true); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('extract_status', ()=>{ |
| 108 | + it('returns status from valid response', ()=>{ |
| 109 | + expect(extract_status({status: 'processing'})).toBe('processing'); |
| 110 | + expect(extract_status({status: 'done'})).toBe('done'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns undefined for invalid input', ()=>{ |
| 114 | + expect(extract_status(null as never)).toBeUndefined(); |
| 115 | + expect(extract_status(undefined as never)).toBeUndefined(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('format_markdown', ()=>{ |
| 120 | + it('formats results as markdown', ()=>{ |
| 121 | + const md = format_markdown([ |
| 122 | + { |
| 123 | + link: 'https://example.com', |
| 124 | + title: 'Example', |
| 125 | + description: 'A description', |
| 126 | + relevance_score: 0.95, |
| 127 | + }, |
| 128 | + ], 'test query'); |
| 129 | + expect(md).toContain('# Discover results for "test query"'); |
| 130 | + expect(md).toContain('**1. [Example](https://example.com)** (95.0%)'); |
| 131 | + expect(md).toContain('A description'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('includes content when present', ()=>{ |
| 135 | + const md = format_markdown([ |
| 136 | + { |
| 137 | + link: 'https://example.com', |
| 138 | + title: 'Example', |
| 139 | + description: 'Desc', |
| 140 | + relevance_score: 0.5, |
| 141 | + content: '# Page content here', |
| 142 | + }, |
| 143 | + ], 'q'); |
| 144 | + expect(md).toContain('# Page content here'); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('print_discover_table', ()=>{ |
| 149 | + it('calls print_table with formatted rows', ()=>{ |
| 150 | + const results = [ |
| 151 | + { |
| 152 | + link: 'https://example.com', |
| 153 | + title: 'Example Title', |
| 154 | + description: 'Desc', |
| 155 | + relevance_score: 0.98184747, |
| 156 | + }, |
| 157 | + ]; |
| 158 | + print_discover_table(results); |
| 159 | + expect(mocks.print_table).toHaveBeenCalledWith( |
| 160 | + [{ |
| 161 | + '#': '1', |
| 162 | + title: 'Example Title', |
| 163 | + score: '98.2%', |
| 164 | + url: 'https://example.com', |
| 165 | + }], |
| 166 | + ['#', 'title', 'score', 'url'] |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('prints dim message when no results', ()=>{ |
| 171 | + const log = vi.spyOn(console, 'log').mockImplementation(()=>{}); |
| 172 | + print_discover_table([]); |
| 173 | + expect(log).toHaveBeenCalled(); |
| 174 | + expect(mocks.print_table).not.toHaveBeenCalled(); |
| 175 | + log.mockRestore(); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('handle_discover', ()=>{ |
| 180 | + it('triggers and polls then prints table', async()=>{ |
| 181 | + mocks.post.mockResolvedValue({status: 'ok', task_id: 'abc123'}); |
| 182 | + mocks.poll_until.mockResolvedValue({ |
| 183 | + result: { |
| 184 | + status: 'done', |
| 185 | + duration_seconds: 5, |
| 186 | + results: [ |
| 187 | + { |
| 188 | + link: 'https://example.com', |
| 189 | + title: 'Result', |
| 190 | + description: 'Desc', |
| 191 | + relevance_score: 0.9, |
| 192 | + }, |
| 193 | + ], |
| 194 | + }, |
| 195 | + attempts: 3, |
| 196 | + }); |
| 197 | + await handle_discover('AI trends', {}); |
| 198 | + expect(mocks.post).toHaveBeenCalledWith( |
| 199 | + 'api_key', |
| 200 | + '/discover', |
| 201 | + {query: 'AI trends'}, |
| 202 | + {timing: undefined} |
| 203 | + ); |
| 204 | + expect(mocks.poll_until).toHaveBeenCalledTimes(1); |
| 205 | + expect(mocks.print_table).toHaveBeenCalledTimes(1); |
| 206 | + }); |
| 207 | + |
| 208 | + it('prints json when --json is set', async()=>{ |
| 209 | + const response = { |
| 210 | + status: 'done', |
| 211 | + duration_seconds: 2, |
| 212 | + results: [{ |
| 213 | + link: 'https://example.com', |
| 214 | + title: 'R', |
| 215 | + description: 'D', |
| 216 | + relevance_score: 0.8, |
| 217 | + }], |
| 218 | + }; |
| 219 | + mocks.post.mockResolvedValue({status: 'ok', task_id: 't1'}); |
| 220 | + mocks.poll_until.mockResolvedValue({result: response, attempts: 1}); |
| 221 | + await handle_discover('q', {json: true}); |
| 222 | + expect(mocks.print).toHaveBeenCalledWith( |
| 223 | + response, |
| 224 | + {json: true, pretty: undefined, output: undefined} |
| 225 | + ); |
| 226 | + expect(mocks.print_table).not.toHaveBeenCalled(); |
| 227 | + }); |
| 228 | + |
| 229 | + it('prints raw JSON when --output is set', async()=>{ |
| 230 | + const response = { |
| 231 | + status: 'done', |
| 232 | + results: [{ |
| 233 | + link: 'https://example.com', |
| 234 | + title: 'R', |
| 235 | + description: 'D', |
| 236 | + relevance_score: 0.7, |
| 237 | + }], |
| 238 | + }; |
| 239 | + mocks.post.mockResolvedValue({status: 'ok', task_id: 't2'}); |
| 240 | + mocks.poll_until.mockResolvedValue({result: response, attempts: 1}); |
| 241 | + await handle_discover('q', {output: 'out.json'}); |
| 242 | + expect(mocks.print).toHaveBeenCalledWith( |
| 243 | + response, |
| 244 | + {json: undefined, pretty: undefined, output: 'out.json'} |
| 245 | + ); |
| 246 | + }); |
| 247 | + |
| 248 | + it('fails when trigger returns no task_id', async()=>{ |
| 249 | + mocks.post.mockResolvedValue({status: 'ok'}); |
| 250 | + const exit = vi.spyOn(process, 'exit') |
| 251 | + .mockImplementation(()=>undefined as never); |
| 252 | + const error = vi.spyOn(console, 'error') |
| 253 | + .mockImplementation(()=>{}); |
| 254 | + await handle_discover('q', {}); |
| 255 | + expect(mocks.fail).toHaveBeenCalled(); |
| 256 | + exit.mockRestore(); |
| 257 | + error.mockRestore(); |
| 258 | + }); |
| 259 | + }); |
| 260 | +}); |
0 commit comments