|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { ResultsCollector } from './page-objects/results-collector.js'; |
| 3 | + |
| 4 | +const HTML = '/duck-ai-data-clearing/index.html'; |
| 5 | +const CONFIG = './integration-test/test-pages/duck-ai-data-clearing/config/enabled.json'; |
| 6 | + |
| 7 | +test('duck-ai-data-clearing feature clears localStorage and IndexedDB', async ({ page }, testInfo) => { |
| 8 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 9 | + collector.withUserPreferences({ |
| 10 | + messageSecret: 'ABC', |
| 11 | + javascriptInterface: 'javascriptInterface', |
| 12 | + messageCallback: 'messageCallback', |
| 13 | + }); |
| 14 | + await collector.load(HTML, CONFIG); |
| 15 | + |
| 16 | + const duckAiDataClearing = new DuckAiDataClearingSpec(page); |
| 17 | + |
| 18 | + // Setup test data |
| 19 | + await duckAiDataClearing.setupTestData(); |
| 20 | + await duckAiDataClearing.waitForDataSetup(); |
| 21 | + |
| 22 | + // Trigger data clearing via messaging |
| 23 | + await collector.simulateSubscriptionMessage('duckAiDataClearing', 'duckAiClearData', {}); |
| 24 | + |
| 25 | + // Wait for completion message |
| 26 | + const messages = await collector.waitForMessage('duckAiClearDataCompleted', 1); |
| 27 | + expect(messages).toHaveLength(1); |
| 28 | + expect(messages[0].payload.method).toBe('duckAiClearDataCompleted'); |
| 29 | + |
| 30 | + // Verify data is actually cleared |
| 31 | + await duckAiDataClearing.verifyDataCleared(); |
| 32 | + await duckAiDataClearing.waitForVerification('Verification complete: All data cleared'); |
| 33 | +}); |
| 34 | + |
| 35 | +test('duck-ai-data-clearing feature handles IndexedDB errors gracefully', async ({ page }, testInfo) => { |
| 36 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 37 | + collector.withUserPreferences({ |
| 38 | + messageSecret: 'ABC', |
| 39 | + javascriptInterface: 'javascriptInterface', |
| 40 | + messageCallback: 'messageCallback', |
| 41 | + }); |
| 42 | + await collector.load(HTML, CONFIG); |
| 43 | + |
| 44 | + const duckAiDataClearing = new DuckAiDataClearingSpec(page); |
| 45 | + |
| 46 | + // Setup localStorage data only (no IndexedDB) |
| 47 | + await duckAiDataClearing.setupLocalStorageOnly(); |
| 48 | + |
| 49 | + // Mock IndexedDB to fail |
| 50 | + await page.evaluate(() => { |
| 51 | + const originalOpen = window.indexedDB.open; |
| 52 | + window.indexedDB.open = () => { |
| 53 | + const request = originalOpen.call(window.indexedDB, 'nonexistent'); |
| 54 | + // Simulate an error |
| 55 | + setTimeout(() => { |
| 56 | + if (request.onerror) { |
| 57 | + request.onerror(new Error('Simulated IndexedDB error')); |
| 58 | + } |
| 59 | + }, 10); |
| 60 | + return request; |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + // Trigger data clearing |
| 65 | + await collector.simulateSubscriptionMessage('duckAiDataClearing', 'duckAiClearData', {}); |
| 66 | + |
| 67 | + // Should still get completion message (localStorage should clear successfully) |
| 68 | + const messages = await collector.waitForMessage('duckAiClearDataFailed', 1); |
| 69 | + expect(messages).toHaveLength(1); |
| 70 | + expect(messages[0].payload.method).toBe('duckAiClearDataFailed'); |
| 71 | +}); |
| 72 | + |
| 73 | +test('duck-ai-data-clearing feature handles localStorage errors gracefully', async ({ page }, testInfo) => { |
| 74 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 75 | + collector.withUserPreferences({ |
| 76 | + messageSecret: 'ABC', |
| 77 | + javascriptInterface: 'javascriptInterface', |
| 78 | + messageCallback: 'messageCallback', |
| 79 | + }); |
| 80 | + await collector.load(HTML, CONFIG); |
| 81 | + |
| 82 | + // Mock localStorage to throw an error |
| 83 | + await page.evaluate(() => { |
| 84 | + const originalRemoveItem = Storage.prototype.removeItem; |
| 85 | + Storage.prototype.removeItem = () => { |
| 86 | + throw new Error('Simulated localStorage error'); |
| 87 | + }; |
| 88 | + }); |
| 89 | + |
| 90 | + // Trigger data clearing |
| 91 | + await collector.simulateSubscriptionMessage('duckAiDataClearing', 'duckAiClearData', {}); |
| 92 | + |
| 93 | + // Should get failure message |
| 94 | + const messages = await collector.waitForMessage('duckAiClearDataFailed', 1); |
| 95 | + expect(messages).toHaveLength(1); |
| 96 | + expect(messages[0].payload.method).toBe('duckAiClearDataFailed'); |
| 97 | +}); |
| 98 | + |
| 99 | +class DuckAiDataClearingSpec { |
| 100 | + /** |
| 101 | + * @param {import("@playwright/test").Page} page |
| 102 | + */ |
| 103 | + constructor(page) { |
| 104 | + this.page = page; |
| 105 | + } |
| 106 | + |
| 107 | + async setupTestData() { |
| 108 | + await this.page.click('#setup-data'); |
| 109 | + } |
| 110 | + |
| 111 | + async setupLocalStorageOnly() { |
| 112 | + await this.page.evaluate(() => { |
| 113 | + localStorage.setItem('savedAIChats', JSON.stringify([ |
| 114 | + { id: 1, message: 'test chat 1' } |
| 115 | + ])); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + async waitForDataSetup() { |
| 120 | + await this.page.waitForFunction(() => { |
| 121 | + const status = document.getElementById('data-status'); |
| 122 | + return status && status.textContent === 'Test data setup complete'; |
| 123 | + }, { timeout: 5000 }); |
| 124 | + } |
| 125 | + |
| 126 | + async verifyDataCleared() { |
| 127 | + await this.page.click('#verify-data'); |
| 128 | + } |
| 129 | + |
| 130 | + async waitForVerification(expectedText) { |
| 131 | + await this.page.waitForFunction((expected) => { |
| 132 | + const status = document.getElementById('verify-status'); |
| 133 | + return status && status.textContent === expected; |
| 134 | + }, expectedText, { timeout: 5000 }); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +export { DuckAiDataClearingSpec }; |
0 commit comments