import assert from 'node:assert/strict'; import process from 'node:process'; const { chromium } = await import( process.env.PLAYWRIGHT_MODULE ?? 'playwright' ); const browser = await chromium.launch({ channel: 'chrome', headless: true }); const base = 'http://127.0.0.1:4186'; const context = await browser.newContext({ viewport: { width: 1440, height: 1000 }, }); const page = await context.newPage(); page.setDefaultTimeout(10000); const errors = []; page.on('pageerror', (error) => errors.push(error.message)); const dialog = () => page.getByRole('dialog'); const button = (name) => dialog().getByRole('button', { name, exact: true }); const state = (value) => dialog() .locator(`[data-form-state="${value}"]`) .waitFor({ state: 'visible' }); const setResult = (value) => context.addCookies([{ name: 'git_test_result', value, url: base }]); const add = () => page.getByRole('button', { name: 'Add connection', exact: true }).click(); const edit = () => page .getByRole('button', { name: 'Edit alex at https://git.example.test', exact: true, }) .click(); const visit = async (query = '?reset') => { await page.goto(`${base}/settings/git-connections${query}`); await page .getByRole('heading', { name: 'Git connections', exact: true }) .waitFor(); }; const noOverflow = async () => assert.equal( await page.evaluate( () => document.documentElement.scrollWidth <= window.innerWidth, ), true, ); const fill = async (token = 'test-secret') => { await dialog() .getByLabel('Gitea base URL', { exact: true }) .fill('https://git.example.test'); await page.locator('#git-token').fill(token); }; const closed = () => dialog().waitFor({ state: 'hidden' }); try { for (const width of [1440, 390, 320]) { await page.setViewportSize({ width, height: 1000 }); for (const theme of ['light', 'dark']) { await page.emulateMedia({ colorScheme: theme }); await visit(); await noOverflow(); assert.equal( await page .locator('[data-slot="card"]') .first() .evaluate( (element) => element.getBoundingClientRect().right <= window.innerWidth, ), true, `Connection list clips at ${width}px in ${theme}`, ); if (width === 390) { await page.screenshot({ path: `/private/tmp/KAN-38-${theme}-list.png`, fullPage: true, }); } await add(); await dialog().waitFor(); await noOverflow(); assert.equal( await page.locator('#git-token').getAttribute('type'), 'password', ); assert.equal( await page.locator('#git-token').getAttribute('autocomplete'), 'new-password', ); await page.keyboard.press('Tab'); assert.equal( await dialog().evaluate((element) => element.contains(document.activeElement), ), true, ); await dialog().evaluate((element) => Promise.all( element .getAnimations() .map((animation) => animation.finished), ), ); if (width === 390) { await page.screenshot({ path: `/private/tmp/KAN-38-${theme}-dialog.png`, fullPage: true, }); } await page.keyboard.press('Escape'); await closed(); assert.equal( await page .getByRole('button', { name: 'Add connection', exact: true, }) .evaluate((element) => document.activeElement === element), true, ); await edit(); assert.equal( await page.locator('#git-base-url').inputValue(), 'https://git.example.test', ); assert.equal(await page.locator('#git-token').inputValue(), ''); await noOverflow(); await button('Cancel').click(); await closed(); } } await visit('?empty'); await page.getByText('No Git connections yet', { exact: true }).waitFor(); await add(); await fill(); await setResult('validation'); await button('Add connection').click(); await state('validation'); assert.equal( await page.locator('#git-token').getAttribute('aria-invalid'), 'true', ); assert.equal(await page.locator('#git-token').inputValue(), 'test-secret'); await page .getByText( 'Gitea could not authenticate with this base URL and token.', { exact: true }, ) .waitFor(); assert.doesNotMatch( await page.evaluate(() => JSON.stringify(history.state)), /test-secret/, ); await page.route('**/settings/git-connections', (route) => route.abort('failed'), ); await button('Add connection').click(); await state('network'); await page.unroute('**/settings/git-connections'); await setResult('server'); await button('Try again').click(); await state('server'); assert.equal(await page.locator('#git-token').inputValue(), 'test-secret'); await setResult('success'); const submitted = page.waitForRequest( (request) => request.method() === 'POST' && request.url().endsWith('/settings/git-connections'), ); await button('Try again').click(); assert.deepEqual((await submitted).postDataJSON(), { provider: 'gitea', base_url: 'https://git.example.test', api_token: 'test-secret', }); await closed(); await page.getByText('Git connection added.', { exact: true }).waitFor(); await add(); assert.equal(await page.locator('#git-token').inputValue(), ''); await button('Cancel').click(); await closed(); await visit(); await edit(); await page.locator('#git-token').fill('updated-secret'); const patch = page.waitForRequest( (request) => request.method() === 'PATCH', ); await button('Save changes').click(); assert.equal((await patch).postDataJSON().api_token, 'updated-secret'); await closed(); await page.getByText('Git connection updated.', { exact: true }).waitFor(); await edit(); assert.equal(await page.locator('#git-token').inputValue(), ''); await page.locator('#git-token').fill('discard-me'); page.once('dialog', (confirmation) => confirmation.dismiss()); await button('Cancel').click(); assert.equal(await dialog().isVisible(), true); page.once('dialog', (confirmation) => confirmation.accept()); await button('Cancel').click(); await closed(); await edit(); assert.equal(await page.locator('#git-token').inputValue(), ''); await button('Cancel').click(); await closed(); await page .getByRole('button', { name: 'Remove alex at https://git.example.test', exact: true, }) .click(); await setResult('forbidden'); await button('Remove connection').click(); await state('server'); await page.getByText('Connection access denied', { exact: true }).waitFor(); assert.equal(await button('Try again').count(), 0); await button('Cancel').click(); await closed(); await page .getByRole('button', { name: 'Remove alex at https://git.example.test', exact: true, }) .click(); await setResult('success'); const deletion = page.waitForRequest( (request) => request.method() === 'DELETE', ); await button('Remove connection').click(); assert.equal( new URL((await deletion).url()).pathname, '/settings/git-connections/1', ); await closed(); await page.getByText('No Git connections yet', { exact: true }).waitFor(); assert.deepEqual(errors, []); console.log( 'Git connections browser checks passed: create, edit, remove, token privacy, provider errors, retries, permissions, discard, keyboard and both themes at 320/390/1440px.', ); } finally { await browser.close(); }