From ac4f39322c0eac974ed91e1dfd40a101b399f4e9 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 23 Nov 2026 13:23:10 +0000 Subject: [PATCH] feat(ui): page, search and filter packages on the server The package list loaded every package, then grouped and paged in the browser. The dashboard did the same to show ten recent rows. - PackageList asks the server for one page of 25 groups. The registry filter and a debounced search box (300 ms) change the request. A newer request replaces the answer of an older one. - Dashboard loads a page of 10 groups sorted by cached_at, newest first, and runs its stats, recent list and chart requests in parallel. - Stats already used /api/stats only and does not change. - The store keeps the loading flag on until the last overlapping request settles. Deleting a package no longer reloads the full list, and the list reloads its own page instead. - VulnerablePackages keeps the unpaginated call because it filters on scan results in the browser. --- frontend/src/components/Dashboard.spec.ts | 55 +++----- frontend/src/components/Dashboard.vue | 18 +-- frontend/src/components/PackageList.spec.ts | 131 +++++++++++++++---- frontend/src/components/PackageList.vue | 135 ++++++++++---------- frontend/src/components/views.smoke.spec.ts | 2 +- frontend/src/stores/packages.spec.ts | 120 +++++++++++++++++ frontend/src/stores/packages.ts | 120 +++++++++++++++-- 7 files changed, 423 insertions(+), 158 deletions(-) create mode 100644 frontend/src/stores/packages.spec.ts diff --git a/frontend/src/components/Dashboard.spec.ts b/frontend/src/components/Dashboard.spec.ts index 9fefe8c..f5aa824 100644 --- a/frontend/src/components/Dashboard.spec.ts +++ b/frontend/src/components/Dashboard.spec.ts @@ -12,7 +12,7 @@ describe('Dashboard.vue', () => { // Mock the fetch functions to prevent actual API calls const store = usePackageStore() vi.spyOn(store, 'fetchStats').mockResolvedValue() - vi.spyOn(store, 'fetchPackages').mockResolvedValue() + vi.spyOn(store, 'fetchRecentPackages').mockResolvedValue() }) it('renders dashboard component', () => { @@ -127,7 +127,7 @@ describe('Dashboard.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [ + store.recentPackages = [ { id: '1', registry: 'npm', @@ -144,22 +144,12 @@ describe('Dashboard.vue', () => { expect(wrapper.text()).toContain('Recent packages') }) - it('shows recent packages sorted by cached_at', async () => { + it('lists recent packages in the order the store holds them, newest first', async () => { const wrapper = mount(Dashboard) const store = usePackageStore() store.loading = false - store.packages = [ - { - id: '1', - registry: 'npm', - name: 'old-package', - version: '1.0.0', - size: 1024, - cached_at: '2025-01-01T00:00:00Z', - last_accessed: '2025-01-01T00:00:00Z', - download_count: 10, - }, + store.recentPackages = [ { id: '2', registry: 'npm', @@ -170,46 +160,33 @@ describe('Dashboard.vue', () => { last_accessed: '2025-01-02T00:00:00Z', download_count: 5, }, + { + id: '1', + registry: 'npm', + name: 'old-package', + version: '1.0.0', + size: 1024, + cached_at: '2025-01-01T00:00:00Z', + last_accessed: '2025-01-01T00:00:00Z', + download_count: 10, + }, ] await wrapper.vm.$nextTick() const recentPackagesSection = wrapper.text().split('Recent packages')[1] - // new-package should appear before old-package since it's more recent + // The store sorts by cached_at; the view keeps that order expect(recentPackagesSection.indexOf('new-package')).toBeLessThan( recentPackagesSection.indexOf('old-package') ) }) - it('limits recent packages to 10 items', async () => { - const wrapper = mount(Dashboard) - const store = usePackageStore() - - store.loading = false - store.packages = Array.from({ length: 15 }, (_, i) => ({ - id: `${i}`, - registry: 'npm', - name: `package${i}`, - version: '1.0.0', - size: 1024, - cached_at: `2025-01-${String(i + 1).padStart(2, '0')}T00:00:00Z`, - last_accessed: `2025-01-${String(i + 1).padStart(2, '0')}T00:00:00Z`, - download_count: i, - })) - await wrapper.vm.$nextTick() - - const recentSection = wrapper.text().split('Recent packages')[1] - // Count how many "package" strings appear (each package has the word "package" in its name) - const packageCount = (recentSection.match(/package\d+/g) || []).length - expect(packageCount).toBeLessThanOrEqual(10) - }) - it('handles empty packages array', async () => { const wrapper = mount(Dashboard) const store = usePackageStore() store.loading = false - store.packages = [] + store.recentPackages = [] await wrapper.vm.$nextTick() expect(wrapper.text()).toContain('0') diff --git a/frontend/src/components/Dashboard.vue b/frontend/src/components/Dashboard.vue index 642a38a..d1ed796 100644 --- a/frontend/src/components/Dashboard.vue +++ b/frontend/src/components/Dashboard.vue @@ -75,7 +75,7 @@ [] = [ function refresh() { void store.fetchStats() - void store.fetchPackages() + void store.fetchRecentPackages() } function getChartLabel(index: number): string { @@ -291,14 +291,6 @@ function getChartLabel(index: number): string { return labels[selectedPeriod.value]?.(index) || `${index}` } -// API returns clean, deduplicated data - just sort and limit -const recentPackages = computed(() => { - return packages.value - .slice() - .sort((a, b) => new Date(b.cached_at).getTime() - new Date(a.cached_at).getTime()) - .slice(0, 10) -}) - // Watch for period changes and fetch new data watch(selectedPeriod, () => { fetchTimeSeriesData() @@ -309,9 +301,7 @@ onMounted(async () => { // initial polling resolves. Singleton survives navigation, so we // intentionally do not disconnect on unmount. rt.connect() - await store.fetchStats() - await store.fetchPackages() - await fetchTimeSeriesData() + await Promise.all([store.fetchStats(), store.fetchRecentPackages(), fetchTimeSeriesData()]) }) diff --git a/frontend/src/components/PackageList.spec.ts b/frontend/src/components/PackageList.spec.ts index ba12842..e0d48d6 100644 --- a/frontend/src/components/PackageList.spec.ts +++ b/frontend/src/components/PackageList.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach, vi } from 'vitest' +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { mount } from '@vue/test-utils' import { createPinia, setActivePinia } from 'pinia' import { createRouter, createMemoryHistory, type Router } from 'vue-router' @@ -6,6 +6,14 @@ import { defineComponent, h } from 'vue' import PackageList from './PackageList.vue' import { usePackageStore } from '../stores/packages' +type Row = ReturnType['packagePage']['packages'][number] + +// A page as the server sends it: whole groups, flattened to rows. +function setRows(store: ReturnType, packages: Row[]) { + const groups = new Set(packages.map((p) => `${p.registry}:${p.name}`)) + store.packagePage = { packages, totalGroups: groups.size, page: 1, pageSize: 25 } +} + const Stub = defineComponent({ render: () => h('div') }) function createTestRouter(): Router { @@ -41,7 +49,7 @@ describe('PackageList.vue', () => { setActivePinia(createPinia()) // Prevent real network calls from store actions on mount const store = usePackageStore() - vi.spyOn(store, 'fetchPackages').mockResolvedValue() + vi.spyOn(store, 'fetchPackagePage').mockResolvedValue() }) it('renders package list component', async () => { @@ -74,7 +82,7 @@ describe('PackageList.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [] + setRows(store, []) await wrapper.vm.$nextTick() expect(wrapper.text()).toContain('No packages cached yet') @@ -85,7 +93,7 @@ describe('PackageList.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [ + setRows(store, [ { id: '1', registry: 'npm', @@ -96,17 +104,17 @@ describe('PackageList.vue', () => { last_accessed: '2025-01-01T00:00:00Z', download_count: 10, }, - ] + ]) await wrapper.vm.$nextTick() expect(wrapper.text()).toContain('test-package') expect(wrapper.text()).toContain('1 version') }) - it('calls fetchPackages on mount', async () => { + it('asks the server for the first page on mount', async () => { const store = usePackageStore() // beforeEach already spied with mockResolvedValue; reuse that spy - const fetchSpy = store.fetchPackages as ReturnType + const fetchSpy = store.fetchPackagePage as ReturnType fetchSpy.mockClear() await mountWithRouter() @@ -119,7 +127,7 @@ describe('PackageList.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [ + setRows(store, [ { id: '1', registry: 'npm', @@ -140,7 +148,7 @@ describe('PackageList.vue', () => { last_accessed: '2025-01-02T00:00:00Z', download_count: 20, }, - ] + ]) await wrapper.vm.$nextTick() expect(wrapper.text()).toContain('test-package') @@ -152,7 +160,7 @@ describe('PackageList.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [ + setRows(store, [ { id: '1', registry: 'npm', @@ -163,7 +171,7 @@ describe('PackageList.vue', () => { last_accessed: '2025-01-01T00:00:00Z', download_count: 10, }, - ] + ]) await wrapper.vm.$nextTick() expect(wrapper.text()).toContain('1 MB') @@ -174,7 +182,17 @@ describe('PackageList.vue', () => { const store = usePackageStore() store.loading = false - store.packages = [ + setRows(store, [ + { + id: '3', + registry: 'go', + name: 'go-module', + version: '1.0.0', + size: 1024, + cached_at: '2025-01-01T00:00:00Z', + last_accessed: '2025-01-01T00:00:00Z', + download_count: 3, + }, { id: '1', registry: 'npm', @@ -195,21 +213,11 @@ describe('PackageList.vue', () => { last_accessed: '2025-01-01T00:00:00Z', download_count: 5, }, - { - id: '3', - registry: 'go', - name: 'go-module', - version: '1.0.0', - size: 1024, - cached_at: '2025-01-01T00:00:00Z', - last_accessed: '2025-01-01T00:00:00Z', - download_count: 3, - }, - ] + ]) await wrapper.vm.$nextTick() // Verify that all registry badges are displayed - // Packages are grouped and sorted alphabetically, so order is: go-module, npm-package, python-package + // The server sends groups in name order: go-module, npm-package, python-package expect(wrapper.text()).toContain('npm') expect(wrapper.text()).toContain('pypi') expect(wrapper.text()).toContain('go') @@ -223,7 +231,7 @@ describe('PackageList.vue', () => { const wrapper = await mountWithRouter() const store = usePackageStore() - store.packages = [ + setRows(store, [ { id: '1', registry: 'pypi', @@ -235,7 +243,7 @@ describe('PackageList.vue', () => { last_accessed: '2026-11-23T00:00:00Z', download_count: 0, }, - ] + ]) await wrapper.vm.$nextTick() await wrapper.find('button[aria-expanded]').trigger('click') await wrapper.vm.$nextTick() @@ -243,4 +251,75 @@ describe('PackageList.vue', () => { expect(wrapper.text()).toContain('v1.17.0') expect(wrapper.text()).not.toContain('py2.py3-none-any.whl') }) + + describe('server-side paging, search and registry filter', () => { + beforeEach(() => { + vi.useFakeTimers() + }) + afterEach(() => { + vi.useRealTimers() + }) + + it('debounces the search box into one request with q and page 1', async () => { + const wrapper = await mountWithRouter() + const store = usePackageStore() + const fetchSpy = store.fetchPackagePage as ReturnType + fetchSpy.mockClear() + + const input = wrapper.find('input[type="search"], input') + await input.setValue('l') + await input.setValue('le') + await input.setValue('left') + expect(fetchSpy).not.toHaveBeenCalled() + + await vi.advanceTimersByTimeAsync(300) + expect(fetchSpy).toHaveBeenCalledTimes(1) + expect(fetchSpy).toHaveBeenCalledWith({ page: 1, pageSize: 25, registry: 'all', q: 'left' }) + }) + + it('sends the registry of the route with every request', async () => { + const router = createTestRouter() + router.push('/packages/pypi') + await router.isReady() + const store = usePackageStore() + const fetchSpy = store.fetchPackagePage as ReturnType + fetchSpy.mockClear() + mount(PackageList, { props: { registry: 'pypi' }, global: { plugins: [router] } }) + expect(fetchSpy).toHaveBeenCalledWith({ page: 1, pageSize: 25, registry: 'pypi', q: '' }) + }) + + it('pages through the server, not the loaded rows', async () => { + const wrapper = await mountWithRouter() + const store = usePackageStore() + store.loading = false + store.packagePage = { + packages: [ + { id: '1', registry: 'npm', name: 'a', version: '1', size: 1, cached_at: '2025-01-01T00:00:00Z', last_accessed: '2025-01-01T00:00:00Z', download_count: 0 }, + ], + totalGroups: 60, + page: 1, + pageSize: 25, + } + await wrapper.vm.$nextTick() + expect(wrapper.text()).toContain('Page 1 of 3 (60 total packages)') + + const fetchSpy = store.fetchPackagePage as ReturnType + fetchSpy.mockClear() + window.scrollTo = vi.fn() as unknown as typeof window.scrollTo + const next = wrapper.findAll('button').find((b) => /next/i.test(b.attributes('aria-label') ?? b.text())) + await next!.trigger('click') + expect(fetchSpy).toHaveBeenCalledWith({ page: 2, pageSize: 25, registry: 'all', q: '' }) + }) + + it('says no packages match when a filter empties the page', async () => { + const wrapper = await mountWithRouter() + const store = usePackageStore() + store.loading = false + setRows(store, []) + await wrapper.find('input').setValue('zzz') + await vi.advanceTimersByTimeAsync(300) + expect(wrapper.text()).toContain('No packages match') + expect(wrapper.text()).not.toContain('No packages cached yet') + }) + }) }) diff --git a/frontend/src/components/PackageList.vue b/frontend/src/components/PackageList.vue index bc105fc..705be54 100644 --- a/frontend/src/components/PackageList.vue +++ b/frontend/src/components/PackageList.vue @@ -2,7 +2,7 @@
@@ -23,26 +23,26 @@ variant="card" title="Could not load packages" :message="error" - @retry="store.fetchPackages()" + @retry="load()" /> @@ -110,12 +110,12 @@

- Page {{ currentPage }} of {{ totalPages }} ({{ allGroupedPackages.length }} total packages) + Page {{ currentPage }} of {{ totalPages }} ({{ totalGroups }} total packages)

@@ -136,7 +136,7 @@