Skip to content

Add Yjs document and provider infrastructure with tests#9

Merged
wmxscott merged 6 commits into
mainfrom
claude/implement-phase-4-5Eyyr
Mar 23, 2026
Merged

Add Yjs document and provider infrastructure with tests#9
wmxscott merged 6 commits into
mainfrom
claude/implement-phase-4-5Eyyr

Conversation

@wmxscott
Copy link
Copy Markdown
Owner

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 accessor

    • createYjsDoc(): Creates isolated Y.Doc instances
    • getSharedTypes(): Centralized accessor for all 8 shared CRDT types (Maps, Arrays, Text)
    • TypeScript interfaces for SharedTypes, AdmittedPeer, and EncryptedChatEntry
    • Comprehensive documentation of encryption boundaries (moderatorChat/Notes are encrypted; editorContent/excalidrawState are not)
  • src/lib/yjs/providers.ts: WebRTC and IndexedDB provider initialization

    • initProviders(): Sets up y-webrtc and y-indexeddb with proper room naming
    • Returns indexeddbSynced promise to gate peer admission until local state is restored
    • Cleanup via destroy() method
  • src/lib/yjs/YjsContext.tsx: React context and hooks for Yjs access

    • YjsProvider: Wraps app with Yjs state; blocks rendering until IndexedDB syncs
    • useYjs(): Hook to access ydoc, shared types, and webrtc provider
    • Enforces admission gate: children only render after local persistence is ready
  • Test files (3 new test suites):

    • doc.test.ts: 13 tests covering document creation, shared type instantiation, and cross-doc sync
    • providers.test.ts: 8 tests verifying provider construction, room naming, and lifecycle
    • YjsContext.test.tsx: 9 tests for React integration, loading states, and context consumption
  • Config updates:

    • vitest.config.ts: Added .test.tsx pattern support
    • package.json: Added @testing-library/react and @testing-library/jest-dom dev dependencies

Notable Implementation Details

  • Admission gate: YjsProvider does not render children until indexeddbSynced resolves, preventing race conditions where owner's burnedJTIs or trustedSigningKeys are not yet loaded when peers connect
  • Encryption boundaries: Clearly documented which shared types contain encrypted data (moderatorChat, moderatorNotes) vs. plaintext (editorContent, excalidrawState)
  • Type safety: Full TypeScript support with generic types for Y.Map and Y.Array
  • Provider cleanup: Proper resource management with destroy callbacks on unmount

https://claude.ai/code/session_01AWujUF4neyha29dXbquNKZ

claude and others added 6 commits March 23, 2026 01:04
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
@wmxscott wmxscott merged commit 3ed9b57 into main Mar 23, 2026
2 checks passed
@wmxscott wmxscott deleted the claude/implement-phase-4-5Eyyr branch March 23, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants