|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { ResultsCollector } from './page-objects/results-collector.js'; |
| 3 | +import { readOutgoingMessages } from '@duckduckgo/messaging/lib/test-utils.mjs'; |
| 4 | + |
| 5 | +const ENABLED_CONFIG = 'integration-test/test-pages/message-bridge/config/message-bridge-enabled.json'; |
| 6 | +const DISABLED_CONFIG = 'integration-test/test-pages/message-bridge/config/message-bridge-disabled.json'; |
| 7 | +const ENABLED_HTML = '/message-bridge/pages/enabled.html'; |
| 8 | +const DISABLED_HTML = '/message-bridge/pages/disabled.html'; |
| 9 | + |
| 10 | +test('message bridge when enabled (android)', async ({ page }, testInfo) => { |
| 11 | + const pageWorld = ResultsCollector.create(page, testInfo.project.use); |
| 12 | + |
| 13 | + // seed the request->re |
| 14 | + pageWorld.withMockResponse({ |
| 15 | + sampleData: /** @type {any} */ ({ |
| 16 | + ghi: 'jkl', |
| 17 | + }), |
| 18 | + }); |
| 19 | + |
| 20 | + pageWorld.withUserPreferences({ |
| 21 | + messageSecret: 'ABC', |
| 22 | + javascriptInterface: 'javascriptInterface', |
| 23 | + messageCallback: 'messageCallback', |
| 24 | + }); |
| 25 | + |
| 26 | + // now load the page |
| 27 | + await pageWorld.load(ENABLED_HTML, ENABLED_CONFIG); |
| 28 | + |
| 29 | + // simulate a push event |
| 30 | + await pageWorld.simulateSubscriptionMessage('exampleFeature', 'onUpdate', { abc: 'def' }); |
| 31 | + |
| 32 | + // get all results |
| 33 | + const results = await pageWorld.results(); |
| 34 | + expect(results['Creating the bridge']).toStrictEqual([ |
| 35 | + { name: 'bridge.notify', result: 'function', expected: 'function' }, |
| 36 | + { name: 'bridge.request', result: 'function', expected: 'function' }, |
| 37 | + { name: 'bridge.subscribe', result: 'function', expected: 'function' }, |
| 38 | + { name: 'data', result: [{ abc: 'def' }, { ghi: 'jkl' }], expected: [{ abc: 'def' }, { ghi: 'jkl' }] }, |
| 39 | + ]); |
| 40 | + |
| 41 | + // verify messaging calls |
| 42 | + const calls = await page.evaluate(readOutgoingMessages); |
| 43 | + expect(calls.length).toBe(2); |
| 44 | + const pixel = calls[0].payload; |
| 45 | + const request = calls[1].payload; |
| 46 | + |
| 47 | + expect(pixel).toStrictEqual({ |
| 48 | + context: 'contentScopeScripts', |
| 49 | + featureName: 'exampleFeature', |
| 50 | + method: 'pixel', |
| 51 | + params: {}, |
| 52 | + }); |
| 53 | + |
| 54 | + const { id, ...rest } = /** @type {import("@duckduckgo/messaging").RequestMessage} */ (request); |
| 55 | + |
| 56 | + expect(rest).toStrictEqual({ |
| 57 | + context: 'contentScopeScripts', |
| 58 | + featureName: 'exampleFeature', |
| 59 | + method: 'sampleData', |
| 60 | + params: {}, |
| 61 | + }); |
| 62 | + |
| 63 | + if (!('id' in request)) throw new Error('unreachable'); |
| 64 | + |
| 65 | + expect(typeof request.id).toBe('string'); |
| 66 | + expect(request.id.length).toBeGreaterThan(10); |
| 67 | +}); |
| 68 | + |
| 69 | +test('message bridge when disabled (android)', async ({ page }, testInfo) => { |
| 70 | + const pageWorld = ResultsCollector.create(page, testInfo.project.use); |
| 71 | + |
| 72 | + // now load the main page |
| 73 | + await pageWorld.load(DISABLED_HTML, DISABLED_CONFIG); |
| 74 | + |
| 75 | + // verify no outgoing calls were made |
| 76 | + const calls = await page.evaluate(readOutgoingMessages); |
| 77 | + expect(calls).toHaveLength(0); |
| 78 | + |
| 79 | + // get all results |
| 80 | + const results = await pageWorld.results(); |
| 81 | + expect(results['Creating the bridge, but it is unavailable']).toStrictEqual([ |
| 82 | + { name: 'error', result: 'Did not install Message Bridge', expected: 'Did not install Message Bridge' }, |
| 83 | + ]); |
| 84 | +}); |
0 commit comments