discourse-shared-edits/spec/services/discourse_shared_edits/revise_spec.rb
Sam 50d9317272
FEATURE: yjs-based collaborative editing with state recovery and cursor sync
Major overhaul of the shared edits plugin to improve reliability, robustness,
and developer experience:

**Backend**
- Extract Revise service for cleaner controller orchestration
- Add StateValidator for base64/Yjs safety, corruption detection, and recovery
- Centralize protocol constants (Ruby + JS) to avoid hardcoded strings
- Add state hash sync verification and vector validation endpoints
- Harden security (guardian checks), error handling, and resource cleanup
- Resize shared edit columns migration; add state_hash column

**Frontend**
- Decompose shared-edit-manager into focused modules: yjs-document,
  markdown-sync, rich-mode-sync, network-manager, encoding-utils
- Add cursor overlay and caret coordinate tracking for selection sharing
- Add ProseMirror extension for rich-mode collaborative editing
- Cache-busted Yjs bundle loading via hashed filenames
- Fix scroll drift during sync

**Testing & Tooling**
- Extensive new specs: state_validator, revision_controller, model, revise service
- New Ember acceptance tests: cursor, lifecycle, sync flows
- Add support scripts: fake_writer (Playwright), state_corruptor, debug_recovery
- Add support/lint wrapper for full CI lint suite
- Update dependencies and rebuild Yjs/y-prosemirror bundles
2026-02-13 11:34:52 +11:00

74 lines
2.1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DiscourseSharedEdits::Revise do
fab!(:admin)
fab!(:post) { Fabricate(:post, user: admin, raw: "initial content") }
before do
SiteSetting.shared_edits_enabled = true
SharedEditRevision.toggle_shared_edits!(post.id, true)
end
def latest_state_for(post)
SharedEditRevision.where(post_id: post.id).order("version desc").limit(1).pluck(:raw).first
end
def revise!(client_id:, state_vector: nil)
latest_state = latest_state_for(post)
update = DiscourseSharedEdits::Yjs.update_from_state(latest_state, "updated content")
described_class.call(
post: post,
current_user: admin,
client_id: client_id,
update: update,
cursor: nil,
awareness: nil,
allow_blank_state: false,
state_vector: state_vector,
)
end
it "applies revisions through the service pipeline" do
response = nil
latest_state = latest_state_for(post)
revision_update = DiscourseSharedEdits::Yjs.update_from_state(latest_state, "updated content")
described_class.call(
post: post,
current_user: admin,
client_id: "abc",
update: revision_update,
cursor: nil,
awareness: nil,
allow_blank_state: false,
state_vector: nil,
) do |result|
on_success do
response = {
version: result[:version],
update: result[:update],
state_hash: result[:state_hash],
}
end
end
expect(response).to be_present
expect(response[:version]).to eq(2)
expect(response[:update]).to eq(revision_update)
expect(response[:state_hash]).to be_present
end
it "raises invalid update when client_id is invalid" do
expect { revise!(client_id: "a" * 256) }.to raise_error(
DiscourseSharedEdits::StateValidator::InvalidUpdateError,
/Invalid client_id/,
)
end
it "raises invalid update when state_vector is invalid" do
expect { revise!(client_id: "abc", state_vector: "invalid!!!") }.to raise_error(
DiscourseSharedEdits::StateValidator::InvalidUpdateError,
/Invalid state vector/,
)
end
end