Compare commits
1
Commits
dev/novalink
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b018fd2e8 |
No files matched your search
@@ -124,6 +124,62 @@ describe('administrator update entry', () => {
|
||||
}
|
||||
)
|
||||
|
||||
test('shows a keyboard-accessible repository link only at the brand position for a regular user', async () => {
|
||||
const user = userEvent.setup()
|
||||
useAuthStore
|
||||
.getState()
|
||||
.auth.setUser({ id: 2, username: 'user', role: ROLE.USER })
|
||||
render(
|
||||
<>
|
||||
<SystemUpdateAction />
|
||||
<SystemUpdateAction presentation='version' />
|
||||
</>,
|
||||
{ wrapper: Wrapper }
|
||||
)
|
||||
const link = screen.getByRole('link', { name: 'Go to GitHub' })
|
||||
expect(link).toHaveAttribute(
|
||||
'href',
|
||||
'https://github.com/zsz2018/NovaLink-new-api'
|
||||
)
|
||||
expect(link).toHaveAttribute('target', '_blank')
|
||||
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
|
||||
await user.tab()
|
||||
expect(link).toHaveFocus()
|
||||
expect(screen.queryByRole('button')).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('removes the repository link on logout and swaps it for updates when an administrator signs in', async () => {
|
||||
useAuthStore.getState().auth.reset()
|
||||
render(<SystemUpdateAction presentation='version' />, { wrapper: Wrapper })
|
||||
expect(screen.queryByRole('link')).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button')).not.toBeInTheDocument()
|
||||
act(() =>
|
||||
useAuthStore
|
||||
.getState()
|
||||
.auth.setUser({ id: 2, username: 'user', role: ROLE.USER })
|
||||
)
|
||||
expect(
|
||||
screen.getByRole('link', { name: 'Go to GitHub' })
|
||||
).toBeInTheDocument()
|
||||
act(() => useAuthStore.getState().auth.reset())
|
||||
expect(screen.queryByRole('link')).not.toBeInTheDocument()
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
act(() =>
|
||||
useAuthStore
|
||||
.getState()
|
||||
.auth.setUser({ id: 1, username: 'root', role: ROLE.SUPER_ADMIN })
|
||||
)
|
||||
expect(
|
||||
await screen.findByRole('button', {
|
||||
name: /System updates, current version:/,
|
||||
})
|
||||
).toBeInTheDocument()
|
||||
expect(screen.queryByRole('link')).not.toBeInTheDocument()
|
||||
await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1))
|
||||
})
|
||||
|
||||
test.each([ROLE.ADMIN, ROLE.SUPER_ADMIN])(
|
||||
'automatically shows the update indicator for administrator role %s without opening a dialog',
|
||||
async (role) => {
|
||||
@@ -137,7 +193,7 @@ describe('administrator update entry', () => {
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
const [url, options] = fetchMock.mock.calls[0]
|
||||
expect(String(url)).toBe(
|
||||
'https://api.github.com/repos/QuantumNous/new-api/releases?per_page=100'
|
||||
'https://api.github.com/repos/zsz2018/NovaLink-new-api/releases?per_page=100'
|
||||
)
|
||||
expect(options?.credentials).toBe('omit')
|
||||
expect(options?.headers).toEqual({
|
||||
@@ -190,7 +246,7 @@ describe('administrator update entry', () => {
|
||||
within(dialog).getByRole('link', { name: 'Go to GitHub' })
|
||||
).toHaveAttribute(
|
||||
'href',
|
||||
'https://github.com/QuantumNous/new-api/releases/tag/v1.0.0-rc.36'
|
||||
'https://github.com/zsz2018/NovaLink-new-api/releases/tag/v1.0.0-rc.36'
|
||||
)
|
||||
await user.keyboard('{Escape}')
|
||||
await waitFor(() =>
|
||||
@@ -370,6 +426,28 @@ describe('administrator update entry', () => {
|
||||
})
|
||||
|
||||
describe('version label presentation', () => {
|
||||
test('keeps the installed NovaLink version in the header and shows the newer stable release separately', async () => {
|
||||
const user = userEvent.setup()
|
||||
client.setQueryData(STATUS_QUERY_KEY, { version: 'v1.0.0' })
|
||||
respondWithRelease('v1.0.1')
|
||||
render(<SystemUpdateAction presentation='version' />, { wrapper: Wrapper })
|
||||
const trigger = await screen.findByRole('button', {
|
||||
name: /System updates, current version: v1\.0\.0.*New version available: v1\.0\.1/s,
|
||||
})
|
||||
expect(within(trigger).getByText('v1.0.0')).toBeInTheDocument()
|
||||
expect(within(trigger).queryByText('v1.0.1')).not.toBeInTheDocument()
|
||||
await user.click(trigger)
|
||||
const dialog = screen.getByRole('dialog', { name: 'System updates' })
|
||||
expect(within(dialog).getByText('v1.0.0')).toBeInTheDocument()
|
||||
expect(within(dialog).getByText('v1.0.1')).toBeInTheDocument()
|
||||
expect(
|
||||
within(dialog).getByRole('link', { name: 'Go to GitHub' })
|
||||
).toHaveAttribute(
|
||||
'href',
|
||||
'https://github.com/zsz2018/NovaLink-new-api/releases/tag/v1.0.1'
|
||||
)
|
||||
})
|
||||
|
||||
test('keeps the current version visible while checking and after finding no newer version', async () => {
|
||||
let finishRequest: ((response: Response) => void) | undefined
|
||||
fetchMock.mockImplementation(
|
||||
@@ -454,14 +532,19 @@ describe('version notification preferences', () => {
|
||||
act(() => first.result.current.setIgnored(true))
|
||||
expect(first.result.current.hasUpdate).toBe(true)
|
||||
expect(first.result.current.shouldNotify).toBe(false)
|
||||
const saved = localStorage.getItem('system-update-preferences:v1')
|
||||
const saved = localStorage.getItem(
|
||||
'system-update-preferences:zsz2018/NovaLink-new-api:v1'
|
||||
)
|
||||
expect(saved).not.toBeNull()
|
||||
first.unmount()
|
||||
client.clear()
|
||||
useSystemUpdateStore.setState({ snapshot: null })
|
||||
useSystemUpdateStore.persist.clearStorage()
|
||||
useSystemUpdatePreferencesStore.setState({ ignoredVersionsByUserId: {} })
|
||||
localStorage.setItem('system-update-preferences:v1', String(saved))
|
||||
localStorage.setItem(
|
||||
'system-update-preferences:zsz2018/NovaLink-new-api:v1',
|
||||
String(saved)
|
||||
)
|
||||
await useSystemUpdatePreferencesStore.persist.rehydrate()
|
||||
client = createAppQueryClient()
|
||||
client.setQueryData(STATUS_QUERY_KEY, { version: 'v1.0.0-rc.35' })
|
||||
@@ -543,10 +626,13 @@ describe('version notification preferences', () => {
|
||||
version: 0,
|
||||
})
|
||||
act(() => {
|
||||
localStorage.setItem('system-update-preferences:v1', saved)
|
||||
localStorage.setItem(
|
||||
'system-update-preferences:zsz2018/NovaLink-new-api:v1',
|
||||
saved
|
||||
)
|
||||
window.dispatchEvent(
|
||||
new StorageEvent('storage', {
|
||||
key: 'system-update-preferences:v1',
|
||||
key: 'system-update-preferences:zsz2018/NovaLink-new-api:v1',
|
||||
newValue: saved,
|
||||
})
|
||||
)
|
||||
@@ -554,10 +640,12 @@ describe('version notification preferences', () => {
|
||||
expect(first.result.current.shouldNotify).toBe(false)
|
||||
expect(second.result.current.shouldNotify).toBe(false)
|
||||
act(() => {
|
||||
localStorage.removeItem('system-update-preferences:v1')
|
||||
localStorage.removeItem(
|
||||
'system-update-preferences:zsz2018/NovaLink-new-api:v1'
|
||||
)
|
||||
window.dispatchEvent(
|
||||
new StorageEvent('storage', {
|
||||
key: 'system-update-preferences:v1',
|
||||
key: 'system-update-preferences:zsz2018/NovaLink-new-api:v1',
|
||||
newValue: null,
|
||||
})
|
||||
)
|
||||
@@ -586,7 +674,7 @@ describe('version notification preferences', () => {
|
||||
})
|
||||
} else {
|
||||
localStorage.setItem(
|
||||
'system-update-preferences:v1',
|
||||
'system-update-preferences:zsz2018/NovaLink-new-api:v1',
|
||||
state === 'corrupt'
|
||||
? '{bad json'
|
||||
: JSON.stringify({
|
||||
@@ -606,6 +694,40 @@ describe('version notification preferences', () => {
|
||||
})
|
||||
|
||||
describe('update cache and scheduling', () => {
|
||||
test('fetches NovaLink releases without reusing cached upstream releases or ignored tags', async () => {
|
||||
const now = Date.now()
|
||||
localStorage.setItem(
|
||||
'system-update:v1',
|
||||
JSON.stringify({
|
||||
state: {
|
||||
snapshot: {
|
||||
release: { ...release, tag_name: 'v9.0.0' },
|
||||
lastCheckedAt: now,
|
||||
lastAttemptAt: now,
|
||||
error: null,
|
||||
},
|
||||
},
|
||||
version: 0,
|
||||
})
|
||||
)
|
||||
localStorage.setItem(
|
||||
'system-update-preferences:v1',
|
||||
JSON.stringify({
|
||||
state: { ignoredVersionsByUserId: { 1: [release.tag_name] } },
|
||||
version: 0,
|
||||
})
|
||||
)
|
||||
await useSystemUpdateStore.persist.rehydrate()
|
||||
await useSystemUpdatePreferencesStore.persist.rehydrate()
|
||||
const hook = renderHook(useSystemUpdate, { wrapper: Wrapper })
|
||||
await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1))
|
||||
await waitFor(() =>
|
||||
expect(hook.result.current.release?.tag_name).toBe(release.tag_name)
|
||||
)
|
||||
expect(hook.result.current.shouldNotify).toBe(true)
|
||||
expect(hook.result.current.isIgnored).toBe(false)
|
||||
})
|
||||
|
||||
test('refreshes the server version during a later automatic check and clears an installed update', async () => {
|
||||
vi.useFakeTimers()
|
||||
fetchMock.mockImplementation(
|
||||
@@ -631,10 +753,15 @@ describe('update cache and scheduling', () => {
|
||||
await waitFor(() => expect(first.result.current.hasUpdate).toBe(true))
|
||||
first.unmount()
|
||||
client.clear()
|
||||
const saved = localStorage.getItem('system-update:v1')
|
||||
const saved = localStorage.getItem(
|
||||
'system-update:zsz2018/NovaLink-new-api:v1'
|
||||
)
|
||||
expect(saved).not.toBeNull()
|
||||
useSystemUpdateStore.setState({ snapshot: null })
|
||||
localStorage.setItem('system-update:v1', String(saved))
|
||||
localStorage.setItem(
|
||||
'system-update:zsz2018/NovaLink-new-api:v1',
|
||||
String(saved)
|
||||
)
|
||||
await useSystemUpdateStore.persist.rehydrate()
|
||||
client = createAppQueryClient()
|
||||
client.setQueryData(STATUS_QUERY_KEY, { version: 'v1.0.0-rc.35' })
|
||||
@@ -647,11 +774,14 @@ describe('update cache and scheduling', () => {
|
||||
'keeps checking when persisted storage is %s',
|
||||
async (state) => {
|
||||
if (state === 'corrupt') {
|
||||
localStorage.setItem('system-update:v1', '{bad json')
|
||||
localStorage.setItem(
|
||||
'system-update:zsz2018/NovaLink-new-api:v1',
|
||||
'{bad json'
|
||||
)
|
||||
await useSystemUpdateStore.persist.rehydrate()
|
||||
} else if (state === 'future') {
|
||||
localStorage.setItem(
|
||||
'system-update:v1',
|
||||
'system-update:zsz2018/NovaLink-new-api:v1',
|
||||
JSON.stringify({
|
||||
state: {
|
||||
snapshot: {
|
||||
|
||||
@@ -16,7 +16,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import { selectLatestRelease, type SystemRelease } from './releases'
|
||||
import {
|
||||
selectLatestRelease,
|
||||
SYSTEM_REPOSITORY,
|
||||
type SystemRelease,
|
||||
} from './releases'
|
||||
|
||||
export type UpdateCheckErrorCode =
|
||||
| 'network'
|
||||
@@ -42,7 +46,7 @@ export async function fetchLatestSystemRelease(
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
'https://api.github.com/repos/QuantumNous/new-api/releases?per_page=100',
|
||||
`https://api.github.com/repos/${SYSTEM_REPOSITORY}/releases?per_page=100`,
|
||||
{
|
||||
credentials: 'omit',
|
||||
headers: { Accept: 'application/vnd.github+json' },
|
||||
|
||||
@@ -18,6 +18,9 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import { z } from 'zod'
|
||||
|
||||
export const SYSTEM_REPOSITORY = 'zsz2018/NovaLink-new-api'
|
||||
export const SYSTEM_REPOSITORY_URL = `https://github.com/${SYSTEM_REPOSITORY}`
|
||||
|
||||
export const systemReleaseSchema = z.object({
|
||||
tag_name: z.string().trim().min(1),
|
||||
name: z.string().nullable().optional(),
|
||||
@@ -111,5 +114,5 @@ export function selectLatestRelease(payload: unknown): SystemRelease | null {
|
||||
}
|
||||
|
||||
export function getSystemReleaseUrl(release: SystemRelease): string {
|
||||
return `https://github.com/QuantumNous/new-api/releases/tag/${encodeURIComponent(release.tag_name)}`
|
||||
return `${SYSTEM_REPOSITORY_URL}/releases/tag/${encodeURIComponent(release.tag_name)}`
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
type StateStorage,
|
||||
} from 'zustand/middleware'
|
||||
|
||||
import { systemReleaseSchema } from './releases'
|
||||
import { SYSTEM_REPOSITORY, systemReleaseSchema } from './releases'
|
||||
|
||||
const updateSnapshotSchema = z
|
||||
.object({
|
||||
@@ -78,7 +78,7 @@ export const useSystemUpdateStore = create<SystemUpdateStore>()(
|
||||
setSnapshot: (snapshot) => set({ snapshot }),
|
||||
}),
|
||||
{
|
||||
name: 'system-update:v1',
|
||||
name: `system-update:${SYSTEM_REPOSITORY}:v1`,
|
||||
storage: createJSONStorage(() => updateStorage),
|
||||
partialize: (state) => ({ snapshot: state.snapshot }),
|
||||
merge: (persisted, current) => {
|
||||
@@ -94,7 +94,7 @@ export const useSystemUpdateStore = create<SystemUpdateStore>()(
|
||||
)
|
||||
)
|
||||
|
||||
const preferencesStorageKey = 'system-update-preferences:v1'
|
||||
const preferencesStorageKey = `system-update-preferences:${SYSTEM_REPOSITORY}:v1`
|
||||
const updatePreferencesSchema = z.object({
|
||||
ignoredVersionsByUserId: z.record(
|
||||
z.string().regex(/^[1-9]\d*$/),
|
||||
|
||||
@@ -25,6 +25,7 @@ import { HugeiconsIcon } from '@hugeicons/react'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { IconGithub } from '@/assets/brand-icons'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Spinner } from '@/components/ui/spinner'
|
||||
@@ -32,6 +33,7 @@ import { ROLE } from '@/lib/roles'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useAuthStore } from '@/stores/auth-store'
|
||||
|
||||
import { SYSTEM_REPOSITORY_URL } from './releases'
|
||||
import { SystemUpdateDialog } from './system-update-dialog'
|
||||
import { useSystemUpdate } from './use-system-update'
|
||||
|
||||
@@ -42,11 +44,31 @@ type SystemUpdateActionProps = {
|
||||
}
|
||||
|
||||
export function SystemUpdateAction(props: SystemUpdateActionProps) {
|
||||
const isAdmin = useAuthStore(
|
||||
(state) => (state.auth.user?.role ?? 0) >= ROLE.ADMIN
|
||||
const { t } = useTranslation()
|
||||
const user = useAuthStore((state) => state.auth.user)
|
||||
if (!user) return null
|
||||
if (user.role >= ROLE.ADMIN) return <AdminSystemUpdateAction {...props} />
|
||||
if (props.presentation !== 'version') return null
|
||||
return (
|
||||
<Button
|
||||
variant='ghost'
|
||||
size='icon'
|
||||
nativeButton={false}
|
||||
role='link'
|
||||
aria-label={t('Go to GitHub')}
|
||||
title={t('Go to GitHub')}
|
||||
className='text-muted-foreground'
|
||||
render={
|
||||
<a
|
||||
href={SYSTEM_REPOSITORY_URL}
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
/>
|
||||
}
|
||||
>
|
||||
<IconGithub aria-hidden='true' />
|
||||
</Button>
|
||||
)
|
||||
if (!isAdmin) return null
|
||||
return <AdminSystemUpdateAction {...props} />
|
||||
}
|
||||
|
||||
function AdminSystemUpdateAction(props: SystemUpdateActionProps) {
|
||||
|
||||
@@ -38,7 +38,7 @@ import {
|
||||
UpdateCheckError,
|
||||
type UpdateCheckErrorCode,
|
||||
} from './api'
|
||||
import { compareSystemVersions } from './releases'
|
||||
import { compareSystemVersions, SYSTEM_REPOSITORY } from './releases'
|
||||
import {
|
||||
subscribeSystemUpdatePreferences,
|
||||
useSystemUpdatePreferencesStore,
|
||||
@@ -47,7 +47,10 @@ import {
|
||||
} from './store'
|
||||
|
||||
export const SYSTEM_UPDATE_INTERVAL = 60 * 60 * 1000
|
||||
export const SYSTEM_UPDATE_QUERY_KEY = ['system-update'] as const
|
||||
export const SYSTEM_UPDATE_QUERY_KEY = [
|
||||
'system-update',
|
||||
SYSTEM_REPOSITORY,
|
||||
] as const
|
||||
|
||||
export function getUpdateErrorMessage(
|
||||
code: UpdateCheckErrorCode,
|
||||
|
||||
Reference in new issue
Block a user