Files
privacy.sexy/tests/unit/infrastructure/RuntimeSanity/Validators/MetadataValidator.spec.ts
undergroundwires 75c9b51bf2 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`.
2023-08-24 20:01:53 +02:00

134 lines
4.5 KiB
TypeScript

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);
}
}