|
| 1 | +/** |
| 2 | + * Tests for suggest functionality |
| 3 | + */ |
| 4 | + |
| 5 | +import * as path from 'path'; |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 8 | + |
| 9 | +import { AVA } from '../src/ava'; |
| 10 | + |
| 11 | +describe('Suggest Tests', () => { |
| 12 | + let ava: AVA; |
| 13 | + const testDataPath = path.join(__dirname, '../data/companies.csv'); |
| 14 | + |
| 15 | + const apiKey = process.env.LING_1T_API_KEY; |
| 16 | + const skipLLMTests = !apiKey; |
| 17 | + |
| 18 | + const getLLMConfig = () => ({ |
| 19 | + model: 'ling-1t', |
| 20 | + apiKey: apiKey || '', |
| 21 | + baseURL: 'https://api.tbox.cn/api/llm/v1', |
| 22 | + }); |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + if (skipLLMTests) { |
| 26 | + // eslint-disable-next-line no-console |
| 27 | + console.log('Skipping LLM integration test: LING_1T_API_KEY not set'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + ava = new AVA({ |
| 32 | + llm: getLLMConfig(), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + if (ava) { |
| 38 | + ava.dispose(); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + describe('suggest method', () => { |
| 43 | + it('should throw error when suggesting without loading data', async () => { |
| 44 | + if (skipLLMTests) return; |
| 45 | + |
| 46 | + await expect(ava.suggest()).rejects.toThrow('No data loaded'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return 3 suggestions by default', async () => { |
| 50 | + if (skipLLMTests) return; |
| 51 | + |
| 52 | + try { |
| 53 | + await ava.loadCSV(testDataPath); |
| 54 | + const suggestions = await ava.suggest(); |
| 55 | + |
| 56 | + expect(suggestions).toBeDefined(); |
| 57 | + expect(Array.isArray(suggestions)).toBe(true); |
| 58 | + expect(suggestions.length).toBe(3); |
| 59 | + |
| 60 | + // Verify each suggestion has required fields |
| 61 | + for (const suggestion of suggestions) { |
| 62 | + expect(suggestion).toHaveProperty('query'); |
| 63 | + expect(suggestion).toHaveProperty('score'); |
| 64 | + expect(suggestion).toHaveProperty('reason'); |
| 65 | + expect(typeof suggestion.query).toBe('string'); |
| 66 | + expect(typeof suggestion.score).toBe('number'); |
| 67 | + expect(typeof suggestion.reason).toBe('string'); |
| 68 | + expect(suggestion.query.length).toBeGreaterThan(0); |
| 69 | + expect(suggestion.reason.length).toBeGreaterThan(0); |
| 70 | + // Score should be between 0 and 1 |
| 71 | + expect(suggestion.score).toBeGreaterThanOrEqual(0); |
| 72 | + expect(suggestion.score).toBeLessThanOrEqual(1); |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + // eslint-disable-next-line no-console |
| 76 | + console.log('Skipping test due to API error:', error instanceof Error ? error.message : String(error)); |
| 77 | + } |
| 78 | + }, 60000); |
| 79 | + |
| 80 | + it('should return specified number of suggestions', async () => { |
| 81 | + if (skipLLMTests) return; |
| 82 | + |
| 83 | + try { |
| 84 | + await ava.loadCSV(testDataPath); |
| 85 | + const suggestions = await ava.suggest(5); |
| 86 | + |
| 87 | + expect(suggestions).toBeDefined(); |
| 88 | + expect(Array.isArray(suggestions)).toBe(true); |
| 89 | + expect(suggestions.length).toBe(5); |
| 90 | + } catch (error) { |
| 91 | + // eslint-disable-next-line no-console |
| 92 | + console.log('Skipping test due to API error:', error instanceof Error ? error.message : String(error)); |
| 93 | + } |
| 94 | + }, 60000); |
| 95 | + |
| 96 | + it('should return suggestions sorted by score in descending order', async () => { |
| 97 | + if (skipLLMTests) return; |
| 98 | + |
| 99 | + try { |
| 100 | + await ava.loadCSV(testDataPath); |
| 101 | + const suggestions = await ava.suggest(); |
| 102 | + |
| 103 | + // Verify suggestions are sorted by score descending |
| 104 | + for (let i = 0; i < suggestions.length - 1; i++) { |
| 105 | + expect(suggestions[i].score).toBeGreaterThanOrEqual(suggestions[i + 1].score); |
| 106 | + } |
| 107 | + } catch (error) { |
| 108 | + // eslint-disable-next-line no-console |
| 109 | + console.log('Skipping test due to API error:', error instanceof Error ? error.message : String(error)); |
| 110 | + } |
| 111 | + }, 60000); |
| 112 | + |
| 113 | + it('should work with loadObject', async () => { |
| 114 | + if (skipLLMTests) return; |
| 115 | + |
| 116 | + try { |
| 117 | + await ava.loadObject([ |
| 118 | + { city: '杭州', gdp: 18753 }, |
| 119 | + { city: '上海', gdp: 43214 }, |
| 120 | + { city: '北京', gdp: 35371 }, |
| 121 | + ]); |
| 122 | + |
| 123 | + const suggestions = await ava.suggest(); |
| 124 | + |
| 125 | + expect(suggestions).toBeDefined(); |
| 126 | + expect(Array.isArray(suggestions)).toBe(true); |
| 127 | + expect(suggestions.length).toBe(3); |
| 128 | + } catch (error) { |
| 129 | + // eslint-disable-next-line no-console |
| 130 | + console.log('Skipping test due to API error:', error instanceof Error ? error.message : String(error)); |
| 131 | + } |
| 132 | + }, 60000); |
| 133 | + |
| 134 | + it('should generate queries that can be used with analysis', async () => { |
| 135 | + if (skipLLMTests) return; |
| 136 | + |
| 137 | + try { |
| 138 | + await ava.loadCSV(testDataPath); |
| 139 | + const suggestions = await ava.suggest(1); |
| 140 | + |
| 141 | + expect(suggestions.length).toBeGreaterThan(0); |
| 142 | + |
| 143 | + // Try to analyze using the first suggested query |
| 144 | + const result = await ava.analysis(suggestions[0].query); |
| 145 | + |
| 146 | + expect(result).toBeDefined(); |
| 147 | + expect(result.text).toBeDefined(); |
| 148 | + expect(typeof result.text).toBe('string'); |
| 149 | + expect(result.text.length).toBeGreaterThan(0); |
| 150 | + } catch (error) { |
| 151 | + // eslint-disable-next-line no-console |
| 152 | + console.log('Skipping test due to API error:', error instanceof Error ? error.message : String(error)); |
| 153 | + } |
| 154 | + }, 120000); // Longer timeout for this test |
| 155 | + }); |
| 156 | +}); |
0 commit comments