Add Yjs document and provider infrastructure with tests#9
Merged
Conversation
Implements the shared Yjs document, WebRTC + IndexedDB providers, and the React context gate that blocks children from rendering until IndexedDB has fully restored state — preventing admission races on burnedJTIs and trustedSigningKeys. - src/lib/yjs/doc.ts — SharedTypes interface, createYjsDoc, getSharedTypes - src/lib/yjs/providers.ts — initProviders (y-webrtc + y-indexeddb) - src/lib/yjs/YjsContext.tsx — YjsProvider (loading gate) + useYjs hook - Comprehensive tests for all three modules (120 tests passing) - Added @testing-library/react for React component testing - Extended vitest.config.ts to include .test.tsx files https://claude.ai/code/session_01AWujUF4neyha29dXbquNKZ
- Replace useRef+ready pattern with useState in YjsProvider to avoid reading ref.current during render (react-hooks/refs violation) - Add eslint-disable for react-refresh/only-export-components on useYjs hook co-located with its provider component - Use mutable object property with eslint-disable for capturing context in test Consumer component, replacing disallowed variable reassignment https://claude.ai/code/session_01AWujUF4neyha29dXbquNKZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces the core Yjs (CRDT) infrastructure for Ratifyd, including document initialization, shared type definitions, provider setup, and React context integration. All new code is fully tested with comprehensive unit and integration tests.
Key Changes
src/lib/yjs/doc.ts: Core Yjs document factory and shared types accessorcreateYjsDoc(): Creates isolated Y.Doc instancesgetSharedTypes(): Centralized accessor for all 8 shared CRDT types (Maps, Arrays, Text)SharedTypes,AdmittedPeer, andEncryptedChatEntrysrc/lib/yjs/providers.ts: WebRTC and IndexedDB provider initializationinitProviders(): Sets up y-webrtc and y-indexeddb with proper room namingindexeddbSyncedpromise to gate peer admission until local state is restoreddestroy()methodsrc/lib/yjs/YjsContext.tsx: React context and hooks for Yjs accessYjsProvider: Wraps app with Yjs state; blocks rendering until IndexedDB syncsuseYjs(): Hook to access ydoc, shared types, and webrtc providerTest files (3 new test suites):
doc.test.ts: 13 tests covering document creation, shared type instantiation, and cross-doc syncproviders.test.ts: 8 tests verifying provider construction, room naming, and lifecycleYjsContext.test.tsx: 9 tests for React integration, loading states, and context consumptionConfig updates:
vitest.config.ts: Added.test.tsxpattern supportpackage.json: Added@testing-library/reactand@testing-library/jest-domdev dependenciesNotable Implementation Details
YjsProviderdoes not render children untilindexeddbSyncedresolves, preventing race conditions where owner'sburnedJTIsortrustedSigningKeysare not yet loaded when peers connecthttps://claude.ai/code/session_01AWujUF4neyha29dXbquNKZ