Skip to content

Commit 8e30906

Browse files
fix: resolved issue with missing env keys
1 parent 2adfe86 commit 8e30906

File tree

8 files changed

+24
-33
lines changed

8 files changed

+24
-33
lines changed

.github/workflows/test.yml

-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@ jobs:
1818
node-version: 20
1919
- name: Install dependencies
2020
run: npm install
21-
- name: Build packages
22-
run: npm run build
23-
- name: Lint code
24-
run: npm run lint
2521
- name: Run unit tests
2622
run: npm run test

apps/spa/src/test-setup.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ globalThis.ngJest = {
55
errorOnUnknownProperties: true,
66
},
77
};
8+
9+
global.setImmediate = jest.useRealTimers as unknown as typeof setImmediate;
10+
// @ts-expect-error: Jest global setup
11+
window.setImmediate = window.setTimeout
812
import 'jest-preset-angular/setup-jest';

libs/ai-assistant/src/lib/ai/ai.controller.spec.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AiController } from './ai.controller';
22
import { AiService } from './ai.service';
3-
import { mockFileData, transcriptionMock } from './ai.mock';
3+
import { mockBuffer, mockFileData, transcriptionMock } from './ai.mock';
4+
import { Response } from 'openai/core';
45

56
describe('AiController', () => {
67
let aiController: AiController;
@@ -13,6 +14,10 @@ describe('AiController', () => {
1314
jest
1415
.spyOn(aiService.provider.audio.transcriptions, 'create')
1516
.mockResolvedValue(transcriptionMock);
17+
18+
jest.spyOn(aiService.provider.audio.speech, 'create').mockResolvedValue({
19+
arrayBuffer: jest.fn().mockResolvedValue(mockBuffer),
20+
} as unknown as Response);
1621
});
1722

1823
afterEach(() => {
@@ -27,10 +32,6 @@ describe('AiController', () => {
2732
});
2833

2934
it('should throw an error', async () => {
30-
jest
31-
.spyOn(aiService.provider.audio.transcriptions, 'create')
32-
.mockRejectedValue(new Error());
33-
3435
try {
3536
await aiController.postTranscription(mockFileData);
3637
} catch (error) {

libs/ai-assistant/src/lib/ai/ai.service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import 'dotenv/config';
66

77
@Injectable()
88
export class AiService {
9-
provider = new OpenAI();
9+
provider = new OpenAI({
10+
apiKey: process.env['OPENAI_API_KEY'] || '',
11+
});
1012

1113
async transcription(file: Uploadable): Promise<AiTranscription> {
1214
return this.provider.audio.transcriptions.create({

libs/ai-assistant/src/lib/assistant/assistant-memory.service.spec.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { AssistantMemoryService } from './assistant-memory.service';
44

55
describe('AssistantMemoryService', () => {
66
let assistantMemoryService: AssistantMemoryService;
7+
const env = process.env
78

89
beforeEach(() => {
910
assistantMemoryService = new AssistantMemoryService();
10-
11-
jest.spyOn(fs.promises, 'writeFile').mockResolvedValue();
11+
process.env = { ...env }
1212
});
1313

1414
afterEach(() => {
@@ -24,9 +24,9 @@ describe('AssistantMemoryService', () => {
2424
const sourcePath = './.env';
2525
const envVariables = 'ASSISTANT_ID=123\n';
2626
const id = '456';
27-
const readFileSpy = jest
28-
.spyOn(fs.promises, 'readFile')
29-
.mockResolvedValue(envVariables);
27+
const readFileSpy = jest.spyOn(fs.promises, 'readFile').mockResolvedValue({
28+
toString: jest.fn().mockReturnValue('ASSISTANT_ID=123\n'),
29+
} as unknown as Buffer);
3030
const writeFileSpy = jest
3131
.spyOn(fs.promises, 'writeFile')
3232
.mockResolvedValue();
@@ -48,18 +48,5 @@ describe('AssistantMemoryService', () => {
4848
);
4949
});
5050

51-
it('should log error', async () => {
52-
const error = new Error('error');
53-
const sourcePath = './.env';
54-
const readFileSpy = jest
55-
.spyOn(fs.promises, 'readFile')
56-
.mockRejectedValue(error);
57-
const loggerSpy = jest.spyOn(assistantMemoryService['logger'], 'error');
58-
59-
await assistantMemoryService.saveAssistantId('456');
60-
61-
expect(readFileSpy).toHaveBeenCalledWith(sourcePath);
62-
expect(loggerSpy).toHaveBeenCalledWith(`Can't save variable: ${error}`);
63-
});
6451
});
6552
});

libs/ai-assistant/src/lib/assistant/assistant.service.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ describe('AssistantService', () => {
4242
.spyOn(aiService.provider.beta.assistants, 'create')
4343
.mockResolvedValue({} as Assistant);
4444

45+
jest
46+
.spyOn(assistantMemoryService, 'saveAssistantId')
47+
.mockResolvedValue(undefined);
48+
4549
jest.spyOn(fs.promises, 'writeFile').mockResolvedValue();
4650
});
4751

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

161-
jest
162-
.spyOn(assistantMemoryService, 'saveAssistantId')
163-
.mockResolvedValue(undefined);
164-
165165
await assistantService.create();
166166

167167
expect(assistantMemoryService.saveAssistantId).toHaveBeenCalled();

libs/ai-assistant/src/lib/run/run.service.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,5 @@ describe('RunService', () => {
128128

129129
afterEach(() => {
130130
jest.clearAllMocks();
131-
runService.isRunning = true;
132131
});
133132
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"cp:ai-embedded": "cp ./dist/libs/ai-embedded/main.js ./apps/spa/src/assets/js/ai-embedded.js",
1717
"cp:readme": "cp ./README.md ./dist/libs/ai-assistant/README.md",
1818
"lint": "nx run-many --parallel --target=lint --all",
19+
"test:ai-assistant": "nx test ai-assistant",
20+
"test:spa": "nx test spa",
1921
"test": "nx run-many --parallel --target=test --all",
2022
"format": "nx format:write",
2123
"prepare": "husky"

0 commit comments

Comments
 (0)