-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ai-assistant): ai assistant test cases (#27)
- Loading branch information
1 parent
2374279
commit 6ab1042
Showing
31 changed files
with
3,199 additions
and
1,217 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'ai-assistant', | ||
preset: '../../jest.preset.js', | ||
|
This file contains 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 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,39 @@ | ||
import { definitionMock } from './agent.mock'; | ||
import { AgentService } from './agent.service'; | ||
import { AgentBase } from './agent.base'; | ||
import { AgentData } from './agent.model'; | ||
|
||
describe('AgentBase', () => { | ||
let agentBase: AgentBase; | ||
let agentService: AgentService; | ||
|
||
beforeEach(() => { | ||
agentService = new AgentService(); | ||
agentBase = new AgentBase(agentService); | ||
}); | ||
|
||
describe('onModuleInit', () => { | ||
it('should call agentService.add with definition and output', () => { | ||
const addSpy = jest.spyOn(agentService, 'add'); | ||
agentBase.definition = definitionMock; | ||
|
||
agentBase.onModuleInit(); | ||
|
||
expect(addSpy).toHaveBeenCalledWith(definitionMock, expect.any(Function)); | ||
}); | ||
}); | ||
|
||
describe('output', () => { | ||
it('should return empty string when params are missing', async () => { | ||
const result = await agentBase.output({} as AgentData); | ||
|
||
expect(result).toBe(''); | ||
}); | ||
|
||
it('should return params when they are provided', async () => { | ||
const result = await agentBase.output({ params: 'test' } as AgentData); | ||
|
||
expect(result).toBe('test'); | ||
}); | ||
}); | ||
}); |
This file contains 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 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,10 @@ | ||
import { AssistantCreateParams } from 'openai/resources/beta'; | ||
|
||
export const agentNameMock = 'agent-name'; | ||
|
||
export const agentMock = async () => 'agent-result'; | ||
|
||
export const definitionMock: AssistantCreateParams.AssistantToolsFunction = { | ||
type: 'function', | ||
function: { name: agentNameMock }, | ||
}; |
This file contains 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 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,61 @@ | ||
import { AiController } from './ai.controller'; | ||
import { AiService } from './ai.service'; | ||
import { mockFileData, transcriptionMock } from './ai.mock'; | ||
|
||
describe('AiController', () => { | ||
let aiController: AiController; | ||
let aiService: AiService; | ||
|
||
beforeEach(() => { | ||
aiService = new AiService(); | ||
aiController = new AiController(aiService); | ||
|
||
jest | ||
.spyOn(aiService.provider.audio.transcriptions, 'create') | ||
.mockResolvedValue(transcriptionMock); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('transcription', () => { | ||
it('should return transcription', async () => { | ||
const transcription = await aiController.postTranscription(mockFileData); | ||
|
||
expect(transcription).toEqual({ content: transcriptionMock.text }); | ||
}); | ||
|
||
it('should throw an error', async () => { | ||
jest | ||
.spyOn(aiService.provider.audio.transcriptions, 'create') | ||
.mockRejectedValue(new Error()); | ||
|
||
try { | ||
await aiController.postTranscription(mockFileData); | ||
} catch (error) { | ||
expect((error as Error).message).toBeTruthy(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('speech', () => { | ||
it('should return speech', async () => { | ||
const speech = await aiController.postSpeech({ content: 'Hello' }); | ||
|
||
expect(speech).toBeInstanceOf(Buffer); | ||
}); | ||
|
||
it('should throw an error', async () => { | ||
jest | ||
.spyOn(aiService.provider.audio.speech, 'create') | ||
.mockRejectedValue(new Error()); | ||
|
||
try { | ||
await aiController.postSpeech({ content: 'Hello' }); | ||
} catch (error) { | ||
expect((error as Error).message).toBeTruthy(); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains 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 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,15 @@ | ||
import { AiTranscription } from './ai.model'; | ||
// @ts-expect-error multer is necessary | ||
// eslint-disable-next-line | ||
import { multer } from 'multer'; | ||
|
||
export const mockBuffer = Buffer.from('fake audio data'); | ||
|
||
export const mockFileData = { | ||
buffer: mockBuffer, | ||
mimetype: 'audio/wav', | ||
} as Express.Multer.File; | ||
|
||
export const transcriptionMock: AiTranscription = { | ||
text: 'Text from transcription', | ||
}; |
This file contains 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,40 @@ | ||
import { toFile } from 'openai/uploads'; | ||
import { Response } from 'openai/core'; | ||
import { AiService } from './ai.service'; | ||
import { mockBuffer, transcriptionMock } from './ai.mock'; | ||
|
||
describe('AiService', () => { | ||
let aiService: AiService; | ||
|
||
beforeEach(() => { | ||
aiService = new AiService(); | ||
|
||
jest | ||
.spyOn(aiService.provider.audio.transcriptions, 'create') | ||
.mockResolvedValue(transcriptionMock); | ||
|
||
jest.spyOn(aiService.provider.audio.speech, 'create').mockResolvedValue({ | ||
arrayBuffer: jest.fn().mockResolvedValue(mockBuffer), | ||
} as unknown as Response); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return transcription', async () => { | ||
const file = await toFile(mockBuffer, 'audio.mp3', { type: 'mp3' }); | ||
|
||
const transcription = await aiService.transcription(file); | ||
|
||
expect(transcription.text).toBe(transcriptionMock.text); | ||
}); | ||
|
||
it('should return speech', async () => { | ||
const data = { content: 'test content' }; | ||
|
||
const speech = await aiService.speech(data); | ||
|
||
expect(speech).toStrictEqual(mockBuffer); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
libs/ai-assistant/src/lib/assistant/assistant-files.service.spec.ts
This file contains 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,60 @@ | ||
import { ConfigService } from '../config'; | ||
import { AiService } from '../ai'; | ||
import { AssistantFilesService } from './assistant-files.service'; | ||
import OpenAI from 'openai'; | ||
|
||
jest.mock('fs', () => ({ | ||
createReadStream: jest.fn().mockReturnValue('file'), | ||
})); | ||
|
||
describe('AssistantFilesService', () => { | ||
let aiService: AiService; | ||
let configService: ConfigService; | ||
let assistantFilesService: AssistantFilesService; | ||
|
||
beforeEach(() => { | ||
aiService = new AiService(); | ||
configService = new ConfigService(); | ||
assistantFilesService = new AssistantFilesService(configService, aiService); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(assistantFilesService).toBeDefined(); | ||
}); | ||
|
||
it('should create files', async () => { | ||
const fileNames = ['file1', 'file2']; | ||
const create = jest.fn().mockResolvedValue({ id: 'id' }); | ||
aiService.provider = { files: { create } } as unknown as OpenAI; | ||
configService.get = jest.fn().mockReturnValue({ filesDir: 'dir' }); | ||
|
||
const result = await assistantFilesService.create(fileNames); | ||
|
||
expect(result).toEqual(['id', 'id']); | ||
expect(create).toHaveBeenCalledTimes(2); | ||
expect(create).toHaveBeenCalledWith({ | ||
file: 'file', | ||
purpose: 'assistants', | ||
}); | ||
}); | ||
|
||
it('should create files without file directory', async () => { | ||
const fileNames = ['file1', 'file2']; | ||
const create = jest.fn().mockResolvedValue({ id: 'id' }); | ||
aiService.provider = { files: { create } } as unknown as OpenAI; | ||
configService.get = jest.fn().mockReturnValue({}); | ||
|
||
const result = await assistantFilesService.create(fileNames); | ||
|
||
expect(result).toEqual(['id', 'id']); | ||
expect(create).toHaveBeenCalledTimes(2); | ||
expect(create).toHaveBeenCalledWith({ | ||
file: 'file', | ||
purpose: 'assistants', | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.