Skip to content

Commit

Permalink
chore(tsconfig): enabling strictNullChecks (#812)
Browse files Browse the repository at this point in the history
* chore(tsconfig): enabling strictNullChecks

Signed-off-by: axel7083 <[email protected]>

* fix: formatting

Signed-off-by: axel7083 <[email protected]>

* fix: tests

Signed-off-by: axel7083 <[email protected]>

* fix: tests

Signed-off-by: axel7083 <[email protected]>

* Apply suggestions from code review

Signed-off-by: axel7083 <[email protected]>

* Update packages/backend/src/studio.ts

Signed-off-by: axel7083 <[email protected]>

* fix: prettier

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Apr 22, 2024
1 parent 17f214b commit 529bc5b
Show file tree
Hide file tree
Showing 33 changed files with 205 additions and 138 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/managers/GPUManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const webviewMock = {

beforeEach(() => {
vi.resetAllMocks();
vi.mocked(webviewMock.postMessage).mockResolvedValue(undefined);
vi.mocked(webviewMock.postMessage).mockResolvedValue(true);

vi.mocked(getProviderContainerConnection).mockReturnValue({
providerId: 'dummyProviderId',
Expand Down
16 changes: 9 additions & 7 deletions packages/backend/src/managers/SnippetManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const telemetryMock = {

beforeEach(() => {
vi.resetAllMocks();
vi.mocked(webviewMock.postMessage).mockResolvedValue(undefined);
vi.mocked(webviewMock.postMessage).mockResolvedValue(true);
});

test('expect init to notify webview', () => {
Expand All @@ -59,9 +59,9 @@ test('expect postman-code-generators to have nodejs supported.', () => {
const languages = manager.getLanguageList();
const nodejs = languages.find(language => language.key === 'nodejs');
expect(nodejs).toBeDefined();
expect(nodejs.variants.length).toBeGreaterThan(0);
expect(nodejs?.variants.length).toBeGreaterThan(0);

const native = nodejs.variants.find(variant => variant.key === 'Request');
const native = nodejs?.variants.find(variant => variant.key === 'Request');
expect(native).toBeDefined();
});

Expand Down Expand Up @@ -97,9 +97,9 @@ test('expect snippet manager to have Quarkus Langchain4J supported.', () => {
const languages = manager.getLanguageList();
const java = languages.find(language => language.key === 'java');
expect(java).toBeDefined();
expect(java.variants.length).toBeGreaterThan(0);
expect(java?.variants.length).toBeGreaterThan(0);

const quarkus_langchain4j = java.variants.find(variant => variant.key === 'Quarkus Langchain4J');
const quarkus_langchain4j = java?.variants.find(variant => variant.key === 'Quarkus Langchain4J');
expect(quarkus_langchain4j).toBeDefined();
});

Expand All @@ -110,12 +110,14 @@ test('expect new variant to replace existing one if same name', () => {
const languages = manager.getLanguageList();
const java = languages.find(language => language.key === 'java');
expect(java).toBeDefined();
expect(java.variants.length).toBeGreaterThan(0);
expect(java?.variants.length).toBeGreaterThan(0);

if (!java) throw new Error('undefined java');

const oldVariantsNumber = java.variants.length;
manager.addVariant('java', java.variants[0].key, vi.fn());
const languages_updated = manager.getLanguageList();
const java_updated = languages_updated.find(language => language.key === 'java');
expect(java_updated).toBeDefined();
expect(java_updated.variants.length).equals(oldVariantsNumber);
expect(java_updated?.variants.length).equals(oldVariantsNumber);
});
4 changes: 3 additions & 1 deletion packages/backend/src/managers/SnippetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ export class SnippetManager extends Publisher<Language[]> implements Disposable

return new Promise((resolve, reject) => {
const request = new Request(requestOptions);
convert(language, variant, request, {}, (error: unknown, snippet: string) => {
convert(language, variant, request, {}, (error: unknown, snippet: string | undefined) => {
if (error) {
reject(error);
return;
} else if (snippet === undefined) {
throw new Error('undefined snippet');
}
resolve(snippet);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('pullApplication', () => {
license: '',
name: 'Model 1',
registry: '',
url: '',
url: 'dummy-url',
memory: 1000,
};
mocks.inspectContainerMock.mockResolvedValue({
Expand Down
54 changes: 30 additions & 24 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import type { Recipe } from '@shared/src/models/IRecipe';
import type { GitCloneInfo, GitManager } from './gitManager';
import fs from 'fs';
import * as path from 'node:path';
import {
type PodCreatePortOptions,
containerEngine,
type TelemetryLogger,
type PodInfo,
type Webview,
type HostConfig,
Disposable,
import { containerEngine, Disposable } from '@podman-desktop/api';
import type {
BuildImageOptions,
PodCreatePortOptions,
TelemetryLogger,
PodInfo,
Webview,
HostConfig,
HealthConfig,
} from '@podman-desktop/api';
import type { AIConfig, AIConfigFile, ContainerConfig } from '../models/AIConfig';
import { parseYamlFile } from '../models/AIConfig';
Expand Down Expand Up @@ -240,7 +241,10 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
let podInfo: ApplicationPodInfo;
try {
podInfo = await this.createPod(recipe, model, images);
task.labels['pod-id'] = podInfo.Id;
task.labels = {
...task.labels,
'pod-id': podInfo.Id,
};
} catch (e) {
console.error('error when creating pod', e);
task.state = 'error';
Expand Down Expand Up @@ -278,9 +282,9 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
const isQEMUVM = await isQEMUMachine();
await Promise.all(
images.map(async image => {
let hostConfig: HostConfig;
let hostConfig: HostConfig | undefined = undefined;
let envs: string[] = [];
let healthcheck: unknown | undefined = undefined;
let healthcheck: HealthConfig | undefined = undefined;
// if it's a model service we mount the model as a volume
if (image.modelService) {
const modelName = path.basename(modelPath);
Expand Down Expand Up @@ -348,13 +352,15 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
for (const image of images) {
for (const exposed of image.ports) {
const localPorts = await getPortsInfo(exposed);
portmappings.push({
container_port: parseInt(exposed),
host_port: parseInt(localPorts),
host_ip: '',
protocol: '',
range: 1,
});
if (localPorts) {
portmappings.push({
container_port: parseInt(exposed),
host_port: parseInt(localPorts),
host_ip: '',
protocol: '',
range: 1,
});
}
}
}

Expand Down Expand Up @@ -425,11 +431,11 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
}

const imageTag = this.getImageTag(recipe, container);
const buildOptions = {
const buildOptions: BuildImageOptions = {
containerFile: container.containerfile,
tag: imageTag,
labels: {
[LABEL_RECIPE_ID]: labels !== undefined && 'recipe-id' in labels ? labels['recipe-id'] : undefined,
[LABEL_RECIPE_ID]: labels !== undefined && 'recipe-id' in labels ? labels['recipe-id'] : '',
},
};

Expand Down Expand Up @@ -495,7 +501,7 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
}

getConfigAndFilterContainers(
recipeBaseDir: string,
recipeBaseDir: string | undefined,
localFolder: string,
labels?: { [key: string]: string },
): AIContainers {
Expand Down Expand Up @@ -537,7 +543,7 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
);
}

getConfiguration(recipeBaseDir: string, localFolder: string): AIConfigFile {
getConfiguration(recipeBaseDir: string | undefined, localFolder: string): AIConfigFile {
let configFile: string;
if (recipeBaseDir !== undefined) {
configFile = path.join(localFolder, recipeBaseDir, CONFIG_FILENAME);
Expand Down Expand Up @@ -745,7 +751,7 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
// a fresh pod could not have been added yet, we will handle it at next iteration
continue;
}
const containerStates = await Promise.all(
const containerStates: (string | undefined)[] = await Promise.all(
pod.Containers.map(container =>
containerEngine.inspectContainer(pod.engineId, container.Id).then(data => data.State.Health?.Status),
),
Expand All @@ -768,7 +774,7 @@ export class ApplicationManager extends Publisher<ApplicationState[]> implements
}
}

getPodHealth(infos: string[]): PodHealth {
getPodHealth(infos: (string | undefined)[]): PodHealth {
const checked = infos.filter(info => !!info && info !== 'none' && info !== '');
if (!checked.length) {
return 'none';
Expand Down
7 changes: 5 additions & 2 deletions packages/backend/src/managers/catalogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CatalogManager extends Publisher<ApplicationCatalog> implements Dis
}

private loadDefaultCatalog(): void {
this.catalog = defaultCatalog;
this.catalog = defaultCatalog as ApplicationCatalog;
this.notify();
}

Expand All @@ -82,7 +82,10 @@ export class CatalogManager extends Publisher<ApplicationCatalog> implements Dis

const sanitize = this.sanitize(content);
this.catalog = {
models: [...defaultCatalog.models.filter(a => !sanitize.models.some(b => a.id === b.id)), ...sanitize.models],
models: [
...defaultCatalog.models.filter(a => !sanitize.models.some(b => a.id === b.id)),
...sanitize.models,
] as ModelInfo[],
recipes: [...defaultCatalog.recipes.filter(a => !sanitize.recipes.some(b => a.id === b.id)), ...sanitize.recipes],
categories: [
...defaultCatalog.categories.filter(a => !sanitize.categories.some(b => a.id === b.id)),
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/managers/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ export class GitManager {
while (remoteCommits.length && localCommits.length) {
const remote = remoteCommits.pop();
const local = localCommits.pop();
if (!remote || !local) {
break;
}
if (remote === local) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ beforeEach(() => {
vi.resetAllMocks();
// Default listContainers is empty
mockListContainers([]);
vi.mocked(webviewMock.postMessage).mockResolvedValue(undefined);
vi.mocked(webviewMock.postMessage).mockResolvedValue(true);
vi.mocked(containerEngine.inspectContainer).mockResolvedValue({
State: {
Status: 'running',
Expand Down Expand Up @@ -565,7 +565,7 @@ describe('containerRegistry events', () => {
const listener = await deferred;

const server = inferenceManager.get('dummyId');
expect(server.status).toBe('running');
expect(server?.status).toBe('running');
expect(containerEngine.inspectContainer).toHaveBeenCalledOnce();

vi.mocked(containerEngine.inspectContainer).mockResolvedValue({
Expand All @@ -578,7 +578,7 @@ describe('containerRegistry events', () => {
listener('die');

await vi.waitFor(() => {
expect(inferenceManager.get('dummyId').status).toBe('stopped');
expect(inferenceManager.get('dummyId')?.status).toBe('stopped');
expect(containerEngine.inspectContainer).toHaveBeenCalledTimes(2);
});

Expand Down Expand Up @@ -611,7 +611,7 @@ describe('containerRegistry events', () => {
const listener = await deferred;

const server = inferenceManager.get('dummyId');
expect(server.status).toBe('running');
expect(server?.status).toBe('running');

listener('remove');

Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/managers/inference/inferenceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ export class InferenceManager extends Publisher<InferenceServer[]> implements Di
* @param containerId the id of the container running the Inference Server
*/
async deleteInferenceServer(containerId: string): Promise<void> {
if (!this.#servers.has(containerId)) {
const server = this.#servers.get(containerId);
if (!server) {
throw new Error(`cannot find a corresponding server for container id ${containerId}.`);
}
const server = this.#servers.get(containerId);

try {
// If the server is running we need to stop it.
Expand Down Expand Up @@ -469,7 +469,7 @@ export class InferenceManager extends Publisher<InferenceServer[]> implements Di
* Stop an inference server from the container id
* @param containerId the identifier of the container to stop
*/
async stopInferenceServer(containerId?: string): Promise<void> {
async stopInferenceServer(containerId: string): Promise<void> {
if (!this.isInitialize()) throw new Error('Cannot stop the inference server.');

const server = this.#servers.get(containerId);
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import os from 'os';
import fs, { type Stats, type PathLike } from 'node:fs';
import path from 'node:path';
import { ModelsManager } from './modelsManager';
import { type TelemetryLogger, type Webview, env, process as coreProcess } from '@podman-desktop/api';
import { env, process as coreProcess } from '@podman-desktop/api';
import type { RunResult, TelemetryLogger, Webview } from '@podman-desktop/api';
import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import * as utils from '../utils/utils';
Expand Down Expand Up @@ -536,7 +537,7 @@ describe('deleting models', () => {

test('deleting on windows should check if models is uploaded', async () => {
vi.mocked(env).isWindows = true;
vi.mocked(coreProcess.exec).mockResolvedValue(undefined);
vi.mocked(coreProcess.exec).mockResolvedValue({} as unknown as RunResult);
mocks.getFirstRunningMachineNameMock.mockReturnValue('dummyMachine');
mocks.getPodmanCliMock.mockReturnValue('dummyCli');

Expand Down
Loading

0 comments on commit 529bc5b

Please sign in to comment.