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 e4a0fae
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 33 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
4 changes: 4 additions & 0 deletions apps/spa/src/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ globalThis.ngJest = {
errorOnUnknownProperties: true,
},
};

global.setImmediate = jest.useRealTimers as unknown as typeof setImmediate;
// @ts-expect-error: Jest global setup
window.setImmediate = window.setTimeout
import 'jest-preset-angular/setup-jest';
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
Expand Up @@ -4,11 +4,11 @@ import { AssistantMemoryService } from './assistant-memory.service';

describe('AssistantMemoryService', () => {
let assistantMemoryService: AssistantMemoryService;
const env = process.env

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

jest.spyOn(fs.promises, 'writeFile').mockResolvedValue();
process.env = { ...env }
});

afterEach(() => {
Expand All @@ -24,9 +24,9 @@ describe('AssistantMemoryService', () => {
const sourcePath = './.env';
const envVariables = 'ASSISTANT_ID=123\n';
const id = '456';
const readFileSpy = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(envVariables);
const readFileSpy = jest.spyOn(fs.promises, 'readFile').mockResolvedValue({
toString: jest.fn().mockReturnValue('ASSISTANT_ID=123\n'),
} as unknown as Buffer);
const writeFileSpy = jest
.spyOn(fs.promises, 'writeFile')
.mockResolvedValue();
Expand All @@ -48,18 +48,5 @@ describe('AssistantMemoryService', () => {
);
});

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}`);
});
});
});
8 changes: 4 additions & 4 deletions libs/ai-assistant/src/lib/assistant/assistant.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe('AssistantService', () => {
.spyOn(aiService.provider.beta.assistants, 'create')
.mockResolvedValue({} as Assistant);

jest
.spyOn(assistantMemoryService, 'saveAssistantId')
.mockResolvedValue(undefined);

jest.spyOn(fs.promises, 'writeFile').mockResolvedValue();
});

Expand Down Expand Up @@ -158,10 +162,6 @@ describe('AssistantService', () => {
.spyOn(assistantService, 'updateFiles')
.mockResolvedValue({ id: '1' } as Assistant);

jest
.spyOn(assistantMemoryService, 'saveAssistantId')
.mockResolvedValue(undefined);

await assistantService.create();

expect(assistantMemoryService.saveAssistantId).toHaveBeenCalled();
Expand Down
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;
});
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"cp:ai-embedded": "cp ./dist/libs/ai-embedded/main.js ./apps/spa/src/assets/js/ai-embedded.js",
"cp:readme": "cp ./README.md ./dist/libs/ai-assistant/README.md",
"lint": "nx run-many --parallel --target=lint --all",
"test:ai-assistant": "nx test ai-assistant",
"test:spa": "nx test spa",
"test": "nx run-many --parallel --target=test --all",
"format": "nx format:write",
"prepare": "husky"
Expand Down

0 comments on commit e4a0fae

Please sign in to comment.