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

Commit d932adc

Browse files
committed
refactor: add models list test cases
1 parent ecc95c7 commit d932adc

File tree

14 files changed

+92
-25
lines changed

14 files changed

+92
-25
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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(async () => {
12+
commandInstance = await CommandTestFactory.createTestingCommand({
13+
imports: [CommandModule],
14+
})
15+
// .overrideProvider(LogService)
16+
// .useValue({})
17+
.compile();
18+
const fileService =
19+
commandInstance.resolve<FileManagerService>(FileManagerService);
20+
21+
// Attempt to create test folder
22+
(await fileService).writeConfigFile({
23+
dataFolderPath: join(__dirname, 'test_data'),
24+
});
25+
});
26+
27+
afterEach(async () => {
28+
// Attempt to clean test folder
29+
try {
30+
await rmSync(join(__dirname, 'test_data'), {
31+
recursive: true,
32+
force: true,
33+
});
34+
} catch (e) {}
35+
});
36+
37+
describe('models list returns array of models', () => {
38+
test('empty model list', async () => {
39+
const logMock = stubMethod(console, 'table');
40+
41+
await CommandTestFactory.run(commandInstance, ['models', 'list']);
42+
expect(logMock.firstCall?.args[0]).toBeInstanceOf(Array);
43+
expect(logMock.firstCall?.args[0].length).toBe(0);
44+
});
45+
46+
test('many models in the list', async () => {
47+
const logMock = stubMethod(console, 'table');
48+
49+
mkdirSync(join(__dirname, 'test_data', 'models'), { recursive: true });
50+
writeFileSync(
51+
join(__dirname, 'test_data', 'models', 'test.yaml'),
52+
'model: test',
53+
'utf8',
54+
);
55+
56+
await CommandTestFactory.run(commandInstance, ['models', 'list']);
57+
expect(logMock.firstCall?.args[0]).toBeInstanceOf(Array);
58+
expect(logMock.firstCall?.args[0].length).toBe(1);
59+
expect(logMock.firstCall?.args[0][0].id).toBe('test');
60+
});
61+
});

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 = [

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FileManagerService } from '@/file-manager/file-manager.service';
2-
import { databaseFile } from '@/../constant';
2+
import { databaseFile } from '@/infrastructure/constants/cortex';
33
import { join } from 'path';
44
import { DataSource } from 'typeorm';
55

0 commit comments

Comments
 (0)