From 9d40c9e5bddf4b260583636bccb4daa500618c55 Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Tue, 22 Oct 2024 16:02:29 -0400 Subject: [PATCH] fix(website): desktop download autoselect --- .changeset/purple-terms-relax.md | 5 + .../components/DownloadDesktopSelect.tsx | 2 +- apps/website/content/downloads.spec.ts | 113 ++++++++++++++++++ apps/website/content/downloads.ts | 19 +-- 4 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 .changeset/purple-terms-relax.md create mode 100644 apps/website/content/downloads.spec.ts diff --git a/.changeset/purple-terms-relax.md b/.changeset/purple-terms-relax.md new file mode 100644 index 000000000..effdd83e6 --- /dev/null +++ b/.changeset/purple-terms-relax.md @@ -0,0 +1,5 @@ +--- +'website': patch +--- + +Fixed an issue where the desktop download widget would not autoselect the correct OS download. diff --git a/apps/website/components/DownloadDesktopSelect.tsx b/apps/website/components/DownloadDesktopSelect.tsx index a343e1a73..519fec6f1 100644 --- a/apps/website/components/DownloadDesktopSelect.tsx +++ b/apps/website/components/DownloadDesktopSelect.tsx @@ -21,7 +21,7 @@ type Props = { } export function DownloadDesktopSelect({ daemon, release, testnetOnly }: Props) { - const downloadLinks = getDownloadLinksDesktop(daemon, release) + const downloadLinks = getDownloadLinksDesktop(release) const { accepted } = useTermsOfService() const [download, setDownload] = useState(downloadLinks[0]) diff --git a/apps/website/content/downloads.spec.ts b/apps/website/content/downloads.spec.ts new file mode 100644 index 000000000..07601e9ce --- /dev/null +++ b/apps/website/content/downloads.spec.ts @@ -0,0 +1,113 @@ +import { getTags, findUserDefaultDownload } from './downloads' + +const downloadsList = [ + // desktop release naming scheme + { name: 'walletd-arm64.dmg' }, + { name: 'walletd-x64.dmg' }, + { name: 'walletd-0.12.0.Setup.exe' }, + { name: 'walletd_0.12.0_arm64.deb' }, + { name: 'walletd_0.12.0_amd64.deb' }, + { name: 'walletd-0.12.0-1.arm64.rpm' }, + { name: 'walletd-0.12.0-1.x86_64.rpm' }, + // daemon release naming scheme + { name: 'walletd_darwin_amd64.zip' }, + { name: 'walletd_darwin_arm64.zip' }, + { name: 'walletd_linux_amd64.zip' }, + { name: 'walletd_linux_arm64.zip' }, + { name: 'walletd_windows_amd64.zip' }, +].map(({ name }) => ({ title: '', link: '', tags: getTags({ name }) })) + +test('should return the correct tags', () => { + expect(getTags({ name: 'walletd-arm64.dmg' })).toEqual(['macos', 'arm64']) + expect(getTags({ name: 'walletd-x64.dmg' })).toEqual(['macos', 'amd64']) + expect(getTags({ name: 'walletd-0.12.0.Setup.exe' })).toEqual([ + 'windows', + 'amd64', + ]) + expect(getTags({ name: 'walletd_0.12.0_arm64.deb' })).toEqual([ + 'linux', + 'arm64', + ]) + expect(getTags({ name: 'walletd_0.12.0_amd64.deb' })).toEqual([ + 'linux', + 'amd64', + ]) + expect(getTags({ name: 'walletd-0.12.0-1.arm64.rpm' })).toEqual([ + 'linux', + 'arm64', + ]) + expect(getTags({ name: 'walletd-0.12.0-1.x86_64.rpm' })).toEqual([ + 'linux', + 'amd64', + ]) + expect(getTags({ name: 'walletd_darwin_amd64.zip' })).toEqual([ + 'macos', + 'amd64', + ]) + expect(getTags({ name: 'walletd_darwin_arm64.zip' })).toEqual([ + 'macos', + 'arm64', + ]) + expect(getTags({ name: 'walletd_linux_amd64.zip' })).toEqual([ + 'linux', + 'amd64', + ]) + expect(getTags({ name: 'walletd_linux_arm64.zip' })).toEqual([ + 'linux', + 'arm64', + ]) + expect(getTags({ name: 'walletd_windows_amd64.zip' })).toEqual([ + 'windows', + 'amd64', + ]) +}) + +describe('findUserDefaultDownload', () => { + test('any windows user-agent should return amd64', () => { + const userAgent = + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0' + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent) + const download = findUserDefaultDownload(downloadsList) + expect(download).toEqual({ + title: '', + link: '', + tags: ['windows', 'amd64'], + }) + }) + + test('any macos user-agent should return arm64', () => { + const userAgent = + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent) + const download = findUserDefaultDownload(downloadsList) + expect(download).toEqual({ + title: '', + link: '', + tags: ['macos', 'arm64'], + }) + }) + + test('linux arm user-agent should return arm64', () => { + const userAgent = + 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent) + const download = findUserDefaultDownload(downloadsList) + expect(download).toEqual({ + title: '', + link: '', + tags: ['linux', 'arm64'], + }) + }) + + test('linux amd user-agent should return amd64', () => { + const userAgent = + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' + jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent) + const download = findUserDefaultDownload(downloadsList) + expect(download).toEqual({ + title: '', + link: '', + tags: ['linux', 'amd64'], + }) + }) +}) diff --git a/apps/website/content/downloads.ts b/apps/website/content/downloads.ts index 6e508c2df..7c3d76dbf 100644 --- a/apps/website/content/downloads.ts +++ b/apps/website/content/downloads.ts @@ -1,4 +1,4 @@ -import { GitHubRelease, GitHubReleaseAsset } from '@siafoundation/data-sources' +import { GitHubRelease } from '@siafoundation/data-sources' type DownloadOption = { title: string; link: string; tags: DownloadTag[] } @@ -25,15 +25,13 @@ export function getDownloadLinksDaemon(daemon: string, release: GitHubRelease) { } export function getDownloadLinksDesktop( - daemon: string, release: GitHubRelease ): DownloadOption[] { if (!release) { return [] } - // Desktop releases include assets for multiple daemons, so we need to filter. - const assets = release.assets.filter((asset) => asset.name.includes(daemon)) + const { assets } = release const final = [] @@ -117,24 +115,29 @@ export function getDownloadLinksDesktop( type DownloadTag = 'zen' | 'windows' | 'macos' | 'linux' | 'amd64' | 'arm64' -function getTags(asset: GitHubReleaseAsset): DownloadTag[] { +export function getTags(asset: { name: string }): DownloadTag[] { const tags: DownloadTag[] = [] if (asset.name.includes('testnet') || asset.name.includes('zen')) { tags.push('zen') } - if (asset.name.includes('windows')) { + if (asset.name.includes('windows') || asset.name.includes('.exe')) { tags.push('windows') } - if (asset.name.includes('darwin')) { + if (asset.name.includes('darwin') || asset.name.includes('.dmg')) { tags.push('macos') } - if (asset.name.includes('linux')) { + if ( + asset.name.includes('linux') || + asset.name.includes('.deb') || + asset.name.includes('.rpm') + ) { tags.push('linux') } if ( asset.name.includes('amd64') || asset.name.includes('x86_64') || asset.name.includes('x64') + // For now assume amd64 for Windows exe. ) { tags.push('amd64') }