Skip to content

Commit

Permalink
Document Type serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
targendaz2 committed May 26, 2024
1 parent fedac90 commit 0f56875
Show file tree
Hide file tree
Showing 17 changed files with 511 additions and 310 deletions.
79 changes: 69 additions & 10 deletions src/factories/documentType.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
import extensionsAndMIMETypes from '@/data/extensionsAndMIMETypes.json';
import roles from '@/data/roles.json';
import type { DocumentType } from '@/types';
import utis from '@/data/utis.json';
import { DocumentType } from '@/lib/macos/launchServices/interfaces';
import { DocumentTypeSchema } from '@/lib/macos/launchServices/schemas';
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

class DocumentTypeFactory extends Factory<DocumentType> {}

export const documentTypeFactory = DocumentTypeFactory.define(() => {
const [extension, mimeType] = faker.helpers.objectEntry(
extensionsAndMIMETypes,
);

const role = faker.helpers.arrayElement(roles);
const appID =
faker.internet.domainSuffix() +
'.' +
faker.internet.domainWord() +
'.' +
faker.word.noun();

return {
CFBundleTypeExtensions: [extension],
CFBundleTypeMIMETypes: [mimeType],
CFBundleTypeRole: role as DocumentType['CFBundleTypeRole'],
uti: faker.helpers.arrayElement(utis),
role: faker.helpers.arrayElement(roles) as DocumentType['role'],
appID,
appVersion: faker.system.semver(),
type: 'DocumentType' as DocumentType['type'],
};
});

class DocumentTypeSchemaFactory extends Factory<DocumentTypeSchema> {}

export const documentTypeSchemaFactory = DocumentTypeSchemaFactory.define(
() => {
const documentType = documentTypeFactory.build();
const LSHandlerContentType = documentType.uti;
const { appID, appVersion } = documentType;

switch (documentType.role) {
case 'Viewer':
return {
LSHandlerContentType,
LSHandlerRoleViewer: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleViewer: appVersion,
},
};
case 'Shell':
return {
LSHandlerContentType,
LSHandlerRoleShell: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleShell: appVersion,
},
};
case 'QLGenerator':
return {
LSHandlerContentType,
LSHandlerRoleQLGenerator: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleQLGenerator: appVersion,
},
};
case 'None':
return {
LSHandlerContentType,
LSHandlerRoleNone: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleNone: appVersion,
},
};
case 'All':
return {
LSHandlerContentType,
LSHandlerRoleAll: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleAll: appVersion,
},
};
default:
throw new Error();

Check failure on line 77 in src/factories/documentType.ts

View workflow job for this annotation

GitHub Actions / test results (macos-12)

Document Type serialization ► Document Type serialization can serialize a valid Document Type schema ► Document Type serialization can serialize a valid Document Type schema

Failed test found in: reports/macos-12.xml Error: Error:
Raw output
Error: 
    at FactoryBuilder.generator (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/src/factories/documentType.ts:77:23)
    at FactoryBuilder.Object.<anonymous>.FactoryBuilder.build (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/fishery/dist/fishery.js:127:55)
    at DocumentTypeSchemaFactory.Object.<anonymous>.Factory.build (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/fishery/dist/fishery.js:238:46)
    at Object.<anonymous> (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/src/tests/unit/macos/launchServices/serialization.test.ts:10:62)
    at Promise.then.completed (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/run.js:316:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/run.js:121:9)
    at run (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-runner/build/runTest.js:444:34)
    at Object.worker (/Users/runner/work/Mac-Set-Default-Apps/Mac-Set-Default-Apps/node_modules/jest-runner/build/testWorker.js:106:12)
}
},
);
8 changes: 4 additions & 4 deletions src/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { documentTypeFactory } from './documentType';
export { infoPlistFactory } from './infoPlist';
export { lsHandlerFactory } from './lsHandler';
export { urlTypeFactory } from './urlType';
export { documentTypeFactory, documentTypeSchemaFactory } from './documentType';
export { launchServicesSchemaFactory } from './launchServices';
export { lsHandlerFactory, lsHandlerSchemaFactory } from './lsHandler';
export { urlSchemeFactory, urlSchemeSchemaFactory } from './urlScheme';
46 changes: 0 additions & 46 deletions src/factories/infoPlist.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/factories/launchServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LaunchServicesSchema } from '@/lib/macos/launchServices/schemas';
import { Factory } from 'fishery';
import { lsHandlerSchemaFactory } from './lsHandler';

class LaunchServicesSchemaFactory extends Factory<LaunchServicesSchema> {}

export const launchServicesSchemaFactory = LaunchServicesSchemaFactory.define(
() => {
const launchServices = lsHandlerSchemaFactory.buildList(25);
return {
LSHandlers: launchServices,
};
},
);
98 changes: 38 additions & 60 deletions src/factories/lsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,42 @@
import roles from '@/data/roles.json';
import urlSchemes from '@/data/urlSchemes.json';
import utis from '@/data/utis.json';
import type {
LSHandler,
LSHandlerDocumentType,
LSHandlerURLScheme,
} from '@/types';
import { LSHandler } from '@/lib/macos/launchServices/interfaces';
import { LSHandlerSchema } from '@/lib/macos/launchServices/schemas';
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

