|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('Session Transfer Functionality', () => { |
| 4 | + test.beforeEach(async ({ page, context }) => { |
| 5 | + await context.addCookies([ |
| 6 | + { |
| 7 | + name: 'token', |
| 8 | + value: 'test-token-123', |
| 9 | + domain: 'localhost', |
| 10 | + path: '/', |
| 11 | + }, |
| 12 | + { |
| 13 | + name: 'prefsHttp', |
| 14 | + value: 'test-prefs', |
| 15 | + domain: 'localhost', |
| 16 | + path: '/', |
| 17 | + }, |
| 18 | + ]); |
| 19 | + |
| 20 | + await page.goto('localhost:9001/'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should open settings dialog and transfer session', async ({ |
| 24 | + page, |
| 25 | + }) => { |
| 26 | + await page.route('**/tokenTransfer', async (route) => { |
| 27 | + await route.fulfill({ |
| 28 | + status: 200, |
| 29 | + contentType: 'application/json', |
| 30 | + body: JSON.stringify({ id: 'transfer-id-12345678-1234-5678' }), |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + await page.locator('.settings-button').click(); |
| 35 | + const dialog = page.locator('#settings-dialog'); |
| 36 | + await expect(dialog).toBeVisible(); |
| 37 | + |
| 38 | + const transferButton = page.locator( |
| 39 | + '[data-l10n-id="index.transferSessionNow"]' |
| 40 | + ); |
| 41 | + await expect(transferButton).toBeVisible(); |
| 42 | + |
| 43 | + await transferButton.click(); |
| 44 | + |
| 45 | + await expect(transferButton).toBeDisabled(); |
| 46 | + await expect(transferButton.locator('svg')).toBeVisible(); |
| 47 | + |
| 48 | + const copyLinkSection = page.locator('#copy-link-section'); |
| 49 | + await expect(copyLinkSection).toBeVisible(); |
| 50 | + |
| 51 | + const copyButton = copyLinkSection.locator('.btn-secondary'); |
| 52 | + await expect(copyButton).toBeVisible(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('should copy transfer ID to clipboard', async ({ page }) => { |
| 56 | + const transferId = 'abc123-transfer-id-xyz789'; |
| 57 | + |
| 58 | + await page.route('**/tokenTransfer', async (route) => { |
| 59 | + await route.fulfill({ |
| 60 | + status: 200, |
| 61 | + contentType: 'application/json', |
| 62 | + body: JSON.stringify({ id: transferId }), |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + await page.locator('.settings-button').click(); |
| 67 | + await page |
| 68 | + .locator('[data-l10n-id="index.transferSessionNow"]') |
| 69 | + .click(); |
| 70 | + |
| 71 | + const copyButton = page.locator('#copy-link-section .btn-secondary'); |
| 72 | + await expect(copyButton).toBeVisible(); |
| 73 | + |
| 74 | + await page.evaluate(() => { |
| 75 | + // @ts-ignore |
| 76 | + window.clipboardData = ''; |
| 77 | + navigator.clipboard.writeText = async (text: string) => { |
| 78 | + // @ts-ignore |
| 79 | + window.clipboardData = text; |
| 80 | + return Promise.resolve(); |
| 81 | + }; |
| 82 | + }); |
| 83 | + |
| 84 | + await copyButton.click(); |
| 85 | + |
| 86 | + await expect(copyButton).toBeDisabled(); |
| 87 | + await expect(copyButton.locator('svg')).toBeVisible(); |
| 88 | + |
| 89 | + const clipboardText = await page.evaluate( |
| 90 | + // @ts-ignore |
| 91 | + () => window.clipboardData |
| 92 | + ); |
| 93 | + expect(clipboardText).toBe(transferId); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should receive session with valid code', async ({ page }) => { |
| 97 | + const validCode = '12345678-1234-5678-1234-567812345678'; |
| 98 | + |
| 99 | + await page.route(`**/tokenTransfer/${validCode}`, async (route) => { |
| 100 | + await route.fulfill({ |
| 101 | + status: 200, |
| 102 | + contentType: 'application/json', |
| 103 | + body: JSON.stringify({ success: true }), |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + await page.locator('.settings-button').click(); |
| 108 | + |
| 109 | + await page |
| 110 | + .locator('#button-bar button[data-l10n-id="index.receiveSessionTitle"]') |
| 111 | + .click(); |
| 112 | + |
| 113 | + const receiveSection = page.locator('#transfer-to-system-section'); |
| 114 | + await expect(receiveSection).toBeVisible(); |
| 115 | + |
| 116 | + const codeInput = page.locator('#codeInput'); |
| 117 | + await expect(codeInput).toBeVisible(); |
| 118 | + |
| 119 | + const transferButton = page.locator('#transferSessionButton'); |
| 120 | + await expect(transferButton).toBeDisabled(); |
| 121 | + |
| 122 | + await codeInput.fill(validCode); |
| 123 | + |
| 124 | + await expect(transferButton).not.toBeDisabled(); |
| 125 | + |
| 126 | + await Promise.all([ |
| 127 | + page.waitForNavigation(), |
| 128 | + transferButton.click(), |
| 129 | + ]); |
| 130 | + }); |
| 131 | + |
| 132 | + test('should keep transfer button disabled for invalid code length', async ({ |
| 133 | + page, |
| 134 | + }) => { |
| 135 | + await page.locator('.settings-button').click(); |
| 136 | + |
| 137 | + await page |
| 138 | + .locator('#button-bar button[data-l10n-id="index.receiveSessionTitle"]') |
| 139 | + .click(); |
| 140 | + |
| 141 | + const codeInput = page.locator('#codeInput'); |
| 142 | + const transferButton = page.locator('#transferSessionButton'); |
| 143 | + |
| 144 | + await codeInput.fill('short-code'); |
| 145 | + await expect(transferButton).toBeDisabled(); |
| 146 | + |
| 147 | + await codeInput.fill( |
| 148 | + '12345678-1234-5678-1234-567812345678-extra' |
| 149 | + ); |
| 150 | + await expect(transferButton).toBeDisabled(); |
| 151 | + |
| 152 | + await codeInput.fill(''); |
| 153 | + await expect(transferButton).toBeDisabled(); |
| 154 | + }); |
| 155 | + |
| 156 | + test('should switch between tabs in settings dialog', async ({ |
| 157 | + page, |
| 158 | + }) => { |
| 159 | + await page.locator('.settings-button').click(); |
| 160 | + |
| 161 | + const transferTab = page.locator( |
| 162 | + '#button-bar button[data-l10n-id="index.transferSessionTitle"]' |
| 163 | + ); |
| 164 | + const receiveTab = page.locator( |
| 165 | + '#button-bar button[data-l10n-id="index.receiveSessionTitle"]' |
| 166 | + ); |
| 167 | + |
| 168 | + await expect(transferTab).toHaveClass(/active-btn/); |
| 169 | + |
| 170 | + await receiveTab.click(); |
| 171 | + await expect(receiveTab).toHaveClass(/active-btn/); |
| 172 | + await expect(transferTab).not.toHaveClass(/active-btn/); |
| 173 | + |
| 174 | + await expect( |
| 175 | + page.locator('#transfer-to-system-section') |
| 176 | + ).toBeVisible(); |
| 177 | + |
| 178 | + await transferTab.click(); |
| 179 | + await expect(transferTab).toHaveClass(/active-btn/); |
| 180 | + }); |
| 181 | + |
| 182 | + test('should close dialog when clicking outside', async ({ page }) => { |
| 183 | + await page.locator('.settings-button').click(); |
| 184 | + const dialog = page.locator('#settings-dialog'); |
| 185 | + |
| 186 | + await expect(dialog).toBeVisible(); |
| 187 | + |
| 188 | + await dialog.evaluate((el) => (el as HTMLElement).click()); |
| 189 | + |
| 190 | + await expect(dialog).not.toBeVisible(); |
| 191 | + }); |
| 192 | +}); |
0 commit comments