From 8e4eadfd01c2c34a3d550aaf38a3d0d3be908548 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Thu, 26 Nov 2026 14:57:22 +0100 Subject: [PATCH 1/8] Bug 1833365 - Use fzf for live runnable job search --- package.json | 2 +- pnpm-lock.yaml | 13 +- tests/ui/job-view/fuzzy_test.jsx | 127 +++++++++++++++++- ui/job-view/pushes/FuzzyJobFinder.jsx | 103 +++++++------- .../pushes/runnableJobSearch.worker.js | 31 +++++ ui/job-view/pushes/useRunnableJobSearch.js | 78 +++++++++++ 6 files changed, 287 insertions(+), 67 deletions(-) create mode 100644 ui/job-view/pushes/runnableJobSearch.worker.js create mode 100644 ui/job-view/pushes/useRunnableJobSearch.js diff --git a/package.json b/package.json index 3784d208765..5762b9e4283 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "buffer": "6.0.3", "crypto-browserify": "3.12.1", "dayjs": "1.11.23", - "fuse.js": "7.5.0", + "fzf": "0.5.2", "js-cookie": "3.0.8", "js-yaml": "5.5.0", "json-e": "4.8.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bb399f8096..cbce7a1685f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,9 +57,9 @@ importers: dayjs: specifier: 1.11.23 version: 1.11.23 - fuse.js: - specifier: 7.5.0 - version: 7.5.0 + fzf: + specifier: 0.5.2 + version: 0.5.2 js-cookie: specifier: 3.0.8 version: 3.0.8 @@ -2830,9 +2830,8 @@ packages: function-bind@1.1.2: resolution: {integrity: sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==} - fuse.js@7.5.0: - resolution: {integrity: sha512-sQtrEfA+ez/3G0cCZecF70oqpCRttCexYUG4mUrtWL49ULUzUyxokt5kyqwtKzj1270RaKih+hcP3qLcumccow==} - engines: {node: '>=10'} + fzf@0.5.2: + resolution: {integrity: sha512-Tt4kuxLXFKHy8KT40zwsUPUkg1CrsgY25FxA2U/j/0WgEDCk3ddc/zLTCCcbSHX9FcKtLuVaDGtGE/STWC+j3Q==} generator-function@2.0.1: resolution: {integrity: sha512-SFdFmIJi+ybC0vjlHN0ZGVGHc3lgE0DxPAT0djjVg+kjOnSqclqmj0KQ7ykTOLP6YxoqOvuAODGdcHJn+43q3g==} @@ -8085,7 +8084,7 @@ snapshots: function-bind@1.1.2: {} - fuse.js@7.5.0: {} + fzf@0.5.2: {} generator-function@2.0.1: {} diff --git a/tests/ui/job-view/fuzzy_test.jsx b/tests/ui/job-view/fuzzy_test.jsx index 23284ed4898..fe45aefbd28 100644 --- a/tests/ui/job-view/fuzzy_test.jsx +++ b/tests/ui/job-view/fuzzy_test.jsx @@ -1,13 +1,35 @@ import { render, fireEvent, waitFor } from '@testing-library/react'; +import { cloneElement } from 'react'; import fuzzyJobList from '../mock/job_list/fuzzy_jobs/fuzzyJobList.json'; import initialJobList from '../mock/job_list/fuzzy_jobs/initial_job_list.json'; import searchLinuxResults from '../mock/job_list/fuzzy_jobs/search_linux_results.json'; import searchDebugResults from '../mock/job_list/fuzzy_jobs/search_debug_results.json'; import FuzzyJobFinder from '../../../ui/job-view/pushes/FuzzyJobFinder'; +import { createSearchHandler } from '../../../ui/job-view/pushes/runnableJobSearch.worker'; + +class SearchWorker { + handleMessage = createSearchHandler((data) => this.onmessage({ data })); + + postMessage(data) { + setTimeout(() => this.handleMessage?.({ data }), 0); + } + + terminate() { + this.handleMessage = null; + } +} describe('FuzzyJobFinder', () => { + const originalWorker = global.Worker; + beforeEach(() => { + global.Worker = SearchWorker; + }); + afterEach(() => { + global.Worker = originalWorker; + }); + const isOpen = true; const decisionTaskId = 'YHKMjYZeSSmEZTrAPdRIag'; const id = 705037; @@ -62,8 +84,6 @@ describe('FuzzyJobFinder', () => { await fireEvent.change(inputElement, { target: { value: 'linux' } }); expect(inputElement.value).toBe('linux'); - await fireEvent.keyDown(inputElement, { key: 'Enter', code: 'Enter' }); - await waitFor(() => { expect(queryAllByTestId('fuzzyList')).toHaveLength(26); const fuzzySearchArray = queryAllByTestId('fuzzyList').map( @@ -72,6 +92,7 @@ describe('FuzzyJobFinder', () => { expect(fuzzySearchArray).toStrictEqual( expect.arrayContaining(searchLinuxResults), ); + expect(fuzzySearchArray[0]).toBe('build-linux/opt'); }); }); @@ -91,8 +112,6 @@ describe('FuzzyJobFinder', () => { await fireEvent.change(inputElement, { target: { value: 'debug$' } }); expect(inputElement.value).toBe('debug$'); - await fireEvent.keyDown(inputElement, { key: 'Enter', code: 'Enter' }); - await waitFor(() => { expect(queryAllByTestId('fuzzyList')).toHaveLength(16); const fuzzySearchArray = queryAllByTestId('fuzzyList').map( @@ -102,6 +121,24 @@ describe('FuzzyJobFinder', () => { expect.arrayContaining(searchDebugResults), ); }); + + fireEvent.change(inputElement, { + target: { value: "^build- 'linux | 'android debug$ !asan !pgo" }, + }); + + await waitFor(() => { + const names = queryAllByTestId('fuzzyList').map((job) => job.textContent); + expect(names).toEqual( + expect.arrayContaining([ + 'build-linux/debug', + 'build-android-aarch64/debug', + ]), + ); + expect( + names.every((name) => /^build-.*(?:linux|android).*debug$/.test(name)), + ).toBe(true); + expect(names.every((name) => !/asan|pgo/.test(name))).toBe(true); + }); }); test('Toggling "Use full job list" switches the source list and keeps the search applied', async () => { @@ -138,8 +175,6 @@ describe('FuzzyJobFinder', () => { // Apply a search term; only the linux job in the filtered list matches. const inputElement = getByTitle('Filter the list of runnable jobs'); await fireEvent.change(inputElement, { target: { value: 'linux' } }); - await fireEvent.keyDown(inputElement, { key: 'Enter', code: 'Enter' }); - await waitFor(() => { const names = queryAllByTestId('fuzzyList').map((job) => job.innerHTML); expect(names).toEqual(['test-linux64/opt']); @@ -164,6 +199,86 @@ describe('FuzzyJobFinder', () => { }); }); + test('Fast typing only makes the latest results available to add', async () => { + const { getByTitle, getByRole, queryAllByTestId } = render(testFuzzyJobFinder); + const input = getByTitle('Filter the list of runnable jobs'); + const addAll = getByRole('button', { name: 'Add all' }); + + fireEvent.change(input, { target: { value: 'linux' } }); + fireEvent.change(input, { target: { value: '^addon-tps-xpi$' } }); + expect(addAll).toHaveAttribute('aria-disabled', 'true'); + fireEvent.click(addAll); + expect( + getByRole('button', { name: 'Trigger (0) Selected Jobs' }), + ).toBeDisabled(); + + await waitFor(() => expect(addAll).not.toHaveAttribute('aria-disabled')); + expect(queryAllByTestId('fuzzyList').map((job) => job.textContent)).toEqual([ + 'addon-tps-xpi', + ]); + + fireEvent.click(addAll); + expect( + getByRole('button', { name: 'Trigger (1) Selected Jobs' }), + ).toBeEnabled(); + }); + + test('Terminates the search worker on list changes, close, and unmount', () => { + const terminate = jest.spyOn(SearchWorker.prototype, 'terminate'); + try { + const finder = cloneElement(testFuzzyJobFinder, { + jobList: [...fuzzyJobList], + }); + const { getByRole, getByTitle, rerender, unmount } = render(finder); + fireEvent.change(getByTitle('Filter the list of runnable jobs'), { + target: { value: 'linux' }, + }); + + fireEvent.click(getByRole('checkbox')); + expect(terminate).toHaveBeenCalledTimes(1); + + rerender(cloneElement(finder, { isOpen: false })); + expect(terminate).toHaveBeenCalledTimes(2); + + rerender(finder); + unmount(); + expect(terminate).toHaveBeenCalledTimes(3); + } finally { + terminate.mockRestore(); + } + }); + + test('Keeps the results list enabled and retains selections that still match', async () => { + const { getByTitle, getByRole } = render(testFuzzyJobFinder); + const input = getByTitle('Filter the list of runnable jobs'); + const list = document.querySelector('#addJobsGroup select'); + const addSelected = getByRole('button', { name: 'Add selected' }); + + const job = getByRole('option', { name: 'addon-tps-xpi' }); + job.selected = true; + fireEvent.change(list); + + fireEvent.change(input, { target: { value: '^addon-tps-xpi$' } }); + expect(list).toBeEnabled(); + expect(job.selected).toBe(true); + expect(addSelected).toHaveAttribute('aria-disabled', 'true'); + fireEvent.click(addSelected); + expect( + getByRole('button', { name: 'Trigger (0) Selected Jobs' }), + ).toBeDisabled(); + + await waitFor(() => expect(list.options).toHaveLength(1)); + expect(job.selected).toBe(true); + expect(addSelected).not.toHaveAttribute('aria-disabled'); + + fireEvent.change(input, { target: { value: 'linux' } }); + await waitFor(() => expect(list.options).toHaveLength(26)); + expect(addSelected).toBeDisabled(); + + fireEvent.change(input, { target: { value: '' } }); + expect(getByRole('option', { name: 'addon-tps-xpi' }).selected).toBe(false); + }); + test('Selecting a job marks it and moves it to the Selected Jobs list', async () => { const { getByText, queryAllByTestId } = await render(testFuzzyJobFinder); diff --git a/ui/job-view/pushes/FuzzyJobFinder.jsx b/ui/job-view/pushes/FuzzyJobFinder.jsx index 9eb97980305..dd4714fefa0 100644 --- a/ui/job-view/pushes/FuzzyJobFinder.jsx +++ b/ui/job-view/pushes/FuzzyJobFinder.jsx @@ -10,21 +10,13 @@ import { ModalFooter, InputGroup, } from 'react-bootstrap'; -import Fuse from 'fuse.js'; import PushModel from '../../models/push'; import { formatTaskclusterError } from '../../helpers/errorMessage'; import { sortAlphaNum } from '../../helpers/sort'; import { notify } from '../../shared/stores/notificationStore'; -const FUSE_OPTIONS = { - // http://fusejs.io/ describes the options available - useExtendedSearch: true, - keys: ['name', 'symbol'], - threshold: 0.4, // This seems like a good threshold to remove most false matches, lower is stricter - matchAllTokens: true, - tokenize: true, -}; +import useRunnableJobSearch from './useRunnableJobSearch'; // Cap how many