Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,6 @@ export class BatchUploader {
mpInstance
);

if (uploadBatchObject) {
try {
mpInstance._Forwarders.sendBatchToForwarders(
uploadBatchObject
);
} catch (e) {
mpInstance.Logger.error(
`Error forwarding batch to kits. ${e}`
);
}
}

const onCreateBatchCallback =
mpInstance._Store.SDKConfig.onCreateBatch;

Expand Down
58 changes: 0 additions & 58 deletions src/forwarders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {
isBlockedByEventFilter,
filterEventAttributes,
filterUserIdentities,
isBatchEventAllowed,
filterBatchEventAttributes,
filterBatchIdentities,
} from './forwarder-utils';

const { Modify, Identify, Login, Logout } = Constants.IdentityMethods;
Expand Down Expand Up @@ -266,61 +263,6 @@ export default function Forwarders(mpInstance, kitBlocker) {
}
};

this.sendBatchToForwarders = function(batch) {
if (
mpInstance._Store.webviewBridgeEnabled ||
!mpInstance._Store.activeForwarders
) {
return;
}

for (const forwarder of mpInstance._Store.activeForwarders) {
if (!forwarder.processBatch) {
continue;
}

try {
const batchCopy = extend(true, {}, batch);

if (batchCopy.events) {
batchCopy.events = batchCopy.events.filter(function(
batchEvent
) {
return isBatchEventAllowed(batchEvent, forwarder);
});

batchCopy.events.forEach(function(batchEvent) {
filterBatchEventAttributes(batchEvent, forwarder);
});
}

batchCopy.user_identities = filterBatchIdentities(
batchCopy.user_identities,
forwarder.userIdentityFilters
);

if (batchCopy.user_attributes) {
batchCopy.user_attributes = KitFilterHelper.filterUserAttributes(
batchCopy.user_attributes,
forwarder.userAttributeFilters
);
}

if (!batchCopy.events || batchCopy.events.length === 0) {
continue;
}

const result = forwarder.processBatch(batchCopy);

if (result) {
mpInstance.Logger.verbose(result);
}
} catch (e) {
mpInstance.Logger.verbose(e);
}
}
};

this.handleForwarderUserAttributes = function(functionNameKey, key, value) {
if (
(kitBlocker && kitBlocker.isAttributeKeyBlocked(key)) ||
Expand Down
226 changes: 0 additions & 226 deletions test/jest/batchUploader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('BatchUploader', () => {
let batchUploader: BatchUploader;
let mockMPInstance: IMParticleWebSDKInstance;
let originalFetch: typeof global.fetch;
let mockSendBatchToForwarders: jest.Mock;

beforeEach(() => {
const now = Date.now();
Expand All @@ -17,7 +16,6 @@ describe('BatchUploader', () => {
global.fetch = jest.fn().mockResolvedValue({ ok: true, status: 200 });

// Create a mock mParticle instance with mocked methods for instantiating a BatchUploader
mockSendBatchToForwarders = jest.fn();
mockMPInstance = {
_Store: {
SDKConfig: {
Expand All @@ -30,9 +28,6 @@ describe('BatchUploader', () => {
createServiceUrl: jest.fn().mockReturnValue('https://mock-url.com'),
generateUniqueId: jest.fn().mockReturnValue('mock-uuid'),
},
_Forwarders: {
sendBatchToForwarders: mockSendBatchToForwarders,
},
Identity: {
getCurrentUser: jest.fn().mockReturnValue({
getMPID: () => 'test-mpid',
Expand All @@ -58,227 +53,6 @@ describe('BatchUploader', () => {
global.fetch = originalFetch;
});

describe('batch forwarding to kits', () => {
it('should call sendBatchToForwarders for each new batch in createNewBatches', () => {
const mockEvents = [
{
EventName: 'Test Event',
EventDataType: 4,
MPID: 'test-mpid',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'test-mpid',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);

expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(1);
expect(mockSendBatchToForwarders).toHaveBeenCalledWith(
expect.objectContaining({
mpid: 'test-mpid',
})
);
});

it('should call sendBatchToForwarders before onCreateBatch', () => {
const callOrder: string[] = [];

mockSendBatchToForwarders.mockImplementation(() => {
callOrder.push('sendBatchToForwarders');
});

mockMPInstance._Store.SDKConfig.onCreateBatch = (batch) => {
callOrder.push('onCreateBatch');
return batch;
};

const mockEvents = [
{
EventName: 'Test Event',
EventDataType: 4,
MPID: 'test-mpid',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'test-mpid',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);

expect(callOrder).toEqual([
'sendBatchToForwarders',
'onCreateBatch',
]);
});

it('should forward batch before onCreateBatch can modify it', () => {
let forwardedEventCount = 0;

mockSendBatchToForwarders.mockImplementation((batch) => {
forwardedEventCount = batch.events.length;
});

mockMPInstance._Store.SDKConfig.onCreateBatch = (batch) => {
batch.modified = true;
batch.events = [];
return batch;
};

const mockEvents = [
{
EventName: 'Test Event',
EventDataType: 4,
MPID: 'test-mpid',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'test-mpid',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);

expect(forwardedEventCount).toBe(1);
});

it('should forward batch even when onCreateBatch drops it', () => {
mockMPInstance._Store.SDKConfig.onCreateBatch = () => {
return null;
};

(mockMPInstance.Logger as any).warning = jest.fn();

const mockEvents = [
{
EventName: 'Test Event',
EventDataType: 4,
MPID: 'test-mpid',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'test-mpid',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);

expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(1);
});

it('should not throw if sendBatchToForwarders errors', () => {
mockSendBatchToForwarders.mockImplementation(() => {
throw new Error('Kit failure');
});

const mockEvents = [
{
EventName: 'Test Event',
EventDataType: 4,
MPID: 'test-mpid',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'test-mpid',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

expect(() => {
BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);
}).not.toThrow();

expect(mockMPInstance.Logger.error).toHaveBeenCalled();
});

it('should create separate batches per MPID and forward each', () => {
const mockEvents = [
{
EventName: 'Event User A',
EventDataType: 4,
MPID: 'mpid-a',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
{
EventName: 'Event User B',
EventDataType: 4,
MPID: 'mpid-b',
SessionId: 'session-1',
EventCategory: 1,
Timestamp: Date.now(),
},
];

const mockUser = {
getMPID: () => 'mpid-a',
getConsentState: () => null,
getUserIdentities: () => ({ userIdentities: {} }),
getAllUserAttributes: () => ({}),
};

BatchUploader['createNewBatches'](
mockEvents as any,
mockUser as any,
mockMPInstance
);

expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(2);
});
});

describe('shouldDebounceAST', () => {
it('should return false for first call', () => {
// First call should not be debounced
Expand Down
Loading
Loading