|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { agentContextStorage } from '#agent/agentContextLocalStorage'; |
| 3 | +import type { AgentContext } from '#shared/agent/agent.model'; |
| 4 | +import { setupConditionalLoggerOutput } from '#test/testUtils'; |
| 5 | +import { Perplexity } from './perplexity'; |
| 6 | + |
| 7 | +describe('Perplexity Integration Tests', () => { |
| 8 | + setupConditionalLoggerOutput(); |
| 9 | + |
| 10 | + let perplexity: Perplexity; |
| 11 | + let mockAgentContext: AgentContext; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + perplexity = new Perplexity(); |
| 15 | + mockAgentContext = { |
| 16 | + agentId: 'test-agent-id', |
| 17 | + memory: {}, |
| 18 | + } as AgentContext; |
| 19 | + }); |
| 20 | + |
| 21 | + describe('research', () => { |
| 22 | + it('should perform research and return results when saveToMemory is false', async function () { |
| 23 | + this.timeout(30000); |
| 24 | + |
| 25 | + const researchTask = 'What are the latest TypeScript 5.9 features?'; |
| 26 | + |
| 27 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 28 | + return await perplexity.research(researchTask, false); |
| 29 | + }); |
| 30 | + |
| 31 | + expect(result).to.be.a('string'); |
| 32 | + expect(result.length).to.be.greaterThan(0); |
| 33 | + expect(result).to.include('TypeScript'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should save research to memory and return memory key when saveToMemory is true', async function () { |
| 37 | + this.timeout(30000); |
| 38 | + |
| 39 | + const researchTask = 'What is the capital of France?'; |
| 40 | + |
| 41 | + const memoryKey = await agentContextStorage.run(mockAgentContext, async () => { |
| 42 | + return await perplexity.research(researchTask, true); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(memoryKey).to.be.a('string'); |
| 46 | + expect(memoryKey).to.match(/^Perplexity-/); |
| 47 | + expect(mockAgentContext.memory[memoryKey]).to.be.a('string'); |
| 48 | + expect(mockAgentContext.memory[memoryKey].length).to.be.greaterThan(0); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle complex research queries', async function () { |
| 52 | + this.timeout(30000); |
| 53 | + |
| 54 | + const researchTask = 'Compare the performance characteristics of Node.js async/await versus callbacks in 2025'; |
| 55 | + |
| 56 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 57 | + return await perplexity.research(researchTask, false); |
| 58 | + }); |
| 59 | + |
| 60 | + expect(result).to.be.a('string'); |
| 61 | + expect(result.length).to.be.greaterThan(100); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle short queries', async function () { |
| 65 | + this.timeout(30000); |
| 66 | + |
| 67 | + const researchTask = 'TypeScript'; |
| 68 | + |
| 69 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 70 | + return await perplexity.research(researchTask, false); |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result).to.be.a('string'); |
| 74 | + expect(result.length).to.be.greaterThan(0); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('search', () => { |
| 79 | + it('should perform basic web search and return results', async function () { |
| 80 | + this.timeout(30000); |
| 81 | + |
| 82 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 83 | + return await perplexity.search('TypeScript 5.9 features', { |
| 84 | + max_results: 5, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(result).to.have.property('results'); |
| 89 | + expect(result.results).to.be.an('array'); |
| 90 | + expect(result.results.length).to.be.greaterThan(0); |
| 91 | + expect(result.results.length).to.be.at.most(5); |
| 92 | + |
| 93 | + const firstResult = result.results[0]; |
| 94 | + expect(firstResult).to.have.property('title'); |
| 95 | + expect(firstResult).to.have.property('url'); |
| 96 | + expect(firstResult.title).to.be.a('string'); |
| 97 | + expect(firstResult.url).to.be.a('string'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should support multi-query search', async function () { |
| 101 | + this.timeout(30000); |
| 102 | + |
| 103 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 104 | + return await perplexity.search(['Node.js performance', 'JavaScript async patterns'], { |
| 105 | + max_results: 10, |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + expect(result.results).to.be.an('array'); |
| 110 | + expect(result.results.length).to.be.greaterThan(0); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should return snippets when requested', async function () { |
| 114 | + this.timeout(30000); |
| 115 | + |
| 116 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 117 | + return await perplexity.search('React best practices 2025', { |
| 118 | + max_results: 3, |
| 119 | + return_snippets: true, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + expect(result.results).to.be.an('array'); |
| 124 | + expect(result.results.length).to.be.greaterThan(0); |
| 125 | + |
| 126 | + const resultsWithSnippets = result.results.filter((r) => r.snippet); |
| 127 | + expect(resultsWithSnippets.length).to.be.greaterThan(0); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should support country filtering', async function () { |
| 131 | + this.timeout(30000); |
| 132 | + |
| 133 | + const result = await agentContextStorage.run(mockAgentContext, async () => { |
| 134 | + return await perplexity.search('local tech events', { |
| 135 | + max_results: 5, |
| 136 | + country: 'US', |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + expect(result.results).to.be.an('array'); |
| 141 | + expect(result.results.length).to.be.greaterThan(0); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle errors gracefully', async function () { |
| 145 | + this.timeout(30000); |
| 146 | + |
| 147 | + await agentContextStorage.run(mockAgentContext, async () => { |
| 148 | + try { |
| 149 | + await perplexity.search('', { |
| 150 | + max_results: 5, |
| 151 | + }); |
| 152 | + expect.fail('Should have thrown an error'); |
| 153 | + } catch (error) { |
| 154 | + expect(error).to.exist; |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments