2026-07-26 02:51:36 +08:00
|
|
|
|
// @vitest-environment jsdom
|
|
|
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
2026-08-04 16:33:35 +08:00
|
|
|
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
|
|
|
|
import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
|
2026-07-31 12:48:39 +08:00
|
|
|
|
import type { GeneralSectionComponentProps } from '../src/client/GeneralSection.tsx'
|
|
|
|
|
|
import { GeneralSection } from '../src/client/GeneralSection.tsx'
|
2026-07-26 12:40:17 +08:00
|
|
|
|
import { CloseLabel, HeaderContent, TriggerContent } from '../src/client/chrome.tsx'
|
2026-07-31 12:48:39 +08:00
|
|
|
|
import type { TriggerContentProps } from '../src/client/chrome.tsx'
|
2026-08-04 16:33:35 +08:00
|
|
|
|
import { SettingsDocumentAction } from '../src/client/SettingsDocumentAction.tsx'
|
|
|
|
|
|
import { SettingsDocumentStore } from '../src/client/settings-document-store.ts'
|
2026-07-26 02:51:36 +08:00
|
|
|
|
import { en } from '../src/client/locales.ts'
|
|
|
|
|
|
|
|
|
|
|
|
afterEach(cleanup)
|
|
|
|
|
|
|
2026-07-30 15:56:41 +08:00
|
|
|
|
// The seat's key domain is settings ∪ common; the stub answers from the
|
|
|
|
|
|
// package dictionary and falls back to the key like the real chain.
|
2026-07-31 12:48:39 +08:00
|
|
|
|
const t: TriggerContentProps['t'] = key => (en as Record<string, string>)[key] ?? key
|
2026-07-26 12:40:17 +08:00
|
|
|
|
|
|
|
|
|
|
// Global standard kit stubs: none of these components consume the hooks.
|
|
|
|
|
|
const unusedHook = (() => { throw new Error('unused by settings-general components') }) as never
|
|
|
|
|
|
const kit = { useSessions: unusedHook, useWorkspaces: unusedHook }
|
|
|
|
|
|
|
|
|
|
|
|
describe('chrome content', () => {
|
|
|
|
|
|
it('TriggerContent renders the icon with the label in the wide column', () => {
|
|
|
|
|
|
const { container } = render(<TriggerContent {...kit} wide t={t} />)
|
|
|
|
|
|
expect(container.querySelector('svg')).toBeTruthy()
|
|
|
|
|
|
expect(screen.getByText('Settings')).toBeTruthy()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('TriggerContent drops the label in the rail state', () => {
|
|
|
|
|
|
const { container } = render(<TriggerContent {...kit} wide={false} t={t} />)
|
|
|
|
|
|
expect(container.querySelector('svg')).toBeTruthy()
|
|
|
|
|
|
expect(screen.queryByText('Settings')).toBeNull()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('HeaderContent and CloseLabel render their translated text', () => {
|
|
|
|
|
|
render(<HeaderContent {...kit} t={t} />)
|
|
|
|
|
|
render(<CloseLabel {...kit} t={t} />)
|
|
|
|
|
|
expect(screen.getByText('Settings')).toBeTruthy()
|
|
|
|
|
|
expect(screen.getByText('Close')).toBeTruthy()
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2026-07-26 02:51:36 +08:00
|
|
|
|
|
|
|
|
|
|
describe('GeneralSection', () => {
|
2026-07-26 12:40:17 +08:00
|
|
|
|
function mount() {
|
|
|
|
|
|
const renderSlot = vi.fn(
|
|
|
|
|
|
((key: string) => <div data-testid={`slot-${key}`} />) as GeneralSectionComponentProps['renderSlot'],
|
|
|
|
|
|
)
|
2026-08-09 00:19:18 +08:00
|
|
|
|
const props: GeneralSectionComponentProps = { ...kit, renderSlot, close: vi.fn() }
|
2026-07-26 12:40:17 +08:00
|
|
|
|
const view = render(<GeneralSection {...props} />)
|
|
|
|
|
|
return { view, renderSlot }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-31 12:48:19 +08:00
|
|
|
|
it('renders the item slot as the section body', () => {
|
|
|
|
|
|
const { renderSlot } = mount()
|
|
|
|
|
|
expect(renderSlot).toHaveBeenCalledWith('settings.general.item', {})
|
|
|
|
|
|
expect(screen.getByTestId('slot-settings.general.item')).toBeTruthy()
|
2026-07-26 02:51:36 +08:00
|
|
|
|
})
|
2026-07-31 12:48:19 +08:00
|
|
|
|
})
|
2026-08-04 16:33:35 +08:00
|
|
|
|
|
|
|
|
|
|
describe('SettingsDocumentAction', () => {
|
|
|
|
|
|
it('appears only for a file-backed provider and requests its Host-owned document', async () => {
|
|
|
|
|
|
const openDocument = vi.fn(() => Promise.resolve({
|
|
|
|
|
|
rpcId: 'document-open' as never,
|
|
|
|
|
|
result: { ok: true as const, value: { opened: true as const } },
|
|
|
|
|
|
}))
|
|
|
|
|
|
const controller = new SettingsDocumentStore({
|
|
|
|
|
|
settings: {
|
|
|
|
|
|
describe: vi.fn(() => Promise.resolve({
|
|
|
|
|
|
rpcId: 'document-action' as never,
|
|
|
|
|
|
result: {
|
|
|
|
|
|
ok: true as const,
|
2026-08-04 17:31:36 +08:00
|
|
|
|
value: { writable: true, hasDocument: true, namespaces: [] },
|
2026-08-04 16:33:35 +08:00
|
|
|
|
},
|
|
|
|
|
|
})),
|
|
|
|
|
|
openDocument,
|
|
|
|
|
|
},
|
|
|
|
|
|
} as never)
|
|
|
|
|
|
render(<SettingsDocumentAction
|
|
|
|
|
|
{...kit}
|
|
|
|
|
|
t={t}
|
|
|
|
|
|
controller={controller}
|
|
|
|
|
|
useSnapshot={bindSnapshotSelector(controller.store)}
|
|
|
|
|
|
/>)
|
|
|
|
|
|
const action = await screen.findByRole('button', { name: 'Open configuration file' })
|
|
|
|
|
|
fireEvent.click(action)
|
|
|
|
|
|
await waitFor(() => { expect(openDocument).toHaveBeenCalledWith({}) })
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-04 17:31:36 +08:00
|
|
|
|
it('stays absent without a document and retries availability after remount', async () => {
|
|
|
|
|
|
const describe = vi.fn()
|
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
|
rpcId: 'document-action-absent' as never,
|
|
|
|
|
|
result: { ok: true as const, value: { writable: true, hasDocument: false, namespaces: [] } },
|
|
|
|
|
|
})
|
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
|
rpcId: 'document-action-ready' as never,
|
|
|
|
|
|
result: { ok: true as const, value: { writable: true, hasDocument: true, namespaces: [] } },
|
|
|
|
|
|
})
|
2026-08-04 16:33:35 +08:00
|
|
|
|
const controller = new SettingsDocumentStore({
|
|
|
|
|
|
settings: {
|
2026-08-04 17:31:36 +08:00
|
|
|
|
describe,
|
2026-08-04 16:33:35 +08:00
|
|
|
|
openDocument: vi.fn(),
|
|
|
|
|
|
},
|
|
|
|
|
|
} as never)
|
2026-08-04 17:31:36 +08:00
|
|
|
|
const first = render(<SettingsDocumentAction
|
2026-08-04 16:33:35 +08:00
|
|
|
|
{...kit}
|
|
|
|
|
|
t={t}
|
|
|
|
|
|
controller={controller}
|
|
|
|
|
|
useSnapshot={bindSnapshotSelector(controller.store)}
|
|
|
|
|
|
/>)
|
|
|
|
|
|
await waitFor(() => { expect(controller.store.getSnapshot().status).toBe('unavailable') })
|
|
|
|
|
|
expect(screen.queryByRole('button', { name: 'Open configuration file' })).toBeNull()
|
2026-08-04 17:31:36 +08:00
|
|
|
|
first.unmount()
|
|
|
|
|
|
render(<SettingsDocumentAction
|
|
|
|
|
|
{...kit}
|
|
|
|
|
|
t={t}
|
|
|
|
|
|
controller={controller}
|
|
|
|
|
|
useSnapshot={bindSnapshotSelector(controller.store)}
|
|
|
|
|
|
/>)
|
|
|
|
|
|
expect(await screen.findByRole('button', { name: 'Open configuration file' })).toBeTruthy()
|
|
|
|
|
|
expect(describe).toHaveBeenCalledTimes(2)
|
2026-08-04 16:33:35 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('keeps the action available and reports a native-open failure', async () => {
|
|
|
|
|
|
const controller = new SettingsDocumentStore({
|
|
|
|
|
|
settings: {
|
|
|
|
|
|
describe: vi.fn(() => Promise.resolve({
|
|
|
|
|
|
rpcId: 'document-action' as never,
|
|
|
|
|
|
result: {
|
|
|
|
|
|
ok: true as const,
|
2026-08-04 17:31:36 +08:00
|
|
|
|
value: { writable: true, hasDocument: true, namespaces: [] },
|
2026-08-04 16:33:35 +08:00
|
|
|
|
},
|
|
|
|
|
|
})),
|
|
|
|
|
|
openDocument: vi.fn(() => Promise.resolve({
|
|
|
|
|
|
rpcId: 'document-open-failed' as never,
|
|
|
|
|
|
result: { ok: false as const, error: { code: 'internal' as const, message: 'xdg-open missing', details: {} } },
|
|
|
|
|
|
})),
|
|
|
|
|
|
},
|
|
|
|
|
|
} as never)
|
|
|
|
|
|
render(<SettingsDocumentAction
|
|
|
|
|
|
{...kit}
|
|
|
|
|
|
t={t}
|
|
|
|
|
|
controller={controller}
|
|
|
|
|
|
useSnapshot={bindSnapshotSelector(controller.store)}
|
|
|
|
|
|
/>)
|
|
|
|
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Open configuration file' }))
|
|
|
|
|
|
expect((await screen.findByRole('alert')).textContent).toBe('Could not open configuration file')
|
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Open configuration file' })).toBeTruthy()
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|