class LSHandlerFactory extends Factory<
LSHandler,
{ documentType?: boolean; urlScheme?: boolean }
> {
buildDocumentType() {
return this.transient({
documentType: true,
}).build() as LSHandlerDocumentType;
}

buildURLScheme() {
return this.transient({
urlScheme: true,
}).build() as LSHandlerURLScheme;
import { documentTypeFactory, documentTypeSchemaFactory } from './documentType';
import { urlSchemeFactory, urlSchemeSchemaFactory } from './urlScheme';

class LSHandlerFactory extends Factory<LSHandler> {}

export const lsHandlerFactory = LSHandlerFactory.define(() => {
const lsHandlerType = faker.helpers.arrayElement([
'DocumentType',
'URLScheme',
]);

switch (lsHandlerType) {
case 'DocumentType':
return documentTypeFactory.build();
case 'URLScheme':
return urlSchemeFactory.build();
default:
throw new Error();
}
}

export const lsHandlerFactory = LSHandlerFactory.define(
({ transientParams }) => {
const appName = faker.word.noun();
const domain = faker.internet.domainName();
const bundleId = domain.split(',').reverse().join('.') + '.' + appName;

let role = '';
const lsHandler = {
LSHandlerPreferredVersions: {},
};

if (transientParams.documentType) {
role = faker.helpers.arrayElement(roles);
const uti = faker.helpers.arrayElement(utis);

// @ts-expect-error 'ignoring implicit any error'
lsHandler['LSHandlerContentType'] = uti;
} else if (transientParams.urlScheme) {
role = 'All';
const urlScheme = faker.helpers.arrayElement(urlSchemes);

// @ts-expect-error 'ignoring implicit any error'
lsHandler['LSHandlerURLScheme'] = urlScheme;
} else {
});

class LSHandlerSchemaFactory extends Factory<LSHandlerSchema> {}

export const lsHandlerSchemaFactory = LSHandlerSchemaFactory.define(() => {
const lsHandlerType = faker.helpers.arrayElement([
'DocumentType',
'URLScheme',
]);

switch (lsHandlerType) {
case 'DocumentType':
return documentTypeSchemaFactory.build();
case 'URLScheme':
return urlSchemeSchemaFactory.build();
default:
throw new Error();
}

// @ts-expect-error 'ignoring implicit any error'
lsHandler[`LSHandlerRole${role}`] = bundleId;

// @ts-expect-error 'ignoring implicit any error'
lsHandler.LSHandlerPreferredVersions[`LSHandlerRole${role}`] = '-';

return lsHandler as LSHandler;
},
);
}
});
39 changes: 39 additions & 0 deletions src/factories/urlScheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import urlSchemes from '@/data/urlSchemes.json';
import { URLScheme } from '@/lib/macos/launchServices/interfaces';
import { URLSchemeSchema } from '@/lib/macos/launchServices/schemas';
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

class URLSchemeFactory extends Factory<URLScheme> {}

export const urlSchemeFactory = URLSchemeFactory.define(() => {
const appID =
faker.internet.domainSuffix() +
'.' +
faker.internet.domainWord() +
'.' +
faker.word.noun();

return {
scheme: faker.helpers.arrayElement(urlSchemes),
role: 'All' as URLScheme['role'],
appID,
appVersion: '-' as URLScheme['appVersion'],
type: 'URLScheme' as URLScheme['type'],
};
});

class URLSchemeSchemaFactory extends Factory<URLSchemeSchema> {}

export const urlSchemeSchemaFactory = URLSchemeSchemaFactory.define(() => {
const urlScheme = urlSchemeFactory.build();
const { scheme, appID, appVersion } = urlScheme;

return {
LSHandlerURLScheme: scheme,
LSHandlerRoleAll: appID,
LSHandlerPreferredVersions: {
LSHandlerRoleAll: appVersion,
},
};
});
14 changes: 0 additions & 14 deletions src/factories/urlType.ts

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions src/lib/macos/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { fs } from './fs';
export { launchServices } from './launchServices';
export { fs } from './fileSystem';
export { launchServices } from './launchServices/api';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fs } from '@/lib/macos';
import { LaunchServicesSchema } from '@/lib/macos/types';
import { LaunchServicesSchema } from './schemas';

export const launchServices = {
read: (forUser: string): LaunchServicesSchema | undefined => {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/macos/launchServices/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface DocumentType {
uti: string;
role: 'Viewer' | 'Shell' | 'QLGenerator' | 'None' | 'All';
appID: string;
appVersion: string;
type: 'DocumentType';
}

export interface URLScheme {
scheme: string;
role: 'All';
appID: string;
appVersion: '-';
type: 'URLScheme';
}

export type LSHandler = DocumentType | URLScheme;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type LSHandlerDocumentTypeSchema = {
export type DocumentTypeSchema = {
LSHandlerContentType: string;
} & (
| {
Expand Down Expand Up @@ -33,17 +33,15 @@ export type LSHandlerDocumentTypeSchema = {
}
);

export type LSHandlerURLSchemeSchema = {
export type URLSchemeSchema = {
LSHandlerURLScheme: string;
LSHandlerPreferredVersions: {
LSHandlerRoleAll: string;
LSHandlerRoleAll: '-';
};
LSHandlerRoleAll: string;
};

export type LSHandlerSchema =
| LSHandlerDocumentTypeSchema
| LSHandlerURLSchemeSchema;
export type LSHandlerSchema = DocumentTypeSchema | URLSchemeSchema;

export type LaunchServicesSchema = {
LSHandlers: LSHandlerSchema[];
Expand Down
Loading

0 comments on commit 0f56875

Please sign in to comment.