Files
deepseek-harness/packages/session-query/session-export/tests/header-action.client.spec.tsx

73 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { useSyncExternalStore } from 'react'
import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
import { SessionExportDownloadController } from '../src/client/controller.ts'
import { SessionExportHeader } from '../src/client/HeaderAction.tsx'
import type { SessionExportDialogProps } from '../src/client/Dialog.tsx'
import { en } from '../src/client/locales.ts'
const SID = 'session-export-header' as SessionId
function bindSessionExport(controller: SessionExportDownloadController) {
return function useSessionExport<T>(selector: (state: ReturnType<typeof controller.store.getSnapshot>) => T): T {
return useSyncExternalStore(
listener => controller.store.subscribe(listener),
() => selector(controller.store.getSnapshot()),
)
}
}
function bench() {
const controller = new SessionExportDownloadController(async () => new Response('zip'), vi.fn())
const request = vi.fn((sessionId: SessionId) => controller.download(sessionId))
const dismiss = vi.fn((sessionId: SessionId) => { controller.dismiss(sessionId) })
const useSessionExport = bindSessionExport(controller)
const props = {
sessionId: SID,
useSessionExport,
request,
dismiss,
t: (key: keyof typeof en): string => en[key],
} as unknown as SessionExportDialogProps
const view = render(<SessionExportHeader {...props} />)
return { controller, request, view }
}
afterEach(cleanup)
describe('Session export Header action', () => {
it('renders the 111×32 text capsule and downloads through the shared controller', async () => {
const b = bench()
const button = b.view.getByRole('button', { name: 'Session log' })
expect(button.querySelector('svg')).not.toBeNull()
fireEvent.click(button)
await waitFor(() => { expect(b.request).toHaveBeenCalledWith(SID) })
expect(await b.view.findByRole('dialog', { name: 'Session download started' })).toBeTruthy()
})
it('disables the capsule while either entry path downloads this Session', async () => {
const b = bench()
let release!: (response: Response) => void
const pending = new Promise<Response>((resolve) => { release = resolve })
const controller = new SessionExportDownloadController(() => pending, vi.fn())
const useSessionExport = bindSessionExport(controller)
b.view.rerender(<SessionExportHeader {...({
sessionId: SID,
useSessionExport,
request: (sessionId: SessionId) => controller.download(sessionId),
dismiss: (sessionId: SessionId) => { controller.dismiss(sessionId) },
t: (key: keyof typeof en): string => en[key],
} as unknown as SessionExportDialogProps)} />)
const download = controller.download(SID)
const button = b.view.getByRole('button', { name: 'Session log' })
await waitFor(() => { expect(button.getAttribute('aria-busy')).toBe('true') })
expect((button as HTMLButtonElement).disabled).toBe(true)
release(new Response('zip'))
await download
await waitFor(() => { expect(button.getAttribute('aria-busy')).toBe('false') })
})
})