diff --git a/frontend/src/components/PackageDetails.vue b/frontend/src/components/PackageDetails.vue index 27d927a..6e75c7e 100644 --- a/frontend/src/components/PackageDetails.vue +++ b/frontend/src/components/PackageDetails.vue @@ -83,7 +83,7 @@ - + diff --git a/frontend/src/components/parts/DownloadsPanel.spec.ts b/frontend/src/components/parts/DownloadsPanel.spec.ts new file mode 100644 index 0000000..6147fc3 --- /dev/null +++ b/frontend/src/components/parts/DownloadsPanel.spec.ts @@ -0,0 +1,58 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import axios from 'axios' +import { flushPromises } from '@vue/test-utils' +import DownloadsPanel from './DownloadsPanel.vue' +import { PACKAGE_ANALYTICS } from '@/test/fixtures' +import { mountStrict } from '@/test/mountStrict' + +vi.mock('axios') + +let restore: (() => void) | undefined + +async function open(props: { registry: string; name: string; downloadCount: number }) { + const mounted = await mountStrict(DownloadsPanel, { props }) + restore = mounted.restore + await flushPromises() + return mounted.wrapper +} + +beforeEach(() => { + vi.mocked(axios.get).mockReset() +}) +afterEach(() => { + restore?.() + restore = undefined +}) + +describe('DownloadsPanel', () => { + it('shows the stored count even when analytics have no record since the restart', async () => { + vi.mocked(axios.get).mockResolvedValue({ data: { package: null } }) + const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 12 }) + + expect(wrapper.text()).toContain('12') + expect(wrapper.text()).toContain('No downloads since the last restart') + expect(wrapper.text()).not.toContain('Could not load') + }) + + it('adds the in-memory totals and labels them as since the restart', async () => { + vi.mocked(axios.get).mockResolvedValue({ data: PACKAGE_ANALYTICS }) + const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 12 }) + + expect(wrapper.text()).toContain('kept across restarts') + expect(wrapper.text()).toContain('2 versions since the last restart') + expect(wrapper.text()).toContain('80 KB') + }) + + it('asks for a Go module by its path, with no cache-file suffix', async () => { + vi.mocked(axios.get).mockResolvedValue({ data: { package: null } }) + await open({ registry: 'go', name: 'github.com/google/uuid/@v/v1.6.0.zip', downloadCount: 2 }) + + expect(vi.mocked(axios.get)).toHaveBeenCalledWith('/api/analytics/package/go/github.com/google/uuid') + }) + + it('reports a failed request', async () => { + vi.mocked(axios.get).mockRejectedValue(new Error('boom')) + const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 1 }) + expect(wrapper.text()).toContain('Could not load download history') + }) +}) diff --git a/frontend/src/components/parts/DownloadsPanel.vue b/frontend/src/components/parts/DownloadsPanel.vue index b3941df..8ac608f 100644 --- a/frontend/src/components/parts/DownloadsPanel.vue +++ b/frontend/src/components/parts/DownloadsPanel.vue @@ -1,27 +1,29 @@ @@ -50,29 +52,26 @@ interface PackageAnalytics { LastDownload: string } -const props = defineProps<{ registry: string; name: string; version: string; downloadCount: number }>() - -const isGo = computed(() => props.registry === 'go') +const props = defineProps<{ registry: string; name: string; downloadCount: number }>() const stats = ref(null) const loading = ref(false) const error = ref(null) -// Go analytics are keyed by cache file, one per version. -const analyticsName = computed(() => (isGo.value ? `${modulePath(props.name)}/@v/${props.version}.zip` : props.name)) +// Analytics are keyed by package name; a Go module is named by its path. +const analyticsName = computed(() => modulePath(props.name)) async function load() { loading.value = true error.value = null try { - const response = await axios.get<{ package: PackageAnalytics }>( + const response = await axios.get<{ package: PackageAnalytics | null }>( `/api/analytics/package/${props.registry}/${analyticsName.value}`, ) stats.value = response.data.package } catch (err) { stats.value = null - const status = (err as { response?: { status?: number } }).response?.status - if (status !== 404) error.value = err instanceof Error ? err.message : 'Failed to load download history' + error.value = err instanceof Error ? err.message : 'Failed to load download history' } finally { loading.value = false }