feat: show the version of Sonda

- GET /version (signed-in users only) answers the version and, when the binary knows it, the commit it was built from and whether the files had uncommitted changes
- the commit is read from the build info Go records in a git checkout, so no build flag is needed
- the side menu shows the version under the privacy line, and Settings has an About section with it
- the menu's own requests (friends, version) are not counted as requests of an admin page in the Shell tests
This commit is contained in:
tgiachi 2026-11-15 17:30:14 +01:00
commit 00e5254fde
11 changed files with 337 additions and 10 deletions

View file

@ -21,6 +21,27 @@ export interface paths {
patch?: never;
trace?: never;
};
"/version": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* Which Sonda is running
* @description The version, and the commit it was built from when the binary knows it. Only a signed-in
* user can ask: the version of a server is not told to a stranger.
*/
get: operations["getVersion"];
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/openapi.yaml": {
parameters: {
query?: never;
@ -4390,6 +4411,37 @@ export interface operations {
};
};
};
getVersion: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description The version of this server. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 0.1.0 */
version: string;
/**
* @description The first characters of the commit. Left out when the build did not record it.
* @example 69c85a7
*/
revision?: string;
/** @description True when the files had changes that were not committed. Left out when false. */
modified?: boolean;
};
};
};
401: components["responses"]["Unauthorized"];
};
};
getOpenAPI: {
parameters: {
query?: never;

18
web/src/api/version.ts Normal file
View file

@ -0,0 +1,18 @@
import { useQuery } from '@tanstack/react-query'
import { api } from '@/api/client'
export interface Version {
version: string
revision?: string
modified?: boolean
}
/** Which Sonda the server is. It cannot change while the page is open, so it is read once. */
export function useVersion() {
return useQuery({
queryKey: ['/version'],
queryFn: () => api.get<Version>('/version'),
staleTime: Infinity,
retry: false,
})
}

View file

@ -0,0 +1,48 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen } from '@testing-library/react'
import { beforeEach, expect, test, vi } from 'vitest'
import { initI18n } from '@/i18n'
import { apiError, mockFetch } from '@/test/mockFetch'
import VersionLabel from './VersionLabel'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
})
function renderLabel() {
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<VersionLabel />
</QueryClientProvider>,
)
}
test('it says the version and the commit the server was built from', async () => {
mockFetch({ 'GET /api/v1/version': () => ({ version: '0.1.0', revision: '69c85a7' }) })
renderLabel()
expect(await screen.findByText('Version 0.1.0 · 69c85a7')).toBeInTheDocument()
})
test('a build that did not record its commit shows only the version', async () => {
mockFetch({ 'GET /api/v1/version': () => ({ version: '0.1.0' }) })
renderLabel()
expect(await screen.findByText('Version 0.1.0')).toBeInTheDocument()
})
test('a build with changes that were not committed says so', async () => {
mockFetch({ 'GET /api/v1/version': () => ({ version: '0.1.0', revision: '69c85a7', modified: true }) })
renderLabel()
expect(await screen.findByText('Version 0.1.0 · 69c85a7 (changed)')).toBeInTheDocument()
})
test('it shows nothing when the version cannot be read', async () => {
const { calls } = mockFetch({ 'GET /api/v1/version': () => apiError(500, 'internal') })
const { container } = render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<VersionLabel />
</QueryClientProvider>,
)
await vi.waitFor(() => expect(calls).toHaveLength(1))
expect(container).toBeEmptyDOMElement()
})

View file

@ -0,0 +1,14 @@
import { useTranslation } from 'react-i18next'
import { useVersion } from '@/api/version'
/**
* The version of Sonda, with the commit it was built from when the server knows it. It shows nothing until the server
* has answered, and nothing when it cannot: the version is information, not something the page waits for.
*/
export default function VersionLabel({ className }: { className?: string }) {
const { t } = useTranslation()
const { data } = useVersion()
if (!data) return null
const key = !data.revision ? 'version.plain' : data.modified ? 'version.changed' : 'version.commit'
return <p className={className}>{t(key, { version: data.version, revision: data.revision })}</p>
}

View file

