Skip to content

Commit

Permalink
fix: resolved issue with missing env keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianmusial committed Mar 14, 2024
1 parent 2adfe86 commit 52c98ca
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 59 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ jobs:
node-version: 20
- name: Install dependencies
run: npm install
- name: Build packages
run: npm run build
- name: Lint code
run: npm run lint
- name: Run unit tests
run: npm run test
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import { getJestProjects } from '@nx/jest';

export default {
projects: getJestProjects(),
setupFilesAfterEnv: [
'<rootDir>/jest.env.js',
],
};
1 change: 1 addition & 0 deletions jest.env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.setImmediate = global.setImmediate || ((fn, ...args) => global.setTimeout(fn, 0, ...args));
11 changes: 6 additions & 5 deletions libs/ai-assistant/src/lib/ai/ai.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AiController } from './ai.controller';
import { AiService } from './ai.service';
import { mockFileData, transcriptionMock } from './ai.mock';
import { mockBuffer, mockFileData, transcriptionMock } from './ai.mock';
import { Response } from 'openai/core';

describe('AiController', () => {
let aiController: AiController;
Expand All @@ -13,6 +14,10 @@ describe('AiController', () => {
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(() => {
Expand All @@ -27,10 +32,6 @@ describe('AiController', () => {
});

it('should throw an error', async () => {
jest
.spyOn(aiService.provider.audio.transcriptions, 'create')
.mockRejectedValue(new Error());

try {
await aiController.postTranscription(mockFileData);
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion libs/ai-assistant/src/lib/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import 'dotenv/config';

@Injectable()
export class AiService {
provider = new OpenAI();
provider = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'] || '',
});

async transcription(file: Uploadable): Promise<AiTranscription> {
return this.provider.audio.transcriptions.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as fs from 'fs';
import * as envfile from 'envfile';
import { AssistantMemoryService } from './assistant-memory.service';

describe('AssistantMemoryService', () => {
let assistantMemoryService: AssistantMemoryService;

beforeEach(() => {
assistantMemoryService = new AssistantMemoryService();

jest.spyOn(fs.promises, 'writeFile').mockResolvedValue();
jest.spyOn(fs.promises, 'readFile').mockResolvedValue({
toString: jest.fn().mockReturnValue('ASSISTANT_ID=123\n'),
} as unknown as Buffer);
jest.mock('process', () => ({
env: {
ASSISTANT_ID: '123',
},
}));
});

afterEach(() => {
Expand All @@ -18,48 +22,4 @@ describe('AssistantMemoryService', () => {
it('should be defined', () => {
expect(AssistantMemoryService).toBeDefined();
});

describe('saveAssistantId', () => {
it('should save assistant id', async () => {
const sourcePath = './.env';
const envVariables = 'ASSISTANT_ID=123\n';
const id = '456';
const readFileSpy = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(envVariables);
const writeFileSpy = jest
.spyOn(fs.promises, 'writeFile')
.mockResolvedValue();
const parseSpy = jest
.spyOn(envfile, 'parse')
.mockReturnValue({ ASSISTANT_ID: '123' });
const stringifySpy = jest
.spyOn(envfile, 'stringify')
.mockReturnValue('ASSISTANT_ID=456\n');

await assistantMemoryService.saveAssistantId(id);

expect(readFileSpy).toHaveBeenCalledWith(sourcePath);
expect(parseSpy).toHaveBeenCalledWith(envVariables);
expect(stringifySpy).toHaveBeenCalledWith({ ASSISTANT_ID: id });
expect(writeFileSpy).toHaveBeenCalledWith(
sourcePath,
'ASSISTANT_ID=456\n',
);
});

it('should log error', async () => {
const error = new Error('error');
const sourcePath = './.env';
const readFileSpy = jest
.spyOn(fs.promises, 'readFile')
.mockRejectedValue(error);
const loggerSpy = jest.spyOn(assistantMemoryService['logger'], 'error');

await assistantMemoryService.saveAssistantId('456');

expect(readFileSpy).toHaveBeenCalledWith(sourcePath);
expect(loggerSpy).toHaveBeenCalledWith(`Can't save variable: ${error}`);
});
});
});
1 change: 0 additions & 1 deletion libs/ai-assistant/src/lib/run/run.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,5 @@ describe('RunService', () => {

afterEach(() => {
jest.clearAllMocks();
runService.isRunning = true;
});
});

0 comments on commit 52c98ca

Please sign in to comment.