Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
wip(daemon): initial merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juligasa committed May 16, 2024
1 parent e432465 commit 50e0fd4
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 223 deletions.
78 changes: 76 additions & 2 deletions backend/daemon/api/documents/v1alpha/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,83 @@ func (api *Server) ListAccountPublications(ctx context.Context, in *documents.Li
return out, nil
}

// MergeChanges implements the corresponding gRPC method.
// MergeChanges implements the corresponding gRPC method. It merges changes and publishes them.
func (api *Server) MergeChanges(ctx context.Context, in *documents.MergeChangesRequest) (*documents.Document, error) {
return nil, status.Errorf(codes.Unimplemented, "Merge functionality is not implemented yet")
me, err := api.getMe()
if err != nil {
return nil, err
}

if in.TargetDocumentId == "" {
return nil, fmt.Errorf("Target ID is mandatory")
}
eid := hyper.EntityID(in.TargetDocumentId)

source, err := hyper.Version(in.SourceDocumentVersion).Parse()
if err != nil {
return nil, err
}

doc, err := api.CreateDraft(ctx, &documents.CreateDraftRequest{
ExistingDocumentId: in.TargetDocumentId,
Version: in.TargetDocumentVersion,
})
if err != nil {
return nil, err
}
if doc.Id != eid.String() {
return nil, fmt.Errorf("Internal error. Draft entity and doc don't match")
}
entity, err := api.blobs.LoadEntity(ctx, eid)
if err != nil {
return nil, err
}
ch, err := api.blobs.GetDraft(ctx, eid)
if err != nil {
return nil, err
}

OUTER:
for _, dep := range source {
for _, alreadyDep := range ch.Deps {
if dep == alreadyDep {
continue OUTER
}
}
ch.Deps = append(ch.Deps, dep)
}

del, err := api.getDelegation(ctx)
if err != nil {
return nil, err
}

hb, err := entity.CreateChange(entity.NextTimestamp(), me.DeviceKey(), del, map[string]any{
// TODO(juligasa): Remove this and add in terra.go NewChange if deps>1 and no patch don't error since its a merge
"isDraft": true,
}, hyper.WithDeps(ch.Deps), hyper.WithAction(hyper.ActionUpdate))

if err != nil {
return nil, err
}
/*
_, err = api.UpdateDraft(ctx, &documents.UpdateDraftRequest{
DocumentId: eid.String(),
Changes: hb.Decoded,
})
if err != nil {
return nil, err
}*/
if err := api.blobs.SaveDraftBlob(ctx, eid, hb); err != nil {
return nil, err
}

return api.GetDraft(ctx, &documents.GetDraftRequest{
DocumentId: eid.String(),
})

//return nil, status.Errorf(codes.Unimplemented, "Merge functionality is not implemented yet"+c.String())

}

Check failure on line 1027 in backend/daemon/api/documents/v1alpha/documents.go

View workflow job for this annotation

GitHub Actions / lint-go

unnecessary trailing newline (whitespace)

