Files
deepseek-harness/packages/client/web-react/tests/bind.spec.tsx
T
imccyu 8b3d1ac943 refactor(gui): move the snapshot-store engine into the client runtime
The data layer no longer depends on the React glue package, and business
plugins no longer depend on web-react at all:

- The store engine (zustand vanilla + immer + persist + dev freeze),
  defineStore, and shallowEqual move to @deepseek-ai/dsh-client-runtime,
  exported from the ./client main entry — no ./store subpath survives on
  either package (the web-react one is deleted, none is opened on runtime).
- Store products are bare snapshot sources: useSelector leaves
  SnapshotStore/StoreInstance and Session; every hook is composed at the
  binding site in web-react's renderer (per-source cached uSES binding).
  The SlotRendererHost sessions face carries bare observables only.
- SessionProvider becomes a standard-kit seat: an entry whose children
  declare a session-scope slot receives the framework component as a prop,
  retiring the last value import of web-react from plugin packages.
  UseSession and the session-area types now live in ui-slots.
- web-react shrinks to the shell-only React glue (renderer, providers,
  uSES bridge); zustand/immer belong to runtime alone; the module-table
  seed and tsdown externals drop the web-react/store seat.
- NODE_ENV replacement is defined once in the shared tsdown client preset
  (browser bundles inline the engine and lost vite's define); the 3-line
  process.env typecheck shim moves to runtime with the engine.
- Stray tsc artifacts (.js/.d.ts/.d.ts.map beside sources under src/)
  swept repo-wide; they shadow real sources under vitest resolution.

Verified: both aggregate typecheck programs at zero; 604 client tests
green; repo-wide grep for web-react/store at zero; real-host playwright
run 7/7 including persist round-trip.

ci: fix test/docs
2026-07-23 03:30:06 +08:00

133 lines
5.5 KiB
TypeScript

// @vitest-environment jsdom
import { StrictMode } from 'react'
import { describe, expect, it, vi } from 'vitest'
import { act, render } from '@testing-library/react'
import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
import type { HostObservable as ObservableSnapshot, SnapshotSelectorHook } from '@deepseek-ai/dsh-client-ui-slots'
// Local one-level equality: the engine's shallowEqual moved to runtime with
// the store relocation, and web-react tests must not import runtime (the
// dependency direction is runtime → web-react). The eq PARAMETER contract is
// what this suite asserts, not any specific equality implementation.
const shallowEqual = (a: Record<string, unknown>, b: Record<string, unknown>): boolean =>
Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((k) => Object.is(a[k], b[k]))
interface Snap { a: number; b: number }
/** Hand-rolled observable source so subscription counting is exact. */
function makeSource(initial: Snap) {
let state = initial
const listeners = new Set<() => void>()
let subscribeCalls = 0
const source: ObservableSnapshot<Snap> = {
getSnapshot: () => state,
subscribe: (fn) => {
subscribeCalls += 1
listeners.add(fn)
return () => { listeners.delete(fn) }
},
}
return {
source,
set: (next: Snap) => {
state = next
for (const fn of [...listeners]) fn()
},
stats: { get subscribeCalls() { return subscribeCalls }, get active() { return listeners.size } },
}
}
function Harness<S>({ useSelector, sel, eq, probe }: {
useSelector: SnapshotSelectorHook<Snap>
sel: (s: Snap) => S
eq?: (a: S, b: S) => boolean
probe: { renders: number; value?: S | undefined }
}) {
probe.renders += 1
probe.value = useSelector(sel, eq)
return null
}
describe('bindSnapshotSelector', () => {
it('re-renders on selected change and bails out when the slice is equal', () => {
const { source, set } = makeSource({ a: 1, b: 10 })
const useSelector = bindSnapshotSelector(source)
const probe = { renders: 0, value: undefined as number | undefined }
render(<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />)
expect(probe.value).toBe(1)
const before = probe.renders
act(() => { set({ a: 1, b: 11 }) }) // unrelated field: Object.is bail
expect(probe.renders).toBe(before)
act(() => { set({ a: 2, b: 11 }) })
expect(probe.renders).toBe(before + 1)
expect(probe.value).toBe(2)
})
it('supports custom equality for object slices', () => {
const { source, set } = makeSource({ a: 1, b: 10 })
const useSelector = bindSnapshotSelector(source)
const probe = { renders: 0, value: undefined as { a: number } | undefined }
render(<Harness useSelector={useSelector} sel={(s) => ({ a: s.a })} eq={shallowEqual} probe={probe} />)
const before = probe.renders
act(() => { set({ a: 1, b: 99 }) }) // fresh object, shallow-equal slice
expect(probe.renders).toBe(before)
act(() => { set({ a: 5, b: 99 }) })
expect(probe.renders).toBe(before + 1)
expect(probe.value).toEqual({ a: 5 })
})
it('does not resubscribe across re-renders of the same component', () => {
const { source, set, stats } = makeSource({ a: 1, b: 10 })
const useSelector = bindSnapshotSelector(source)
const probe = { renders: 0, value: undefined as number | undefined }
const { rerender } = render(<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />)
const after = stats.subscribeCalls
rerender(<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />)
act(() => { set({ a: 2, b: 10 }) })
rerender(<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />)
expect(stats.subscribeCalls).toBe(after)
})
it('is StrictMode-safe and cleans up subscriptions on unmount', () => {
const { source, stats } = makeSource({ a: 1, b: 10 })
const useSelector = bindSnapshotSelector(source)
const probe = { renders: 0, value: undefined as number | undefined }
const view = render(
<StrictMode>
<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />
</StrictMode>,
)
expect(probe.value).toBe(1)
view.unmount()
expect(stats.active).toBe(0)
})
it('binds method-style sources without losing this', () => {
class MethodSource implements ObservableSnapshot<Snap> {
private state: Snap = { a: 7, b: 0 }
private listeners = new Set<() => void>()
getSnapshot(): Snap { return this.state }
subscribe(fn: () => void): () => void {
this.listeners.add(fn)
return () => { this.listeners.delete(fn) }
}
}
const useSelector = bindSnapshotSelector(new MethodSource())
const probe = { renders: 0, value: undefined as number | undefined }
render(<Harness useSelector={useSelector} sel={(s) => s.a} probe={probe} />)
expect(probe.value).toBe(7)
})
it('memoizes the selector result against getSnapshot spam', () => {
const { source } = makeSource({ a: 1, b: 10 })
const sel = vi.fn((s: Snap) => s.a)
const useSelector = bindSnapshotSelector(source)
const probe = { renders: 0, value: undefined as number | undefined }
const { rerender } = render(<Harness useSelector={useSelector} sel={sel} probe={probe} />)
const calls = sel.mock.calls.length
rerender(<Harness useSelector={useSelector} sel={sel} probe={probe} />)
// Same snapshot + same selector reference: no recompute beyond bookkeeping.
expect(sel.mock.calls.length).toBeLessThanOrEqual(calls + 1)
})
})