From 9f93e8d808e5a1aa9935f4a7dce6d11101f3e15b Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 12 Nov 2026 07:12:55 +0000 Subject: [PATCH] test: add e2e tests for comments, file tree, actions and branch protection (#40313) Add e2e tests for flows that had no browser coverage: editing and quote-replying an issue with an uploaded image, file tree and go-to-file navigation, the Actions run view and workflow dispatch, and branch protection rules with their priority. The tests exposed bugs that are fixed here: - Quote reply into an empty comment box left the Comment button disabled - Removing an uploaded attachment did not update the comment buttons - Going back from a file opened via the file tree to the initially loaded page kept showing the other file Assisted-by: Claude Code:claude-opus-5-5 --- tests/e2e/actions.test.ts | 42 ++++++++++++++++- tests/e2e/issue-comment.test.ts | 44 +++++++++++++----- tests/e2e/repo-settings.test.ts | 45 +++++++++++++++++++ tests/e2e/repo.test.ts | 30 ++++++++++++- tests/e2e/utils.ts | 7 +++ web_src/js/components/ViewFileTree.vue | 10 ++++- web_src/js/components/ViewFileTreeStore.ts | 4 +- .../js/features/comp/ComboMarkdownEditor.ts | 2 +- web_src/js/features/comp/EditorUpload.ts | 5 ++- web_src/js/utils/url.ts | 4 ++ 10 files changed, 174 insertions(+), 19 deletions(-) create mode 100644 tests/e2e/repo-settings.test.ts diff --git a/tests/e2e/actions.test.ts b/tests/e2e/actions.test.ts index 02e2be369e0..c64d1fa260a 100644 --- a/tests/e2e/actions.test.ts +++ b/tests/e2e/actions.test.ts @@ -1,6 +1,6 @@ import {env} from 'node:process'; import {expect, test} from '@playwright/test'; -import {apiCreateFiles, apiCreateRepo, apiHeaders, randomString} from './utils.ts'; +import {apiCreateFiles, apiCreateRepo, apiHeaders, login, randomString} from './utils.ts'; test('job queue refreshes with filter open', async ({page, request}) => { const owner = env.GITEA_TEST_E2E_USER; @@ -28,7 +28,45 @@ test('job queue refreshes with filter open', async ({page, request}) => { await expect(waitingFilter).toBeVisible(); }); -test('job log line permalink expands its step', async ({page}) => { +test('workflow graph opens a job and steps expand by click or log permalink', async ({page}) => { + await page.goto('/devtest/repo-action-view/runs/10'); + await page.locator('.workflow-graph .job-name').getByText('job-100', {exact: true}).click(); + await page.waitForURL('**/devtest/repo-action-view/runs/10/jobs/10000'); + await page.getByText('step 1 (mock fast)', {exact: true}).click(); + await expect(page.getByText('message for: step=1, cursor=23', {exact: true})).toBeVisible(); + await page.goto('/devtest/repo-action-view/runs/10/jobs/1#jobstep-1-23'); await expect(page.getByRole('link', {name: '23', exact: true})).toBeInViewport(); }); + +test('run workflow dispatches a queued run with an input', async ({page, request}) => { + const owner = env.GITEA_TEST_E2E_USER; + const repo = `e2e-dispatch-${randomString(8)}`; + await Promise.all([ + (async () => { + await apiCreateRepo(request, {name: repo, autoInit: false}); + await apiCreateFiles(request, owner, repo, [{ + path: '.gitea/workflows/dispatch.yml', + content: `run-name: Dispatch \${{ inputs.message }} +on: + workflow_dispatch: + inputs: + message: + description: Run message +jobs: + queued: + runs-on: no-runner + steps: + - run: exit 0 +`, + }]); + })(), + login(page), + ]); + await page.goto(`/${owner}/${repo}/actions?workflow=dispatch.yml`); + await page.getByRole('button', {name: 'Run Workflow', exact: true}).click(); + const dispatchDialog = page.getByRole('dialog'); + await dispatchDialog.getByLabel('Run message').fill('from UI'); + await dispatchDialog.getByRole('button', {name: 'Run Workflow', exact: true}).click(); + await expect(page.getByRole('link', {name: 'Dispatch from UI', exact: true})).toBeVisible(); +}); diff --git a/tests/e2e/issue-comment.test.ts b/tests/e2e/issue-comment.test.ts index be430ecbfb7..c56ef85578f 100644 --- a/tests/e2e/issue-comment.test.ts +++ b/tests/e2e/issue-comment.test.ts @@ -2,25 +2,47 @@ import {env} from 'node:process'; import {test, expect} from '@playwright/test'; import {login, apiCreateRepo, apiCreateIssue, apiEditIssue, randomString} from './utils.ts'; -test('comment on and close an issue', async ({page, request}) => { +test('edit an issue, quote-reply it with an uploaded image and close it', async ({page, request}) => { const repoName = `e2e-issue-comment-${randomString(8)}`; const owner = env.GITEA_TEST_E2E_USER; - await apiCreateRepo(request, {name: repoName, autoInit: false}); await Promise.all([ - apiCreateIssue(request, {owner, repo: repoName, title: 'Comment test'}), + (async () => { + await apiCreateRepo(request, {name: repoName, autoInit: false}); + await apiCreateIssue(request, {owner, repo: repoName, title: 'Comment test', body: 'Original body'}); + })(), login(page), ]); await page.goto(`/${owner}/${repoName}/issues/1`); + const commentButton = page.getByRole('button', {name: 'Comment', exact: true}); + await expect(commentButton).toBeDisabled(); - const body = `e2e-comment-${randomString(8)}`; - await page.getByPlaceholder('Leave a comment').fill(body); - // exact match: the status button reads "Close with Comment" while the box has content, which substring-matches "Comment" - await page.getByRole('button', {name: 'Comment', exact: true}).click(); - await expect(page.locator('.comment-body').filter({hasText: body})).toBeVisible(); + const contextMenu = page.getByRole('heading', {level: 3}).getByRole('menu'); + await contextMenu.click(); + await page.getByRole('menuitem', {name: 'Edit', exact: true}).click(); + const issueBody = page.getByRole('article'); + await expect(issueBody.getByRole('textbox')).toHaveValue('Original body'); + await issueBody.getByRole('textbox').fill('Updated **body**'); + await issueBody.getByRole('button', {name: 'Save', exact: true}).click(); + await expect(issueBody.getByText('Updated body', {exact: true})).toBeVisible(); - // wait for the form to re-initialize (the empty box disables the comment button); a close click - // before that does a native submit which lands on a raw JSON page instead of reloading the issue - await expect(page.getByRole('button', {name: 'Comment', exact: true})).toBeDisabled(); + await contextMenu.click(); + await page.getByRole('menuitem', {name: 'Quote reply', exact: true}).click(); + await expect(commentButton).toBeEnabled(); + const editor = page.getByRole('textbox', {name: 'Leave a comment'}); + await editor.evaluate((textarea) => { + const dataTransfer = new DataTransfer(); + dataTransfer.items.add(new File([ + Uint8Array.from(atob('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='), (character) => character.charCodeAt(0)), + ], 'pixel.png', {type: 'image/png'})); + textarea.dispatchEvent(new DragEvent('drop', {bubbles: true, cancelable: true, dataTransfer})); + }); + await expect(editor).toHaveValue(/^> Updated \*\*body\*\*\n\n!\[pixel\.png\]\(attachments\/[\da-f-]+\)$/); + await commentButton.click(); + await page.waitForURL(/#issuecomment-\d+$/); + await expect(page.getByRole('article').getByRole('blockquote')).toHaveText('Updated body'); + await expect(page.getByRole('article').getByRole('img', {name: 'pixel.png', exact: true})).toHaveJSProperty('naturalWidth', 1); + + await expect(commentButton).toBeDisabled(); await page.getByRole('button', {name: 'Close Issue'}).click(); await expect(page.getByRole('button', {name: 'Reopen Issue'})).toBeVisible(); }); diff --git a/tests/e2e/repo-settings.test.ts b/tests/e2e/repo-settings.test.ts new file mode 100644 index 00000000000..c16831b72f4 --- /dev/null +++ b/tests/e2e/repo-settings.test.ts @@ -0,0 +1,45 @@ +import {env} from 'node:process'; +import {expect, test} from '@playwright/test'; +import {apiCreateBranchProtection, apiCreateRepo, login, randomString} from './utils.ts'; + +test('branch protection rule keeps its push allowlist and dragged priority', async ({page, request}) => { + const owner = env.GITEA_TEST_E2E_USER; + const repo = `e2e-protection-${randomString(8)}`; + await Promise.all([ + (async () => { + await apiCreateRepo(request, {name: repo, autoInit: false}); + await apiCreateBranchProtection(request, owner, repo, 'release/stable'); + })(), + login(page), + ]); + await page.goto(`/${owner}/${repo}/settings/branches/edit`); + const ruleNameInput = page.getByRole('textbox', {name: /^Protected Branch Name Pattern/}); + const allowlistRadio = page.getByRole('radio', {name: 'Allowlist Restricted Push', exact: true}); + await ruleNameInput.fill('release/**'); + await allowlistRadio.check(); + await page.locator('#whitelist_box').getByRole('combobox').click(); + await page.getByRole('option', {name: owner}).click(); + await page.getByRole('button', {name: 'Save Rule', exact: true}).click(); + await page.waitForURL(`/${owner}/${repo}/settings/branches`); + + const rules = page.locator('#protected-branches-list > .item'); + await expect(rules).toHaveText([/release\/stable/, /release\/\*\*/]); + const rule = rules.filter({hasText: 'release/**'}); + const handle = rule.locator('.drag-handle'); + const saved = page.waitForResponse('**/settings/branches/priority'); + await handle.hover(); + await page.mouse.down(); + await handle.hover({position: {x: 2, y: 2}}); + await expect(rule).toHaveClass(/card-ghost/); + await rules.first().hover({position: {x: 10, y: 10}}); + await expect(rules.first()).toContainText('release/**'); + await page.mouse.up(); + await saved; + await page.reload(); + await expect(rules).toHaveText([/release\/\*\*/, /release\/stable/]); + + await rule.getByRole('link', {name: 'Edit', exact: true}).click(); + await expect(ruleNameInput).toHaveValue('release/**'); + await expect(allowlistRadio).toBeChecked(); + await expect(page.locator('#whitelist_box .dropdown > .label')).toHaveText(owner); +}); diff --git a/tests/e2e/repo.test.ts b/tests/e2e/repo.test.ts index 9e26bf7d0e1..5cd2bada557 100644 --- a/tests/e2e/repo.test.ts +++ b/tests/e2e/repo.test.ts @@ -1,6 +1,6 @@ import {env} from 'node:process'; import {test, expect} from '@playwright/test'; -import {login, apiCreateBranch, apiCreateRepo, randomString} from './utils.ts'; +import {login, apiCreateBranch, apiCreateFiles, apiCreateRepo, randomString} from './utils.ts'; test('create a repository', async ({page}) => { const repoName = `e2e-repo-${randomString(8)}`; @@ -28,3 +28,31 @@ test('delete a branch via its modal', async ({page, request}) => { await modal.getByRole('button', {name: 'Yes'}).click(); await expect(page.locator('.flash-message')).toContainText('Branch "feature" has been deleted.'); }); + +test('file tree, history and go to file navigate between nested files', async ({page, request}) => { + const repoName = `e2e-file-nav-${randomString(8)}`; + const owner = env.GITEA_TEST_E2E_USER; + await Promise.all([ + (async () => { + await apiCreateRepo(request, {name: repoName, autoInit: false}); + await apiCreateFiles(request, owner, repoName, [ + {path: 'start.txt', content: 'Starting file content\n'}, + {path: 'docs/guide.txt', content: 'Nested guide content\n'}, + ]); + })(), + login(page), + ]); + await page.goto(`/${owner}/${repoName}/src/branch/main/start.txt`); + const tree = page.locator('#view-file-tree'); + await tree.getByRole('link', {name: 'docs', exact: true}).locator('.item-toggle svg').click(); + await tree.getByRole('link', {name: 'guide.txt', exact: true}).click(); + await expect(page.getByText('Nested guide content', {exact: true})).toBeVisible(); + await page.goBack(); + await expect(page.getByText('Starting file content', {exact: true})).toBeVisible(); + + const search = page.getByRole('combobox', {name: 'Go to file'}); + await search.fill('docs/gu'); + await expect(page.getByRole('option')).toHaveText(['docs/guide.txt']); + await search.press('Enter'); + await expect(page.getByText('Nested guide content', {exact: true})).toBeVisible(); +}); diff --git a/tests/e2e/utils.ts b/tests/e2e/utils.ts index 9cde276f009..93fb30404ef 100644 --- a/tests/e2e/utils.ts +++ b/tests/e2e/utils.ts @@ -105,6 +105,13 @@ export async function apiCreateBranch(requestContext: APIRequestContext, owner: }), 'apiCreateBranch'); } +export async function apiCreateBranchProtection(requestContext: APIRequestContext, owner: string, repo: string, ruleName: string) { + await apiRetry(() => requestContext.post(`${baseUrl()}/api/v1/repos/${owner}/${repo}/branch_protections`, { + headers: apiHeaders(), + data: {rule_name: ruleName}, + }), 'apiCreateBranchProtection'); +} + /** Create a PR via API. Returns the PR index for subsequent operations. */ export async function apiCreatePR(requestContext: APIRequestContext, owner: string, repo: string, head: string, base: string, title: string, {headers}: {headers?: Record} = {}): Promise { const response = await apiRetry(() => requestContext.post(`${baseUrl()}/api/v1/repos/${owner}/${repo}/pulls`, { diff --git a/web_src/js/components/ViewFileTree.vue b/web_src/js/components/ViewFileTree.vue index c3344c91e2d..1dae079e75f 100644 --- a/web_src/js/components/ViewFileTree.vue +++ b/web_src/js/components/ViewFileTree.vue @@ -2,6 +2,7 @@ import ViewFileTreeItem from './ViewFileTreeItem.vue'; import {onMounted, useTemplateRef, type ShallowRef} from 'vue'; import {createViewFileTreeStore} from './ViewFileTreeStore.ts'; +import {pathWithQuery} from '../utils/url.ts'; const elRoot = useTemplateRef('elRoot') as Readonly>; @@ -16,8 +17,13 @@ onMounted(async () => { store.rootFiles = await store.loadChildren('', props.treePath); elRoot.value.closest('.is-loading')?.classList?.remove('is-loading'); window.addEventListener('popstate', (e) => { - store.selectedItem = e.state?.treePath || ''; - if (e.state?.url) store.loadViewContent(e.state.url); + if (pathWithQuery(window.location) === store.viewContentUrl) return; + if (e.state?.url) { + store.selectedItem = e.state.treePath; + store.loadViewContent(e.state.url); + } else { + window.location.reload(); // not pushed by the tree, e.g. the initial page or an editor page + } }); }); diff --git a/web_src/js/components/ViewFileTreeStore.ts b/web_src/js/components/ViewFileTreeStore.ts index 6e120d25094..1cc9dfbed78 100644 --- a/web_src/js/components/ViewFileTreeStore.ts +++ b/web_src/js/components/ViewFileTreeStore.ts @@ -1,6 +1,6 @@ import {reactive} from 'vue'; import {GET} from '../modules/fetch.ts'; -import {pathEscapeSegments} from '../utils/url.ts'; +import {pathEscapeSegments, pathWithQuery} from '../utils/url.ts'; import {createElementFromHTML} from '../utils/dom.ts'; import {html} from '../utils/html.ts'; @@ -18,6 +18,7 @@ export function createViewFileTreeStore(props: {repoLink: string, treePath: stri const store = reactive({ rootFiles: [] as Array, selectedItem: props.treePath, + viewContentUrl: pathWithQuery(window.location), async loadChildren(treePath: string, subPath: string = '') { // there is no git ref if no commits were made yet (an empty repo) @@ -39,6 +40,7 @@ export function createViewFileTreeStore(props: {repoLink: string, treePath: stri async loadViewContent(url: string) { const u = new URL(url, window.origin); + store.viewContentUrl = pathWithQuery(u); u.searchParams.set('only_content', 'true'); const response = await GET(u.href); const elViewContent = document.querySelector('.repo-view-content')!; diff --git a/web_src/js/features/comp/ComboMarkdownEditor.ts b/web_src/js/features/comp/ComboMarkdownEditor.ts index 83fc7c47df3..328a5f9ed87 100644 --- a/web_src/js/features/comp/ComboMarkdownEditor.ts +++ b/web_src/js/features/comp/ComboMarkdownEditor.ts @@ -256,7 +256,6 @@ export class ComboMarkdownEditor { return; } this.value(newContent); - triggerEditorContentChanged(this.container); }); } }; @@ -411,6 +410,7 @@ export class ComboMarkdownEditor { this.easyMDE.value(v); } else { this.textarea.value = v; + triggerEditorContentChanged(this.container); } this.textareaAutosize?.resizeToFit(); } diff --git a/web_src/js/features/comp/EditorUpload.ts b/web_src/js/features/comp/EditorUpload.ts index f5c8d091e71..ad2fe0c362e 100644 --- a/web_src/js/features/comp/EditorUpload.ts +++ b/web_src/js/features/comp/EditorUpload.ts @@ -166,6 +166,9 @@ export function initTextareaEvents(textarea: HTMLTextAreaElement, dropzoneEl: HT }); dropzoneEl?.dropzone.on(DropzoneCustomEventRemovedFile, ({fileUuid}: {fileUuid: string}) => { const newText = removeAttachmentLinksFromMarkdown(textarea.value, fileUuid); - if (textarea.value !== newText) textarea.value = newText; + if (textarea.value !== newText) { + textarea.value = newText; + triggerEditorContentChanged(textarea); + } }); } diff --git a/web_src/js/utils/url.ts b/web_src/js/utils/url.ts index cc33cf85ca5..887562ecaf0 100644 --- a/web_src/js/utils/url.ts +++ b/web_src/js/utils/url.ts @@ -44,3 +44,7 @@ export function pathEscapeSegments(s: string): string { // The same as backend's PathEscapeSegments return s.split('/').map(pathEscape).join('/'); } + +export function pathWithQuery(url: URL | Location) { + return url.pathname + url.search; +}