Skip to content

Commit

Permalink
test applicationManager.pullApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jan 22, 2024
1 parent b3e8634 commit 6a0c083
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import type { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import type { ExtensionContext } from '@podman-desktop/api';
import type { GitManager } from './gitManager';
import os from 'os';
import fs, { type Dirent } from 'fs';
import fs, { Stats, type Dirent } from 'fs';
import path from 'path';
import { Recipe } from '@shared/src/models/IRecipe';

Check failure on line 9 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

All imports in the declaration are only used as types. Use `import type`
import { ModelInfo } from '@shared/src/models/IModelInfo';

Check failure on line 10 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

All imports in the declaration are only used as types. Use `import type`

Check failure on line 10 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

All imports in the declaration are only used as types. Use `import type`

Check failure on line 10 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

All imports in the declaration are only used as types. Use `import type`

const mocks = vi.hoisted(() => {
return {
parseYamlMock: vi.fn(),
};
});

vi.mock('../models/AIConfig', () => ({
parseYaml: mocks.parseYamlMock,
}));

test('appUserDirectory should be under home directory', () => {
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
Expand Down Expand Up @@ -54,3 +66,73 @@ test('getLocalModels should return models in local directory', () => {
},
]);
});

test('pullApplication should clone repository and call downloadModelMain', async () => {
const setStatusMock = vi.fn();
const cloneRepositoryMock = vi.fn();
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
vi.spyOn(fs, 'mkdirSync').mockReturnValue(undefined);
vi.spyOn(fs, 'existsSync').mockImplementation((path: string) => {
if (path.endsWith('recipe1')) {
return false;
} else if (path.endsWith('ai-studio.yaml')) {
return true;
}
return false;
});
vi.spyOn(fs, 'statSync').mockImplementation((path: string) => {
if (path.endsWith('recipe1')) {
const stat = new Stats();
stat.isDirectory = () => true;
return stat;
} else if (path.endsWith('ai-studio.yaml')) {
const stat = new Stats();
stat.isDirectory = () => false;
return stat;
}
});
vi.spyOn(fs, 'readFileSync').mockImplementation((path: string) => {

Check failure on line 94 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

'path' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 94 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

'path' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 94 in packages/backend/src/managers/applicationManager.spec.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

'path' is defined but never used. Allowed unused args must match /^_/u
return '';
});
mocks.parseYamlMock.mockReturnValue({
application: {
containers: [],
},
});
const manager = new ApplicationManager(
{
cloneRepository: cloneRepositoryMock,
} as unknown as GitManager,
{
setStatus: setStatusMock,
} as unknown as RecipeStatusRegistry,
{} as ExtensionContext,
);

const getLocalModelsSpy = vi.spyOn(manager, 'getLocalModels');
getLocalModelsSpy.mockReturnValue([]);
const downloadModelMainSpy = vi.spyOn(manager, 'downloadModelMain');
downloadModelMainSpy.mockResolvedValue('');

const recipe: Recipe = {
id: 'recipe1',
name: 'Recipe 1',
categories: [],
description: '',
readme: '',
repository: 'repo',
};
const model: ModelInfo = {
id: 'model1',
description: '',
hw: '',
license: '',
name: 'Model 1',
popularity: 1,
registry: '',
url: '',
};
await manager.pullApplication(recipe, model);
expect(cloneRepositoryMock).toHaveBeenNthCalledWith(1, 'repo', '/home/user/podman-desktop/ai-studio/recipe1');
expect(downloadModelMainSpy).toHaveBeenCalledOnce();
});

0 comments on commit 6a0c083

Please sign in to comment.