129 lines
5.9 KiB
TypeScript
129 lines
5.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { PluginSettingsSection } from '../src/client/PluginSettingsSection.tsx'
|
|
import type {
|
|
PluginSettingsSectionInjected,
|
|
PluginSettingsSectionProps,
|
|
} from '../src/client/PluginSettingsSection.tsx'
|
|
import { en, type PluginsKey } from '../src/client/locales.ts'
|
|
|
|
afterEach(cleanup)
|
|
|
|
type Snapshot = Awaited<ReturnType<PluginSettingsSectionInjected['list']>>
|
|
const t = ((key: PluginsKey): string => en[key]) as PluginSettingsSectionProps['t']
|
|
const unusedHook = (() => { throw new Error('unused by plugin inventory') }) as never
|
|
|
|
function props(list: PluginSettingsSectionInjected['list']): PluginSettingsSectionProps {
|
|
return {
|
|
close: vi.fn(),
|
|
useSessions: unusedHook,
|
|
useWorkspaces: unusedHook,
|
|
t,
|
|
list,
|
|
}
|
|
}
|
|
|
|
const SNAPSHOT = {
|
|
entries: [
|
|
{ entryId: '8a1b2c3d', moduleName: '@deepseek-ai/cordis-plugin-hmr', enabled: true, fiberPhase: 'active' },
|
|
{ entryId: 'pending', moduleName: 'cordis:pending-name', enabled: true, fiberPhase: 'pending' },
|
|
{ entryId: 'loading', moduleName: '@fixture/loading-name', enabled: true, fiberPhase: 'loading' },
|
|
{ entryId: 'failed', moduleName: '@fixture/failed-name', enabled: true, fiberPhase: 'failed' },
|
|
{ entryId: 'unloading', moduleName: '@fixture/unloading-name', enabled: true, fiberPhase: 'unloading' },
|
|
{ entryId: 'disabled-entry', moduleName: '@deepseek-ai/dsh-host-directory-picker-native', enabled: false, fiberPhase: null },
|
|
],
|
|
} as unknown as Snapshot
|
|
|
|
describe('PluginSettingsSection', () => {
|
|
it('renders searchable two-column-card semantics with dots and tags', async () => {
|
|
const deferred = Promise.withResolvers<Snapshot>()
|
|
const list = vi.fn(() => deferred.promise)
|
|
const view = render(<PluginSettingsSection {...props(list)} />)
|
|
expect(screen.getByText(en.loading)).toBeTruthy()
|
|
|
|
await act(async () => { deferred.resolve(SNAPSHOT) })
|
|
expect(list).toHaveBeenCalledOnce()
|
|
expect(screen.getByRole('searchbox', { name: en.search })).toBeTruthy()
|
|
expect(screen.getByRole('heading', { name: en.catalog })).toBeTruthy()
|
|
expect(view.container.querySelector('[data-plugin-count]')?.textContent).toBe('6')
|
|
expect(screen.getAllByRole('listitem')).toHaveLength(6)
|
|
expect(screen.getAllByText(en.enabledTag)).toHaveLength(5)
|
|
expect(screen.getByText(en.disabledTag)).toBeTruthy()
|
|
for (const value of [
|
|
'Mounted',
|
|
'Waiting for dependencies',
|
|
'Loading',
|
|
'Mount failed',
|
|
'Unloading',
|
|
'Not mounted',
|
|
]) {
|
|
expect(screen.getByRole('img', { name: value })).toBeTruthy()
|
|
}
|
|
const active = screen.getByRole('button', { name: 'hmr, Mounted, Enabled' })
|
|
expect(active.getAttribute('aria-expanded')).toBe('false')
|
|
fireEvent.click(active)
|
|
expect(active.getAttribute('aria-expanded')).toBe('true')
|
|
expect(view.container.querySelector('[data-loader-entry]')?.textContent).toBe('8a1b2c3d')
|
|
expect(screen.getByText(en.configuration)).toBeTruthy()
|
|
expect(screen.getByText(en.cordis)).toBeTruthy()
|
|
fireEvent.click(active)
|
|
expect(view.container.querySelector('[data-loader-entry]')).toBeNull()
|
|
|
|
fireEvent.click(active)
|
|
fireEvent.change(screen.getByRole('searchbox', { name: en.search }), {
|
|
target: { value: 'disabled-entry' },
|
|
})
|
|
expect(view.container.querySelector('[data-loader-entry]')).toBeNull()
|
|
fireEvent.click(screen.getByRole('button', { name: 'directory-picker-native, Not mounted, Disabled' }))
|
|
expect(screen.getAllByText(en.disabledTag)).toHaveLength(2)
|
|
})
|
|
|
|
it('filters by module name or Loader entry id', async () => {
|
|
render(<PluginSettingsSection {...props(async () => SNAPSHOT)} />)
|
|
const search = await screen.findByRole('searchbox', { name: en.search })
|
|
|
|
fireEvent.change(search, { target: { value: 'disabled-entry' } })
|
|
expect(screen.getAllByRole('listitem')).toHaveLength(1)
|
|
expect(screen.getByText('directory-picker-native')).toBeTruthy()
|
|
|
|
fireEvent.change(search, { target: { value: 'cordis-plugin-hmr' } })
|
|
expect(screen.getAllByRole('listitem')).toHaveLength(1)
|
|
expect(screen.getByText('hmr')).toBeTruthy()
|
|
|
|
fireEvent.change(search, { target: { value: 'not-a-plugin' } })
|
|
expect(screen.queryAllByRole('listitem')).toHaveLength(0)
|
|
expect(screen.getByText(en.emptySearch)).toBeTruthy()
|
|
})
|
|
|
|
it('shows a generic failure and retries into the empty state', async () => {
|
|
const list = vi.fn<PluginSettingsSectionInjected['list']>()
|
|
.mockRejectedValueOnce(new Error('private transport detail'))
|
|
.mockResolvedValueOnce({ entries: [] })
|
|
render(<PluginSettingsSection {...props(list)} />)
|
|
|
|
expect((await screen.findByRole('alert')).textContent).toBe(en.error)
|
|
expect(screen.queryByText('private transport detail')).toBeNull()
|
|
fireEvent.click(screen.getByRole('button', { name: en.retry }))
|
|
await waitFor(() => { expect(list).toHaveBeenCalledTimes(2) })
|
|
expect(await screen.findByText(en.empty)).toBeTruthy()
|
|
})
|
|
|
|
it('contains a synchronous Remote failure and ignores a result after unmount', async () => {
|
|
const syncFailure = vi.fn(() => { throw new Error('namespace unavailable') }) as PluginSettingsSectionInjected['list']
|
|
const failed = render(<PluginSettingsSection {...props(syncFailure)} />)
|
|
expect((await screen.findByRole('alert')).textContent).toBe(en.error)
|
|
failed.unmount()
|
|
|
|
const deferred = Promise.withResolvers<Snapshot>()
|
|
const pending = render(<PluginSettingsSection {...props(() => deferred.promise)} />)
|
|
pending.unmount()
|
|
await act(async () => { deferred.resolve(SNAPSHOT) })
|
|
|
|
const deferredFailure = Promise.withResolvers<Snapshot>()
|
|
const pendingFailure = render(<PluginSettingsSection {...props(() => deferredFailure.promise)} />)
|
|
pendingFailure.unmount()
|
|
await act(async () => { deferredFailure.reject(new Error('late failure')) })
|
|
})
|
|
})
|