remove service compat too

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-08-15 16:46:19 +02:00
parent d425fc4dfd
commit 9080f57970
57 changed files with 381 additions and 946 deletions
+25 -117
View File
@@ -38,7 +38,6 @@ import { RootLifecycleService } from '@backstage/backend-plugin-api';
import { RootLoggerService } from '@backstage/backend-plugin-api';
import { SchedulerService } from '@backstage/backend-plugin-api';
import { ServiceFactory } from '@backstage/backend-plugin-api';
import { ServiceFactoryCompat } from '@backstage/backend-plugin-api';
import { ServiceRef } from '@backstage/backend-plugin-api';
import { TokenManagerService } from '@backstage/backend-plugin-api';
import { UrlReaderService } from '@backstage/backend-plugin-api';
@@ -150,12 +149,7 @@ export namespace mockServices {
// (undocumented)
export namespace auth {
const // (undocumented)
factory: ServiceFactoryCompat<
AuthService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<AuthService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<AuthService> | undefined,
@@ -164,12 +158,7 @@ export namespace mockServices {
// (undocumented)
export namespace cache {
const // (undocumented)
factory: ServiceFactoryCompat<
CacheService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<CacheService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<CacheService> | undefined,
@@ -178,12 +167,7 @@ export namespace mockServices {
// (undocumented)
export namespace database {
const // (undocumented)
factory: ServiceFactoryCompat<
DatabaseService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<DatabaseService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<DatabaseService> | undefined,
@@ -194,12 +178,7 @@ export namespace mockServices {
// (undocumented)
export namespace discovery {
const // (undocumented)
factory: ServiceFactoryCompat<
DiscoveryService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<DiscoveryService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<DiscoveryService> | undefined,
@@ -208,12 +187,7 @@ export namespace mockServices {
// (undocumented)
export namespace events {
const // (undocumented)
factory: ServiceFactoryCompat<
EventsService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<EventsService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<EventsService> | undefined,
@@ -225,10 +199,9 @@ export namespace mockServices {
}): HttpAuthService;
// (undocumented)
export namespace httpAuth {
const factory: ((options?: {
const factory: (options?: {
defaultCredentials?: BackstageCredentials;
}) => ServiceFactory<HttpAuthService, 'plugin', 'singleton'>) &
ServiceFactory<HttpAuthService, 'plugin', 'singleton'>;
}) => ServiceFactory<HttpAuthService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<HttpAuthService> | undefined,
@@ -237,12 +210,7 @@ export namespace mockServices {
// (undocumented)
export namespace httpRouter {
const // (undocumented)
factory: ServiceFactoryCompat<
HttpRouterService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<HttpRouterService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<HttpRouterService> | undefined,
@@ -253,12 +221,7 @@ export namespace mockServices {
// (undocumented)
export namespace identity {
const // (undocumented)
factory: ServiceFactoryCompat<
IdentityService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<IdentityService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<IdentityService> | undefined,
@@ -267,12 +230,7 @@ export namespace mockServices {
// (undocumented)
export namespace lifecycle {
const // (undocumented)
factory: ServiceFactoryCompat<
LifecycleService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<LifecycleService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<LifecycleService> | undefined,
@@ -281,12 +239,7 @@ export namespace mockServices {
// (undocumented)
export namespace logger {
const // (undocumented)
factory: ServiceFactoryCompat<
LoggerService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<LoggerService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<LoggerService> | undefined,
@@ -295,12 +248,7 @@ export namespace mockServices {
// (undocumented)
export namespace permissions {
const // (undocumented)
factory: ServiceFactoryCompat<
PermissionsService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<PermissionsService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<PermissionsService> | undefined,
@@ -315,28 +263,14 @@ export namespace mockServices {
data?: JsonObject;
};
const // (undocumented)
factory: ServiceFactory<
RootConfigService,
'root',
'singleton' | 'multiton'
> &
((
options?: Options | undefined,
) => ServiceFactory<
RootConfigService,
'root',
'singleton' | 'multiton'
>);
factory: (
options?: Options | undefined,
) => ServiceFactory<RootConfigService, 'root', 'singleton' | 'multiton'>;
}
// (undocumented)
export namespace rootHealth {
const // (undocumented)
factory: ServiceFactoryCompat<
RootHealthService,
'root',
'singleton',
undefined
>;
factory: () => ServiceFactory<RootHealthService, 'root', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<RootHealthService> | undefined,
@@ -345,7 +279,7 @@ export namespace mockServices {
// (undocumented)
export namespace rootHttpRouter {
const // (undocumented)
factory: ((
factory: () => ((
options?: RootHttpRouterFactoryOptions | undefined,
) => ServiceFactory<RootHttpRouterService, 'root', 'singleton'>) &
ServiceFactory<RootHttpRouterService, 'root', 'singleton'>;
@@ -357,12 +291,7 @@ export namespace mockServices {
// (undocumented)
export namespace rootLifecycle {
const // (undocumented)
factory: ServiceFactoryCompat<
RootLifecycleService,
'root',
'singleton',
undefined
>;
factory: () => ServiceFactory<RootLifecycleService, 'root', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<RootLifecycleService> | undefined,
@@ -377,10 +306,9 @@ export namespace mockServices {
level?: 'none' | 'error' | 'warn' | 'info' | 'debug';
};
const // (undocumented)
factory: ServiceFactory<LoggerService, 'root', 'singleton' | 'multiton'> &
((
options?: Options | undefined,
) => ServiceFactory<LoggerService, 'root', 'singleton' | 'multiton'>);
factory: (
options?: Options | undefined,
) => ServiceFactory<LoggerService, 'root', 'singleton' | 'multiton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<RootLoggerService> | undefined,
@@ -389,12 +317,7 @@ export namespace mockServices {
// (undocumented)
export namespace scheduler {
const // (undocumented)
factory: ServiceFactoryCompat<
SchedulerService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<SchedulerService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<SchedulerService> | undefined,
@@ -405,12 +328,7 @@ export namespace mockServices {
// (undocumented)
export namespace tokenManager {
const // (undocumented)
factory: ServiceFactoryCompat<
TokenManagerService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<TokenManagerService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<TokenManagerService> | undefined,
@@ -419,12 +337,7 @@ export namespace mockServices {
// (undocumented)
export namespace urlReader {
const // (undocumented)
factory: ServiceFactoryCompat<
UrlReaderService,
'plugin',
'singleton',
undefined
>;
factory: () => ServiceFactory<UrlReaderService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<UrlReaderService> | undefined,
@@ -435,12 +348,7 @@ export namespace mockServices {
): UserInfoService;
// (undocumented)
export namespace userInfo {
const factory: ServiceFactoryCompat<
UserInfoService,
'plugin',
'singleton',
undefined
>;
const factory: () => ServiceFactory<UserInfoService, 'plugin', 'singleton'>;
const // (undocumented)
mock: (
partialImpl?: Partial<UserInfoService> | undefined,
@@ -77,8 +77,7 @@ function simpleFactoryWithOptions<
>(
ref: ServiceRef<TService, TScope>,
factory: (...options: TOptions) => TService,
): ServiceFactory<TService, TScope> &
((...options: TOptions) => ServiceFactory<TService, TScope>) {
): (...options: TOptions) => ServiceFactory<TService, TScope> {
const factoryWithOptions = (...options: TOptions) =>
createServiceFactory({
service: ref as ServiceRef<TService, any>,
@@ -86,7 +85,7 @@ function simpleFactoryWithOptions<
async factory() {
return factory(...options);
},
})();
});
return Object.assign(
factoryWithOptions,
factoryWithOptions(...([undefined] as unknown as TOptions)),
@@ -126,7 +125,7 @@ function simpleMock<TService>(
service: ref,
deps: {},
factory: () => mock,
})(),
}),
}) as ServiceMock<TService>;
};
}
@@ -181,11 +180,12 @@ export namespace mockServices {
};
}
export namespace tokenManager {
export const factory = createServiceFactory({
service: coreServices.tokenManager,
deps: {},
factory: () => tokenManager(),
});
export const factory = () =>
createServiceFactory({
service: coreServices.tokenManager,
deps: {},
factory: () => tokenManager(),
});
export const mock = simpleMock(coreServices.tokenManager, () => ({
authenticate: jest.fn(),
getToken: jest.fn(),
@@ -196,11 +196,12 @@ export namespace mockServices {
return new MockIdentityService();
}
export namespace identity {
export const factory = createServiceFactory({
service: coreServices.identity,
deps: {},
factory: () => identity(),
});
export const factory = () =>
createServiceFactory({
service: coreServices.identity,
deps: {},
factory: () => identity(),
});
export const mock = simpleMock(coreServices.identity, () => ({
getIdentity: jest.fn(),
}));
@@ -216,24 +217,25 @@ export namespace mockServices {
});
}
export namespace auth {
export const factory = createServiceFactory({
service: coreServices.auth,
deps: {
plugin: coreServices.pluginMetadata,
config: coreServices.rootConfig,
},
factory({ plugin, config }) {
const disableDefaultAuthPolicy = Boolean(
config.getOptionalBoolean(
'backend.auth.dangerouslyDisableDefaultAuthPolicy',
),
);
return new MockAuthService({
pluginId: plugin.getId(),
disableDefaultAuthPolicy,
});
},
});
export const factory = () =>
createServiceFactory({
service: coreServices.auth,
deps: {
plugin: coreServices.pluginMetadata,
config: coreServices.rootConfig,
},
factory({ plugin, config }) {
const disableDefaultAuthPolicy = Boolean(
config.getOptionalBoolean(
'backend.auth.dangerouslyDisableDefaultAuthPolicy',
),
);
return new MockAuthService({
pluginId: plugin.getId(),
disableDefaultAuthPolicy,
});
},
});
export const mock = simpleMock(coreServices.auth, () => ({
authenticate: jest.fn(),
getNoneCredentials: jest.fn(),
@@ -257,7 +259,7 @@ export namespace mockServices {
);
}
export namespace discovery {
export const factory = discoveryServiceFactory;
export const factory = () => discoveryServiceFactory;
export const mock = simpleMock(coreServices.discovery, () => ({
getBaseUrl: jest.fn(),
getExternalBaseUrl: jest.fn(),
@@ -288,7 +290,14 @@ export namespace mockServices {
);
}
export namespace httpAuth {
const factoryWithOptions = (options?: {
/**
* Creates a mock service factory for the `HttpAuthService`.
*
* By default all requests without credentials are treated as requests from
* the default mock user principal. This behavior can be configured with the
* `defaultCredentials` option.
*/
export const factory = (options?: {
defaultCredentials?: BackstageCredentials;
}) =>
createServiceFactory({
@@ -299,18 +308,7 @@ export namespace mockServices {
plugin.getId(),
options?.defaultCredentials ?? mockCredentials.user(),
),
})();
/**
* Creates a mock service factory for the `HttpAuthService`.
*
* By default all requests without credentials are treated as requests from
* the default mock user principal. This behavior can be configured with the
* `defaultCredentials` option.
*/
export const factory = Object.assign(
factoryWithOptions,
factoryWithOptions(),
);
});
export const mock = simpleMock(coreServices.httpAuth, () => ({
credentials: jest.fn(),
issueUserCookie: jest.fn(),
@@ -336,13 +334,14 @@ export namespace mockServices {
* By default it extracts the user's entity ref from a user principal and
* returns that as the only ownership entity ref.
*/
export const factory = createServiceFactory({
service: coreServices.userInfo,
deps: {},
factory() {
return new MockUserInfoService();
},
});
export const factory = () =>
createServiceFactory({
service: coreServices.userInfo,
deps: {},
factory() {
return new MockUserInfoService();
},
});
export const mock = simpleMock(coreServices.userInfo, () => ({
getUserInfo: jest.fn(),
}));
@@ -352,7 +351,7 @@ export namespace mockServices {
// some may need a bit more refactoring for it to be simpler to
// re-implement functioning mock versions here.
export namespace cache {
export const factory = cacheServiceFactory;
export const factory = () => cacheServiceFactory;
export const mock = simpleMock(coreServices.cache, () => ({
delete: jest.fn(),
get: jest.fn(),
@@ -362,14 +361,14 @@ export namespace mockServices {
}
export namespace database {
export const factory = databaseServiceFactory;
export const factory = () => databaseServiceFactory;
export const mock = simpleMock(coreServices.database, () => ({
getClient: jest.fn(),
}));
}
export namespace rootHealth {
export const factory = rootHealthServiceFactory;
export const factory = () => rootHealthServiceFactory;
export const mock = simpleMock(coreServices.rootHealth, () => ({
getLiveness: jest.fn(),
getReadiness: jest.fn(),
@@ -377,7 +376,7 @@ export namespace mockServices {
}
export namespace httpRouter {
export const factory = httpRouterServiceFactory;
export const factory = () => httpRouterServiceFactory;
export const mock = simpleMock(coreServices.httpRouter, () => ({
use: jest.fn(),
addAuthPolicy: jest.fn(),
@@ -385,14 +384,14 @@ export namespace mockServices {
}
export namespace rootHttpRouter {
export const factory = rootHttpRouterServiceFactory;
export const factory = () => rootHttpRouterServiceFactory;
export const mock = simpleMock(coreServices.rootHttpRouter, () => ({
use: jest.fn(),
}));
}
export namespace lifecycle {
export const factory = lifecycleServiceFactory;
export const factory = () => lifecycleServiceFactory;
export const mock = simpleMock(coreServices.lifecycle, () => ({
addShutdownHook: jest.fn(),
addStartupHook: jest.fn(),
@@ -400,15 +399,14 @@ export namespace mockServices {
}
export namespace logger {
export const factory = loggerServiceFactory;
export const factory = () => loggerServiceFactory;
export const mock = simpleMock(coreServices.logger, () =>
createLoggerMock(),
);
}
export namespace permissions {
export const factory = permissionsServiceFactory;
export const factory = () => permissionsServiceFactory;
export const mock = simpleMock(coreServices.permissions, () => ({
authorize: jest.fn(),
authorizeConditional: jest.fn(),
@@ -416,7 +414,7 @@ export namespace mockServices {
}
export namespace rootLifecycle {
export const factory = rootLifecycleServiceFactory;
export const factory = () => rootLifecycleServiceFactory;
export const mock = simpleMock(coreServices.rootLifecycle, () => ({
addShutdownHook: jest.fn(),
addStartupHook: jest.fn(),
@@ -424,7 +422,7 @@ export namespace mockServices {
}
export namespace scheduler {
export const factory = schedulerServiceFactory;
export const factory = () => schedulerServiceFactory;
export const mock = simpleMock(coreServices.scheduler, () => ({
createScheduledTaskRunner: jest.fn(),
getScheduledTasks: jest.fn(),
@@ -434,7 +432,7 @@ export namespace mockServices {
}
export namespace urlReader {
export const factory = urlReaderServiceFactory;
export const factory = () => urlReaderServiceFactory;
export const mock = simpleMock(coreServices.urlReader, () => ({
readTree: jest.fn(),
readUrl: jest.fn(),
@@ -443,7 +441,7 @@ export namespace mockServices {
}
export namespace events {
export const factory = eventsServiceFactory;
export const factory = () => eventsServiceFactory;
export const mock = simpleMock(eventsServiceRef, () => ({
publish: jest.fn(),
subscribe: jest.fn(),
@@ -93,7 +93,7 @@ describe('ServiceFactoryTester', () => {
deps: { root: rootServiceRef, plugin: pluginServiceRef },
factory: async ({ root, plugin }) => `${root}, ${plugin}`,
}),
{ dependencies: [rootFactory, pluginFactory()] },
{ dependencies: [rootFactory, pluginFactory] },
);
await expect(tester.getSubject('x')).resolves.toBe('root, x-plugin');
@@ -106,7 +106,7 @@ describe('ServiceFactoryTester', () => {
deps: { shared: sharedPluginServiceRef, plugin: pluginServiceRef },
factory: async ({ shared, plugin }) => `${shared}, ${plugin}`,
}),
{ dependencies: [sharedPluginFactory(), pluginFactory] },
{ dependencies: [sharedPluginFactory, pluginFactory] },
);
await expect(tester.getSubject('x')).resolves.toBe('x-1-plugin, x-plugin');
@@ -71,34 +71,34 @@ describe('TestBackend', () => {
features: [
// @ts-expect-error
[extensionPoint1, { a: 'a' }],
createServiceFactory(() => ({
createServiceFactory({
service: serviceRef,
deps: {},
// @ts-expect-error
factory: async () => ({ a: 'a' }),
})),
createServiceFactory(() => ({
}),
createServiceFactory({
service: serviceRef,
deps: {},
factory: async () => ({ a: 'a', b: 'b' }),
})),
createServiceFactory(() => ({
}),
createServiceFactory({
service: serviceRef,
deps: {},
// @ts-expect-error
factory: async () => ({ c: 'c' }),
})),
createServiceFactory(() => ({
}),
createServiceFactory({
service: serviceRef,
deps: {},
// @ts-expect-error
factory: async () => ({ a: 'a', c: 'c' }),
})),
createServiceFactory(() => ({
}),
createServiceFactory({
service: serviceRef,
deps: {},
factory: async () => ({ a: 'a', b: 'b', c: 'c' }),
})),
}),
],
extensionPoints: [
// @ts-expect-error
@@ -144,7 +144,7 @@ describe('TestBackend', () => {
});
await startTestBackend({
features: [testModule, sf()],
features: [testModule, sf],
});
expect(testFn).toHaveBeenCalledWith('winning');