|
| 1 | +const { expect } = require("chai"); |
| 2 | +const { formatLogsForFeed, mapify } = require("../../../utils/logs"); |
| 3 | + |
| 4 | +describe("logs utils", function () { |
| 5 | + describe("formatLogsForFeed", function () { |
| 6 | + const usersMap = { |
| 7 | + user1: { username: "palak-gupta" }, |
| 8 | + user2: { username: "mock-2" }, |
| 9 | + }; |
| 10 | + |
| 11 | + const tasksMap = { |
| 12 | + task1: { title: "Link details page to status site" }, |
| 13 | + task2: { title: "Introduce /ooo command on discord" }, |
| 14 | + }; |
| 15 | + |
| 16 | + it("should format logs for OOO type", function () { |
| 17 | + const logsSnapshot = { |
| 18 | + meta: { |
| 19 | + createdAt: 1710181066410, |
| 20 | + createdBy: "user1", |
| 21 | + requestId: "request123", |
| 22 | + action: "create", |
| 23 | + }, |
| 24 | + type: "REQUEST_CREATED", |
| 25 | + body: { |
| 26 | + createdAt: 1710181064968, |
| 27 | + requestedBy: "user1", |
| 28 | + from: 1710288000000, |
| 29 | + until: 1710288050000, |
| 30 | + id: "NOAWuKmazlIJHaN7Pihg", |
| 31 | + state: "PENDING", |
| 32 | + type: "OOO", |
| 33 | + message: "For testing purpose", |
| 34 | + updatedAt: 1710181064968, |
| 35 | + }, |
| 36 | + timestamp: { |
| 37 | + _seconds: 1710181066, |
| 38 | + _nanoseconds: 410000000, |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + const formattedLog = formatLogsForFeed(logsSnapshot, usersMap); |
| 43 | + |
| 44 | + expect(formattedLog).to.deep.equal({ |
| 45 | + user: "palak-gupta", |
| 46 | + requestId: "request123", |
| 47 | + from: 1710288000000, |
| 48 | + until: 1710288050000, |
| 49 | + message: "For testing purpose", |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should format logs for extensionRequests type", function () { |
| 54 | + const logsSnapshot = { |
| 55 | + meta: { |
| 56 | + extensionRequestId: "po1gNOCXUP2IFsChcmn8", |
| 57 | + userId: "user2", |
| 58 | + taskId: "task1", |
| 59 | + username: "techlord", |
| 60 | + }, |
| 61 | + type: "extensionRequests", |
| 62 | + body: { |
| 63 | + status: "APPROVED", |
| 64 | + }, |
| 65 | + timestamp: { |
| 66 | + _seconds: 1709316797, |
| 67 | + _nanoseconds: 616000000, |
| 68 | + }, |
| 69 | + }; |
| 70 | + |
| 71 | + const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap); |
| 72 | + |
| 73 | + expect(formattedLog).to.deep.equal({ |
| 74 | + extensionRequestId: "po1gNOCXUP2IFsChcmn8", |
| 75 | + status: "APPROVED", |
| 76 | + taskId: "task1", |
| 77 | + taskTitle: "Link details page to status site", |
| 78 | + type: "extensionRequests", |
| 79 | + user: "mock-2", |
| 80 | + userId: "user2", |
| 81 | + username: "techlord", |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should return empty object when usersMap does not contain requestedBy user", function () { |
| 86 | + const invalidLogsSnapshot = { |
| 87 | + meta: { requestId: "request123", taskId: "task456" }, |
| 88 | + body: { |
| 89 | + type: "OOO", |
| 90 | + requestedBy: 3, // User not in usersMap |
| 91 | + from: "2024-03-24", |
| 92 | + until: "2024-03-25", |
| 93 | + message: "Out of office", |
| 94 | + extensionRequestId: "extension123", |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + const formattedLog = formatLogsForFeed(invalidLogsSnapshot, usersMap); |
| 99 | + |
| 100 | + expect(formattedLog).to.deep.equal({}); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should format logs for task type", function () { |
| 104 | + const logsSnapshot = { |
| 105 | + meta: { |
| 106 | + userId: "user1", |
| 107 | + taskId: "task2", |
| 108 | + username: "shubham-sharma", |
| 109 | + }, |
| 110 | + type: "task", |
| 111 | + body: { |
| 112 | + new: { |
| 113 | + percentCompleted: 40, |
| 114 | + }, |
| 115 | + subType: "update", |
| 116 | + }, |
| 117 | + timestamp: { |
| 118 | + _seconds: 1711273137, |
| 119 | + _nanoseconds: 96000000, |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + const formattedLog = formatLogsForFeed(logsSnapshot, usersMap, tasksMap); |
| 124 | + |
| 125 | + expect(formattedLog).to.deep.equal({ |
| 126 | + percentCompleted: 40, |
| 127 | + subType: "update", |
| 128 | + taskId: "task2", |
| 129 | + taskTitle: "Introduce /ooo command on discord", |
| 130 | + type: "task", |
| 131 | + user: "shubham-sharma", |
| 132 | + userId: "user1", |
| 133 | + username: "shubham-sharma", |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should format logs for PROFILE_DIFF_REJECTED type", function () { |
| 138 | + const logsSnapshot = { |
| 139 | + meta: { |
| 140 | + rejectedBy: "user2", |
| 141 | + userId: "user1", |
| 142 | + }, |
| 143 | + type: "PROFILE_DIFF_REJECTED", |
| 144 | + body: { |
| 145 | + profileDiffId: "F8e0II1X7qZwzA1CbF0l", |
| 146 | + message: "", |
| 147 | + }, |
| 148 | + timestamp: { |
| 149 | + _seconds: 1708098695, |
| 150 | + _nanoseconds: 709000000, |
| 151 | + }, |
| 152 | + }; |
| 153 | + |
| 154 | + const formattedLog = formatLogsForFeed(logsSnapshot, usersMap); |
| 155 | + |
| 156 | + expect(formattedLog).to.deep.equal({ |
| 157 | + user: "palak-gupta", |
| 158 | + rejectedBy: "mock-2", |
| 159 | + message: "", |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it("should format logs for PROFILE_DIFF_APPROVED type", function () { |
| 164 | + const logsSnapshot = { |
| 165 | + meta: { |
| 166 | + approvedBy: "user1", |
| 167 | + userId: "user2", |
| 168 | + }, |
| 169 | + type: "PROFILE_DIFF_APPROVED", |
| 170 | + body: { |
| 171 | + profileDiffId: "7sPvm4ooC1PyC91A5KVS", |
| 172 | + message: "", |
| 173 | + }, |
| 174 | + timestamp: { |
| 175 | + _seconds: 1707253607, |
| 176 | + _nanoseconds: 697000000, |
| 177 | + }, |
| 178 | + }; |
| 179 | + |
| 180 | + const formattedLog = formatLogsForFeed(logsSnapshot, usersMap); |
| 181 | + |
| 182 | + expect(formattedLog).to.deep.equal({ |
| 183 | + approvedBy: "palak-gupta", |
| 184 | + user: "mock-2", |
| 185 | + message: "", |
| 186 | + }); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe("mapify function", function () { |
| 191 | + const data = [ |
| 192 | + { username: "palak", id: "100", task: "task2" }, |
| 193 | + { username: "mock-user-1", id: "101", task: "task23" }, |
| 194 | + { username: "mock-user-2", id: "102", task: "task4" }, |
| 195 | + ]; |
| 196 | + |
| 197 | + it("mapify data based on username", function () { |
| 198 | + const mapifiedData = mapify(data, "username"); |
| 199 | + expect(mapifiedData).to.deep.equal({ |
| 200 | + "mock-user-1": { |
| 201 | + id: "101", |
| 202 | + username: "mock-user-1", |
| 203 | + task: "task23", |
| 204 | + }, |
| 205 | + "mock-user-2": { |
| 206 | + id: "102", |
| 207 | + username: "mock-user-2", |
| 208 | + task: "task4", |
| 209 | + }, |
| 210 | + palak: { |
| 211 | + id: "100", |
| 212 | + username: "palak", |
| 213 | + task: "task2", |
| 214 | + }, |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments