Skip to content

Commit 2323712

Browse files
committed
better handling of global
1 parent 2e85613 commit 2323712

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

special-pages/pages/new-tab/app/omnibar/components/PersistentOmnibarValuesProvider.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,18 @@ export function useModeWithLocalPersistence(tabId, defaultMode) {
101101
useEffect(() => {
102102
if (!service) return;
103103
return service.onConfig((v) => {
104-
if (tabId && v.source === 'manual') {
105-
if (v.data.enableAi === false) {
106-
values?.updateAll({ value: 'search' });
107-
} else {
108-
values?.update({ id: tabId, value: v.data.mode });
109-
}
104+
if (!tabId) return;
105+
106+
// when manually updated + enableAi === 'true', allow this tab to be recorded
107+
if (v.source === 'manual') {
108+
values?.update({ id: tabId, value: v.data.mode });
109+
}
110+
111+
// when `enableAi` is false, we reset ALL tabs to 'search'
112+
if (v.data.enableAi === false) {
113+
values?.updateAll({ value: 'search' });
110114
}
115+
111116
setState(v.data.mode);
112117
});
113118
}, [service, tabId, values, defaultMode]);

special-pages/pages/new-tab/app/omnibar/integration-tests/omnibar.page.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,18 @@ export class OmnibarPage {
187187
*/
188188
async didSwitchToTab(tabId, tabIds) {
189189
await test.step(`simulate tab change event, to: ${tabId} `, async () => {
190-
await this.ntp.mocks.simulateSubscriptionMessage(sub('tabs_onDataUpdate'), tabs({ tabId, tabIds }));
190+
const event = sub('tabs_onDataUpdate').payload({ tabId, tabIds });
191+
await this.ntp.mocks.simulateSubscriptionEvent(event);
192+
});
193+
}
194+
195+
/**
196+
* @returns {Promise<void>}
197+
*/
198+
async didDisableGlobally() {
199+
const event = sub('omnibar_onConfigUpdate').payload({ mode: 'search', enableAi: false, showAiSetting: false });
200+
await test.step(`simulates global disabled (eg: settings): ${JSON.stringify(event.name)} ${JSON.stringify(event.payload)} `, async () => {
201+
await this.ntp.mocks.simulateSubscriptionEvent(event);
191202
});
192203
}
193204

@@ -218,6 +229,7 @@ export class OmnibarPage {
218229
return await expect(this.chatInput()).toHaveValue(value);
219230
}
220231
case 'search': {
232+
await this.searchInput().waitFor({ timeout: 1000 });
221233
return await expect(this.searchInput()).toHaveValue(value);
222234
}
223235
}
@@ -242,15 +254,14 @@ export class OmnibarPage {
242254
}
243255

244256
/**
245-
* @param {import("../../../types/new-tab.js").NewTabMessages["subscriptions"]["subscriptionEvent"]} name
257+
* @template {import("../../../types/new-tab.js").NewTabMessages["subscriptions"]["subscriptionEvent"]} SubName
258+
* @param {SubName} name
259+
* @return {{payload: (payload: Extract<import("../../../types/new-tab.js").NewTabMessages["subscriptions"], {subscriptionEvent: SubName}>['params']) => {name: string, payload: any}}}
246260
*/
247261
function sub(name) {
248-
return name;
249-
}
250-
251-
/**
252-
* @param {import("../../../types/new-tab.js").Tabs} t
253-
*/
254-
function tabs(t) {
255-
return t;
262+
return {
263+
payload: (payload) => {
264+
return { name, payload };
265+
},
266+
};
256267
}

special-pages/pages/new-tab/app/omnibar/integration-tests/omnibar.persistence.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,26 @@ test.describe('omnibar widget persistence', () => {
7373
await omnibar.didSwitchToTab('01', ['01', '02']);
7474
await omnibar.expectValue({ value: '', mode: 'search' });
7575
});
76+
test('adjusts mode of other tabs when duck.ai is globally disabled', async ({ page }, workerInfo) => {
77+
const ntp = NewtabPage.create(page, workerInfo);
78+
const omnibar = new OmnibarPage(ntp);
79+
await ntp.reducedMotion();
80+
await ntp.openPage({ additional: { omnibar: true, tabs: true, 'tabs.debug': true } });
81+
await omnibar.ready();
82+
83+
// first tab, switch to ai mode
84+
await omnibar.switchMode({ mode: 'ai' });
85+
86+
// switch to second tab, should be empty but still on duck.ai
87+
await omnibar.didSwitchToTab('02', ['01', '02']);
88+
await omnibar.expectValue({ value: '', mode: 'ai' });
89+
90+
// disable globally
91+
await omnibar.didDisableGlobally();
92+
await omnibar.expectValue({ value: '', mode: 'search' });
93+
94+
// back to first tab, should now also be search
95+
await omnibar.didSwitchToTab('01', ['01', '02']);
96+
await omnibar.expectValue({ value: '', mode: 'search' });
97+
});
7698
});

special-pages/shared/mocks.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ export class Mocks {
9292
});
9393
}
9494

95+
/**
96+
* @param {object} props
97+
* @param {string} props.name
98+
* @param {Record<string, any>} props.payload
99+
*/
100+
async simulateSubscriptionEvent(props) {
101+
await this.page.evaluate(simulateSubscriptionMessage, {
102+
messagingContext: this.messagingContext,
103+
name: props.name,
104+
payload: props.payload,
105+
injectName: this.build.name,
106+
});
107+
}
108+
95109
/**
96110
* @param {{names: string[]}} [opts]
97111
* @returns {Promise<any[]>}

0 commit comments

Comments
 (0)