# Mesh SDK Test Overview

A plain-English description of every test in the suite.

---

## 1. getScriptAttributes

These tests verify that the SDK correctly reads its configuration from the HTML `<script>` tag on the page.

- **Reads sdkKey from the data-mesh-sdk attribute** — When the page contains a `<script id="mesh-analytics-sdk" data-mesh-sdk="test-key-123">`, calling `getScriptAttributes` stores `"test-key-123"` in `glob.sdkKey`.
- **Parses sdkAttributes from data-mesh-sdk-attributes** — When the script tag has a JSON string in `data-mesh-sdk-attributes`, the function parses it and stores the resulting object (tracking flags, appMode, etc.) in `glob.sdkAttributes`.
- **Handles a missing script element gracefully** — If there is no `<script id="mesh-analytics-sdk">` on the page at all, the function does not throw; `sdkKey` ends up undefined and `sdkAttributes` falls back to an empty object.
- **Handles missing data-mesh-sdk-attributes gracefully** — If the script tag exists but has no `data-mesh-sdk-attributes`, the `sdkKey` is still read correctly and `sdkAttributes` defaults to an empty object.

---

## 2. getWorkspaceDetails

These tests verify that the SDK fetches workspace configuration from the API and stores it correctly.

- **Calls apiRequest with the correct payload** — The function sends a POST to `init-sdk` with the current page domain and the SDK key from `glob.sdkKey`.
- **Populates glob with workspace data** — After a successful response, `glob.workspaceId`, `glob.workspaceName`, `glob.fullContactKey`, `glob.active`, and `glob.contactResolutionEnabled` are all set from the API response.
- **Sets rb2bEnabled and vectorEnabled from response** — The boolean flags `rb2bEnabled` and `vectorEnabled` are stored in glob based on what the API returns.
- **Defaults rb2bEnabled / vectorEnabled to false when absent** — If the API response does not include `rb2bEnabled` or `vectorEnabled`, both default to `false` rather than being undefined.

---

## 3. preLoadEvents and removeConflictingScripts

### preLoadEvents

- **Adds a "message" event listener** — The function attaches a `message` listener to the window so the SDK can communicate with embedded iframes.
- **Does NOT attempt to remove scripts when contactResolutionEnabled is false** — When the workspace has contact resolution turned off, any conflicting third-party scripts already on the page are left alone.

### removeConflictingScripts

- **Removes a Vector pixel script from the DOM** — If a `<script src="...cdn.vector.co/pixel.js">` element exists, it is removed from the document.
- **Removes an RB2B pixel script from the DOM** — If a `<script src="...ddwl4m2hdecbv.cloudfront.net...">` element exists, it is removed from the document.
- **Cleans up window.vector and window.reb2b after removal** — After removing conflicting scripts, the global `window.vector` and `window.reb2b` objects are deleted so they don't interfere with the SDK's own resolution scripts.
- **Exits early when no conflicting scripts exist** — If there are no Vector or RB2B scripts on the page, the function returns immediately without waiting for the 5-second inline-script scan.

---

## 4. initializeVisitor

### Returning visitor (cookie already exists)

- **Uses the existing visitor ID from the cookie** — When a `mesh-visitor-id` cookie is present, the function returns that ID and sets `glob.visitorId` to it.
- **Does NOT generate a new UUID** — `crypto.randomUUID` is never called for returning visitors.
- **Does NOT create a new contact (no contacts POST)** — No POST request to the `contacts` endpoint is made; the existing contact is reused.
- **Creates a touchpoint (touches POST)** — A POST to the `touches` endpoint is still fired to record the new page view.
- **Populates fingerprints** — `glob.fingerprints` is set with `hem`, `maid`, and `personId` fields (all null when fingerprinting is not enabled).
- **Sets contactResolutionPayload** — `glob.contactResolutionPayload` is a JSON string containing the visitor ID and workspace ID, used for downstream identity resolution.
- **Sends visitor ID to frames** — The resolved visitor ID is broadcast to all embedded iframes via `postMessageToAllFrames`.

### New visitor (no cookie)

