Skip to content

Commit

Permalink
fix: correct tests setup (#654)
Browse files Browse the repository at this point in the history
louis-menlo authored Jun 3, 2024
1 parent 51a58cb commit b879f66
Showing 21 changed files with 168 additions and 44 deletions.
5 changes: 4 additions & 1 deletion cortex-js/package.json
Original file line number Diff line number Diff line change
@@ -107,6 +107,9 @@
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"@/(.*)$": "<rootDir>/$1"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AssistantsController } from './assistants.controller';
import { AssistantsUsecases } from '@/usecases/assistants/assistants.usecases';
import { DatabaseModule } from '../database/database.module';

describe('AssistantsController', () => {
let controller: AssistantsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
controllers: [AssistantsController],
providers: [AssistantsUsecases],
exports: [AssistantsUsecases],
}).compile();

controller = module.get<AssistantsController>(AssistantsController);
20 changes: 15 additions & 5 deletions cortex-js/src/infrastructure/controllers/assistants.controller.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import {
ApiOperation,
ApiParam,
ApiTags,
ApiResponse
ApiResponse,
} from '@nestjs/swagger';
import { AssistantEntity } from '../entities/assistant.entity';
import { TransformInterceptor } from '../interceptors/transform.interceptor';
@@ -41,7 +41,8 @@ export class AssistantsController {

@ApiOperation({
summary: 'List assistants',
description: 'Retrieves all the available assistants along with their settings.',
description:
'Retrieves all the available assistants along with their settings.',
})
@ApiOkResponse({
description: 'Ok',
@@ -54,13 +55,18 @@ export class AssistantsController {

@ApiOperation({
summary: 'Get assistant',
description: "Retrieves a specific assistant defined by an assistant's `id`.",
description:
"Retrieves a specific assistant defined by an assistant's `id`.",
})
@ApiOkResponse({
description: 'Ok',
type: AssistantEntity,
})
@ApiParam({ name: 'id', required: true, description: "The unique identifier of the assistant." })
@ApiParam({
name: 'id',
required: true,
description: 'The unique identifier of the assistant.',
})
@Get(':id')
findOne(@Param('id') id: string) {
return this.assistantsService.findOne(id);
@@ -75,7 +81,11 @@ export class AssistantsController {
description: 'The assistant has been successfully deleted.',
type: DeleteAssistantResponseDto,
})
@ApiParam({ name: 'id', required: true, description: "The unique identifier of the assistant." })
@ApiParam({
name: 'id',
required: true,
description: 'The unique identifier of the assistant.',
})
@Delete(':id')
remove(@Param('id') id: string) {
return this.assistantsService.remove(id);
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ChatController } from './chat.controller';
import { ChatUsecases } from '@/usecases/chat/chat.usecases';
import { DatabaseModule } from '../database/database.module';
import { ExtensionModule } from '../repositories/extensions/extension.module';

describe('ChatController', () => {
let controller: ChatController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule, ExtensionModule],
controllers: [ChatController],
providers: [ChatUsecases],
}).compile();

controller = module.get<ChatController>(ChatController);
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MessagesController } from './messages.controller';
import { MessagesUsecases } from '@/usecases/messages/messages.usecases';
import { DatabaseModule } from '../database/database.module';

describe('MessagesController', () => {
let controller: MessagesController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
controllers: [MessagesController],
providers: [MessagesUsecases],
exports: [MessagesUsecases],
}).compile();

controller = module.get<MessagesController>(MessagesController);
52 changes: 36 additions & 16 deletions cortex-js/src/infrastructure/controllers/messages.controller.ts
Original file line number Diff line number Diff line change
@@ -15,14 +15,7 @@ import { UpdateMessageDto } from '@/infrastructure/dtos/messages/update-message.
import { ListMessagesResponseDto } from '@/infrastructure/dtos/messages/list-message.dto';
import { GetMessageResponseDto } from '@/infrastructure/dtos/messages/get-message.dto';
import { DeleteMessageResponseDto } from '@/infrastructure/dtos/messages/delete-message.dto';
import {
ApiCreatedResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiTags,
ApiResponse
} from '@nestjs/swagger';
import { ApiOperation, ApiParam, ApiTags, ApiResponse } from '@nestjs/swagger';
import { TransformInterceptor } from '../interceptors/transform.interceptor';

@ApiTags('Messages')
@@ -37,7 +30,10 @@ export class MessagesController {
description: 'The message has been successfully created.',
type: CreateMessageDto,
})
@ApiOperation({ summary: 'Create message', description: "Creates a message in a thread." })
@ApiOperation({
summary: 'Create message',
description: 'Creates a message in a thread.',
})
@Post()
create(@Body() createMessageDto: CreateMessageDto) {
return this.messagesUsecases.create(createMessageDto);
@@ -49,7 +45,10 @@ export class MessagesController {
description: 'Ok',
type: ListMessagesResponseDto,
})
@ApiOperation({ summary: 'List messages', description: "Retrieves all the messages in a thread." })
@ApiOperation({
summary: 'List messages',
description: 'Retrieves all the messages in a thread.',
})
@Get()
findAll() {
return this.messagesUsecases.findAll();
@@ -61,8 +60,15 @@ export class MessagesController {
description: 'Ok',
type: GetMessageResponseDto,
})
@ApiOperation({ summary: 'Retrieve message', description: "Retrieves a specific message defined by a message's `id`." })
@ApiParam({ name: 'id', required: true, description: "The unique identifier of the message." })
@ApiOperation({
summary: 'Retrieve message',
description: "Retrieves a specific message defined by a message's `id`.",
})
@ApiParam({
name: 'id',
required: true,
description: 'The unique identifier of the message.',
})
@Get(':id')
findOne(@Param('id') id: string) {
return this.messagesUsecases.findOne(id);
@@ -74,8 +80,15 @@ export class MessagesController {
description: 'The message has been successfully updated.',
type: UpdateMessageDto,
})
@ApiOperation({ summary: 'Update message', description: "Updates a specific message defined by a message's `id`." })
@ApiParam({ name: 'id', required: true, description: "The unique identifier of the message." })
@ApiOperation({
summary: 'Update message',
description: "Updates a specific message defined by a message's `id`.",
})
@ApiParam({
name: 'id',
required: true,
description: 'The unique identifier of the message.',
})
@Patch(':id')
update(@Param('id') id: string, @Body() updateMessageDto: UpdateMessageDto) {
return this.messagesUsecases.update(id, updateMessageDto);
@@ -87,8 +100,15 @@ export class MessagesController {
description: 'Successfully deleted the message.',
type: DeleteMessageResponseDto,
})
@ApiOperation({ summary: 'Delete message', description: "Deletes a specific message defined by a message's `id`." })
@ApiParam({ name: 'id', required: true, description: "The unique identifier of the message." })
@ApiOperation({
summary: 'Delete message',
description: "Deletes a specific message defined by a message's `id`.",
})
@ApiParam({
name: 'id',
required: true,
description: 'The unique identifier of the message.',
})
@Delete(':id')
remove(@Param('id') id: string) {
return this.messagesUsecases.remove(id);
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ModelsController } from './models.controller';
import { ModelsUsecases } from '@/usecases/models/models.usecases';
import { DatabaseModule } from '../database/database.module';
import { ExtensionModule } from '../repositories/extensions/extension.module';
import { FileManagerModule } from '@/file-manager/file-manager.module';
import { HttpModule } from '@nestjs/axios';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';

describe('ModelsController', () => {
let controller: ModelsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule, ExtensionModule, FileManagerModule, HttpModule],
controllers: [ModelsController],
providers: [ModelsUsecases],
providers: [ModelsUsecases, CortexUsecases],
exports: [ModelsUsecases],
}).compile();

