Bin
2025-12-17 1442f92732d7c5311a627a7ba3aaa0bb8ffc539f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const mockAppSettings = {
  debug: false,
  sentry_dsn: "test-dsn",
  sentry_environment: "test",
  sentry_rate: "0.25",
  user: { email: "test@example.com", username: "testuser" },
  version: { "label-studio-os-package": { version: "1.0.0", commit: "abc123" } },
};
 
(global as any).APP_SETTINGS = mockAppSettings;
Object.defineProperty(window, "APP_SETTINGS", {
  writable: true,
  value: mockAppSettings,
});
 
jest.mock("@sentry/browser", () => ({
  captureException: jest.fn(() => "test-event-id"),
}));
 
describe("Sentry Configuration (Open Source)", () => {
  let Sentry: any;
  let captureException: any;
 
  beforeAll(async () => {
    Sentry = await import("@sentry/browser");
    const SentryModule = await import("./Sentry");
    captureException = SentryModule.captureException;
  });
 
  beforeEach(() => {
    jest.clearAllMocks();
  });
 
  describe("captureException with sentry_skip flag", () => {
    it("should call Sentry.captureException for normal errors", () => {
      const error = new Error("Test error");
      const context = { extra: { source: "test" } };
 
      captureException(error, context);
 
      expect(Sentry.captureException).toHaveBeenCalledWith(error, context);
    });
 
    it("should skip Sentry when sentry_skip flag is true", () => {
      const error = new Error("Test error");
      const context = { extra: { sentry_skip: true } };
 
      const result = captureException(error, context);
 
      expect(Sentry.captureException).not.toHaveBeenCalled();
      expect(result).toBe("");
    });
 
    it("should call Sentry when sentry_skip flag is false", () => {
      const error = new Error("Test error");
      const context = { extra: { sentry_skip: false } };
 
      captureException(error, context);
 
      expect(Sentry.captureException).toHaveBeenCalledWith(error, context);
    });
 
    it("should call Sentry when sentry_skip flag is missing", () => {
      const error = new Error("Test error");
      const context = { extra: { other: "data" } };
 
      captureException(error, context);
 
      expect(Sentry.captureException).toHaveBeenCalledWith(error, context);
    });
 
    it("should call Sentry when context has no extra property", () => {
      const error = new Error("Test error");
      const context = { tags: { source: "test" } };
 
      captureException(error, context);
 
      expect(Sentry.captureException).toHaveBeenCalledWith(error, context);
    });
 
    it("should call Sentry when context is undefined", () => {
      const error = new Error("Test error");
 
      captureException(error);
 
      expect(Sentry.captureException).toHaveBeenCalledWith(error, undefined);
    });
  });
 
  describe("SENTRY_ENABLED=false behavior", () => {
    let consoleSpy: jest.SpyInstance;
 
    beforeEach(() => {
      consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
    });
 
    afterEach(() => {
      consoleSpy.mockRestore();
    });
 
    it("should not call Sentry when debug=true", async () => {
      jest.resetModules();
      (global as any).APP_SETTINGS = { ...mockAppSettings, debug: true };
 
      const SentryModule = await import("./Sentry");
      const error = new Error("Test error");
      const result = SentryModule.captureException(error);
 
      expect(Sentry.captureException).not.toHaveBeenCalled();
      expect(console.error).toHaveBeenCalledWith(error, undefined);
      expect(result).toBe("");
 
      (global as any).APP_SETTINGS = mockAppSettings;
    });
 
    it("should not call Sentry when DSN is missing", async () => {
      jest.resetModules();
      (global as any).APP_SETTINGS = { ...mockAppSettings, sentry_dsn: null };
 
      const SentryModule = await import("./Sentry");
      const error = new Error("Test error");
      const result = SentryModule.captureException(error);
 
      expect(Sentry.captureException).not.toHaveBeenCalled();
      expect(console.error).toHaveBeenCalledWith(error, undefined);
      expect(result).toBe("");
 
      (global as any).APP_SETTINGS = mockAppSettings;
    });
  });
});