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..2468d4840 --- /dev/null +++ b/packages/core/src/api/DeviceSdk.test.ts @@ -0,0 +1,47 @@ +/* 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"); + }); + }); +}); 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/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/logger/service/LoggerService.ts b/packages/core/src/internal/logger/service/LoggerService.ts index cd74ba439..6991b6ce2 100644 --- a/packages/core/src/internal/logger/service/LoggerService.ts +++ b/packages/core/src/internal/logger/service/LoggerService.ts @@ -1,12 +1,4 @@ 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[];