|
| 1 | +import { mergeIntoPubProvidedId } from "./pubProvidedId"; |
| 2 | + |
| 3 | +type FakePbjs = { |
| 4 | + que: Array<() => void>; |
| 5 | + getConfig: jest.Mock; |
| 6 | + setConfig: jest.Mock; |
| 7 | + refreshUserIds: jest.Mock; |
| 8 | +}; |
| 9 | + |
| 10 | +function makePbjs(userSync: Record<string, unknown> = {}): FakePbjs { |
| 11 | + return { |
| 12 | + que: [], |
| 13 | + getConfig: jest.fn(() => userSync), |
| 14 | + setConfig: jest.fn(), |
| 15 | + refreshUserIds: jest.fn(), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +const w = window as unknown as Record<string, any>; |
| 20 | + |
| 21 | +const EIDS = [ |
| 22 | + { source: "uidapi.com", uids: [{ atype: 3, id: "uid2-token" }] }, |
| 23 | + { source: "id5-sync.com", uids: [{ atype: 1, id: "id5-id" }] }, |
| 24 | +]; |
| 25 | + |
| 26 | +function seedCache(eids: unknown[], key = "OPTABLE_RESOLVED") { |
| 27 | + localStorage.setItem(key, JSON.stringify({ ortb2: { user: { data: [], eids } } })); |
| 28 | +} |
| 29 | + |
| 30 | +function drain(pbjs: FakePbjs) { |
| 31 | + pbjs.que.forEach((cmd) => cmd()); |
| 32 | +} |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + localStorage.clear(); |
| 36 | + delete w.pbjs; |
| 37 | + delete w.owpbjs; |
| 38 | +}); |
| 39 | + |
| 40 | +describe("mergeIntoPubProvidedId", () => { |
| 41 | + it("merges cached EIDs into a single pubProvidedId entry and refreshes it", () => { |
| 42 | + seedCache(EIDS); |
| 43 | + const pbjs = makePbjs({}); |
| 44 | + w.pbjs = pbjs; |
| 45 | + |
| 46 | + mergeIntoPubProvidedId(); |
| 47 | + drain(pbjs); |
| 48 | + |
| 49 | + const config = pbjs.setConfig.mock.calls[0][0]; |
| 50 | + expect(config.userSync.userIds).toEqual([{ name: "pubProvidedId", params: { eids: EIDS } }]); |
| 51 | + expect(pbjs.refreshUserIds).toHaveBeenCalledWith({ submoduleNames: ["pubProvidedId"] }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("queues onto a stub global when prebid has not loaded yet", () => { |
| 55 | + seedCache(EIDS); |
| 56 | + mergeIntoPubProvidedId(); |
| 57 | + |
| 58 | + expect(w.pbjs.que).toHaveLength(1); |
| 59 | + expect(() => w.pbjs.que.forEach((cmd: () => void) => cmd())).not.toThrow(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("preserves other providers' EIDs and replaces ours by source", () => { |
| 63 | + seedCache(EIDS); |
| 64 | + const pbjs = makePbjs({ |
| 65 | + userIds: [ |
| 66 | + { |
| 67 | + name: "pubProvidedId", |
| 68 | + params: { |
| 69 | + eids: [ |
| 70 | + { source: "uidapi.com", uids: [{ id: "old-uid2" }] }, |
| 71 | + { source: "publisher.com", uids: [{ id: "pub-own" }] }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + }, |
| 75 | + ], |
| 76 | + }); |
| 77 | + w.pbjs = pbjs; |
| 78 | + |
| 79 | + mergeIntoPubProvidedId(); |
| 80 | + drain(pbjs); |
| 81 | + |
| 82 | + const eids = pbjs.setConfig.mock.calls[0][0].userSync.userIds[0].params.eids; |
| 83 | + expect(eids.map((e: any) => e.source)).toEqual(["publisher.com", "uidapi.com", "id5-sync.com"]); |
| 84 | + expect(eids.find((e: any) => e.source === "uidapi.com").uids[0].id).toBe("uid2-token"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("collapses duplicate pubProvidedId entries and keeps other submodules", () => { |
| 88 | + seedCache(EIDS); |
| 89 | + const pbjs = makePbjs({ |
| 90 | + syncDelay: 5000, |
| 91 | + userIds: [ |
| 92 | + { name: "sharedId" }, |
| 93 | + { name: "pubProvidedId", params: { eids: [{ source: "a.com", uids: [{ id: "a" }] }] } }, |
| 94 | + { name: "pubProvidedId", params: { eids: [{ source: "b.com", uids: [{ id: "b" }] }] } }, |
| 95 | + ], |
| 96 | + }); |
| 97 | + w.pbjs = pbjs; |
| 98 | + |
| 99 | + mergeIntoPubProvidedId(); |
| 100 | + drain(pbjs); |
| 101 | + |
| 102 | + const config = pbjs.setConfig.mock.calls[0][0]; |
| 103 | + expect(config.userSync.syncDelay).toBe(5000); |
| 104 | + const names = config.userSync.userIds.map((u: any) => u.name); |
| 105 | + expect(names).toEqual(["sharedId", "pubProvidedId"]); |
| 106 | + const eids = config.userSync.userIds[1].params.eids; |
| 107 | + expect(eids.map((e: any) => e.source)).toEqual(["a.com", "b.com", "uidapi.com", "id5-sync.com"]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("strips underscore-prefixed cache sidecars before handing EIDs to prebid", () => { |
| 111 | + seedCache([{ source: "uidapi.com", uids: [{ id: "x" }], _ref: { refresh_token: "rt" }, _id5: { t: 1 } }]); |
| 112 | + const pbjs = makePbjs({}); |
| 113 | + w.pbjs = pbjs; |
| 114 | + |
| 115 | + mergeIntoPubProvidedId(); |
| 116 | + drain(pbjs); |
| 117 | + |
| 118 | + const eid = pbjs.setConfig.mock.calls[0][0].userSync.userIds[0].params.eids[0]; |
| 119 | + expect(eid).toEqual({ source: "uidapi.com", uids: [{ id: "x" }] }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("does nothing when the cache has no EIDs", () => { |
| 123 | + const pbjs = makePbjs({}); |
| 124 | + w.pbjs = pbjs; |
| 125 | + |
| 126 | + mergeIntoPubProvidedId(); |
| 127 | + |
| 128 | + expect(pbjs.que).toHaveLength(0); |
| 129 | + }); |
| 130 | + |
| 131 | + it("merges into every configured instance", () => { |
| 132 | + seedCache(EIDS); |
| 133 | + const a = makePbjs({}); |
| 134 | + const b = makePbjs({}); |
| 135 | + w.pbjs = a; |
| 136 | + w.owpbjs = b; |
| 137 | + |
| 138 | + mergeIntoPubProvidedId({ instances: ["pbjs", "owpbjs"] }); |
| 139 | + drain(a); |
| 140 | + drain(b); |
| 141 | + |
| 142 | + expect(a.setConfig).toHaveBeenCalled(); |
| 143 | + expect(b.setConfig).toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("accepts explicit eids and a custom cacheKey", () => { |
| 147 | + seedCache(EIDS, "MY_CACHE"); |
| 148 | + const pbjs = makePbjs({}); |
| 149 | + w.pbjs = pbjs; |
| 150 | + |
| 151 | + mergeIntoPubProvidedId({ cacheKey: "MY_CACHE" }); |
| 152 | + drain(pbjs); |
| 153 | + expect(pbjs.setConfig.mock.calls[0][0].userSync.userIds[0].params.eids).toEqual(EIDS); |
| 154 | + |
| 155 | + const direct = makePbjs({}); |
| 156 | + w.pbjs = direct; |
| 157 | + mergeIntoPubProvidedId({ eids: [{ source: "direct.com", uids: [{ id: "d" }] }] }); |
| 158 | + drain(direct); |
| 159 | + expect(direct.setConfig.mock.calls[0][0].userSync.userIds[0].params.eids).toEqual([ |
| 160 | + { source: "direct.com", uids: [{ id: "d" }] }, |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("refreshAll refreshes every user-id submodule instead of only pubProvidedId", () => { |
| 165 | + seedCache(EIDS); |
| 166 | + const pbjs = makePbjs({}); |
| 167 | + w.pbjs = pbjs; |
| 168 | + |
| 169 | + mergeIntoPubProvidedId({ refreshAll: true }); |
| 170 | + drain(pbjs); |
| 171 | + |
| 172 | + expect(pbjs.refreshUserIds).toHaveBeenCalledWith(); |
| 173 | + }); |
| 174 | + |
| 175 | + it("a throwing prebid config call does not break the queue", () => { |
| 176 | + seedCache(EIDS); |
| 177 | + const pbjs = makePbjs({}); |
| 178 | + pbjs.getConfig.mockImplementation(() => { |
| 179 | + throw new Error("boom"); |
| 180 | + }); |
| 181 | + w.pbjs = pbjs; |
| 182 | + |
| 183 | + mergeIntoPubProvidedId(); |
| 184 | + expect(() => drain(pbjs)).not.toThrow(); |
| 185 | + expect(pbjs.setConfig).not.toHaveBeenCalled(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments