diff --git a/packages/backend/src/managers/applicationManager.spec.ts b/packages/backend/src/managers/applicationManager.spec.ts index dca3a71d2..3206df0c4 100644 --- a/packages/backend/src/managers/applicationManager.spec.ts +++ b/packages/backend/src/managers/applicationManager.spec.ts @@ -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 { TelemetryLogger } from '@podman-desktop/api'; const mocks = vi.hoisted(() => { return { @@ -27,6 +28,7 @@ const mocks = vi.hoisted(() => { startContainerMock: vi.fn(), startPod: vi.fn(), deleteContainerMock: vi.fn(), + logUsageMock: vi.fn(), }; }); vi.mock('../models/AIConfig', () => ({ @@ -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'); @@ -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'); @@ -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(); @@ -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( @@ -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 = { @@ -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') @@ -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); @@ -413,6 +434,9 @@ describe('downloadModel', () => { isModelOnDisk: isModelOnDiskMock, getLocalModelPath: getLocalModelPathMock, } as unknown as ModelsManager, + { + logUsage: mocks.logUsageMock, + } as unknown as TelemetryLogger, ); await manager.downloadModel( { @@ -431,6 +455,7 @@ describe('downloadModel', () => { }, state: 'success', }); + expect(mocks.logUsageMock).not.toHaveBeenCalled(); }); }); @@ -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); @@ -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); @@ -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); @@ -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'); @@ -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); @@ -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); @@ -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]; @@ -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 () => { @@ -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( @@ -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); @@ -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', @@ -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', diff --git a/packages/backend/src/managers/modelsManager.spec.ts b/packages/backend/src/managers/modelsManager.spec.ts index 83f301309..6aa24394f 100644 --- a/packages/backend/src/managers/modelsManager.spec.ts +++ b/packages/backend/src/managers/modelsManager.spec.ts @@ -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(), }; }); @@ -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([ { @@ -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') { @@ -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, { @@ -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'); @@ -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 () => { @@ -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');