Migrate to electron-vite and electron-builder
- Switch from deprecated Vue CLI plugin to `electron-vite` (see nklayman/vue-cli-plugin-electron-builder#1982) - Update main/preload scripts to use `index.cjs` filenames to support `"type": "module"`, resolving crash issue (#233). This crash was related to Electron not supporting ESM (see electron/asar#249, electron/electron#21457). - This commit completes migration to Vite from Vue CLI (#230). Structure changes: - Introduce separate folders for Electron's main and preload processes. - Move TypeHelpers to `src/` to mark tit as accessible by the rest of the code. Config changes: - Make `vite.config.ts` reusable by Electron configuration. - On electron-builder, use `--publish` flag instead of `-p` for clarity. Tests: - Add log for preload script loading verification. - Implement runtime environment sanity checks. - Enhance logging in `check-desktop-runtime-errors`.
This commit is contained in:
155
tests/unit/infrastructure/RuntimeSanity/SanityChecks.spec.ts
Normal file
155
tests/unit/infrastructure/RuntimeSanity/SanityChecks.spec.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/ISanityCheckOptions';
|
||||
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/ISanityValidator';
|
||||
import { SanityValidatorStub } from '@tests/unit/shared/Stubs/SanityValidatorStub';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
|
||||
describe('SanityChecks', () => {
|
||||
describe('validateRuntimeSanity', () => {
|
||||
describe('parameter validation', () => {
|
||||
describe('options', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing options';
|
||||
const context = new TestContext()
|
||||
.withOptions(absentValue);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
it('throws when validators are empty', () => {
|
||||
// arrange
|
||||
const expectedError = 'missing validators';
|
||||
const context = new TestContext()
|
||||
.withValidators([]);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('aggregates validators', () => {
|
||||
it('does not throw if all validators pass', () => {
|
||||
// arrange
|
||||
const context = new TestContext()
|
||||
.withValidators([
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(false),
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(false),
|
||||
]);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.not.throw();
|
||||
});
|
||||
it('does not throw if a validator return errors but pass', () => {
|
||||
// arrange
|
||||
const context = new TestContext()
|
||||
.withValidators([
|
||||
new SanityValidatorStub()
|
||||
.withErrorsResult(['should be ignored'])
|
||||
.withShouldValidateResult(false),
|
||||
]);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.not.throw();
|
||||
});
|
||||
it('does not throw if validators return no errors', () => {
|
||||
// arrange
|
||||
const context = new TestContext()
|
||||
.withValidators([
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(true)
|
||||
.withErrorsResult([]),
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(true)
|
||||
.withErrorsResult([]),
|
||||
]);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.not.throw();
|
||||
});
|
||||
it('throws if single validator has errors', () => {
|
||||
// arrange
|
||||
const firstError = 'first-error';
|
||||
const secondError = 'second-error';
|
||||
let actualError = '';
|
||||
const context = new TestContext()
|
||||
.withValidators([
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(true)
|
||||
.withErrorsResult([firstError, secondError]),
|
||||
]);
|
||||
// act
|
||||
try {
|
||||
context.validateRuntimeSanity();
|
||||
} catch (err) {
|
||||
actualError = err.toString();
|
||||
}
|
||||
// assert
|
||||
expect(actualError).to.have.length.above(0);
|
||||
expect(actualError).to.include(firstError);
|
||||
expect(actualError).to.include(secondError);
|
||||
});
|
||||
it('accumulates error messages from validators', () => {
|
||||
// arrange
|
||||
const errorFromFirstValidator = 'first-error';
|
||||
const errorFromSecondValidator = 'second-error';
|
||||
let actualError = '';
|
||||
const context = new TestContext()
|
||||
.withValidators([
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(true)
|
||||
.withErrorsResult([errorFromFirstValidator]),
|
||||
new SanityValidatorStub()
|
||||
.withShouldValidateResult(true)
|
||||
.withErrorsResult([errorFromSecondValidator]),
|
||||
]);
|
||||
// act
|
||||
try {
|
||||
context.validateRuntimeSanity();
|
||||
} catch (err) {
|
||||
actualError = err.toString();
|
||||
}
|
||||
// assert
|
||||
expect(actualError).to.have.length.above(0);
|
||||
expect(actualError).to.include(errorFromFirstValidator);
|
||||
expect(actualError).to.include(errorFromSecondValidator);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private options: ISanityCheckOptions = new SanityCheckOptionsStub();
|
||||
|
||||
private validators: ISanityValidator[] = [new SanityValidatorStub()];
|
||||
|
||||
public withOptionsSetup(
|
||||
setup: (stub: SanityCheckOptionsStub) => SanityCheckOptionsStub,
|
||||
) {
|
||||
return this.withOptions(setup(new SanityCheckOptionsStub()));
|
||||
}
|
||||
|
||||
public withOptions(options: ISanityCheckOptions): this {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withValidators(validators: ISanityValidator[]): this {
|
||||
this.validators = validators;
|
||||
return this;
|
||||
}
|
||||
|
||||
public validateRuntimeSanity(): ReturnType<typeof validateRuntimeSanity> {
|
||||
return validateRuntimeSanity(this.options, this.validators);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { MetadataValidator } from '@/infrastructure/RuntimeSanity/Validators/MetadataValidator';
|
||||
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
|
||||
describe('MetadataValidator', () => {
|
||||
describe('shouldValidate', () => {
|
||||
it('returns true when validateMetadata is true', () => {
|
||||
// arrange
|
||||
const expectedValue = true;
|
||||
const options = new SanityCheckOptionsStub()
|
||||
.withValidateMetadata(true);
|
||||
const validator = new TestContext()
|
||||
.createSut();
|
||||
// act
|
||||
const actualValue = validator.shouldValidate(options);
|
||||
// assert
|
||||
expect(actualValue).to.equal(expectedValue);
|
||||
});
|
||||
|
||||
it('returns false when validateMetadata is false', () => {
|
||||
// arrange
|
||||
const expectedValue = false;
|
||||
const options = new SanityCheckOptionsStub()
|
||||
.withValidateMetadata(false);
|
||||
const validator = new TestContext()
|
||||
.createSut();
|
||||
// act
|
||||
const actualValue = validator.shouldValidate(options);
|
||||
// assert
|
||||
expect(actualValue).to.equal(expectedValue);
|
||||
});
|
||||
});
|
||||
describe('collectErrors', () => {
|
||||
describe('yields "missing metadata" if metadata is not provided', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing metadata';
|
||||
const validator = new TestContext()
|
||||
.withMetadata(absentValue)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
});
|
||||
it('yields missing keys if metadata has keys without values', () => {
|
||||
// arrange
|
||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||
const metadata = new AppMetadataStub()
|
||||
.witName(undefined)
|
||||
.withHomepageUrl(undefined);
|
||||
const validator = new TestContext()
|
||||
.withMetadata(metadata)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('yields missing keys if metadata has getters instead of properties', () => {
|
||||
/*
|
||||
This test may behave differently in unit testing vs. production due to how code
|
||||
is transformed, especially around class getters and their enumerability during bundling.
|
||||
*/
|
||||
// arrange
|
||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||
const stubWithGetters: Partial<IAppMetadata> = {
|
||||
get name() {
|
||||
return undefined;
|
||||
},
|
||||
get homepageUrl() {
|
||||
return undefined;
|
||||
},
|
||||
};
|
||||
const stub: IAppMetadata = {
|
||||
...new AppMetadataStub(),
|
||||
...stubWithGetters,
|
||||
};
|
||||
const validator = new TestContext()
|
||||
.withMetadata(stub)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('yields unable to capture metadata if metadata has no getter values', () => {
|
||||
// arrange
|
||||
const expectedError = 'Unable to capture metadata key/value pairs';
|
||||
const stub = {} as IAppMetadata;
|
||||
const validator = new TestContext()
|
||||
.withMetadata(stub)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('does not yield errors if all metadata keys have values', () => {
|
||||
// arrange
|
||||
const metadata = new AppMetadataStub();
|
||||
const validator = new TestContext()
|
||||
.withMetadata(metadata)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
public metadata: IAppMetadata = new AppMetadataStub();
|
||||
|
||||
public withMetadata(metadata: IAppMetadata): this {
|
||||
this.metadata = metadata;
|
||||
return this;
|
||||
}
|
||||
|
||||
public createSut(): MetadataValidator {
|
||||
const mockFactory = () => this.metadata;
|
||||
return new MetadataValidator(mockFactory);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user