Skip to content

Commit

Permalink
#256 - add environment variables to EnvironmentService
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulKa committed Feb 17, 2025
1 parent 14fe482 commit 2a18463
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 6 deletions.
126 changes: 126 additions & 0 deletions src/main/environment/service/environment-serice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { EnvironmentService } from './environment-service';
import { Collection } from 'shim/objects/collection';
import { VariableMap, VariableObject } from 'shim/objects/variables';
import { PersistenceService } from 'main/persistence/service/persistence-service';
import { randomUUID } from 'node:crypto';
import { getSystemVariables } from './system-variable';

const environmentService = EnvironmentService.instance;

const environmentKey = 'test';
const variableKey = 'var1';
const collection: Partial<Collection> = {
id: randomUUID(),
environments: {
[environmentKey]: {
variables: {
[variableKey]: {
value: 'some-value',
},
},
},
},
variables: {},
};

describe('EnvironmentService', () => {
beforeEach(() => {
environmentService.currentCollection = structuredClone(collection) as Collection;
environmentService.currentEnvironmentKey = environmentKey;
});

it('currentEnvironment getter should return the currently selected environment', () => {
// Act
let currentEnvironment = environmentService.currentEnvironment;

// Assert
expect(currentEnvironment).toEqual(collection.environments[environmentKey]);

// Arrange
environmentService.currentEnvironmentKey = undefined;

// Act
currentEnvironment = environmentService.currentEnvironment;

// Assert
expect(currentEnvironment).toBeUndefined();
});

it('setCollectionVariables() should override all existing collection variables', () => {
// Arrange
const newVariables: VariableMap = { newVar: { value: 'new-value' } };

// Assert
expect(environmentService.currentCollection.variables).toEqual(collection.variables);

// Act
environmentService.setCollectionVariables(newVariables);

// Assert
expect(environmentService.currentCollection.variables).toEqual(newVariables);
});

it('changeCollection() should load the new collection from PersistenceService', async () => {
// Arrange
const newCollection: Partial<Collection> = { id: randomUUID(), dirPath: '/some/path' };
const loadCollectionSpy = vi
.spyOn(PersistenceService.instance, 'loadCollection')
.mockResolvedValueOnce(newCollection as Collection);

// Act
const result = await environmentService.changeCollection(newCollection.dirPath);

// Assert
expect(loadCollectionSpy).toHaveBeenCalledWith(newCollection.dirPath);
expect(result).toEqual(newCollection as Collection);
});

it('getVariables() should return all currently available variables', () => {
// Arrange
const systemVariables = getSystemVariables();

// Act
const variables = environmentService.getVariables();

// Assert
expect(variables).toEqual([
...Object.entries(collection.environments[environmentKey].variables),
...Object.entries(collection.variables),
...systemVariables,
]);
});

it('getVariable() should return the first matching variable by the given key', () => {
// Arrange
const [key, systemVariable] = getSystemVariables()[0];
const collectionVariable: VariableObject = { value: randomUUID() };
const environmentVariable: VariableObject = { value: randomUUID() };
environmentService.currentCollection.variables[key] = collectionVariable;
environmentService.currentEnvironment.variables[key] = environmentVariable;

// Act
let result = environmentService.getVariable(key);

// Assert
expect(result).toEqual(environmentVariable);

// Arrange
delete environmentService.currentEnvironment.variables[key];

// Act
result = environmentService.getVariable(key);

// Assert
expect(result).toEqual(collectionVariable);

// Arrange
delete environmentService.currentCollection.variables[key];

// Act
result = environmentService.getVariable(key);

// Assert
expect(result).toEqual(systemVariable);
});
});
29 changes: 23 additions & 6 deletions src/main/environment/service/environment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TemplateReplaceStream } from 'template-replace-stream';
import { Initializable } from 'main/shared/initializable';
import { PersistenceService } from 'main/persistence/service/persistence-service';
import { Collection } from 'shim/objects/collection';
import { VariableMap, VariableObject } from 'shim/objects/variables';
import { VariableMap } from 'shim/objects/variables';
import { getSystemVariable, getSystemVariables } from './system-variable';

const persistenceService = PersistenceService.instance;
Expand All @@ -16,8 +16,18 @@ const persistenceService = PersistenceService.instance;
export class EnvironmentService implements Initializable {
public static readonly instance: EnvironmentService = new EnvironmentService();

/** The current collection that is being used. */
public currentCollection: Collection;

/** The key of the current environment in the current collection. */
public currentEnvironmentKey?: string;

/** The currently selected environment in the current collection. */
public get currentEnvironment() {
if (this.currentEnvironmentKey == null) return;
return this.currentCollection.environments[this.currentEnvironmentKey];
}

/**
* Initializes the environment service by loading the last used collection.
*/
Expand Down Expand Up @@ -58,19 +68,26 @@ export class EnvironmentService implements Initializable {
* system variables.
*/
public getVariables() {
return Object.entries(this.currentCollection.variables).concat(getSystemVariables());
return Object.entries(this.currentEnvironment.variables)
.concat(Object.entries(this.currentCollection.variables))
.concat(getSystemVariables());
}

/**
* Returns the value of a variable. The hierarchy is as follows:
* 1. Collection variables
* 2. System variables
* 1. Environment variables
* 2. Collection variables
* 3. System variables
*
* @param key The key of the variable.
* @returns The value of the variable if it exists, otherwise undefined.
*/
public getVariable(key: string): VariableObject | undefined {
return this.currentCollection.variables[key] ?? getSystemVariable(key);
public getVariable(key: string) {
return (
this.currentEnvironment?.variables[key] ??
this.currentCollection.variables[key] ??
getSystemVariable(key)
);
}

private getVariableValue(key: string) {
Expand Down

0 comments on commit 2a18463

Please sign in to comment.