Skip to content

Commit cdc2625

Browse files
aitorvsmalmstein
andauthored
Fix issue where context is not attach on first use (#7790)
Task/Issue URL: https://app.asana.com/1/137249556945/project/1211654189969294/task/1213386554167128?focus=true ### Description On first time use the page content is not attached. Repro steps: * fresh install and go through onboarding to get to the point where contextual can be used * go to eg. bbc.com and tap on duck.ai icon to open the contextual sheet * sheet opens and shows the first time run * "attach page content" placeholder is shown alongside the chip suggestions * tap on the "summarize" suggestion chip OR tap on placeholder * Expected: content is attached * ❌ Actual: content is NOT attached ### Steps to test this PR The following repro steps should now work Repro steps: * fresh install and go through onboarding to get to the point where contextual can be used * go to eg. bbc.com and tap on duck.ai icon to open the contextual sheet * sheet opens and shows the first time run * "attach page content" placeholder is shown alongside the chip suggestions * tap on the "summarize" suggestion chip OR tap on placeholder * Expected: content is attached * ❌ Actual: content is NOT attached <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Small, localized change to when a JS subscription event is emitted; main risk is timing/regression in contextual sheet analytics or page-context attachment triggering. > > **Overview** > Fixes first-run Duck.ai contextual mode not attaching page content by triggering page-context collection when the contextual bottom sheet is shown. > > Moves the page-context subscription event send out of the omnibar click path into a new `BrowserTabViewModel.collectPageContext()` called from `BrowserTabFragment.showDuckChatContextualSheet`, and adds unit tests to verify the event is emitted only when explicitly collecting context (and not during onboarding). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 7f779d0. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: David González <malmstein@gmail.com>
1 parent 18f8275 commit cdc2625

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,6 +3431,8 @@ class BrowserTabFragment :
34313431

34323432
reactToDuckChatContextualSheetResult()
34333433
ensureBrowserIsCompatibleWithContextualSheetState()
3434+
3435+
viewModel.collectPageContext()
34343436
}
34353437

34363438
private fun createNewContextualFragment(tabId: String) {

app/src/main/java/com/duckduckgo/app/browser/BrowserTabViewModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,6 +4692,13 @@ class BrowserTabViewModel @Inject constructor(
46924692
duckChat.openDuckChat()
46934693
}
46944694

4695+
fun collectPageContext() {
4696+
viewModelScope.launch {
4697+
val subscriptionEvent = pageContextJSHelper.onContextualOpened()
4698+
_subscriptionEventDataChannel.send(subscriptionEvent)
4699+
}
4700+
}
4701+
46954702
fun onDuckChatOmnibarButtonClicked(
46964703
query: String?,
46974704
hasFocus: Boolean,
@@ -4711,10 +4718,6 @@ class BrowserTabViewModel @Inject constructor(
47114718
viewModelScope.launch {
47124719
if (duckChat.isContextualOnboardingCompleted()) {
47134720
command.value = Command.ShowDuckAIContextualMode(tabId)
4714-
viewModelScope.launch {
4715-
val subscriptionEvent = pageContextJSHelper.onContextualOpened()
4716-
_subscriptionEventDataChannel.send(subscriptionEvent)
4717-
}
47184721
} else {
47194722
command.value = Command.ShowDuckAIContextualOnboarding
47204723
}

app/src/test/java/com/duckduckgo/app/browser/BrowserTabViewModelTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8857,6 +8857,37 @@ class BrowserTabViewModelTest {
88578857
verify(mockDuckChat, never()).openDuckChat()
88588858
}
88598859

8860+
@Test
8861+
fun whenCollectPageContextThenPageContextSubscriptionEventSent() = runTest {
8862+
val expectedEvent = SubscriptionEventData(
8863+
featureName = PAGE_CONTEXT_FEATURE_NAME,
8864+
subscriptionName = "collect",
8865+
params = JSONObject(),
8866+
)
8867+
whenever(mockPageContextJSHelper.onContextualOpened()).thenReturn(expectedEvent)
8868+
8869+
testee.collectPageContext()
8870+
8871+
testee.subscriptionEventDataFlow.test {
8872+
val emittedEvent = awaitItem()
8873+
assertEquals(PAGE_CONTEXT_FEATURE_NAME, emittedEvent.featureName)
8874+
cancelAndIgnoreRemainingEvents()
8875+
}
8876+
}
8877+
8878+
@Test
8879+
fun whenOnDuckChatOmnibarButtonClickedAndOnboardingNotCompletedThenNoPageContextSubscriptionEventSent() = runTest {
8880+
mockDuckAiContextualModeFlow.emit(true)
8881+
whenever(mockDuckChat.isContextualOnboardingCompleted()).thenReturn(false)
8882+
8883+
testee.subscriptionEventDataFlow.test {
8884+
testee.onDuckChatOmnibarButtonClicked(query = "example", hasFocus = false, isNtp = false)
8885+
8886+
expectNoEvents()
8887+
cancelAndIgnoreRemainingEvents()
8888+
}
8889+
}
8890+
88608891
@Test
88618892
fun whenSubscriptionChangesWhileOnDuckAiThenAuthUpdateEventSent() = runTest {
88628893
val duckAiUrl = "https://duck.ai/chat"

0 commit comments

Comments
 (0)