Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 09b920e

Browse files
authored
refactor: add models list test cases (#666)
1 parent ecc95c7 commit 09b920e

File tree

15 files changed

+101
-26
lines changed

15 files changed

+101
-26
lines changed

cortex-js/constant.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

cortex-js/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@
5555
"sqlite3": "^5.1.7",
5656
"typeorm": "^0.3.20",
5757
"ulid": "^2.3.0",
58-
"yaml": "^2.4.2",
58+
"update-notifier": "^5.0.0",
5959
"uuid": "^9.0.1",
60-
"update-notifier": "^5.0.0"
60+
"yaml": "^2.4.2"
6161
},
6262
"devDependencies": {
63-
"cpx": "^1.5.0",
6463
"@nestjs/cli": "^10.0.0",
6564
"@nestjs/schematics": "^10.0.0",
6665
"@nestjs/testing": "^10.0.0",
@@ -76,18 +75,21 @@
7675
"@types/uuid": "^9.0.8",
7776
"@typescript-eslint/eslint-plugin": "^6.0.0",
7877
"@typescript-eslint/parser": "^6.0.0",
78+
"cpx": "^1.5.0",
7979
"eslint": "^8.42.0",
8080
"eslint-config-prettier": "^9.0.0",
8181
"eslint-plugin-prettier": "^5.0.0",
82+
"hanbi": "^1.0.3",
8283
"jest": "^29.5.0",
84+
"nest-commander-testing": "^3.3.0",
8385
"prettier": "^3.0.0",
86+
"run-script-os": "^1.1.6",
8487
"source-map-support": "^0.5.21",
8588
"supertest": "^6.3.3",
8689
"ts-jest": "^29.1.0",
8790
"ts-loader": "^9.4.3",
8891
"tsconfig-paths": "^4.2.0",
89-
"typescript": "^5.1.3",
90-
"run-script-os": "^1.1.6"
92+
"typescript": "^5.1.3"
9193
},
9294
"files": [
9395
"dist"

cortex-js/src/file-manager/file-manager.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class FileManagerService {
4343
}
4444
}
4545

