Skip to content

Commit

Permalink
add backend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jan 25, 2024
1 parent bd11b63 commit 8e3d2ce
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
131 changes: 131 additions & 0 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,134 @@ test('loadLocalModels should post a message with the message on disk and on cata
],
});
});

test('deleteLocalModel deletes the model folder', async () => {
let appdir: string;
if (process.platform === 'win32') {
appdir = 'C:\\home\\user\\aistudio';
} else {
appdir = '/home/user/aistudio';
}
const now = new Date();
mockFiles(now);
const rmSpy = vi.spyOn(fs.promises, 'rm');
rmSpy.mockResolvedValue();
const postMessageMock = vi.fn();
const manager = new ModelsManager(
appdir,
{
postMessage: postMessageMock,
} as unknown as Webview,
{
getModels: () => {
return [
{
id: 'model-id-1',
},
] as ModelInfo[];
},
} as CatalogManager,
);
manager.getLocalModelsFromDisk();
await manager.deleteLocalModel('model-id-1');
// check that the model's folder is removed from disk
if (process.platform === 'win32') {
expect(rmSpy).toBeCalledWith('C:\\home\\user\\aistudio\\models\\model-id-1', { recursive: true });
} else {
expect(rmSpy).toBeCalledWith('/home/user/aistudio/models/model-id-1', { recursive: true });
}
expect(postMessageMock).toHaveBeenCalledTimes(2);
// check that a state is sent with the model being deleted
expect(postMessageMock).toHaveBeenCalledWith({
id: 'new-local-models-state',
body: [
{
file: {
creation: now,
file: 'model-id-1-model',
id: 'model-id-1',
size: 32000,
path: path.resolve(dirent[0].path, dirent[0].name, 'model-id-1-model'),
},
id: 'model-id-1',
state: 'deleting',
},
],
});
// check that a new state is sent with the model removed
expect(postMessageMock).toHaveBeenCalledWith({
id: 'new-local-models-state',
body: [],
});
});

test('deleteLocalModel fails to delete the model folder', async () => {
let appdir: string;
if (process.platform === 'win32') {
appdir = 'C:\\home\\user\\aistudio';
} else {
appdir = '/home/user/aistudio';
}
const now = new Date();
mockFiles(now);
const rmSpy = vi.spyOn(fs.promises, 'rm');
rmSpy.mockRejectedValue(new Error('failed'));
const postMessageMock = vi.fn();
const manager = new ModelsManager(
appdir,
{
postMessage: postMessageMock,
} as unknown as Webview,
{
getModels: () => {
return [
{
id: 'model-id-1',
},
] as ModelInfo[];
},
} as CatalogManager,
);
manager.getLocalModelsFromDisk();
await manager.deleteLocalModel('model-id-1');
// check that the model's folder is removed from disk
if (process.platform === 'win32') {
expect(rmSpy).toBeCalledWith('D:\\home\\user\\aistudio\\models\\model-id-1', { recursive: true });
} else {
expect(rmSpy).toBeCalledWith('/home/user/aistudio/models/model-id-1', { recursive: true });
}
expect(postMessageMock).toHaveBeenCalledTimes(2);
// check that a state is sent with the model being deleted
expect(postMessageMock).toHaveBeenCalledWith({
id: 'new-local-models-state',
body: [
{
file: {
creation: now,
file: 'model-id-1-model',
id: 'model-id-1',
size: 32000,
path: path.resolve(dirent[0].path, dirent[0].name, 'model-id-1-model'),
},
id: 'model-id-1',
state: 'deleting',
},
],
});
// check that a new state is sent with the model non removed
expect(postMessageMock).toHaveBeenCalledWith({
id: 'new-local-models-state',
body: [
{
file: {
creation: now,
file: 'model-id-1-model',
id: 'model-id-1',
size: 32000,
path: path.resolve(dirent[0].path, dirent[0].name, 'model-id-1-model'),
},
id: 'model-id-1',
},
],
});
});
6 changes: 5 additions & 1 deletion packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ export class ModelsManager {
return path.resolve(this.#modelsDir, modelId, info.file);
}

getLocalModelFolder(modelId: string): string {
return path.resolve(this.#modelsDir, modelId);
}

getLocalModels(): LocalModelInfo[] {
return Array.from(this.#localModels.values());
}

async deleteLocalModel(modelId: string): Promise<void> {
const modelDir = this.getLocalModelPath(modelId);
const modelDir = this.getLocalModelFolder(modelId);
this.#deleted.add(modelId);
await this.sendModelsInfo();
try {
Expand Down

0 comments on commit 8e3d2ce

Please sign in to comment.