controller = module.get<ModelsController>(ModelsController);
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ThreadsController } from './threads.controller';
import { ThreadsUsecases } from '@/usecases/threads/threads.usecases';
import { DatabaseModule } from '../database/database.module';

describe('ThreadsController', () => {
let controller: ThreadsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
controllers: [ThreadsController],
providers: [ThreadsUsecases],
}).compile();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileManagerService } from '@/file-manager/file-manager.service';
import { databaseFile } from 'constant';
import { databaseFile } from '@/../constant';
import { join } from 'path';
import { DataSource } from 'typeorm';

Original file line number Diff line number Diff line change
@@ -25,31 +25,50 @@ export class CreateChatCompletionDto {
@IsString()
model: string;

@ApiProperty({description: "Determines the format for output generation. If set to `true`, the output is generated continuously, allowing for real-time streaming of responses. If set to `false`, the output is delivered in a single JSON file."})
@ApiProperty({
description:
'Determines the format for output generation. If set to `true`, the output is generated continuously, allowing for real-time streaming of responses. If set to `false`, the output is delivered in a single JSON file.',
})
@IsBoolean()
stream: boolean;

@ApiProperty({description: "Sets the upper limit on the number of tokens the model can generate in a single output."})
@ApiProperty({
description:
'Sets the upper limit on the number of tokens the model can generate in a single output.',
})
@IsNumber()
max_tokens: number;

@ApiProperty({description: "Defines specific tokens or phrases that signal the model to stop producing further output."})
@ApiProperty({
description:
'Defines specific tokens or phrases that signal the model to stop producing further output.',
})
@IsArray()
stop: string[];

@ApiProperty({description: "Modifies the likelihood of the model repeating the same words or phrases within a single output."})
@ApiProperty({
description:
'Modifies the likelihood of the model repeating the same words or phrases within a single output.',
})
@IsNumber()
frequency_penalty: number;

@ApiProperty({description: "Reduces the likelihood of repeating tokens, promoting novelty in the output."})
@ApiProperty({
description:
'Reduces the likelihood of repeating tokens, promoting novelty in the output.',
})
@IsNumber()
presence_penalty: number;

@ApiProperty({description: "Influences the randomness of the model's output."})
@ApiProperty({
description: "Influences the randomness of the model's output.",
})
@IsNumber()
temperature: number;

@ApiProperty({description: "Sets probability threshold for more relevant outputs."})
@ApiProperty({
description: 'Sets probability threshold for more relevant outputs.',
})
@IsNumber()
top_p: number;
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { PromptTemplate } from '@/domain/models/prompt-template.interface';
import { join } from 'path';
import { Model, ModelSettingParams } from '@/domain/models/model.interface';
import { HttpService } from '@nestjs/axios';
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
import { defaultCortexCppHost, defaultCortexCppPort } from '@/../constant';
import { readdirSync } from 'node:fs';
import { normalizeModelId } from '@/infrastructure/commanders/utils/normalize-model-id';
import { firstValueFrom } from 'rxjs';
29 changes: 23 additions & 6 deletions cortex-js/src/main.ts
Original file line number Diff line number Diff line change
@@ -25,13 +25,30 @@ async function bootstrap() {

const config = new DocumentBuilder()
.setTitle('Cortex API')
.setDescription('Cortex API provides a command-line interface (CLI) for seamless interaction with large language models (LLMs). Fully compatible with the [OpenAI API](https://platform.openai.com/docs/api-reference), it enables straightforward command execution and management of LLM interactions.')
.setDescription(
'Cortex API provides a command-line interface (CLI) for seamless interaction with large language models (LLMs). Fully compatible with the [OpenAI API](https://platform.openai.com/docs/api-reference), it enables straightforward command execution and management of LLM interactions.',
)
.setVersion('1.0')
.addTag('Inference', 'This endpoint initiates interaction with a Language Learning Model (LLM).')
.addTag('Assistants', 'These endpoints manage the lifecycle of an Assistant within a conversation thread.')
.addTag('Models', 'These endpoints provide a list and descriptions of all available models within the Cortex framework.')
.addTag('Messages', "These endpoints manage the retrieval and storage of conversation content, including responses from LLMs and other metadata related to chat interactions.")
.addTag('Threads', 'These endpoints handle the creation, retrieval, updating, and deletion of conversation threads.')
.addTag(
'Inference',
'This endpoint initiates interaction with a Language Learning Model (LLM).',
)
.addTag(
'Assistants',
'These endpoints manage the lifecycle of an Assistant within a conversation thread.',
)
.addTag(
'Models',
'These endpoints provide a list and descriptions of all available models within the Cortex framework.',
)
.addTag(
'Messages',
'These endpoints manage the retrieval and storage of conversation content, including responses from LLMs and other metadata related to chat interactions.',
)
.addTag(
'Threads',
'These endpoints handle the creation, retrieval, updating, and deletion of conversation threads.',
)
.addServer('http://localhost:1337')
.addServer('http://localhost:1337/v1')
.build();
3 changes: 3 additions & 0 deletions cortex-js/src/usecases/assistants/assistants.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AssistantsUsecases } from './assistants.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';

describe('AssistantsService', () => {
let service: AssistantsUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
exports: [AssistantsUsecases],
providers: [AssistantsUsecases],
}).compile();

4 changes: 4 additions & 0 deletions cortex-js/src/usecases/chat/chat.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ChatUsecases } from './chat.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';
import { ExtensionModule } from '@/infrastructure/repositories/extensions/extension.module';