46-
private async writeConfigFile(config: Config): Promise<void> {
46+
async writeConfigFile(config: Config): Promise<void> {
4747
const homeDir = os.homedir();
4848
const configPath = join(homeDir, this.configFile);
4949

cortex-js/src/infrastructure/commanders/serve.command.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { spawn } from 'child_process';
2-
import { defaultCortexJsHost, defaultCortexJsPort } from 'constant';
2+
import {
3+
defaultCortexJsHost,
4+
defaultCortexJsPort,
5+
} from '@/infrastructure/constants/cortex';
36
import { CommandRunner, SubCommand, Option } from 'nest-commander';
47
import { join } from 'path';
58

cortex-js/src/infrastructure/commanders/shortcuts/run.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from 'nest-commander';
88
import { exit } from 'node:process';
99
import { ChatCliUsecases } from '../usecases/chat.cli.usecases';
10-
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
10+
import { defaultCortexCppHost, defaultCortexCppPort } from '@/infrastructure/constants/cortex';
1111
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';
1212
import { isLocalModel } from '../utils/normalize-model-id';
1313
import { ModelNotFoundException } from '@/infrastructure/exception/model-not-found.exception';
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { TestingModule } from '@nestjs/testing';
2+
import { stubMethod } from 'hanbi';
3+
import { CommandTestFactory } from 'nest-commander-testing';
4+
import { CommandModule } from '@/command.module';
5+
import { FileManagerService } from '@/file-manager/file-manager.service';
6+
import { join } from 'path';
7+
import { mkdirSync, rmSync, writeFileSync } from 'fs';
8+
9+
let commandInstance: TestingModule;
10+
11+
beforeEach(
12+
() =>
13+
new Promise<void>(async (res) => {
14+
commandInstance = await CommandTestFactory.createTestingCommand({
15+
imports: [CommandModule],
16+
})
17+
// .overrideProvider(LogService)
18+
// .useValue({})
19+
.compile();
20+
const fileService =
21+
await commandInstance.resolve<FileManagerService>(FileManagerService);
22+
23+
// Attempt to create test folder
24+
await fileService.writeConfigFile({
25+
dataFolderPath: join(__dirname, 'test_data'),
26+
});
27+
res();
28+
}),
29+
);
30+
31+
afterEach(
32+
() =>
33+
new Promise<void>(async (res) => {
34+
// Attempt to clean test folder
35+
rmSync(join(__dirname, 'test_data'), {
36+
recursive: true,
37+
force: true,
38+
});
39+
res();
40+
}),
41+
);
42+
43+
describe('models list returns array of models', () => {
44+
test('empty model list', async () => {
45+
const logMock = stubMethod(console, 'table');
46+
47+
await CommandTestFactory.run(commandInstance, ['models', 'list']);
48+
expect(logMock.firstCall?.args[0]).toBeInstanceOf(Array);
49+
expect(logMock.firstCall?.args[0].length).toBe(0);
50+
});
51+
52+
test('many models in the list', async () => {
53+
const logMock = stubMethod(console, 'table');
54+
55+
mkdirSync(join(__dirname, 'test_data', 'models'), { recursive: true });
56+
writeFileSync(
57+
join(__dirname, 'test_data', 'models', 'test.yaml'),
58+
'model: test',
59+
'utf8',
60+
);
61+
62+
await CommandTestFactory.run(commandInstance, ['models', 'list']);
63+
expect(logMock.firstCall?.args[0]).toBeInstanceOf(Array);
64+
expect(logMock.firstCall?.args[0].length).toBe(1);
65+
expect(logMock.firstCall?.args[0][0].id).toBe('test');
66+
});
67+
});

cortex-js/src/infrastructure/commanders/usecases/models.cli.usecases.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,14 @@ export class ModelsCliUsecases {
397397
private async parsePreset(preset?: string): Promise<object> {
398398
const presetsFolder = await this.fileService.getPresetsPath();
399399

400+
if (!existsSync(presetsFolder)) return {};
401+
400402
const presetFile = readdirSync(presetsFolder).find(
401403
(file) =>
402404
file.toLowerCase() === `${preset?.toLowerCase()}.yaml` ||
403405
file.toLowerCase() === `${preset?.toLocaleLowerCase()}.yml`,
404406
);
405-
if (!presetFile) throw new Error(`Preset ${preset} not found`);
407+
if (!presetFile) return {};
406408
const presetPath = join(presetsFolder, presetFile);
407409

408410
if (!preset || !existsSync(presetPath)) return {};

cortex-js/src/infrastructure/commanders/usecases/ps.cli.usecases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
2+
import { defaultCortexCppHost, defaultCortexCppPort } from '@/infrastructure/constants/cortex';
33

44
export interface ModelStat {
55
modelId: string;

cortex-js/src/infrastructure/constants/cortex.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { defaultCortexCppHost, defaultCortexCppPort } from '@/../constant';
1+
export const databaseName = 'cortex';
22

3+
export const databaseFile = `${databaseName}.db`;
4+
5+
export const defaultCortexJsHost = 'localhost';
6+
export const defaultCortexJsPort = 1337;
7+
8+
export const defaultCortexCppHost = '127.0.0.1';
9+
export const defaultCortexCppPort = 3928;
310
// CORTEX CPP
411
export const CORTEX_CPP_EMBEDDINGS_URL = (
512
host: string = defaultCortexCppHost,

cortex-js/src/infrastructure/database/mysql-database.providers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { databaseName } from 'constant';
1+
import { databaseName } from '@/infrastructure/constants/cortex';
22
import { DataSource } from 'typeorm';
33

44
export const mysqlDatabaseProviders = [

0 commit comments

Comments
 (0)