All editors now work on a server document at the same time instead of taking turns behind an edit lock. - The document's content is a sequence of Yjs updates (document_updates); DocumentChannel stores and relays them immediately, checks access on subscribe and again whenever links or memberships change. - app/frontend/collab/: the store stays position-based; a binding maps it to a Yjs form with stable row/column ids, so a row inserted by one person doesn't misplace what another is typing. Remote changes are applied only after pending local ones went into Yjs. - Per-person undo (Y.UndoManager), presence via awareness: the others' selections as coloured boxes with names, avatars in the toolbar. - Existing JSON documents convert on first open, deterministically, so two people opening one at once end up with the same Yjs state. - Editors compact the stored updates after 200 and keep a JSON snapshot current for export and duplicating. Edit lock and polling are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.8 KiB
Ruby
89 lines
2.8 KiB
Ruby
require "test_helper"
|
|
|
|
class DocumentsApiTest < ActionDispatch::IntegrationTest
|
|
include ActionCable::TestHelper
|
|
|
|
setup do
|
|
@anna = create_user("anna@example.com")
|
|
@bob = create_user("bob@example.com")
|
|
@doc = create_document(@anna)
|
|
end
|
|
|
|
def rename(doc, title, headers: {})
|
|
patch "/api/documents/#{doc.id}", params: { title: }, headers:, as: :json
|
|
end
|
|
|
|
test "listing and creating need an account" do
|
|
get "/api/documents"
|
|
assert_response :unauthorized
|
|
post "/api/documents", params: { title: "x", content: DOC_JSON }, as: :json
|
|
assert_response :unauthorized
|
|
end
|
|
|
|
test "create (upload), list, read" do
|
|
sign_in @anna
|
|
post "/api/documents", params: { title: "Hochgeladen", content: DOC_JSON }, as: :json
|
|
assert_response :created
|
|
id = json.dig("document", "id")
|
|
|
|
get "/api/documents"
|
|
assert_equal [ id, @doc.id ].sort, json["documents"].map { _1["id"] }.sort
|
|
|
|
get "/api/documents/#{id}"
|
|
assert_equal DOC_JSON, json.dig("document", "content")
|
|
assert_equal "owner", json.dig("document", "role")
|
|
end
|
|
|
|
test "someone else's document is a 404" do
|
|
sign_in @bob
|
|
get "/api/documents/#{@doc.id}"
|
|
assert_response :not_found
|
|
end
|
|
|
|
test "share link without account: read with token; viewer cannot write" do
|
|
link = @doc.share_links.create!(role: :viewer, created_by: @anna)
|
|
headers = { "X-Share-Token" => link.token }
|
|
|
|
post "/api/documents/#{@doc.id}/join", params: { token: link.token }, as: :json
|
|
assert_equal "viewer", json.dig("document", "role")
|
|
get "/api/documents/#{@doc.id}", headers: headers
|
|
assert_response :success
|
|
rename(@doc, "Neu", headers:)
|
|
assert_response :forbidden
|
|
|
|
get "/api/documents/#{@doc.id}", headers: { "X-Share-Token" => "wrong" }
|
|
assert_response :not_found
|
|
end
|
|
|
|
test "share link with account: joining adds it to the list" do
|
|
link = @doc.share_links.create!(role: :editor, created_by: @anna)
|
|
sign_in @bob
|
|
post "/api/documents/#{@doc.id}/join", params: { token: link.token }, as: :json
|
|
get "/api/documents"
|
|
assert_equal [ [ @doc.id, "editor" ] ], json["documents"].map { [ _1["id"], _1["role"] ] }
|
|
rename(@doc, "Von Bob")
|
|
assert_response :success
|
|
end
|
|
|
|
test "renaming tells everyone connected" do
|
|
sign_in @anna
|
|
assert_broadcast_on(DocumentChannel.broadcasting_for(@doc), { type: "meta", title: "Neu" }) do
|
|
rename(@doc, "Neu")
|
|
end
|
|
assert_equal "Neu", @doc.reload.title
|
|
end
|
|
|
|
test "owner deletes; a member only leaves" do
|
|
DocumentMembership.grant!(document: @doc, user: @bob, role: :editor, source: :invite)
|
|
sign_in @bob
|
|
delete "/api/documents/#{@doc.id}"
|
|
assert_response :no_content
|
|
assert Document.exists?(@doc.id)
|
|
assert_nil @doc.role_for(user: @bob)
|
|
|
|
sign_in @anna
|
|
delete "/api/documents/#{@doc.id}"
|
|
assert_not Document.exists?(@doc.id)
|
|
end
|
|
end
|