describe('ChatService', () => {
let service: ChatUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule, ExtensionModule],
providers: [ChatUsecases],
exports: [ChatUsecases],
}).compile();

service = module.get<ChatUsecases>(ChatUsecases);
5 changes: 5 additions & 0 deletions cortex-js/src/usecases/cortex/cortex.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CortexUsecases } from './cortex.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';
import { HttpModule } from '@nestjs/axios';
import { FileManagerModule } from '@/file-manager/file-manager.module';

describe('CortexUsecases', () => {
let cortexUsecases: CortexUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule, HttpModule, FileManagerModule],
providers: [CortexUsecases],
exports: [],
}).compile();

cortexUsecases = module.get<CortexUsecases>(CortexUsecases);
2 changes: 1 addition & 1 deletion cortex-js/src/usecases/cortex/cortex.usecases.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { ChildProcess, spawn } from 'child_process';
import { join } from 'path';
import { CortexOperationSuccessfullyDto } from '@/infrastructure/dtos/cortex/cortex-operation-successfully.dto';
import { HttpService } from '@nestjs/axios';
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
import { defaultCortexCppHost, defaultCortexCppPort } from '@/../constant';
import { existsSync } from 'node:fs';
import { firstValueFrom } from 'rxjs';
import { FileManagerService } from '@/file-manager/file-manager.service';
3 changes: 3 additions & 0 deletions cortex-js/src/usecases/messages/messages.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MessagesUsecases } from './messages.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';

