import { AvoInspector } from "../AvoInspector"; import { AvoInspectorEnv } from "../AvoInspectorEnv"; import { error, defaultOptions } from "./constants"; import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler"; jest.mock("../AvoNetworkCallsHandler"); describe("Initialization", () => { test("Network Handler is initialized on Inspector init", () => { const inspectorVersion = process.env.npm_package_version || ""; const { apiKey, env, version, appName } = defaultOptions; let inspector = new AvoInspector(defaultOptions); inspector.enableLogging(true); expect(AvoNetworkCallsHandler).toHaveBeenCalledTimes(1); expect(AvoNetworkCallsHandler).toHaveBeenCalledWith(apiKey, env, "my-test-app", version, inspectorVersion, undefined); }); test("Api Key is set", () => { // Given const apiKey = "api-key-xxx"; // When let inspector = new AvoInspector({ env: AvoInspectorEnv.Prod, version: "0", apiKey, }); // Then expect(inspector.apiKey).toBe(apiKey); }); test("Error is thrown when Api Key is not set", () => { // Given // @ts-ignore let apiKey; // Then expect(() => { new AvoInspector({ env: AvoInspectorEnv.Prod, version: "0", // @ts-ignore apiKey, }); }).toThrow(error.API_KEY); }); test("Error is thrown when whitespace Api Key is used", () => { // Given const apiKey = " "; // Then expect(() => { new AvoInspector({ env: AvoInspectorEnv.Prod, version: "0", apiKey, }); }).toThrow(error.API_KEY); }); test("Error is thrown when empty string Api Key is used", () => { expect(() => { new AvoInspector({ env: AvoInspectorEnv.Prod, version: "0", apiKey: "", }); }).toThrow(error.API_KEY); }); test("Error is thrown when Api Key is set to null", () => { // Given const apiKey = null; // Then expect(() => { new AvoInspector({ env: AvoInspectorEnv.Prod, version: "0", // @ts-ignore apiKey, }); }).toThrow(error.API_KEY); }); test("Dev environment is used when env is not provided", () => { // Given // @ts-ignore let env; // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", version: "0", // @ts-ignore env, }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Dev); }); test("Dev environment is used when empty string is used", () => { // Given const env = ""; // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", version: "0", // @ts-ignore env, }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Dev); }); test("Dev env is set using AvoInspectorEnv", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Dev); }); test("Dev environment is set using string", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: "dev", version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Dev); }); test("Staging env is set using AvoInspectorEnv", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Staging, version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Staging); }); test("Staging environment is set using string", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: "staging", version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Staging); }); test("Prod env is set using AvoInspectorEnv", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Prod, version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Prod); }); test("Prod environment is set using string", () => { // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: "prod", version: "0", }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Prod); }); test("Environment other than Dev, Staging, Prod falls back to Dev", () => { // When const env = "test"; let inspector = new AvoInspector({ apiKey: "api-key-xxx", version: "0", // @ts-ignore env, }); // Then expect(inspector.environment).toBe(AvoInspectorEnv.Dev); }); test("Version is set", () => { const version = "1"; // When let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Prod, version, }); // Then expect(inspector.version).toBe(version); }); test("Error is thrown when version is not set", () => { // Given // @ts-ignore let version; // Then expect(() => { new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Prod, // @ts-ignore version, }); }).toThrow(error.VERSION); }); test("Error is thrown when version is set to empty string", () => { // Given const version = " "; // Then expect(() => { new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Prod, version, }); }).toThrow(error.VERSION); }); test("Error is thrown when version is set to null", () => { // Given const version = null; // Then expect(() => { new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Prod, // @ts-ignore version, }); }).toThrow(error.VERSION); }); test("publicEncryptionKey is forwarded to AvoNetworkCallsHandler when provided", () => { const inspectorVersion = process.env.npm_package_version || ""; (AvoNetworkCallsHandler as unknown as jest.Mock).mockClear(); let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", publicEncryptionKey: "my-public-key-123", }); expect(AvoNetworkCallsHandler).toHaveBeenCalledWith( "api-key-xxx", AvoInspectorEnv.Dev, "", "1", inspectorVersion, "my-public-key-123" ); }); test("publicEncryptionKey is forwarded as undefined to AvoNetworkCallsHandler when not provided", () => { const inspectorVersion = process.env.npm_package_version || ""; (AvoNetworkCallsHandler as unknown as jest.Mock).mockClear(); let inspector = new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", }); expect(AvoNetworkCallsHandler).toHaveBeenCalledWith( "api-key-xxx", AvoInspectorEnv.Dev, "", "1", inspectorVersion, undefined ); }); test("warns when publicEncryptionKey is malformed in dev/staging", () => { const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", publicEncryptionKey: "not-valid-hex", }); expect(consoleWarnSpy).toHaveBeenCalledWith( expect.stringContaining("publicEncryptionKey does not look like a valid") ); consoleWarnSpy.mockRestore(); }); test("does not warn when publicEncryptionKey is a valid 130-char uncompressed hex key in dev/staging", () => { const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); const validKey = "04" + "ab".repeat(64); // 2 + 128 = 130 hex chars new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", publicEncryptionKey: validKey, }); expect(consoleWarnSpy).not.toHaveBeenCalled(); consoleWarnSpy.mockRestore(); }); test("does not warn when publicEncryptionKey is a valid 66-char compressed hex key in dev/staging", () => { const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); const validKey = "02" + "4e".repeat(32); // 2 + 64 = 66 hex chars new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", publicEncryptionKey: validKey, }); expect(consoleWarnSpy).not.toHaveBeenCalled(); consoleWarnSpy.mockRestore(); }); test("constructor works without publicEncryptionKey (backwards compatible)", () => { expect(() => { new AvoInspector({ apiKey: "api-key-xxx", env: AvoInspectorEnv.Dev, version: "1", }); }).not.toThrow(); }); });