import { expectTypeOf, test } from 'vitest' import { visible } from '../hydration' import { Hydrate } from '../Hydrate' import type { HydrateOptions, HydrateProps, HydrationPrefetchFunction, HydrationPrefetchStrategy, HydrationStrategy, } from '../Hydrate' import type { HydrationStrategy as CoreHydrationStrategy } from '@tanstack/start-client-core/hydration' import type { JSX } from 'solid-js' type CommonHydrateProps = { fallback?: JSX.Element onHydrated?: () => void children: JSX.Element } type SplitHydrateProps = CommonHydrateProps & { when: HydrationStrategy | (() => HydrationStrategy) prefetch?: never split?: boolean } type PrefetchHydrateProps = CommonHydrateProps & { when: HydrationStrategy | (() => HydrationStrategy) prefetch: HydrationPrefetchStrategy split?: true } type FunctionPrefetchHydrateProps = CommonHydrateProps & { when: HydrationStrategy | (() => HydrationStrategy) prefetch: HydrationPrefetchFunction split?: boolean } test('Hydrate component accepts the public HydrateProps type', () => { expectTypeOf(Hydrate).toBeFunction() expectTypeOf(Hydrate).parameter(0).branded.toEqualTypeOf() }) test('HydrateOptions supports reusable spread props', () => { const belowFoldProps = { when: () => visible({ rootMargin: '800px' }), } satisfies HydrateOptions expectTypeOf(belowFoldProps).toMatchTypeOf() const withFunctionPrefetch = { when: visible(), split: false, prefetch: (ctx) => { expectTypeOf(ctx.element).toEqualTypeOf() expectTypeOf(ctx.signal).toEqualTypeOf() expectTypeOf(ctx.preload).returns.toEqualTypeOf>() expectTypeOf(ctx.waitFor).returns.toEqualTypeOf< Promise<'prefetch' | 'hydrate' | 'abort'> >() }, } satisfies HydrateOptions expectTypeOf(withFunctionPrefetch).toMatchTypeOf() }) test('Hydrate props are exact for strategy and prefetch forms', () => { expectTypeOf< Extract >().branded.toEqualTypeOf() expectTypeOf< Extract >().branded.toEqualTypeOf() expectTypeOf< Extract >().branded.toEqualTypeOf() }) test('Hydrate requires a strategy', () => { expectTypeOf<{ when: HydrationStrategy children: JSX.Element }>().toMatchTypeOf() expectTypeOf<{ when: () => HydrationStrategy children: JSX.Element }>().toMatchTypeOf() expectTypeOf<{ children: JSX.Element }>().not.toMatchTypeOf() expectTypeOf<{ when: () => true children: JSX.Element }>().not.toMatchTypeOf() expectTypeOf<{ when: false children: JSX.Element }>().not.toMatchTypeOf() }) test('Hydrate requires a framework-renderable strategy', () => { expectTypeOf().not.toMatchTypeOf() expectTypeOf>().toMatchTypeOf() expectTypeOf<{ when: CoreHydrationStrategy children: JSX.Element }>().not.toMatchTypeOf() }) test('Hydrate enforces prefetch only with split boundaries', () => { expectTypeOf<{ when: HydrationStrategy prefetch: HydrationPrefetchStrategy children: JSX.Element }>().toMatchTypeOf() expectTypeOf<{ when: HydrationStrategy prefetch: HydrationPrefetchStrategy split: true children: JSX.Element }>().toMatchTypeOf() expectTypeOf<{ when: HydrationStrategy prefetch: HydrationPrefetchStrategy split: false children: JSX.Element }>().not.toMatchTypeOf() expectTypeOf<{ when: HydrationStrategy prefetch: HydrationPrefetchFunction split: false children: JSX.Element }>().toMatchTypeOf() expectTypeOf<{ when: HydrationStrategy prefetch: HydrationPrefetchFunction children: JSX.Element }>().toMatchTypeOf() })