describe('MessagesService', () => {
let service: MessagesUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
providers: [MessagesUsecases],
exports: [MessagesUsecases],
}).compile();

service = module.get<MessagesUsecases>(MessagesUsecases);
13 changes: 13 additions & 0 deletions cortex-js/src/usecases/models/models.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ModelsUsecases } from './models.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';
import { ModelsModule } from './models.module';
import { ExtensionModule } from '@/infrastructure/repositories/extensions/extension.module';
import { FileManagerModule } from '@/file-manager/file-manager.module';
import { HttpModule } from '@nestjs/axios';

describe('ModelsService', () => {
let service: ModelsUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
DatabaseModule,
ModelsModule,
ExtensionModule,
FileManagerModule,
HttpModule,
],
providers: [ModelsUsecases],
exports: [ModelsUsecases],
}).compile();

service = module.get<ModelsUsecases>(ModelsUsecases);
2 changes: 2 additions & 0 deletions cortex-js/src/usecases/threads/threads.usecases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ThreadsUsecases } from './threads.usecases';
import { DatabaseModule } from '@/infrastructure/database/database.module';

describe('ThreadsService', () => {
let service: ThreadsUsecases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
providers: [ThreadsUsecases],
}).compile();

3 changes: 3 additions & 0 deletions cortex-js/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -5,5 +5,8 @@
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"@/(.*)$": "<rootDir>/../src/$1"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../@/app.module';
import request from 'supertest';
import { AppModule } from '@/app.module';

describe('AppController (e2e)', () => {
describe('ModelsController (e2e)', () => {
let app: INestApplication;

beforeEach(async () => {
@@ -17,8 +17,8 @@ describe('AppController (e2e)', () => {

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.get('/models')
.expect(200)
.expect('Hello World!');
.expect((e) => Array.isArray(e));
});
});

0 comments on commit b879f66

Please sign in to comment.