Skip to content

Commit

Permalink
uni tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jan 29, 2024
1 parent edc8f89 commit 772c882
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
61 changes: 61 additions & 0 deletions packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { AIConfig, ContainerConfig } from '../models/AIConfig';
import * as portsUtils from '../utils/ports';
import { goarch } from '../utils/arch';
import * as utils from '../utils/utils';
import type { TelemetryLogger } from '@podman-desktop/api';

const mocks = vi.hoisted(() => {
return {
Expand All @@ -27,6 +28,7 @@ const mocks = vi.hoisted(() => {
startContainerMock: vi.fn(),
startPod: vi.fn(),
deleteContainerMock: vi.fn(),
logUsageMock: vi.fn(),
};
});
vi.mock('../models/AIConfig', () => ({
Expand Down Expand Up @@ -143,6 +145,9 @@ describe('pullApplication', () => {
isModelOnDisk: isModelOnDiskMock,
getLocalModelPath: getLocalModelPathMock,
} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
doDownloadModelWrapperSpy = vi.spyOn(manager, 'doDownloadModelWrapper');
doDownloadModelWrapperSpy.mockResolvedValue('path');
Expand Down Expand Up @@ -282,6 +287,9 @@ describe('doCheckout', () => {
} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
await manager.doCheckout('repo', 'folder', taskUtils);
expect(cloneRepositoryMock).toBeCalledWith('repo', 'folder');
Expand Down Expand Up @@ -309,6 +317,9 @@ describe('doCheckout', () => {
} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
await manager.doCheckout('repo', 'folder', taskUtils);
expect(mkdirSyncMock).not.toHaveBeenCalled();
Expand All @@ -331,6 +342,9 @@ describe('getConfiguration', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
expect(() => manager.getConfiguration('config', 'local')).toThrowError(
Expand All @@ -344,6 +358,9 @@ describe('getConfiguration', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
const stats = {
Expand Down Expand Up @@ -378,6 +395,9 @@ describe('downloadModel', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{ isModelOnDisk: isModelOnDiskMock } as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const doDownloadModelWrapperMock = vi
.spyOn(manager, 'doDownloadModelWrapper')
Expand All @@ -401,6 +421,7 @@ describe('downloadModel', () => {
},
state: 'loading',
});
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.download', { 'model.id': 'id' });
});
test('retrieve model path if already on disk', async () => {
const isModelOnDiskMock = vi.fn().mockReturnValue(true);
Expand All @@ -413,6 +434,9 @@ describe('downloadModel', () => {
isModelOnDisk: isModelOnDiskMock,
getLocalModelPath: getLocalModelPathMock,
} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
await manager.downloadModel(
{
Expand All @@ -431,6 +455,7 @@ describe('downloadModel', () => {
},
state: 'success',
});
expect(mocks.logUsageMock).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -458,6 +483,9 @@ describe('filterContainers', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const containers = manager.filterContainers(aiConfig);
expect(containers.length).toBe(0);
Expand Down Expand Up @@ -493,6 +521,9 @@ describe('filterContainers', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const containers = manager.filterContainers(aiConfig);
expect(containers.length).toBe(1);
Expand Down Expand Up @@ -538,6 +569,9 @@ describe('filterContainers', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const containers = manager.filterContainers(aiConfig);
expect(containers.length).toBe(2);
Expand All @@ -553,6 +587,9 @@ describe('getRandomName', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const randomName = manager.getRandomName('base');
expect(randomName).not.equal('base');
Expand All @@ -564,6 +601,9 @@ describe('getRandomName', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const randomName = manager.getRandomName('');
expect(randomName.length).toBeGreaterThan(0);
Expand All @@ -586,6 +626,9 @@ describe('buildImages', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
test('setTaskState should be called with error if context does not exist', async () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
Expand Down Expand Up @@ -655,6 +698,9 @@ describe('createPod', async () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
test('throw an error if there is no sample image', async () => {
const images = [imageInfo2];
Expand Down Expand Up @@ -705,6 +751,9 @@ describe('createApplicationPod', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const images = [imageInfo1, imageInfo2];
test('throw if createPod fails', async () => {
Expand Down Expand Up @@ -755,6 +804,9 @@ describe('doDownloadModelWrapper', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
test('returning model path if model has been downloaded', async () => {
vi.spyOn(manager, 'doDownloadModel').mockImplementation(
Expand Down Expand Up @@ -805,6 +857,9 @@ describe('restartContainerWhenEndpointIsUp', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
test('restart container if endpoint is alive', async () => {
vi.spyOn(utils, 'isEndpointAlive').mockResolvedValue(true);
Expand All @@ -819,6 +874,9 @@ describe('runApplication', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const pod: PodInfo = {
engineId: 'engine',
Expand Down Expand Up @@ -852,6 +910,9 @@ describe('createAndAddContainersToPod', () => {
{} as unknown as GitManager,
{} as unknown as RecipeStatusRegistry,
{} as unknown as ModelsManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
const pod: PodInfo = {
engineId: 'engine',
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { GitManager } from './gitManager';
import fs from 'fs';
import * as https from 'node:https';
import * as path from 'node:path';
import { type PodCreatePortOptions, containerEngine, TelemetryLogger } from '@podman-desktop/api';
import { type PodCreatePortOptions, containerEngine, type TelemetryLogger } from '@podman-desktop/api';
import type { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import type { AIConfig, AIConfigFile, ContainerConfig } from '../models/AIConfig';
import { parseYaml } from '../models/AIConfig';
Expand Down
31 changes: 28 additions & 3 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import os from 'os';
import fs from 'node:fs';
import path from 'node:path';
import { ModelsManager } from './modelsManager';
import type { Webview } from '@podman-desktop/api';
import type { TelemetryLogger, Webview } from '@podman-desktop/api';
import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';

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

Expand Down Expand Up @@ -89,7 +90,14 @@ test('getLocalModelsFromDisk should get models in local directory', () => {
} else {
appdir = '/home/user/aistudio';
}
const manager = new ModelsManager(appdir, {} as Webview, {} as CatalogManager);
const manager = new ModelsManager(
appdir,
{} as Webview,
{} as CatalogManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
manager.getLocalModelsFromDisk();
expect(manager.getLocalModels()).toEqual([
{
Expand Down Expand Up @@ -119,7 +127,14 @@ test('getLocalModelsFromDisk should return an empty array if the models folder d
} else {
appdir = '/home/user/aistudio';
}
const manager = new ModelsManager(appdir, {} as Webview, {} as CatalogManager);
const manager = new ModelsManager(
appdir,
{} as Webview,
{} as CatalogManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
manager.getLocalModelsFromDisk();
expect(manager.getLocalModels()).toEqual([]);
if (process.platform === 'win32') {
Expand Down Expand Up @@ -154,6 +169,9 @@ test('loadLocalModels should post a message with the message on disk and on cata
] as ModelInfo[];
},
} as CatalogManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
await manager.loadLocalModels();
expect(postMessageMock).toHaveBeenNthCalledWith(1, {
Expand Down Expand Up @@ -199,6 +217,9 @@ test('deleteLocalModel deletes the model folder', async () => {
] as ModelInfo[];
},
} as CatalogManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
manager.getLocalModelsFromDisk();
await manager.deleteLocalModel('model-id-1');
Expand Down Expand Up @@ -231,6 +252,7 @@ test('deleteLocalModel deletes the model folder', async () => {
id: 'new-local-models-state',
body: [],
});
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.delete', { 'model.id': 'model-id-1' });
});

test('deleteLocalModel fails to delete the model folder', async () => {
Expand Down Expand Up @@ -259,6 +281,9 @@ test('deleteLocalModel fails to delete the model folder', async () => {
] as ModelInfo[];
},
} as CatalogManager,
{
logUsage: mocks.logUsageMock,
} as unknown as TelemetryLogger,
);
manager.getLocalModelsFromDisk();
await manager.deleteLocalModel('model-id-1');
Expand Down

0 comments on commit 772c882

Please sign in to comment.