chore: Bump @ogre-tools/* to 15.3.1 and fixup telemetry decorator

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-04-13 11:13:26 -04:00
1 parent 672ddc7b8d
commit 1c48dda707
22 files changed
+169 -194

No files matched your search

@@ -2,11 +2,11 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { computed, runInAction } from "mobx";
import { runInAction } from "mobx";
import type { DiContainer } from "@ogre-tools/injectable";
import { getInjectable } from "@ogre-tools/injectable";
import { getDiForUnitTesting } from "../../renderer/getDiForUnitTesting";
import telemetryWhiteListForFunctionsInjectable from "./renderer/telemetry-white-list-for-functions.injectable";
import telemetryWhiteListForParameterlessFunctionsInjectable from "./renderer/telemetry-white-list-for-parameterless-functions.injectable";
import emitEventInjectable from "../../common/app-event-bus/emit-event.injectable";
import logErrorInjectable from "../../common/log-error.injectable";
import telemetryDecoratorInjectable from "./renderer/telemetry-decorator.injectable";
@@ -24,66 +24,35 @@ describe("emit-telemetry-from-specific-function-calls", () => {
let emitEventMock: jest.Mock;
beforeEach(() => {
di.override(telemetryWhiteListForFunctionsInjectable, () => [
di.override(telemetryWhiteListForParameterlessFunctionsInjectable, () => new Set([
"some-white-listed-function",
{
id: "some-white-listed-function-with-white-listed-argument",
getParams: (irrelevantArg, arg) => ({ someParam: arg }),
},
{
id: "some-white-listed-function-with-bad-config",
getParams: () => {
throw new Error("some-error-from-bad-configuration");
},
},
]);
]));
emitEventMock = jest.fn();
di.override(emitEventInjectable, () => emitEventMock);
});
describe("given instances of white-listed and non-white-listed functions", () => {
let whiteListedFunctionMock: jest.Mock;
let nonWhiteListedFunctionMock: jest.Mock;
let whiteListedFunction: jest.Mock;
let whiteListedFunctionWithArgument: jest.Mock;
let whiteListedFunctionWithFaultyConfig: jest.Mock;
let nonWhiteListedFunction: jest.Mock;
let logErrorMock: jest.Mock;
beforeEach(() => {
whiteListedFunctionMock = jest.fn();
nonWhiteListedFunctionMock = jest.fn();
logErrorMock = jest.fn();
const whiteListedInjectable = getInjectable({
id: "some-white-listed-function",
instantiate: () => whiteListedFunctionMock,
});
const whiteListedInjectableWithArgument = getInjectable({
id: "some-white-listed-function-with-white-listed-argument",
instantiate: () => whiteListedFunctionMock,
});
const whiteListedInjectableWithBadConfig = getInjectable({
id: "some-white-listed-function-with-bad-config",
instantiate: () => whiteListedFunctionMock,
instantiate: () => jest.fn(),
});
const nonWhiteListedInjectable = getInjectable({
id: "some-non-white-listed-function",
instantiate: () => nonWhiteListedFunctionMock,
instantiate: () => jest.fn(),
});
runInAction(() => {
di.register(
whiteListedInjectable,
whiteListedInjectableWithArgument,
whiteListedInjectableWithBadConfig,
nonWhiteListedInjectable,
);
});
@@ -91,15 +60,6 @@ describe("emit-telemetry-from-specific-function-calls", () => {
di.override(logErrorInjectable, () => logErrorMock);
whiteListedFunction = di.inject(whiteListedInjectable);
whiteListedFunctionWithArgument = di.inject(
whiteListedInjectableWithArgument,
);
whiteListedFunctionWithFaultyConfig = di.inject(
whiteListedInjectableWithBadConfig,
);
nonWhiteListedFunction = di.inject(nonWhiteListedInjectable);
});
@@ -107,7 +67,7 @@ describe("emit-telemetry-from-specific-function-calls", () => {
expect(emitEventMock).not.toHaveBeenCalled();
});
it("doesn't log errors, at least yet", () => {
it("doesn't log errors yet", () => {
expect(logErrorMock).not.toHaveBeenCalled();
});
@@ -125,89 +85,6 @@ describe("emit-telemetry-from-specific-function-calls", () => {
});
});
describe("when a white-listed function with a white-listed argument is called with arguments", () => {
beforeEach(() => {
whiteListedFunctionWithArgument("some-arg", "some-other-arg");
});
it("telemetry is emitted in event bus with the arguments as params", () => {
expect(emitEventMock).toHaveBeenCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "some-white-listed-function-with-white-listed-argument",
params: { someParam: "some-other-arg" },
});
});
});
describe("when a white-listed function with a white-listed argument is called without arguments", () => {
beforeEach(() => {
whiteListedFunctionWithArgument();
});
it("telemetry is emitted in event bus without params", () => {
expect(emitEventMock).toHaveBeenCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "some-white-listed-function-with-white-listed-argument",
params: { someParam: undefined },
});
});
});
describe("given a faulty configuration, when a white-listed function is called", () => {
beforeEach(() => {
whiteListedFunctionWithFaultyConfig();
});
it("telemetry is still emitted in event bus, but with params indicating bad configuration, ", () => {
expect(emitEventMock).toHaveBeenCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "some-white-listed-function-with-bad-config",
params: { error: "Tried to produce params for telemetry, but getParams() threw an error" },
});
});
it("logs error", () => {
expect(logErrorMock).toHaveBeenCalledWith(
'Tried to produce params for telemetry of "some-white-listed-function-with-bad-config", but getParams() threw an error',
expect.objectContaining({ message: "some-error-from-bad-configuration" }),
);
});
});
describe("when a white-listed function with a white-listed argument is called with MobX reactive content", () => {
beforeEach(() => {
const someComputedProperty = computed(() => "some-computed-value");
const someObservable = {
someStaticProperty: "some-static-value",
someComputedProperty,
};
whiteListedFunctionWithArgument(
"irrelevant-argument",
someObservable,
);
});
it("telemetry is emitted in event bus without MobX internals or computeds", () => {
expect(emitEventMock).toHaveBeenCalledWith({
destination: "auto-capture",
action: "telemetry-from-business-action",
name: "some-white-listed-function-with-white-listed-argument",
params: {
someParam: {
someStaticProperty: "some-static-value",
someComputedProperty: "some-computed-value",
},
},
});
});
});
describe("when the non-white-listed function is called", () => {
beforeEach(() => {
nonWhiteListedFunction();
@@ -3,6 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, createInstantiationTargetDecorator, instantiationDecoratorToken } from "@ogre-tools/injectable";
import { pick } from "lodash";
import { parseKubeApi } from "../../../common/k8s-api/kube-api-parse";
import showDetailsInjectable from "../../../renderer/components/kube-detail-params/show-details.injectable";
import emitTelemetryInjectable from "./emit-telemetry.injectable";
@@ -16,17 +17,26 @@ const telemetryDecoratorForShowDetailsInjectable = getInjectable({
const showDetails = instantiate(di);
return (...args) => {
emitTelemetry({
action: showDetailsInjectable.id,
params: {
kind: (() => {
const params = args[0]
? {
action: "open",
...(() => {
try {
return parseKubeApi(args[0] || "").resource;
} catch {
return "";
return {
resource: pick(parseKubeApi(args[0]), "apiPrefix", "apiVersion", "apiGroup", "namespace", "resource", "name"),
};
} catch (error) {
return { error: `${error}` };
}
})(),
},
}
: {
action: "close",
};
emitTelemetry({
action: showDetailsInjectable.id,
params,
});
return showDetails(...args);
@@ -2,16 +2,16 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { injectionDecoratorToken, getInjectable } from "@ogre-tools/injectable";
import { getInjectable, instantiationDecoratorToken, createInstantiationTargetDecorator } from "@ogre-tools/injectable";
import assert from "assert";
import emitTelemetryInjectable from "./emit-telemetry.injectable";
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
import telemetryWhiteListForParameterlessFunctionsInjectable from "./telemetry-white-list-for-parameterless-functions.injectable";
import { isFunction } from "@k8slens/utilities";
const basicTelemetryDecoratorInjectable = getInjectable({
id: "basic-telemetry-decorator",
instantiate: (diForDecorator) => ({
instantiate: (diForDecorator) => createInstantiationTargetDecorator({
decorate: (instantiateToBeDecorated) =>
(di, instantiationParameter) => {
const instance = instantiateToBeDecorated(di, instantiationParameter);
@@ -23,7 +23,7 @@ const basicTelemetryDecoratorInjectable = getInjectable({
assert(currentContext);
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
const whiteList = diForDecorator.inject(telemetryWhiteListForFunctionsInjectable);
const whiteList = diForDecorator.inject(telemetryWhiteListForParameterlessFunctionsInjectable);
if (whiteList.has(currentContext.injectable.id)) {
emitTelemetry({
@@ -40,7 +40,7 @@ const basicTelemetryDecoratorInjectable = getInjectable({
}),
decorable: false,
injectionToken: injectionDecoratorToken,
injectionToken: instantiationDecoratorToken,
});
export default basicTelemetryDecoratorInjectable;
@@ -92,8 +92,8 @@ const externalActions = ["open-link-in-browser"];
const terminal = ["create-terminal-tab"];
const telemetryWhiteListForFunctionsInjectable = getInjectable({
id: "telemetry-white-list-for-functions",
const telemetryWhiteListForParameterlessFunctionsInjectable = getInjectable({
id: "telemetry-white-list-for-parameterless-functions",
instantiate: () => new Set([
...navigateTo,
...helmInjectableIds,
@@ -105,4 +105,4 @@ const telemetryWhiteListForFunctionsInjectable = getInjectable({
decorable: false,
});
export default telemetryWhiteListForFunctionsInjectable;
export default telemetryWhiteListForParameterlessFunctionsInjectable;
@@ -0,0 +1,84 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { EmitAppEvent } from "../../common/app-event-bus/emit-event.injectable";
import emitAppEventInjectable from "../../common/app-event-bus/emit-event.injectable";
import type { ShowDetails } from "../../renderer/components/kube-detail-params/show-details.injectable";
import showDetailsInjectable from "../../renderer/components/kube-detail-params/show-details.injectable";
import { getDiForUnitTesting } from "../../renderer/getDiForUnitTesting";
describe("emit telemetry with params for calls to showDetails", () => {
let emitAppEventMock: jest.MockedFunction<EmitAppEvent>;
let showDetails: ShowDetails;
beforeEach(() => {
const di = getDiForUnitTesting();
emitAppEventMock = jest.fn();
di.override(emitAppEventInjectable, () => emitAppEventMock);
showDetails = di.inject(showDetailsInjectable);
});
it("when showDetails is called with no selflink (ie closing) should emit telemetry with param indicating closing the drawer", () => {
showDetails(undefined);
expect(emitAppEventMock).toBeCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "show-details",
params: {
action: "close",
},
});
});
it("when showDetails is called with empty selflink (ie closing) should emit telemetry with param indicating closing the drawer", () => {
showDetails("");
expect(emitAppEventMock).toBeCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "show-details",
params: {
action: "close",
},
});
});
it("when showDetails is called with valid selflink should emit telemetry with param indicating opening the drawer with that resource", () => {
showDetails("/api/v1/namespaces/default/pods/some-name");
expect(emitAppEventMock).toBeCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "show-details",
params: {
action: "open",
resource: {
apiPrefix: "/api",
apiGroup: "",
apiVersion: "v1",
name: "some-name",
namespace: "default",
resource: "pods",
},
},
});
});
it("when showDetails is called with invalid selflink should emit telemetry with param indicating opening the drawer but also show error", () => {
showDetails("some-non-self-link-value");
expect(emitAppEventMock).toBeCalledWith({
action: "telemetry-from-business-action",
destination: "auto-capture",
name: "show-details",
params: {
action: "open",
error: "Error: invalid apiPath: /some-non-self-link-value",
},
});
});
});