|
| 1 | +/* eslint-disable @typescript-eslint/no-require-imports -- need require() after resetModules to get fresh module with env */ |
| 2 | +describe("sentry anonymizer", () => { |
| 3 | + const origEnv = process.env; |
| 4 | + |
| 5 | + beforeEach(() => { |
| 6 | + jest.resetModules(); |
| 7 | + process.env = { ...origEnv }; |
| 8 | + }); |
| 9 | + |
| 10 | + afterAll(() => { |
| 11 | + process.env = origEnv; |
| 12 | + }); |
| 13 | + |
| 14 | + describe("filepath", () => { |
| 15 | + it("should return path unchanged when env paths are not set", () => { |
| 16 | + process.env.LEDGER_CONFIG_DIRECTORY = ""; |
| 17 | + process.env.HOME_DIRECTORY = ""; |
| 18 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 19 | + expect(anonymizer.filepath("/some/path")).toBe(""); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should return path unchanged for app:// URLs", () => { |
| 23 | + process.env.LEDGER_CONFIG_DIRECTORY = "/tmp/config"; |
| 24 | + process.env.HOME_DIRECTORY = "/tmp/home"; |
| 25 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 26 | + expect(anonymizer.filepath("app://foo/bar")).toBe("app://foo/bar"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should replace USER_DATA path when LEDGER_CONFIG_DIRECTORY is set", () => { |
| 30 | + process.env.LEDGER_CONFIG_DIRECTORY = "/tmp/user-data"; |
| 31 | + process.env.HOME_DIRECTORY = "/tmp/home"; |
| 32 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 33 | + expect(anonymizer.filepath("/tmp/user-data/foo/log.txt")).toBe("$USER_DATA/foo/log.txt"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should replace HOME path when HOME_DIRECTORY is set", () => { |
| 37 | + process.env.LEDGER_CONFIG_DIRECTORY = "/tmp/config"; |
| 38 | + process.env.HOME_DIRECTORY = "/Users/test"; |
| 39 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 40 | + expect(anonymizer.filepath("/Users/test/file")).toBe("$HOME/file"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should replace both paths when both are set", () => { |
| 44 | + process.env.LEDGER_CONFIG_DIRECTORY = "/tmp/config"; |
| 45 | + process.env.HOME_DIRECTORY = "/tmp/home"; |
| 46 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 47 | + expect(anonymizer.filepath("/tmp/home/.config/app")).toBe("$HOME/.config/app"); |
| 48 | + expect(anonymizer.filepath("/tmp/config/log")).toBe("$USER_DATA/log"); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("filepathRecursiveReplacer", () => { |
| 53 | + beforeEach(() => { |
| 54 | + process.env.LEDGER_CONFIG_DIRECTORY = "/tmp/config"; |
| 55 | + process.env.HOME_DIRECTORY = "/tmp/home"; |
| 56 | + }); |
| 57 | + |
| 58 | + it("should replace path in plain object", () => { |
| 59 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 60 | + const obj = { path: "/tmp/config/foo" }; |
| 61 | + anonymizer.filepathRecursiveReplacer(obj); |
| 62 | + expect(obj.path).toBe("$USER_DATA/foo"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should replace path in array", () => { |
| 66 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 67 | + const arr = ["/tmp/home/x"]; |
| 68 | + anonymizer.filepathRecursiveReplacer(arr); |
| 69 | + expect(arr[0]).toBe("$HOME/x"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should replace path in Error message", () => { |
| 73 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 74 | + const err = new Error("Failed at /tmp/config/file.ts"); |
| 75 | + anonymizer.filepathRecursiveReplacer(err); |
| 76 | + expect(err.message).toBe("Failed at $USER_DATA/file.ts"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should handle nested objects", () => { |
| 80 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 81 | + const obj = { inner: { path: "/tmp/home/bar" } }; |
| 82 | + anonymizer.filepathRecursiveReplacer(obj); |
| 83 | + expect(obj).toMatchObject({ inner: { path: "$HOME/bar" } }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should not recurse into already-seen references (circular)", () => { |
| 87 | + const anonymizer = require("~/sentry/anonymizer").default; |
| 88 | + const obj: Record<string, unknown> = { a: "/tmp/config/x" }; |
| 89 | + obj.self = obj; |
| 90 | + anonymizer.filepathRecursiveReplacer(obj); |
| 91 | + expect(obj.a).toBe("$USER_DATA/x"); |
| 92 | + expect(obj.self).toBe(obj); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments