From c2b475d6eec7be51684d000e85cadf586d5de8c4 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 12 Feb 2024 11:28:33 +0100 Subject: [PATCH] chore(test): add tests to existing files --- packages/core/jest.config.ts | 3 +- packages/core/src/api/DeviceSdk.test.ts | 62 +++++++++++++++++++ packages/core/src/api/DeviceSdk.ts | 2 +- .../core/src/api/DeviceSdkBuilder.test.ts | 28 +++++++++ packages/core/src/api/DeviceSdkBuilder.ts | 5 +- packages/core/src/api/Logger.test.ts | 33 ++++++---- packages/core/src/api/Logger.ts | 1 + packages/core/src/api/index.ts | 9 ++- packages/core/src/di.ts | 4 +- .../config/data/LocalConfigDataSource.stub.ts | 2 +- .../service/DefaultConfigService.test.ts | 3 +- .../config/service/DefaultConfigService.ts | 2 +- .../service/DefaultLoggerService.test.ts | 24 +++++-- .../logger/service/DefaultLoggerService.ts | 5 ++ .../core/src/internal/logger/service/Log.ts | 1 + .../src/internal/logger/service/LogBuilder.ts | 2 +- .../internal/logger/service/LoggerService.ts | 13 +--- 17 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 packages/core/src/api/DeviceSdk.test.ts create mode 100644 packages/core/src/api/DeviceSdkBuilder.test.ts diff --git a/packages/core/jest.config.ts b/packages/core/jest.config.ts index 2769f8e0c..5c7f9b398 100644 --- a/packages/core/jest.config.ts +++ b/packages/core/jest.config.ts @@ -6,9 +6,10 @@ const config: JestConfigWithTsJest = { setupFiles: ["/jest.setup.ts"], testPathIgnorePatterns: ["/lib/"], collectCoverageFrom: [ - // TODO: remove internal when the rest of the files are setup "src/**/*.ts", "!src/**/*.stub.ts", + "!src/index.ts", + "!src/api/index.ts", ], moduleNameMapper: { "^@internal/(.*)$": "/src/internal/$1", diff --git a/packages/core/src/api/DeviceSdk.test.ts b/packages/core/src/api/DeviceSdk.test.ts new file mode 100644 index 000000000..14f4b0e29 --- /dev/null +++ b/packages/core/src/api/DeviceSdk.test.ts @@ -0,0 +1,62 @@ +/* eslint-disable no-restricted-imports */ +import pkg from "../../package.json"; +import { DeviceSdk } from "./DeviceSdk"; + +let sdk: DeviceSdk; +const logger = { + log: jest.fn(), +}; +describe("DeviceSdk", () => { + describe("clean", () => { + beforeEach(() => { + sdk = new DeviceSdk({ stub: false, loggers: [logger] }); + }); + + it("should create an instance", () => { + expect(sdk).toBeDefined(); + expect(sdk).toBeInstanceOf(DeviceSdk); + }); + + it("should return a clean `version`", async () => { + expect(await sdk.getVersion()).toBe(pkg.version); + }); + + it("startScan should ....", () => { + expect(sdk.startScan()).toBeFalsy(); + }); + + it("stopScan should ....", () => { + expect(sdk.stopScan()).toBeFalsy(); + }); + }); + + describe("stubbed", () => { + beforeEach(() => { + sdk = new DeviceSdk({ stub: true, loggers: [] }); + }); + + it("should create a stubbed version", () => { + expect(sdk).toBeDefined(); + expect(sdk).toBeInstanceOf(DeviceSdk); + }); + + it("should return a stubbed `version`", async () => { + expect(await sdk.getVersion()).toBe("0.0.0-stub.1"); + }); + }); + + describe("without args", () => { + beforeEach(() => { + sdk = new DeviceSdk(); + }); + + it("should create an instance", () => { + expect(sdk).toBeDefined(); + expect(sdk).toBeInstanceOf(DeviceSdk); + }); + + it("should return a clean `version`", async () => { + expect(await sdk.getVersion()).toBe(pkg.version); + }); + }); +}); diff --git a/packages/core/src/api/DeviceSdk.ts b/packages/core/src/api/DeviceSdk.ts index 9376c61d1..457b3e2e0 100644 --- a/packages/core/src/api/DeviceSdk.ts +++ b/packages/core/src/api/DeviceSdk.ts @@ -7,7 +7,7 @@ import { makeContainer, MakeContainerProps } from "@root/src/di"; export class DeviceSdk { container: Container; /** @internal */ - constructor({ stub, loggers }: MakeContainerProps) { + constructor({ stub, loggers }: Partial = {}) { // NOTE: MakeContainerProps might not be the exact type here // For the init of the project this is sufficient, but we might need to // update the constructor arguments as we go (we might have more than just the container config) diff --git a/packages/core/src/api/DeviceSdkBuilder.test.ts b/packages/core/src/api/DeviceSdkBuilder.test.ts new file mode 100644 index 000000000..c6f987ee8 --- /dev/null +++ b/packages/core/src/api/DeviceSdkBuilder.test.ts @@ -0,0 +1,28 @@ +import { DeviceSdk } from "./DeviceSdk"; +import { LedgerDeviceSdkBuilder } from "./DeviceSdkBuilder"; + +let builder: LedgerDeviceSdkBuilder; +const logger = { + log: jest.fn(), +}; + +describe("LedgerDeviceSdkBuilder", () => { + beforeEach(() => { + builder = new LedgerDeviceSdkBuilder(); + }); + + it("should build a DeviceSdk instance", () => { + const sdk: DeviceSdk = builder.build(); + expect(sdk).toBeInstanceOf(DeviceSdk); + }); + + it("should set the stub flag", () => { + builder.setStub(true); + expect(builder.stub).toBe(true); + }); + + it("should add a logger", () => { + builder.addLogger(logger); + expect(builder.loggers).toContain(logger); + }); +}); diff --git a/packages/core/src/api/DeviceSdkBuilder.ts b/packages/core/src/api/DeviceSdkBuilder.ts index 4af25084b..ecb44de6d 100644 --- a/packages/core/src/api/DeviceSdkBuilder.ts +++ b/packages/core/src/api/DeviceSdkBuilder.ts @@ -5,15 +5,12 @@ import { DeviceSdk } from "./DeviceSdk"; export class LedgerDeviceSdkBuilder { stub = false; loggers: LoggerSubscriber[] = []; - constructor() { - console.log("New build"); - } build(): DeviceSdk { return new DeviceSdk({ stub: this.stub, loggers: this.loggers }); } - setStub(stubbed = true): LedgerDeviceSdkBuilder { + setStub(stubbed: boolean): LedgerDeviceSdkBuilder { this.stub = stubbed; return this; } diff --git a/packages/core/src/api/Logger.test.ts b/packages/core/src/api/Logger.test.ts index 2a1ad1351..34e3b34ac 100644 --- a/packages/core/src/api/Logger.test.ts +++ b/packages/core/src/api/Logger.test.ts @@ -11,20 +11,31 @@ const log = jest.spyOn(console, "log").mockImplementation(jest.fn()); let logger: ConsoleLogger; let logObject: Log; describe("ConsoleLogger", () => { - beforeEach(() => { - logger = new ConsoleLogger(); - logObject = LogBuilder.build({}, {}, "test"); - }); - - afterAll(() => { - warn.mockRestore(); - info.mockRestore(); - debug.mockRestore(); - error.mockRestore(); - log.mockRestore(); + describe("exports", () => { + it("Log", () => { + const lgg = new Log({ + messages: [], + data: {}, + context: {}, + }); + expect(lgg).toBeInstanceOf(Log); + }); }); describe("log", () => { + beforeEach(() => { + logger = new ConsoleLogger(); + logObject = LogBuilder.build({}, {}, "test"); + }); + + afterAll(() => { + warn.mockRestore(); + info.mockRestore(); + debug.mockRestore(); + error.mockRestore(); + log.mockRestore(); + }); + it("should log Info level", () => { logObject.setLevel(LogLevel.Info); logger.log(logObject); diff --git a/packages/core/src/api/Logger.ts b/packages/core/src/api/Logger.ts index 17252486b..09653849f 100644 --- a/packages/core/src/api/Logger.ts +++ b/packages/core/src/api/Logger.ts @@ -4,6 +4,7 @@ export { Log, LogLevel }; export type { LogContext, LogData, + LoggerSubscriber, LogMessages, } from "@internal/logger/service/Log"; diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index b8545e4e3..4e62c93f6 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -2,4 +2,11 @@ export { DeviceSdk } from "./DeviceSdk"; export { LedgerDeviceSdkBuilder as DeviceSdkBuilder } from "./DeviceSdkBuilder"; -export { ConsoleLogger } from "./Logger"; +export type { + LogContext, + LogData, + LoggerSubscriber, + LogLevel, + LogMessages, +} from "./Logger"; +export { ConsoleLogger, Log } from "./Logger"; diff --git a/packages/core/src/di.ts b/packages/core/src/di.ts index 79deea439..bca563c75 100644 --- a/packages/core/src/di.ts +++ b/packages/core/src/di.ts @@ -17,7 +17,7 @@ export type MakeContainerProps = { export const makeContainer = ({ stub = false, loggers = [], -}: Partial = {}) => { +}: Partial) => { const container = new Container(); // Uncomment this line to enable the logger middleware @@ -25,7 +25,7 @@ export const makeContainer = ({ container.load( configModuleFactory({ stub }), - loggerModuleFactory({ subscribers: loggers }) + loggerModuleFactory({ subscribers: loggers }), // modules go here ); diff --git a/packages/core/src/internal/config/data/LocalConfigDataSource.stub.ts b/packages/core/src/internal/config/data/LocalConfigDataSource.stub.ts index 2b6ab90b4..2bf993c15 100644 --- a/packages/core/src/internal/config/data/LocalConfigDataSource.stub.ts +++ b/packages/core/src/internal/config/data/LocalConfigDataSource.stub.ts @@ -16,7 +16,7 @@ export class StubLocalConfigDataSource implements LocalConfigDataSource { getConfig(): Either { return Either.of({ name: "DeviceSDK", - version: "0.0.0-mock.1", + version: "0.0.0-stub.1", }); } } diff --git a/packages/core/src/internal/config/service/DefaultConfigService.test.ts b/packages/core/src/internal/config/service/DefaultConfigService.test.ts index 9aa9f4ad3..30157d45a 100644 --- a/packages/core/src/internal/config/service/DefaultConfigService.test.ts +++ b/packages/core/src/internal/config/service/DefaultConfigService.test.ts @@ -20,6 +20,7 @@ const loggerService = { debug: jest.fn(), warn: jest.fn(), error: jest.fn(), + fatal: jest.fn(), }; let service: ConfigService; @@ -36,7 +37,7 @@ describe("DefaultConfigService", () => { service = new DefaultConfigService( localDataSource, remoteDataSource, - loggerService + loggerService, ); }); diff --git a/packages/core/src/internal/config/service/DefaultConfigService.ts b/packages/core/src/internal/config/service/DefaultConfigService.ts index 4694bfd69..1134f406b 100644 --- a/packages/core/src/internal/config/service/DefaultConfigService.ts +++ b/packages/core/src/internal/config/service/DefaultConfigService.ts @@ -20,7 +20,7 @@ export class DefaultConfigService implements ConfigService { constructor( @inject(configTypes.LocalConfigDataSource) local: LocalConfigDataSource, @inject(configTypes.RemoteConfigDataSource) remote: RemoteConfigDataSource, - @inject(loggerTypes.LoggerService) logger: LoggerService + @inject(loggerTypes.LoggerService) logger: LoggerService, ) { this._local = local; this._remote = remote; diff --git a/packages/core/src/internal/logger/service/DefaultLoggerService.test.ts b/packages/core/src/internal/logger/service/DefaultLoggerService.test.ts index 46e45f510..c44585eb6 100644 --- a/packages/core/src/internal/logger/service/DefaultLoggerService.test.ts +++ b/packages/core/src/internal/logger/service/DefaultLoggerService.test.ts @@ -27,7 +27,7 @@ describe("LoggerService", () => { const log = LogBuilder.build( { type: "test" }, { key: "value" }, - "message" + "message", ); service.info(log); @@ -43,7 +43,7 @@ describe("LoggerService", () => { const log = LogBuilder.build( { type: "test" }, { key: "value" }, - "message" + "message", ); service.debug(log); @@ -59,7 +59,7 @@ describe("LoggerService", () => { const log = LogBuilder.build( { type: "test" }, { key: "value" }, - "message" + "message", ); service.warn(log); @@ -75,7 +75,7 @@ describe("LoggerService", () => { const log = LogBuilder.buildFromError( new Error("test"), { type: "test" }, - { key: "value" } + { key: "value" }, ); service.error(log); @@ -83,4 +83,20 @@ describe("LoggerService", () => { expect(log.level).toBe(LogLevel.Error); }); }); + + describe("fatal", () => { + it("should have the correct LogLevel", () => { + subscriber.log.mockClear(); + service = new DefaultLoggerService([subscriber]); + const log = LogBuilder.build( + { type: "test" }, + { key: "value" }, + "message", + ); + + service.fatal(log); + + expect(log.level).toBe(LogLevel.Fatal); + }); + }); }); diff --git a/packages/core/src/internal/logger/service/DefaultLoggerService.ts b/packages/core/src/internal/logger/service/DefaultLoggerService.ts index cd4227a9c..2ff59f26b 100644 --- a/packages/core/src/internal/logger/service/DefaultLoggerService.ts +++ b/packages/core/src/internal/logger/service/DefaultLoggerService.ts @@ -16,6 +16,11 @@ export class DefaultLoggerService implements LoggerService { }); } + fatal(log: Log): void { + log.setLevel(LogLevel.Fatal); + this._log(log); + } + info(log: Log): void { log.setLevel(LogLevel.Info); this._log(log); diff --git a/packages/core/src/internal/logger/service/Log.ts b/packages/core/src/internal/logger/service/Log.ts index 0faec7035..34d30fc91 100644 --- a/packages/core/src/internal/logger/service/Log.ts +++ b/packages/core/src/internal/logger/service/Log.ts @@ -1,4 +1,5 @@ export enum LogLevel { + Fatal = -1, Error = 0, Warning = 1, Info = 2, diff --git a/packages/core/src/internal/logger/service/LogBuilder.ts b/packages/core/src/internal/logger/service/LogBuilder.ts index 8d7e4cd61..f32ddd0fc 100644 --- a/packages/core/src/internal/logger/service/LogBuilder.ts +++ b/packages/core/src/internal/logger/service/LogBuilder.ts @@ -14,7 +14,7 @@ export class LogBuilder { static buildFromError( error: SdkError | Error, context: LogContext = { type: "error" }, - data: LogData = {} + data: LogData = {}, ) { const isSdkError = "_tag" in error; diff --git a/packages/core/src/internal/logger/service/LoggerService.ts b/packages/core/src/internal/logger/service/LoggerService.ts index cd74ba439..37fcd177d 100644 --- a/packages/core/src/internal/logger/service/LoggerService.ts +++ b/packages/core/src/internal/logger/service/LoggerService.ts @@ -1,18 +1,11 @@ import { Log, LoggerSubscriber } from "./Log"; -/** - * [IDEAS] - * a data object in looger - * context in logger (object || string ?) context.tag / context.type / context.id / context.os ... - * message (string[]) in logger - * - * EXPOSE TO OUSTIDE => MOVE OUT OF INTERNAL - */ export interface LoggerService { subscribers: LoggerSubscriber[]; - info(log: Log): void; + fatal(log: Log): void; + error(log: Log): void; warn(log: Log): void; + info(log: Log): void; debug(log: Log): void; - error(log: Log): void; }