- **Generates a new UUID for the visitor** — `crypto.randomUUID` is called and the result becomes the new visitor ID stored in `glob.visitorId`.
- **Sets a cookie with the new visitor ID** — A `mesh-visitor-id` cookie is set with a 30-day expiry so the visitor is recognized on future visits.
- **Creates a contact AND a touchpoint** — Both a `contacts` POST (to create the identity) and a `touches` POST (to record the view) are made.
- **Populates fingerprints** — Same as the returning visitor case: `glob.fingerprints` is populated with the default fields.
- **Sets contactResolutionPayload with new ID** — The payload JSON contains the newly generated visitor ID.
- **Captures email from URL param m_email in fingerprints** — If the page URL contains `?m_email=test@example.com`, that email is added to `glob.fingerprints.email`.
- **Does NOT set cookie when useFingerprint is true** — When cookieless fingerprinting is enabled (`useFingerprint: true`), no `mesh-visitor-id` cookie is set (verified indirectly by confirming the cookie IS set when `useFingerprint` is false).

---

## 5. startContactResolution

### Guard clauses

- **Exits early when contactResolutionEnabled is false** — If the workspace doesn't have contact resolution turned on, no scripts are injected and no polling occurs.
- **Exits early when rb2b cookie status is "success"** — If a `mesh-rb2b-status` cookie with value `"success"` already exists, resolution is skipped to avoid redundant attempts.
- **Exits early when vector cookie status is "success"** — Same as above, but for the `mesh-vec-status` cookie.
- **Exits early when BOTH cookie statuses are "success"** — If both cookies indicate recent success, no action is taken.

### RB2B enabled path

- **Activates RB2B: injects script, sets window config, sets cookie** — When RB2B is enabled and polling resolves successfully, the RB2B script is injected into the page, `window.rb2bConfig` is set with the visitor's contact resolution payload, `window.avina_rb2bScriptInjected` is flagged true, and a `mesh-rb2b-status` cookie is set.
- **Returns early if RB2B resolves successfully (no Vector fallback)** — When RB2B resolves the identity, Vector is not activated even if it's enabled.
- **Falls through to Vector when RB2B times out and vector is enabled** — If RB2B polling times out without resolution, the SDK waterfalls to Vector and injects the Vector script as well.
- **Does NOT fall through to Vector when RB2B times out but vector is disabled** — If RB2B times out and Vector is not enabled for the workspace, only RB2B's script is injected and no further action is taken.
- **Does not inject RB2B script twice if already injected** — If the `window.avina_rb2bScriptInjected` flag is already true, `injectScript` is not called again.

### Vector-only path (RB2B disabled)

- **Activates Vector directly without any RB2B delay** — When only Vector is enabled, the Vector script is injected immediately without waiting for RB2B polling.
- **Does NOT inject RB2B script** — The RB2B script content is never passed to `injectScript`.
- **Does not inject Vector script twice if already injected** — If the `window.avina_vectorScriptInjected` flag is already true, no duplicate script injection occurs.

### Neither enabled

- **Does nothing when both are disabled** — No scripts are injected and no polling happens if neither RB2B nor Vector is enabled.

---

## 6. activateRb2b (unit)

- **Injects the RB2B script and sets window flags** — Calls `injectScript` with the RB2B script content, sets `window.avina_rb2bScriptInjected` to true, and sets `window.rb2bConfig` with the contact resolution payload.
- **Sets the mesh-rb2b-status cookie to "success"** — After activation, a cookie is set so the SDK knows resolution was recently attempted.
- **Exits early if script was already injected** — If the window flag is already true, the function returns without doing anything.

---

## 7. activateVector (unit)

- **Injects the Vector script and sets window flag** — Calls `injectScript` with the Vector script content and sets `window.avina_vectorScriptInjected` to true.
- **Does not inject script if already injected** — If the window flag is already true, no duplicate injection occurs.

---

## 8. waitForResolution (unit)

- **Returns true when poll resolves successfully** — When the polling function reports the contact was resolved, `waitForResolution` returns `true`.
- **Returns false when poll times out** — When the polling function exhausts all attempts without resolution, the function returns `false`.
- **Calls poll with the correct timeout** — The timeout value passed to `waitForResolution` is forwarded to the underlying `poll` utility as its third argument.
