From 93767259bd1fcd898cb1507dc278d7724f64e55e Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Mon, 22 Jan 2024 16:27:59 +0100 Subject: [PATCH] add some unit tests for applicationManager --- .../src/managers/applicationManager.spec.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/backend/src/managers/applicationManager.spec.ts diff --git a/packages/backend/src/managers/applicationManager.spec.ts b/packages/backend/src/managers/applicationManager.spec.ts new file mode 100644 index 000000000..b999536e0 --- /dev/null +++ b/packages/backend/src/managers/applicationManager.spec.ts @@ -0,0 +1,56 @@ +import { expect, test, vi } from 'vitest'; +import { ApplicationManager } from './applicationManager'; +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 path from 'path'; + +test('appUserDirectory should be under home directory', () => { + vi.spyOn(os, 'homedir').mockReturnValue('/home/user'); + const manager = new ApplicationManager({} as GitManager, {} as RecipeStatusRegistry, {} as ExtensionContext); + expect(manager.appUserDirectory).toMatch(/^\/home\/user/); +}); + +test('getLocalModels should return models in local directory', () => { + vi.spyOn(os, 'homedir').mockReturnValue('/home/user'); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.spyOn(fs, 'readdirSync').mockImplementation((dir: string): any => { + // TODO(feloy): fix any + if (dir.endsWith('model-id-1') || dir.endsWith('model-id-2')) { + const base = path.basename(dir); + return [base + '-model']; + } else { + return [ + { + isDirectory: () => true, + path: '/home/user/appstudio-dir', + name: 'model-id-1', + }, + { + isDirectory: () => true, + path: '/home/user/appstudio-dir', + name: 'model-id-2', + }, + { + isDirectory: () => false, + path: '/home/user/appstudio-dir', + name: 'other-file-should-be-ignored.txt', + }, + ] as Dirent[]; + } + }); + const manager = new ApplicationManager({} as GitManager, {} as RecipeStatusRegistry, {} as ExtensionContext); + const models = manager.getLocalModels(); + expect(models).toEqual([ + { + id: 'model-id-1', + file: 'model-id-1-model', + }, + { + id: 'model-id-2', + file: 'model-id-2-model', + }, + ]); +});