|
| 1 | +import {expect, sinon} from '@loopback/testlab'; |
| 2 | +import {BarVisualizer} from '../../../../components/visualization/visualizers/bar.visualizer'; |
| 3 | +import {LLMProvider} from '../../../../types'; |
| 4 | +import {fail} from 'assert'; |
| 5 | +import {VisualizationGraphState} from '../../../../components'; |
| 6 | + |
| 7 | +describe('BarVisualizer Unit', function () { |
| 8 | + let visualizer: BarVisualizer; |
| 9 | + let llmProvider: sinon.SinonStubbedInstance<LLMProvider>; |
| 10 | + let withStructuredOutputStub: sinon.SinonStub; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + // Create stub for LLM provider |
| 14 | + withStructuredOutputStub = sinon.stub(); |
| 15 | + llmProvider = { |
| 16 | + withStructuredOutput: withStructuredOutputStub, |
| 17 | + } as sinon.SinonStubbedInstance<LLMProvider>; |
| 18 | + |
| 19 | + visualizer = new BarVisualizer(llmProvider); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + sinon.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should have correct name and description', () => { |
| 27 | + expect(visualizer.name).to.equal('bar'); |
| 28 | + expect(visualizer.description).to.match(/bar chart/); |
| 29 | + expect(visualizer.description).to.match(/comparing values/); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should have valid schema with required fields', () => { |
| 33 | + const schema = visualizer.schema; |
| 34 | + expect(schema).to.be.ok(); |
| 35 | + |
| 36 | + // Test schema structure by trying to parse valid data |
| 37 | + const validData = { |
| 38 | + categoryColumn: 'category', |
| 39 | + valueColumn: 'value', |
| 40 | + orientation: 'vertical', |
| 41 | + }; |
| 42 | + |
| 43 | + const result = schema.safeParse(validData); |
| 44 | + expect(result.success).to.be.true(); |
| 45 | + |
| 46 | + if (result.success) { |
| 47 | + expect(result.data).to.deepEqual(validData); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('should validate schema with default orientation', () => { |
| 52 | + const schema = visualizer.schema; |
| 53 | + const dataWithoutOrientation = { |
| 54 | + categoryColumn: 'category', |
| 55 | + valueColumn: 'value', |
| 56 | + }; |
| 57 | + |
| 58 | + const result = schema.safeParse(dataWithoutOrientation); |
| 59 | + expect(result.success).to.be.true(); |
| 60 | + |
| 61 | + if (result.success) { |
| 62 | + expect(result.data.orientation).to.equal('vertical'); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('should reject invalid orientation values', () => { |
| 67 | + const schema = visualizer.schema; |
| 68 | + const invalidData = { |
| 69 | + categoryColumn: 'category', |
| 70 | + valueColumn: 'value', |
| 71 | + orientation: 42, // invalid type |
| 72 | + }; |
| 73 | + |
| 74 | + const result = schema.safeParse(invalidData); |
| 75 | + expect(result.success).to.be.false(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should throw error when state is invalid (missing sql)', async () => { |
| 79 | + const invalidState = { |
| 80 | + prompt: 'test prompt', |
| 81 | + datasetId: 'test-id', |
| 82 | + queryDescription: 'test description', |
| 83 | + // sql is missing - will be undefined |
| 84 | + } as unknown as VisualizationGraphState; |
| 85 | + |
| 86 | + try { |
| 87 | + await visualizer.getConfig(invalidState); |
| 88 | + fail('Should have thrown an error'); |
| 89 | + } catch (error) { |
| 90 | + expect(error).to.have.property('message', 'Invalid State'); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw error when state is invalid (missing queryDescription)', async () => { |
| 95 | + const invalidState = { |
| 96 | + prompt: 'test prompt', |
| 97 | + datasetId: 'test-id', |
| 98 | + sql: 'SELECT * FROM test', |
| 99 | + // queryDescription is missing - will be undefined |
| 100 | + } as unknown as VisualizationGraphState; |
| 101 | + |
| 102 | + try { |
| 103 | + await visualizer.getConfig(invalidState); |
| 104 | + fail('Should have thrown an error'); |
| 105 | + } catch (error) { |
| 106 | + expect(error).to.have.property('message', 'Invalid State'); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + it('should throw error when state is invalid (missing prompt)', async () => { |
| 111 | + const invalidState = { |
| 112 | + datasetId: 'test-id', |
| 113 | + sql: 'SELECT * FROM test', |
| 114 | + queryDescription: 'test description', |
| 115 | + // prompt is missing - will be undefined |
| 116 | + } as unknown as VisualizationGraphState; |
| 117 | + |
| 118 | + try { |
| 119 | + await visualizer.getConfig(invalidState); |
| 120 | + fail('Should have thrown an error'); |
| 121 | + } catch (error) { |
| 122 | + expect(error).to.have.property('message', 'Invalid State'); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('should successfully generate config with valid state', async () => { |
| 127 | + const mockLLMResponse = { |
| 128 | + categoryColumn: 'department', |
| 129 | + valueColumn: 'salary', |
| 130 | + orientation: 'vertical', |
| 131 | + }; |
| 132 | + |
| 133 | + const mockInvoke = sinon.stub().resolves(mockLLMResponse); |
| 134 | + withStructuredOutputStub.returns(mockInvoke); |
| 135 | + |
| 136 | + const validState = { |
| 137 | + prompt: 'Show me a bar chart of salaries by department', |
| 138 | + datasetId: 'test-dataset', |
| 139 | + sql: 'SELECT department, AVG(salary) as avg_salary FROM employees GROUP BY department', |
| 140 | + queryDescription: 'Average salary by department', |
| 141 | + } as unknown as VisualizationGraphState; |
| 142 | + |
| 143 | + const config = await visualizer.getConfig(validState); |
| 144 | + |
| 145 | + expect(config).to.deepEqual(mockLLMResponse); |
| 146 | + expect( |
| 147 | + withStructuredOutputStub.calledOnceWith(visualizer.schema), |
| 148 | + ).to.be.true(); |
| 149 | + expect(mockInvoke.calledOnce).to.be.true(); |
| 150 | + |
| 151 | + // Check that the mock was called with a StringPromptValue containing our data |
| 152 | + const invokeArgs = mockInvoke.getCall(0).args[0]; |
| 153 | + expect(invokeArgs).to.have.property('value'); |
| 154 | + // Escape special regex characters in SQL |
| 155 | + const escapedSQL = |
| 156 | + validState.sql?.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') ?? ''; |
| 157 | + expect(invokeArgs.value).to.match(new RegExp(escapedSQL)); |
| 158 | + expect(invokeArgs.value).to.match( |
| 159 | + new RegExp(validState.queryDescription ?? ''), |
| 160 | + ); |
| 161 | + expect(invokeArgs.value).to.match(new RegExp(validState.prompt)); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should handle LLM errors gracefully', async () => { |
| 165 | + const mockError = new Error('LLM processing failed'); |
| 166 | + const mockInvoke = sinon.stub().rejects(mockError); |
| 167 | + withStructuredOutputStub.returns(mockInvoke); |
| 168 | + |
| 169 | + const validState = { |
| 170 | + prompt: 'test prompt', |
| 171 | + datasetId: 'test-dataset', |
| 172 | + sql: 'SELECT * FROM test', |
| 173 | + queryDescription: 'test description', |
| 174 | + } as unknown as VisualizationGraphState; |
| 175 | + |
| 176 | + try { |
| 177 | + await visualizer.getConfig(validState); |
| 178 | + fail('Should have thrown an error'); |
| 179 | + } catch (error) { |
| 180 | + expect(error).to.equal(mockError); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + it('should contain proper prompt template structure', () => { |
| 185 | + const promptTemplate = visualizer.renderPrompt; |
| 186 | + expect(promptTemplate).to.be.ok(); |
| 187 | + |
| 188 | + const templateText = promptTemplate.template; |
| 189 | + expect(templateText).to.match(/bar chart/); |
| 190 | + expect(templateText).to.match(/\{sql\}/); |
| 191 | + expect(templateText).to.match(/\{description\}/); |
| 192 | + expect(templateText).to.match(/\{userPrompt\}/); |
| 193 | + expect(templateText).to.match(/x-axis/); |
| 194 | + expect(templateText).to.match(/y-axis/); |
| 195 | + }); |
| 196 | +}); |
0 commit comments