# Phase 6 - Desktop Live Endpoint Integration

Status: completed  
Date: 2026-04-18  
Target: `WepAppOpusApiDesktop`

## 1) Objective

Integrate live data flow into Desktop main process (no web server required) for:
1. live account lookup
2. live dashboard aggregation
3. token/profile resolution parity with web phase 6 contract

## 2) Core implementation

## 2.1 Live runtime modules

Added new modules under `WepAppOpusApiDesktop/main/live`:
1. `meta-client.js`
   - Graph API client for Desktop main process
   - retry + timeout + rate limit support
   - supports `/me/adaccounts` and insights pagination
2. `dashboard.js`
   - desktop JS port of `WepAppOpusApi/src/live/dashboard.ts`
   - exports:
     - `fetchAdAccounts`
     - `fetchLiveDashboard`
     - `fetchLiveDashboardMulti`
3. `live-service.js`
   - orchestrates token resolution, account selection, and dashboard request execution
   - normalizes payload and returns contract-safe response/error

## 2.2 Token resolution order (Desktop)

Implemented in `live-service.js` with strict order:
1. if `profileId` is provided -> resolve saved token profile (`resolveTokenProfileForExecution`)
2. else if inline token payload exists (`tokenSources`, `accessTokens`, `accessToken`) -> use inline token(s)
3. else fallback to default saved token profile on the machine

Behavior details:
1. profile preset accounts are auto-applied when request has no explicit account selection
2. `viewMode=single` uses first preset, other selection modes use full preset list
3. `viewMode=all` resolves accounts from all effective token sources

## 2.3 IPC contract channels

Added IPC contract channels in `main/ipc-handlers.js`:
1. `live:get-accounts-contract`
2. `live:get-dashboard-contract`

Response style:
1. success: `{ ok: true, ...payload }`
2. failure: `{ ok: false, error, errorCode, statusCode }`

Preload bridge (`main/preload.js`) updated with:
1. `getLiveAccountsContract(payload)`
2. `getLiveDashboardContract(payload)`

Renderer fallback client (`renderer/src/utils/apiClient.js`) updated with no-op stubs for both methods.

## 2.4 Testability hardening

`live-service.js` includes dependency injection helpers for deterministic unit tests:
1. `__private.setDependenciesForTest()`
2. `__private.resetDependenciesForTest()`

This avoids hard dependency on local native DB modules during live-service unit tests.

## 3) Verification

Commands executed:
1. `npm run lint`
2. `npm run test`
3. `npm run build:renderer`
4. `npm run build:win`

Result:
1. all checks passed
2. Windows artifacts generated in `WepAppOpusApiDesktop/release`:
   - `WepAppOpusApiDesktop Setup 0.1.0.exe`
   - `WepAppOpusApiDesktop 0.1.0.exe`
