From 36546040dbef964f6e0c1451b374ad9b1900d1a0 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 5 Jun 2024 14:54:15 +0200 Subject: [PATCH 1/5] test: remove logger mock --- tests/mock/utils/logger.ts | 87 ----------------------------------- tests/unit/allocation.test.ts | 9 +--- tests/unit/ws-browser.spec.ts | 17 +++---- 3 files changed, 7 insertions(+), 106 deletions(-) delete mode 100644 tests/mock/utils/logger.ts diff --git a/tests/mock/utils/logger.ts b/tests/mock/utils/logger.ts deleted file mode 100644 index 4bd3aa952..000000000 --- a/tests/mock/utils/logger.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { Logger } from "../../../src"; - -function ctxToString(ctx: Record | Error | undefined) { - if (!ctx) return "[no context]"; - if (ctx instanceof Error) return ctx.message; - try { - return JSON.stringify(ctx); - } catch (e) { - return ctx.toString(); - } -} - -/** - * @deprecated Avoid using this in new tests, this is going to be removed - use ts-mockito instead. In general, don't test with log lines ;) - */ -export class LoggerMock implements Logger { - private _logs: { msg: string; ctx?: Record | Error }[] = []; - - constructor(private silent = true) {} - - child(): Logger { - return this; - } - - async expectToInclude(msg: string, ctx?: Record | Error, waitMs?: number) { - if (waitMs) await new Promise((res) => setTimeout(res, waitMs)); - - return expect(this._logs).toContainEqual({ msg, ctx }); - } - - async expectToMatch(msg: RegExp, waitMs?: number) { - if (waitMs) await new Promise((res) => setTimeout(res, waitMs)); - - return expect(this.logs).toMatch(msg); - } - - async expectToNotMatch(msg: RegExp, waitMs?: number) { - if (waitMs) await new Promise((res) => setTimeout(res, waitMs)); - return expect(this.logs).not.toMatch(msg); - } - - get logs() { - return this._logs.map(({ ctx, msg }) => `${msg} ${ctxToString(ctx)}`).join("\n"); - } - - clear() { - this._logs = []; - } - - error(msg: string, ctx?: Record | Error) { - this.log(msg, ctx, "error"); - } - - info(msg: string, ctx?: Record | Error) { - this.log(msg, ctx, "info"); - } - - warn(msg: string, ctx?: Record | Error) { - this.log(msg, ctx, "warn"); - } - - debug(msg: string, ctx?: Record | Error) { - this.log(msg, ctx, "debug"); - } - - private log(msg: string, ctx?: Record | Error, level = "info") { - if (!this.silent) - console.log( - `\x1b[32m[test]\x1b[0m \x1b[36m${new Date().toISOString()}\x1b[0m ${this.levelColor( - level, - )} ${msg} ${ctxToString(ctx)}`, - ); - this._logs.push({ - msg, - ctx, - }); - } - - levelColor(level) { - switch (level) { - case "warn": - return `\x1b[33m[${level}]\x1b[0m`; - case "info": - return `\x1b[34m[${level}]\x1b[0m`; - } - } -} diff --git a/tests/unit/allocation.test.ts b/tests/unit/allocation.test.ts index 7e629a5e7..e86d69ce7 100644 --- a/tests/unit/allocation.test.ts +++ b/tests/unit/allocation.test.ts @@ -1,20 +1,13 @@ -import { Allocation, GolemConfigError, GolemPaymentError, PaymentErrorCode, YagnaApi } from "../../src"; -import { AllocationOptions } from "../../src/payment/allocation"; -import { LoggerMock } from "../mock/utils/logger"; +import { Allocation, GolemConfigError, YagnaApi } from "../../src"; import { anything, imock, instance, mock, reset, when } from "@johanblumenberg/ts-mockito"; import { PaymentApi } from "ya-ts-client"; -const logger = new LoggerMock(); -const account = { address: "test_address", platform: "test_platform" }; - const mockYagna = mock(YagnaApi); const mockPayment = mock(PaymentApi.RequestorService); const mockAllocation = imock(); describe("Allocation", () => { beforeEach(() => { - logger.clear(); - reset(mockYagna); reset(mockPayment); reset(mockAllocation); diff --git a/tests/unit/ws-browser.spec.ts b/tests/unit/ws-browser.spec.ts index 7dd50c555..68d496959 100644 --- a/tests/unit/ws-browser.spec.ts +++ b/tests/unit/ws-browser.spec.ts @@ -1,11 +1,10 @@ -import { GolemInternalError, nullLogger, WebSocketBrowserStorageProvider, YagnaApi } from "../../src"; +import { GolemInternalError, Logger, nullLogger, WebSocketBrowserStorageProvider, YagnaApi } from "../../src"; // .js added for ESM compatibility import { encode, toObject } from "flatbuffers/js/flexbuffers.js"; import * as jsSha3 from "js-sha3"; import { TEST_IDENTITY } from "../fixtures"; import { GsbApi, IdentityApi } from "ya-ts-client"; -import { LoggerMock } from "../mock/utils/logger"; -import { anything, instance, mock, reset, resetCalls, verify, when } from "@johanblumenberg/ts-mockito"; +import { anything, imock, instance, mock, reset, resetCalls, verify, when } from "@johanblumenberg/ts-mockito"; jest.mock("uuid", () => ({ v4: () => "uuid" })); @@ -14,12 +13,10 @@ type UploadChunkChunk = { offset: number; content: Uint8Array }; const mockYagna = mock(YagnaApi); const mockIdentity = mock(IdentityApi.DefaultService); const mockGsb = mock(GsbApi.RequestorService); - +const logger = imock(); const yagnaApi = instance(mockYagna); describe("WebSocketBrowserStorageProvider", () => { - let logger: LoggerMock; - const createProvider = () => new WebSocketBrowserStorageProvider(yagnaApi, { logger, @@ -27,7 +24,6 @@ describe("WebSocketBrowserStorageProvider", () => { let provider: WebSocketBrowserStorageProvider; beforeEach(() => { - logger = new LoggerMock(); provider = createProvider(); jest.clearAllMocks(); @@ -35,6 +31,7 @@ describe("WebSocketBrowserStorageProvider", () => { reset(mockYagna); reset(mockIdentity); reset(mockGsb); + reset(logger); when(mockYagna.yagnaOptions).thenReturn({ apiKey: "example-api-key", @@ -157,7 +154,7 @@ describe("WebSocketBrowserStorageProvider", () => { expect(spy1).toHaveBeenCalled(); }); - it("should log invalid requests", async () => { + it("should not respond to invalid requests", async () => { const data = { id: "foo", component: "Foo", @@ -167,7 +164,6 @@ describe("WebSocketBrowserStorageProvider", () => { const spy1 = jest.spyOn(provider as any, "respond").mockReturnThis(); socket.dispatchEvent(new MessageEvent("message", { data: encode(data).buffer })); expect(spy1).not.toHaveBeenCalled(); - await logger.expectToInclude("[WebSocketBrowserStorageProvider] Unsupported message in publishData(): Foo"); }); }); }); @@ -247,7 +243,7 @@ describe("WebSocketBrowserStorageProvider", () => { expect(callback).toHaveBeenCalledWith(result); }); - it("should log invalid requests", async () => { + it("should not respond to invalid requests", async () => { const callback = jest.fn(); const data = { id: "foo", @@ -260,7 +256,6 @@ describe("WebSocketBrowserStorageProvider", () => { socket.dispatchEvent(new MessageEvent("message", { data: encode(data).buffer })); expect(spy1).not.toHaveBeenCalled(); expect(spy2).not.toHaveBeenCalled(); - await logger.expectToInclude("[WebSocketBrowserStorageProvider] Unsupported message in receiveData(): Foo"); }); }); }); From 4a7659ece0c271f8384faefb11be41865fc4b4b7 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 5 Jun 2024 14:55:51 +0200 Subject: [PATCH 2/5] feat: remove unused activity events --- src/activity/activity.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/activity/activity.ts b/src/activity/activity.ts index b45c0e341..ba866b7a1 100644 --- a/src/activity/activity.ts +++ b/src/activity/activity.ts @@ -1,16 +1,8 @@ import { Logger } from "../shared/utils"; import { Agreement, ProviderInfo } from "../market/agreement"; -import { EventEmitter } from "eventemitter3"; import { ExecutionOptions, ExeScriptExecutor } from "./exe-script-executor"; import { IActivityApi } from "./types"; -/** @deprecated */ -export interface ActivityEvents { - scriptSent: (details: { activityId: string; agreementId: string }) => void; - scriptExecuted: (details: { activityId: string; agreementId: string; success: boolean }) => void; - destroyed: (details: { id: string; agreementId: string }) => void; -} - export enum ActivityStateEnum { New = "New", Initialized = "Initialized", @@ -38,8 +30,6 @@ export interface IActivityRepository { * As part of a given activity, it is possible to execute exe script commands and capture their results. */ export class Activity { - public readonly events = new EventEmitter(); - /** * @param id The ID of the activity in Yagna * @param agreement The agreement that's related to this activity From 5be77a001a452d7e0a7276cf91e79c441b6f250e Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 5 Jun 2024 14:56:51 +0200 Subject: [PATCH 3/5] feat: remove `spawn` from WorkContext BREAKING CHANGE: All users should migrate to `.runAndStream` instead --- src/activity/work/work.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/activity/work/work.ts b/src/activity/work/work.ts index 4a90574a9..638d5144d 100644 --- a/src/activity/work/work.ts +++ b/src/activity/work/work.ts @@ -216,19 +216,6 @@ export class WorkContext { return this.runOneCommand(run, runOptions); } - /** @deprecated Use {@link WorkContext.runAndStream} instead */ - async spawn(commandLine: string, options?: Omit): Promise; - /** @deprecated Use {@link WorkContext.runAndStream} instead */ - async spawn(executable: string, args: string[], options?: CommandOptions): Promise; - /** @deprecated Use {@link WorkContext.runAndStream} instead */ - async spawn(exeOrCmd: string, argsOrOptions?: string[] | CommandOptions, options?: CommandOptions) { - if (Array.isArray(argsOrOptions)) { - return this.runAndStream(exeOrCmd, argsOrOptions, options); - } else { - return this.runAndStream(exeOrCmd, options); - } - } - /** * Run an executable on provider and return {@link RemoteProcess} that will allow streaming * that contain stdout and stderr as Readable From 52414ce45ab204254aba4a79de58161e778d6b59 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 5 Jun 2024 15:19:35 +0200 Subject: [PATCH 4/5] docs: remove hidden tags from elements --- src/activity/script/command.ts | 22 ---------------------- src/payment/rejection.ts | 6 ------ 2 files changed, 28 deletions(-) diff --git a/src/activity/script/command.ts b/src/activity/script/command.ts index a8f00e5f9..464ba40f4 100644 --- a/src/activity/script/command.ts +++ b/src/activity/script/command.ts @@ -1,9 +1,5 @@ import { ActivityApi } from "ya-ts-client"; import { StorageProvider } from "../../shared/storage"; - -// importing from '../activity' makes Result undefined -// this is likely due to some circular dependency -// temporarily importing from '../activity/results' to fix the issue import { Result } from "../results"; const EMPTY_ERROR_RESULT = new Result({ @@ -63,18 +59,12 @@ export class Command { } } -/** - * @hidden - */ export class Deploy extends Command { constructor(args?: Record) { super("deploy", args); } } -/** - * @hidden - */ export class Start extends Command { constructor(args?: Record) { super("start", args); @@ -94,9 +84,6 @@ type CapturePart = { head: number } | { tail: number } | { headTail: number }; type CaptureFormat = "string" | "binary"; -/** - * @hidden - */ export class Run extends Command { constructor(cmd: string, args?: string[] | null, env?: object | null, capture?: Capture) { const captureOpt = capture || { @@ -118,9 +105,6 @@ export class Terminate extends Command { } } -/** - * @hidden - */ export class Transfer extends Command { constructor( protected from?: string, @@ -131,9 +115,6 @@ export class Transfer extends Command { } } -/** - * @hidden - */ export class UploadFile extends Transfer { constructor( private storageProvider: StorageProvider, @@ -177,9 +158,6 @@ export class UploadData extends Transfer { } } -/** - * @hidden - */ export class DownloadFile extends Transfer { constructor( private storageProvider: StorageProvider, diff --git a/src/payment/rejection.ts b/src/payment/rejection.ts index cde5d18ed..ac74a28ab 100644 --- a/src/payment/rejection.ts +++ b/src/payment/rejection.ts @@ -1,6 +1,3 @@ -/** - * @hidden - */ export enum RejectionReason { UnsolicitedService = "UNSOLICITED_SERVICE", BadService = "BAD_SERVICE", @@ -15,9 +12,6 @@ export enum RejectionReason { AgreementFinalized = "AGREEMENT_FINALIZED", } -/** - * @hidden - */ export interface Rejection { rejectionReason: RejectionReason; totalAmountAccepted: string; From eb3ba129d7ba7be95301f58cfd421cb44b3e1204 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 5 Jun 2024 16:33:52 +0200 Subject: [PATCH 5/5] test: use mock logger instance in ws-browser test --- tests/unit/ws-browser.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ws-browser.spec.ts b/tests/unit/ws-browser.spec.ts index 68d496959..e51affa42 100644 --- a/tests/unit/ws-browser.spec.ts +++ b/tests/unit/ws-browser.spec.ts @@ -19,7 +19,7 @@ const yagnaApi = instance(mockYagna); describe("WebSocketBrowserStorageProvider", () => { const createProvider = () => new WebSocketBrowserStorageProvider(yagnaApi, { - logger, + logger: instance(logger), }); let provider: WebSocketBrowserStorageProvider;