@ -25,8 +25,8 @@ async function renderAt(path: string) {
await screen.findByRole('main')
}
/** The menu asks which friends there are; the tests below are about what an admin page asks. */
const notFriends = (c: { url: URL }) => c.url.pathname !== '/api/v1/friends'
/** What the menu asks for on its own (the friends, to know whether to show Friends, and the version): the tests below are about what an admin page asks. */
const notTheMenu = (c: { url: URL }) => !['/api/v1/friends', '/api/v1/version'].includes(c.url.pathname)
function mainNav() {
return screen.getByRole('navigation', { name: 'Main' })
@ -98,7 +98,7 @@ test('someone who is not an admin who opens the jobs address gets a notice and n
await renderAt('/admin/jobs')
expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument()
await new Promise((r) => setTimeout(r, 30))
expect(calls.filter(notFriends)).toHaveLength(0)
expect(calls.filter(notTheMenu)).toHaveLength(0)
})
test('someone who is not an admin who opens the providers address gets a notice and no request', async () => {
@ -107,7 +107,7 @@ test('someone who is not an admin who opens the providers address gets a notice
await renderAt('/admin/providers')
expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument()
await new Promise((r) => setTimeout(r, 30))
expect(calls.filter(notFriends)).toHaveLength(0)
expect(calls.filter(notTheMenu)).toHaveLength(0)
})
test('someone who is not an admin who opens the users address gets a notice and no request', async () => {
@ -116,7 +116,7 @@ test('someone who is not an admin who opens the users address gets a notice and
await renderAt('/admin/users')
expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument()
await new Promise((r) => setTimeout(r, 30))
expect(calls.filter(notFriends)).toHaveLength(0)
expect(calls.filter(notTheMenu)).toHaveLength(0)
})
test('someone who is not an admin who opens the admin address gets a notice and no request', async () => {

View file

@ -15,7 +15,7 @@ beforeEach(async () => {
})
function renderSidebar(friends: { id: number; name: string }[] = [], path = '/albums') {
mockFetch({ 'GET /api/v1/friends': () => ({ friends }) })
mockFetch({ 'GET /api/v1/friends': () => ({ friends }), 'GET /api/v1/version': () => ({ version: '0.1.0', revision: '69c85a7' }) })
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<MemoryRouter initialEntries={[path]}>
@ -79,6 +79,13 @@ test('the choice is kept on this device and applies the next time', async () =>
expect(localStorage.getItem('sonda.sidebar')).toBe('open')
})
test('the menu says which version of Sonda this is, and folding it leaves the version out', async () => {
const nav = renderSidebar()
expect(await within(nav).findByText('Version 0.1.0 · 69c85a7')).toBeInTheDocument()
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse menu' }))
expect(within(nav).queryByText(/Version 0.1.0/)).not.toBeInTheDocument()
})
test('every link has an icon, and the words stay: the icon is decoration, not the name', () => {
const nav = renderSidebar()
const links = within(nav).getAllByRole('link')

View file

@ -6,6 +6,7 @@ import { useTranslation } from 'react-i18next'
import { useFriends } from '@/api/friends'
import { useSession } from '@/auth/session'
import Logo from '@/components/Logo'
import VersionLabel from '@/components/VersionLabel'
import { cn } from '@/lib/utils'
interface Item {
@ -184,7 +185,12 @@ export default function Sidebar() {
))}
</ul>
</div>
{!collapsed && <p className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">{t('app.privacy')}</p>}
{!collapsed && (
<div className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">
<p>{t('app.privacy')}</p>
<VersionLabel className="mt-3" />
</div>
)}
</nav>
)
}

View file

@ -114,7 +114,8 @@
"passwordHint": "8 to 256 characters. Changing it signs you out everywhere.",
"passwordLength": "The new password needs 8 to 256 characters.",
"currentPasswordWrong": "The current password is wrong.",
"changePassword": "Change password"
"changePassword": "Change password",
"about": "About"
},
"admin": {
"title": "Admin",
@ -1081,5 +1082,10 @@
"later": "Later",
"install": "Install Sonda",
"installIntro": "Install Sonda on this device to open it like an app, from its own icon and in its own window."
},
"version": {
"plain": "Version {{version}}",
"commit": "Version {{version}} · {{revision}}",
"changed": "Version {{version}} · {{revision}} (changed)"
}
}

View file

@ -114,7 +114,8 @@
"passwordHint": "Da 8 a 256 caratteri. Cambiarla ti disconnette ovunque.",
"passwordLength": "La nuova password deve avere da 8 a 256 caratteri.",
"currentPasswordWrong": "La password attuale è sbagliata.",
"changePassword": "Cambia password"
"changePassword": "Cambia password",
"about": "Informazioni"
},
"admin": {
"title": "Admin",
@ -1081,5 +1082,10 @@
"later": "Più tardi",
"install": "Installa Sonda",
"installIntro": "Installa Sonda su questo dispositivo per aprirla come un'app, con la sua icona e nella sua finestra."
},
"version": {
"plain": "Versione {{version}}",
"commit": "Versione {{version}} · {{revision}}",
"changed": "Versione {{version}} · {{revision}} (modificata)"
}
}

View file

@ -26,7 +26,7 @@ function renderSettings() {
}
beforeEach(async () => {
mockFetch({ 'GET /api/v1/me/settings': () => ({ discover_from_listening: false }), 'GET /api/v1/me/mastodon': () => ({ connected: false }) })
mockFetch({ 'GET /api/v1/me/settings': () => ({ discover_from_listening: false }), 'GET /api/v1/me/mastodon': () => ({ connected: false }), 'GET /api/v1/version': () => ({ version: '0.1.0', revision: '69c85a7' }) })
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
initTheme()
@ -241,3 +241,8 @@ test('there is a place to connect a Mastodon account, off until it is connected'
const section = await screen.findByRole('region', { name: 'Mastodon' })
expect(await within(section).findByRole('button', { name: 'Connect' })).toBeInTheDocument()
})
test('the page says which version of Sonda this is', async () => {
renderSettings()
expect(await screen.findByText('Version 0.1.0 · 69c85a7')).toBeInTheDocument()
})

View file

@ -7,6 +7,7 @@ import ChoiceGroup from '@/components/ChoiceGroup'
import ListeningSwitch from '@/components/ListeningSwitch'
import MastodonBlock from '@/components/MastodonBlock'
import Section from '@/components/Section'
import VersionLabel from '@/components/VersionLabel'
import { useInstall } from '@/pwa/useInstall'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
@ -194,6 +195,10 @@ export default function SettingsPage() {
<Section title={t('settings.password')}>
<ChangePassword />
</Section>
<Section title={t('settings.about')}>
<VersionLabel />
</Section>
</div>
)
}