func (api *Server) getMe() (core.Identity, error) {
Expand Down
6 changes: 2 additions & 4 deletions backend/daemon/api/documents/v1alpha/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func TestAPIPublishDraft_E2E(t *testing.T) {
}

func TestMerge(t *testing.T) {
t.Skip("Not ready yet")
//t.Skip("Not ready yet")
t.Parallel()

api := newTestDocsAPI(t, "alice")
Expand Down Expand Up @@ -619,15 +619,13 @@ func TestMerge(t *testing.T) {
Compare(t, "publication in the list must be the same as published")

mergedPub, err := api.MergeChanges(ctx, &documents.MergeChangesRequest{
SourceDocumentId: sourceDoc.Document.Id,
SourceDocumentVersion: sourceDoc.Document.Version,
TargetDocumentId: targetDoc.Document.Id,
TargetDocumentVersion: targetDoc.Document.Version,
})
require.NoError(t, err)
require.Equal(t, targetDoc.Document.Id, mergedPub.Id)
require.Equal(t, targetDoc.Document.Version, mergedPub.PreviousVersion)

require.Equal(t, sourceDoc.Document.Version, mergedPub.PreviousVersion)
}
func TestAPIUpdateDraft_WithList(t *testing.T) {
api := newTestDocsAPI(t, "alice")
Expand Down
362 changes: 175 additions & 187 deletions backend/genproto/documents/v1alpha/documents.pb.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions backend/genproto/documents/v1alpha/documents_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions backend/hyper/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ func WithAction(action string) ChangeOption {
}
}

// WithDeps sets the depts field of the change.
func WithDeps(deps []cid.Cid) ChangeOption {
return func(c *Change) {
c.Deps = deps
}
}

// WithMessage sets the message field of the change.
func WithMessage(msg string) ChangeOption {
return func(c *Change) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ export const Publications = {
} as const;

/**
* Publications service provides access to published documents.
* Merge service provides access to merge documents.
*
* @generated from service com.mintter.documents.v1alpha.Merge
*/
export const Merge = {
typeName: "com.mintter.documents.v1alpha.Merge",
methods: {
/**
* Gets a single publication.
* Merge changes.
*
* @generated from rpc com.mintter.documents.v1alpha.Merge.MergeChanges
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,31 +874,24 @@ export class ListAccountPublicationsRequest extends Message<ListAccountPublicati
* @generated from message com.mintter.documents.v1alpha.MergeChangesRequest
*/
export class MergeChangesRequest extends Message<MergeChangesRequest> {
/**
* Required. ID of the document containing the changes to merge.
*
* @generated from field: string source_document_id = 1;
*/
sourceDocumentId = "";

/**
* Required. Version of the document containing the changes to merge.
*
* @generated from field: string source_document_version = 2;
* @generated from field: string source_document_version = 1;
*/
sourceDocumentVersion = "";

/**
* Required. ID of the base document to merge into.
*
* @generated from field: string target_document_id = 3;
* @generated from field: string target_document_id = 2;
*/
targetDocumentId = "";

/**
* Optional. Version of the base document to merge into.
*
* @generated from field: string target_document_version = 4;
* @generated from field: string target_document_version = 3;
*/
targetDocumentVersion = "";

Expand All @@ -910,10 +903,9 @@ export class MergeChangesRequest extends Message<MergeChangesRequest> {
static readonly runtime: typeof proto3 = proto3;
static readonly typeName = "com.mintter.documents.v1alpha.MergeChangesRequest";
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "source_document_id", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 2, name: "source_document_version", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "target_document_id", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 4, name: "target_document_version", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 1, name: "source_document_version", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 2, name: "target_document_id", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "target_document_version", kind: "scalar", T: 9 /* ScalarType.STRING */ },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): MergeChangesRequest {
Expand Down
13 changes: 5 additions & 8 deletions proto/documents/v1alpha/documents.proto
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,22 @@ message ListAccountPublicationsRequest {

// === Merge Service ===

// Publications service provides access to published documents.
// Merge service provides access to merge documents.
service Merge {
// Gets a single publication.
// Merge changes.
rpc MergeChanges(MergeChangesRequest) returns (Publication);
}

// Request for merging changes in a document.
message MergeChangesRequest {
// Required. ID of the document containing the changes to merge.
string source_document_id = 1;

// Required. Version of the document containing the changes to merge.
string source_document_version = 2;
string source_document_version = 1;

// Required. ID of the base document to merge into.
string target_document_id = 3;
string target_document_id = 2;

// Optional. Version of the base document to merge into.
string target_document_version = 4;
string target_document_version = 3;

}

Expand Down
4 changes: 2 additions & 2 deletions proto/documents/v1alpha/go.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: ae800889f67c1447810f9f69f0318a41
outs: 2b9ffa4191c89f4dda5f95af8fb2239b
srcs: 0a9aa79228fdda7a09803da0e610c014
outs: 781fa7dc1a65d9955debcfcb6ace0db8
4 changes: 2 additions & 2 deletions proto/documents/v1alpha/js.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: ae800889f67c1447810f9f69f0318a41
outs: 69a5ca0129dcca03dadb7eab2cc6472b
srcs: 0a9aa79228fdda7a09803da0e610c014
outs: 27f5804793679ab4f40d5f88fb0ee417

0 comments on commit 50e0fd4

Please sign in to comment.