logger.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. async function importLogger() {
  3. vi.resetModules();
  4. return import("../src/logger.js");
  5. }
  6. describe("ui logger", () => {
  7. afterEach(() => {
  8. vi.unstubAllEnvs();
  9. vi.unstubAllGlobals();
  10. vi.restoreAllMocks();
  11. });
  12. it("keeps console and desktop bridge logging in non-production builds", async () => {
  13. vi.stubEnv("PROD", false);
  14. const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
  15. const consoleDebug = vi.spyOn(console, "debug").mockImplementation(() => {});
  16. const bridgeLog = vi.fn();
  17. vi.stubGlobal("window", { zcode: { log: bridgeLog } });
  18. const { isLoggerLevelEnabled, logger } = await importLogger();
  19. expect(isLoggerLevelEnabled("debug")).toBe(true);
  20. expect(isLoggerLevelEnabled("info")).toBe(true);
  21. logger.info("visible", { id: "info-1" });
  22. logger.debug("debug-only");
  23. expect(consoleLog).toHaveBeenCalledWith(expect.stringContaining("[ui]"), "visible", {
  24. id: "info-1",
  25. });
  26. expect(consoleDebug).toHaveBeenCalledWith(expect.stringContaining("[ui]"), "debug-only");
  27. expect(bridgeLog).toHaveBeenCalledTimes(1);
  28. expect(bridgeLog).toHaveBeenCalledWith("info", ["visible", { id: "info-1" }]);
  29. });
  30. it("does not touch console or desktop bridge in production builds", async () => {
  31. vi.stubGlobal("__ZCODE_RENDERER_DISABLE_LOGGING__", true);
  32. const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
  33. const consoleDebug = vi.spyOn(console, "debug").mockImplementation(() => {});
  34. const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
  35. const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
  36. const bridgeLog = vi.fn();
  37. vi.stubGlobal("window", { zcode: { log: bridgeLog } });
  38. const { isLoggerLevelEnabled, logger } = await importLogger();
  39. expect(isLoggerLevelEnabled("debug")).toBe(false);
  40. expect(isLoggerLevelEnabled("info")).toBe(false);
  41. logger.debug("hidden-debug");
  42. logger.info("hidden-info");
  43. logger.warn("hidden-warn");
  44. logger.error("hidden-error");
  45. logger.trace("trace-1", "error", "hidden-trace");
  46. expect(consoleDebug).not.toHaveBeenCalled();
  47. expect(consoleLog).not.toHaveBeenCalled();
  48. expect(consoleWarn).not.toHaveBeenCalled();
  49. expect(consoleError).not.toHaveBeenCalled();
  50. expect(bridgeLog).not.toHaveBeenCalled();
  51. });
  52. it("forwards lifecycle diagnostics to the desktop bridge in production builds", async () => {
  53. vi.stubEnv("NODE_ENV", "production");
  54. const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
  55. const bridgeLog = vi.fn();
  56. vi.stubGlobal("window", { zcode: { log: bridgeLog } });
  57. const { logger } = await importLogger();
  58. logger.info("ordinary info must stay disabled");
  59. logger.lifecycle.info("v4 subscription started", {
  60. event: "v4.conversation.subscribe.started",
  61. sessionId: "sess-1",
  62. });
  63. expect(consoleLog).not.toHaveBeenCalled();
  64. expect(bridgeLog).toHaveBeenCalledTimes(1);
  65. expect(bridgeLog).toHaveBeenCalledWith("info", [
  66. "v4 subscription started",
  67. {
  68. event: "v4.conversation.subscribe.started",
  69. sessionId: "sess-1",
  70. },
  71. ]);
  72. });
  73. });