This is an automated email from the ASF dual-hosted git repository.
sergehuber pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/unomi-tracker.git
The following commit(s) were added to refs/heads/main by this push:
new 2fe6ab8 UNOMI-1005: Add an option to send tracker requests without
cookies (#26)
2fe6ab8 is described below
commit 2fe6ab8e91d5739fb5946779bae26e7f63b80b95
Author: Serge Huber <[email protected]>
AuthorDate: Sat Oct 24 07:27:34 2026 +0200
UNOMI-1005: Add an option to send tracker requests without cookies (#26)
* UNOMI-1005: Add an option to send tracker requests without cookies
sendCredentials defaults to true so existing pages keep sending cookies.
Set it to false when the site does not need the visitor cookie and the
server allow-list is empty.
* chore: fix lint errors in bug demonstration tests
Indentation and quote-props only, applied with eslint --fix. No
behaviour change.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* UNOMI-1005: Declare sendCredentials and test it
Declare sendCredentials on the tracker object so the type check passes,
document that profiles do not persist across sessions without cookies,
and test withCredentials for the default, true and false cases.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
src/apache-unomi-tracker.js | 4 +-
src/types.ts | 6 +
test/bugs/bug-demonstration.test.js | 532 ++++++++++++++++++------------------
test/spec.js | 34 +++
4 files changed, 309 insertions(+), 267 deletions(-)
diff --git a/src/apache-unomi-tracker.js b/src/apache-unomi-tracker.js
index aeb6a32..d5a710d 100644
--- a/src/apache-unomi-tracker.js
+++ b/src/apache-unomi-tracker.js
@@ -26,6 +26,7 @@ export const useTracker = () => {
trackerSessionIdCookieName: 'wem-session-id',
browserGeneratedSessionSuffix: '',
disableTrackedConditionsListeners: false,
+ sendCredentials: true,
activateWem: true,
contextServerCookieName: 'context-profile-id',
contextServerUrl: '',
@@ -53,6 +54,7 @@ export const useTracker = () => {
wem.trackerSessionIdCookieName =
wem.digitalData.wemInitConfig.trackerSessionIdCookieName ?
wem.digitalData.wemInitConfig.trackerSessionIdCookieName : 'wem-session-id';
wem.browserGeneratedSessionSuffix =
wem.digitalData.wemInitConfig.browserGeneratedSessionSuffix ?
wem.digitalData.wemInitConfig.browserGeneratedSessionSuffix : '';
wem.disableTrackedConditionsListeners =
wem.digitalData.wemInitConfig.disableTrackedConditionsListeners || false;
+ wem.sendCredentials =
wem.digitalData.wemInitConfig.sendCredentials !== false;
wem.activateWem = wem.digitalData.wemInitConfig.activateWem;
const { contextServerUrl, timeoutInMilliseconds,
contextServerCookieName } = wem.digitalData.wemInitConfig;
@@ -688,7 +690,7 @@ export const useTracker = () => {
var xhr = new XMLHttpRequest();
if ('withCredentials' in xhr) {
xhr.open(options.type, options.url, options.async);
- xhr.withCredentials = true;
+ xhr.withCredentials = wem.sendCredentials;
} else if (typeof XDomainRequest != 'undefined') {
/* global XDomainRequest */
xhr = new XDomainRequest();
diff --git a/src/types.ts b/src/types.ts
index 635cdd5..ed23102 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -29,6 +29,12 @@ export interface DigitalData {
trackerSessionIdCookieName?: string;
browserGeneratedSessionSuffix?: string;
disableTrackedConditionsListeners?: boolean;
+ /**
+ * When false, tracker requests are sent without cookies. Default true.
+ * The server profile cookie is then not sent, so the profile is resolved
+ * from the session only and does not persist across sessions.
+ */
+ sendCredentials?: boolean;
};
personalizationCallback: Array<{
personalization: Personalization;
diff --git a/test/bugs/bug-demonstration.test.js
b/test/bugs/bug-demonstration.test.js
index 0e600c7..9ab4bf9 100644
--- a/test/bugs/bug-demonstration.test.js
+++ b/test/bugs/bug-demonstration.test.js
@@ -35,7 +35,7 @@ const { describe, it, expect } = require('vitest');
describe('Bug Demonstrations - These tests prove bugs exist', () => {
- describe('BUG #1: Incorrect splice operation in sendClickEvent (Line 448)',
() => {
+ describe('BUG #1: Incorrect splice operation in sendClickEvent (Line
448)', () => {
/**
* BUG DESCRIPTION:
* In sendClickEvent, when an event is already in eventsPrevented array,
@@ -51,76 +51,76 @@ describe('Bug Demonstrations - These tests prove bugs
exist', () => {
* - Array grows indefinitely on repeated clicks
*/
- it('should demonstrate that splice(eventIndex, 0) does not remove
elements', () => {
- // Simulate the buggy code
- const eventsPrevented = ['button1', 'button2'];
- const eventIndex = eventsPrevented.indexOf('button1');
+ it('should demonstrate that splice(eventIndex, 0) does not remove
elements', () => {
+ // Simulate the buggy code
+ const eventsPrevented = ['button1', 'button2'];
+ const eventIndex = eventsPrevented.indexOf('button1');
- expect(eventIndex).toBe(0); // button1 is at index 0
+ expect(eventIndex).toBe(0); // button1 is at index 0
- // This is what the current code does (WRONG)
- eventsPrevented.splice(eventIndex, 0);
+ // This is what the current code does (WRONG)
+ eventsPrevented.splice(eventIndex, 0);
- // The element is still there! (BUG)
- expect(eventsPrevented.length).toBe(2); // Should be 1 if we wanted to
remove it
- expect(eventsPrevented).toContain('button1'); // Still present
+ // The element is still there! (BUG)
+ expect(eventsPrevented.length).toBe(2); // Should be 1 if we
wanted to remove it
+ expect(eventsPrevented).toContain('button1'); // Still present
- // What it SHOULD do:
- // eventsPrevented.splice(eventIndex, 1); // Remove 1 element
- // expect(eventsPrevented.length).toBe(1);
- // expect(eventsPrevented).not.toContain('button1');
- });
+ // What it SHOULD do:
+ // eventsPrevented.splice(eventIndex, 1); // Remove 1 element
+ // expect(eventsPrevented.length).toBe(1);
+ // expect(eventsPrevented).not.toContain('button1');
+ });
- it('should demonstrate eventsPrevented array grows indefinitely', () => {
- // Simulate multiple clicks on the same button
- const eventsPrevented = [];
- const targetId = 'my-button';
-
- // First click - adds to array
- if (eventsPrevented.indexOf(targetId) === -1) {
- eventsPrevented.push(targetId);
- }
- expect(eventsPrevented.length).toBe(1);
-
- // Second click - should remove or skip, but current code does nothing
- const eventIndex = eventsPrevented.indexOf(targetId);
- if (eventIndex !== -1) {
- // BUGGY CODE: This doesn't remove anything
- eventsPrevented.splice(eventIndex, 0);
- } else {
- eventsPrevented.push(targetId);
- }
-
- // Array still has the element (BUG)
- expect(eventsPrevented.length).toBe(1); // Actually this test shows the
logic issue
- // But if the code path is wrong, it might add again:
-
- // Third click - might add again because splice didn't remove it
- const eventIndex2 = eventsPrevented.indexOf(targetId);
- if (eventIndex2 !== -1) {
- eventsPrevented.splice(eventIndex2, 0); // Still doesn't remove
- } else {
- eventsPrevented.push(targetId); // This won't execute
- }
-
- // The real issue: if the logic intended to remove, it doesn't work
- // If the logic intended to skip, the array management is broken
- });
+ it('should demonstrate eventsPrevented array grows indefinitely', ()
=> {
+ // Simulate multiple clicks on the same button
+ const eventsPrevented = [];
+ const targetId = 'my-button';
+
+ // First click - adds to array
+ if (eventsPrevented.indexOf(targetId) === -1) {
+ eventsPrevented.push(targetId);
+ }
+ expect(eventsPrevented.length).toBe(1);
+
+ // Second click - should remove or skip, but current code does
nothing
+ const eventIndex = eventsPrevented.indexOf(targetId);
+ if (eventIndex !== -1) {
+ // BUGGY CODE: This doesn't remove anything
+ eventsPrevented.splice(eventIndex, 0);
+ } else {
+ eventsPrevented.push(targetId);
+ }
+
+ // Array still has the element (BUG)
+ expect(eventsPrevented.length).toBe(1); // Actually this test
shows the logic issue
+ // But if the code path is wrong, it might add again:
+
+ // Third click - might add again because splice didn't remove it
+ const eventIndex2 = eventsPrevented.indexOf(targetId);
+ if (eventIndex2 !== -1) {
+ eventsPrevented.splice(eventIndex2, 0); // Still doesn't remove
+ } else {
+ eventsPrevented.push(targetId); // This won't execute
+ }
+
+ // The real issue: if the logic intended to remove, it doesn't work
+ // If the logic intended to skip, the array management is broken
+ });
- it('should show the correct behavior after fix', () => {
- // This test shows what SHOULD happen after the bug is fixed
- const eventsPrevented = ['button1'];
- const eventIndex = eventsPrevented.indexOf('button1');
+ it('should show the correct behavior after fix', () => {
+ // This test shows what SHOULD happen after the bug is fixed
+ const eventsPrevented = ['button1'];
+ const eventIndex = eventsPrevented.indexOf('button1');
- // CORRECT CODE: Remove 1 element
- eventsPrevented.splice(eventIndex, 1);
+ // CORRECT CODE: Remove 1 element
+ eventsPrevented.splice(eventIndex, 1);
- expect(eventsPrevented.length).toBe(0);
- expect(eventsPrevented).not.toContain('button1');
+ expect(eventsPrevented.length).toBe(0);
+ expect(eventsPrevented).not.toContain('button1');
+ });
});
- });
- describe('BUG #2: Broken _resolveId function (Lines 1420-1428)', () => {
+ describe('BUG #2: Broken _resolveId function (Lines 1420-1428)', () => {
/**
* BUG DESCRIPTION:
* The _resolveId function has multiple issues:
@@ -139,221 +139,221 @@ describe('Bug Demonstrations - These tests prove bugs
exist', () => {
* - Misses matches at the beginning of the string
*/
- it('should demonstrate that filter returns an array, not a string', () => {
- // Simulate the buggy code
- const sourceLocalIdentifierMap = {
- 'old-prefix': 'new-prefix',
- 'another-old': 'another-new'
- };
-
- const id = 'old-prefix-item-123';
-
- // This is what the current code does (WRONG)
- const source = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id.indexOf(source) > 0; // BUG: > 0 misses index 0
- });
-
- // source is an ARRAY, not a string!
- expect(Array.isArray(source)).toBe(true);
- expect(typeof source).toBe('object');
- expect(source).not.toBe('old-prefix'); // It's an array containing
'old-prefix'
-
- // The code then tries to do: id.replace(source, ...)
- // This will fail because replace() expects a string, not an array
- expect(() => {
- id.replace(source, sourceLocalIdentifierMap[source]); // This would
fail
- }).toThrow(); // Or return wrong result
- });
+ it('should demonstrate that filter returns an array, not a string', ()
=> {
+ // Simulate the buggy code
+ const sourceLocalIdentifierMap = {
+ 'old-prefix': 'new-prefix',
+ 'another-old': 'another-new'
+ };
+
+ const id = 'old-prefix-item-123';
+
+ // This is what the current code does (WRONG)
+ const source =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) > 0; // BUG: > 0 misses index 0
+ });
+
+ // source is an ARRAY, not a string!
+ expect(Array.isArray(source)).toBe(true);
+ expect(typeof source).toBe('object');
+ expect(source).not.toBe('old-prefix'); // It's an array containing
'old-prefix'
+
+ // The code then tries to do: id.replace(source, ...)
+ // This will fail because replace() expects a string, not an array
+ expect(() => {
+ id.replace(source, sourceLocalIdentifierMap[source]); // This
would fail
+ }).toThrow(); // Or return wrong result
+ });
- it('should demonstrate that indexOf > 0 misses matches at position 0', ()
=> {
- const sourceLocalIdentifierMap = {
- 'prefix': 'new-prefix'
- };
-
- const id = 'prefix-item-123'; // Match at index 0
- const id2 = 'some-prefix-item-123'; // Match at index 5
-
- // Current buggy code
- const source1 = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id.indexOf(source) > 0; // BUG: > 0
- });
-
- const source2 = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id2.indexOf(source) > 0;
- });
-
- // First ID has match at index 0, so > 0 is false, filter returns empty
array
- expect(source1.length).toBe(0); // BUG: Should find 'prefix'
-
- // Second ID has match at index 5, so > 0 is true, filter returns array
with 'prefix'
- expect(source2.length).toBe(1); // This one works (by accident)
-
- // CORRECT CODE should use !== -1 or >= 0
- const correctSource1 =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
- return id.indexOf(source) !== -1; // CORRECT
- });
- expect(correctSource1.length).toBe(1); // Now it finds it
- });
+ it('should demonstrate that indexOf > 0 misses matches at position 0',
() => {
+ const sourceLocalIdentifierMap = {
+ prefix: 'new-prefix'
+ };
+
+ const id = 'prefix-item-123'; // Match at index 0
+ const id2 = 'some-prefix-item-123'; // Match at index 5
+
+ // Current buggy code
+ const source1 =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) > 0; // BUG: > 0
+ });
+
+ const source2 =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id2.indexOf(source) > 0;
+ });
+
+ // First ID has match at index 0, so > 0 is false, filter returns
empty array
+ expect(source1.length).toBe(0); // BUG: Should find 'prefix'
+
+ // Second ID has match at index 5, so > 0 is true, filter returns
array with 'prefix'
+ expect(source2.length).toBe(1); // This one works (by accident)
+
+ // CORRECT CODE should use !== -1 or >= 0
+ const correctSource1 =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) !== -1; // CORRECT
+ });
+ expect(correctSource1.length).toBe(1); // Now it finds it
+ });
- it('should demonstrate the complete broken behavior', () => {
- const sourceLocalIdentifierMap = {
- 'old': 'new'
- };
-
- const id = 'old-item'; // Match at index 0
-
- // Simulate the complete buggy function
- function buggy_resolveId(id) {
- if (sourceLocalIdentifierMap) {
- var source = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id.indexOf(source) > 0; // BUG 1: > 0 misses index 0
- });
- // BUG 2: source is an array, not a string
- // BUG 3: Can't use array in replace()
- return source ? id.replace(source, sourceLocalIdentifierMap[source])
: id;
- }
- return id;
- }
-
- // This will either throw an error or return wrong result
- const result = buggy_resolveId(id);
-
- // The function likely returns the original ID unchanged (because source
is empty array, falsy)
- // OR it throws an error when trying to use array in replace()
- expect(result).toBe(id); // Returns unchanged (wrong behavior)
- // OR expect(() => buggy_resolveId(id)).toThrow(); // If replace() throws
- });
+ it('should demonstrate the complete broken behavior', () => {
+ const sourceLocalIdentifierMap = {
+ old: 'new'
+ };
+
+ const id = 'old-item'; // Match at index 0
+
+ // Simulate the complete buggy function
+ function buggy_resolveId(id) {
+ if (sourceLocalIdentifierMap) {
+ var source =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) > 0; // BUG 1: > 0 misses
index 0
+ });
+ // BUG 2: source is an array, not a string
+ // BUG 3: Can't use array in replace()
+ return source ? id.replace(source,
sourceLocalIdentifierMap[source]) : id;
+ }
+ return id;
+ }
+
+ // This will either throw an error or return wrong result
+ const result = buggy_resolveId(id);
+
+ // The function likely returns the original ID unchanged (because
source is empty array, falsy)
+ // OR it throws an error when trying to use array in replace()
+ expect(result).toBe(id); // Returns unchanged (wrong behavior)
+ // OR expect(() => buggy_resolveId(id)).toThrow(); // If replace()
throws
+ });
- it('should show the correct behavior after fix', () => {
- const sourceLocalIdentifierMap = {
- 'old': 'new',
- 'prefix': 'new-prefix'
- };
-
- // CORRECT implementation
- function correct_resolveId(id) {
- if (sourceLocalIdentifierMap) {
- var sources = Object.keys(sourceLocalIdentifierMap);
- for (var i = 0; i < sources.length; i++) {
- var source = sources[i];
- if (id.indexOf(source) !== -1) { // CORRECT: !== -1 finds all
matches
- return id.replace(source, sourceLocalIdentifierMap[source]);
+ it('should show the correct behavior after fix', () => {
+ const sourceLocalIdentifierMap = {
+ old: 'new',
+ prefix: 'new-prefix'
+ };
+
+ // CORRECT implementation
+ function correct_resolveId(id) {
+ if (sourceLocalIdentifierMap) {
+ var sources = Object.keys(sourceLocalIdentifierMap);
+ for (var i = 0; i < sources.length; i++) {
+ var source = sources[i];
+ if (id.indexOf(source) !== -1) { // CORRECT: !== -1
finds all matches
+ return id.replace(source,
sourceLocalIdentifierMap[source]);
+ }
+ }
+ }
+ return id;
}
- }
- }
- return id;
- }
- // Test with match at index 0
- expect(correct_resolveId('old-item')).toBe('new-item');
+ // Test with match at index 0
+ expect(correct_resolveId('old-item')).toBe('new-item');
- // Test with match at index > 0
- expect(correct_resolveId('some-old-item')).toBe('some-new-item');
+ // Test with match at index > 0
+ expect(correct_resolveId('some-old-item')).toBe('some-new-item');
- // Test with no match
- expect(correct_resolveId('other-item')).toBe('other-item');
- });
+ // Test with no match
+ expect(correct_resolveId('other-item')).toBe('other-item');
+ });
- it('should demonstrate real-world impact: ID mapping fails', () => {
- // Real-world scenario: Unomi backend uses IDs like "site:page:123"
- // Frontend uses IDs like "page-123"
- // sourceLocalIdentifierMap should map between them
-
- const sourceLocalIdentifierMap = {
- 'site:page:': 'page-',
- 'site:video:': 'video-'
- };
-
- // Backend ID from Unomi
- const backendId = 'site:page:123';
-
- // Current buggy code
- function buggy_resolveId(id) {
- if (sourceLocalIdentifierMap) {
- var source = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id.indexOf(source) > 0; // BUG: 'site:page:' is at index 0,
so > 0 is false
- });
- return source ? id.replace(source, sourceLocalIdentifierMap[source])
: id;
- }
- return id;
- }
-
- const result = buggy_resolveId(backendId);
-
- // Should be 'page-123' but buggy code returns unchanged
- expect(result).toBe(backendId); // BUG: Returns 'site:page:123' instead
of 'page-123'
-
- // This means the tracker can't find elements on the page!
- // document.getElementById('page-123') won't find element with id
'site:page:123'
+ it('should demonstrate real-world impact: ID mapping fails', () => {
+ // Real-world scenario: Unomi backend uses IDs like "site:page:123"
+ // Frontend uses IDs like "page-123"
+ // sourceLocalIdentifierMap should map between them
+
+ const sourceLocalIdentifierMap = {
+ 'site:page:': 'page-',
+ 'site:video:': 'video-'
+ };
+
+ // Backend ID from Unomi
+ const backendId = 'site:page:123';
+
+ // Current buggy code
+ function buggy_resolveId(id) {
+ if (sourceLocalIdentifierMap) {
+ var source =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) > 0; // BUG: 'site:page:' is
at index 0, so > 0 is false
+ });
+ return source ? id.replace(source,
sourceLocalIdentifierMap[source]) : id;
+ }
+ return id;
+ }
+
+ const result = buggy_resolveId(backendId);
+
+ // Should be 'page-123' but buggy code returns unchanged
+ expect(result).toBe(backendId); // BUG: Returns 'site:page:123'
instead of 'page-123'
+
+ // This means the tracker can't find elements on the page!
+ // document.getElementById('page-123') won't find element with id
'site:page:123'
+ });
});
- });
- describe('Integration: How bugs affect real functionality', () => {
- it('should demonstrate Bug #1 prevents proper click event handling', () =>
{
- // Scenario: User clicks a button multiple times
- // After first click, event should be prevented and tracked
- // On subsequent clicks, the behavior should be consistent
-
- const eventsPrevented = [];
- const targetId = 'submit-button';
-
- // First click
- let eventIndex = eventsPrevented.indexOf(targetId);
- if (eventIndex === -1) {
- eventsPrevented.push(targetId);
- // Event is prevented and sent to Unomi
- }
- expect(eventsPrevented.length).toBe(1);
-
- // Second click - current buggy code
- eventIndex = eventsPrevented.indexOf(targetId);
- if (eventIndex !== -1) {
- eventsPrevented.splice(eventIndex, 0); // BUG: Doesn't remove
- }
-
- // Array still has the element
- expect(eventsPrevented.length).toBe(1);
- expect(eventsPrevented).toContain(targetId);
-
- // The logic is unclear:
- // - If it should remove: bug prevents removal
- // - If it should skip: why check and do nothing?
- // Either way, the behavior is incorrect
- });
+ describe('Integration: How bugs affect real functionality', () => {
+ it('should demonstrate Bug #1 prevents proper click event handling',
() => {
+ // Scenario: User clicks a button multiple times
+ // After first click, event should be prevented and tracked
+ // On subsequent clicks, the behavior should be consistent
+
+ const eventsPrevented = [];
+ const targetId = 'submit-button';
+
+ // First click
+ let eventIndex = eventsPrevented.indexOf(targetId);
+ if (eventIndex === -1) {
+ eventsPrevented.push(targetId);
+ // Event is prevented and sent to Unomi
+ }
+ expect(eventsPrevented.length).toBe(1);
+
+ // Second click - current buggy code
+ eventIndex = eventsPrevented.indexOf(targetId);
+ if (eventIndex !== -1) {
+ eventsPrevented.splice(eventIndex, 0); // BUG: Doesn't remove
+ }
+
+ // Array still has the element
+ expect(eventsPrevented.length).toBe(1);
+ expect(eventsPrevented).toContain(targetId);
+
+ // The logic is unclear:
+ // - If it should remove: bug prevents removal
+ // - If it should skip: why check and do nothing?
+ // Either way, the behavior is incorrect
+ });
- it('should demonstrate Bug #2 prevents element lookup', () => {
- // Scenario: Unomi returns tracked condition with ID
"site:page:contact-form"
- // Frontend HTML has id="contact-form"
- // sourceLocalIdentifierMap should map "site:page:" to ""
-
- const sourceLocalIdentifierMap = {
- 'site:page:': '',
- 'site:video:': ''
- };
-
- const unomiId = 'site:page:contact-form';
- const expectedFrontendId = 'contact-form';
-
- // Current buggy code
- function buggy_resolveId(id) {
- if (sourceLocalIdentifierMap) {
- var source = Object.keys(sourceLocalIdentifierMap).filter(function
(source) {
- return id.indexOf(source) > 0; // BUG: 'site:page:' at index 0
- });
- return source ? id.replace(source, sourceLocalIdentifierMap[source])
: id;
- }
- return id;
- }
-
- const resolvedId = buggy_resolveId(unomiId);
-
- // Should resolve to 'contact-form' but doesn't
- expect(resolvedId).toBe(unomiId); // BUG: Returns
'site:page:contact-form'
-
- // Now try to find element:
- // document.getElementById('contact-form') - won't find it
- // document.getElementById('site:page:contact-form') - also won't find
it (wrong ID)
- // Result: Tracker can't attach listeners to form!
+ it('should demonstrate Bug #2 prevents element lookup', () => {
+ // Scenario: Unomi returns tracked condition with ID
"site:page:contact-form"
+ // Frontend HTML has id="contact-form"
+ // sourceLocalIdentifierMap should map "site:page:" to ""
+
+ const sourceLocalIdentifierMap = {
+ 'site:page:': '',
+ 'site:video:': ''
+ };
+
+ const unomiId = 'site:page:contact-form';
+ const expectedFrontendId = 'contact-form';
+
+ // Current buggy code
+ function buggy_resolveId(id) {
+ if (sourceLocalIdentifierMap) {
+ var source =
Object.keys(sourceLocalIdentifierMap).filter(function (source) {
+ return id.indexOf(source) > 0; // BUG: 'site:page:' at
index 0
+ });
+ return source ? id.replace(source,
sourceLocalIdentifierMap[source]) : id;
+ }
+ return id;
+ }
+
+ const resolvedId = buggy_resolveId(unomiId);
+
+ // Should resolve to 'contact-form' but doesn't
+ expect(resolvedId).toBe(unomiId); // BUG: Returns
'site:page:contact-form'
+
+ // Now try to find element:
+ // document.getElementById('contact-form') - won't find it
+ // document.getElementById('site:page:contact-form') - also won't
find it (wrong ID)
+ // Result: Tracker can't attach listeners to form!
+ });
});
- });
});
diff --git a/test/spec.js b/test/spec.js
index 7db513e..4e868d9 100644
--- a/test/spec.js
+++ b/test/spec.js
@@ -20,4 +20,38 @@ const unomi = require('..');
const tracker = unomi.useTracker();
assert(tracker !== null);
+
+// sendCredentials controls xhr.withCredentials
+globalThis.document = { cookie: 'wem-session-id=test-session' };
+globalThis.XMLHttpRequest = class {
+ constructor() {
+ this.withCredentials = undefined;
+ }
+ open() {}
+ setRequestHeader() {}
+ send() {}
+};
+
+function withCredentialsFor(wemInitConfig) {
+ const t = unomi.useTracker();
+ t.initTracker({ wemInitConfig: Object.assign({ contextServerUrl: '',
timeoutInMilliseconds: -1 }, wemInitConfig) });
+ let sentXhr;
+ const OriginalXHR = globalThis.XMLHttpRequest;
+ globalThis.XMLHttpRequest = class extends OriginalXHR {
+ send() {
+ sentXhr = this;
+ }
+ };
+ try {
+ t.ajax({ type: 'POST', url: '/context.json', async: true });
+ } finally {
+ globalThis.XMLHttpRequest = OriginalXHR;
+ }
+ return sentXhr.withCredentials;
+}
+
+assert.strictEqual(withCredentialsFor({}), true, 'credentials are sent by
default');
+assert.strictEqual(withCredentialsFor({ sendCredentials: true }), true,
'credentials are sent when sendCredentials is true');
+assert.strictEqual(withCredentialsFor({ sendCredentials: false }), false,
'credentials are not sent when sendCredentials is false');
+
console.log('Tests passed');
\ No newline at end of file