1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

Fix various tests

This commit is contained in:
Sergey 2024-08-29 16:58:54 +02:00
parent 3483d5c155
commit b5ae07ae87
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
9 changed files with 150 additions and 62 deletions

View file

@ -221,26 +221,26 @@ func collectUseCaseInfo(files []*zip.File, fileName string) (info *useCaseInfo,
return nil, fmt.Errorf("failed to extract snapshot from file %s: %w", f.Name, err)
}
id := snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
name := snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyName, "")
id := pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyId.String())
name := pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyName.String())
info.objects[id] = objectInfo{
Type: snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyType, ""),
Type: pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyType.String()),
Name: name,
SbType: smartblock.SmartBlockType(snapshot.SbType),
}
switch snapshot.SbType {
case model.SmartBlockType_STRelation:
uk := snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyUniqueKey, "")
uk := pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyUniqueKey.String())
key := strings.TrimPrefix(uk, addr.RelationKeyToIdPrefix)
info.relations[id] = domain.RelationKey(key)
format := snapshot.Snapshot.Data.Details.GetInt64OrDefault(bundle.RelationKeyRelationFormat, 0)
if !bundle.HasRelation(key) {
format := pbtypes.GetInt64(snapshot.Snapshot.Data.Details, bundle.RelationKeyRelationFormat.String())
if !bundle.HasRelation(domain.RelationKey(key)) {
info.customTypesAndRelations[key] = customInfo{id: id, isUsed: false, relationFormat: model.RelationFormat(format)}
}
case model.SmartBlockType_STType:
uk := snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyUniqueKey, "")
uk := pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyUniqueKey.String())
key := strings.TrimPrefix(uk, addr.ObjectTypeKeyToIdPrefix)
info.types[id] = domain.TypeKey(key)
if !bundle.HasObjectTypeByKey(domain.TypeKey(key)) {
@ -256,15 +256,15 @@ func collectUseCaseInfo(files []*zip.File, fileName string) (info *useCaseInfo,
} else if strings.HasPrefix(id, addr.RelationKeyToIdPrefix) {
key := strings.TrimPrefix(id, addr.RelationKeyToIdPrefix)
info.relations[id] = domain.RelationKey(key)
format := snapshot.Snapshot.Data.Details.GetInt64OrDefault(bundle.RelationKeyRelationFormat, 0)
if !bundle.HasRelation(key) {
format := pbtypes.GetInt64(snapshot.Snapshot.Data.Details, bundle.RelationKeyRelationFormat.String())
if !bundle.HasRelation(domain.RelationKey(key)) {
info.customTypesAndRelations[key] = customInfo{id: id, isUsed: false, relationFormat: model.RelationFormat(format)}
}
}
case model.SmartBlockType_Template:
info.templates[id] = snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyTargetObjectType, "")
info.templates[id] = pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyTargetObjectType.String())
case model.SmartBlockType_STRelationOption:
info.options[id] = domain.RelationKey(snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyRelationKey, ""))
info.options[id] = domain.RelationKey(pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyRelationKey.String()))
case model.SmartBlockType_FileObject:
info.files = append(info.files, id)
}
@ -418,7 +418,7 @@ func extractSnapshotAndType(data []byte, name string) (s *pb.SnapshotWithType, i
func validate(snapshot *pb.SnapshotWithType, info *useCaseInfo) (err error) {
isValid := true
id := snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyId.String())
for _, v := range validators {
if e := v(snapshot, info); e != nil {
if errors.Is(e, errSkipObject) {
@ -430,14 +430,14 @@ func validate(snapshot *pb.SnapshotWithType, info *useCaseInfo) (err error) {
}
if !isValid {
return fmt.Errorf("object '%s' (name: '%s') is invalid: %w",
id[len(id)-4:], snapshot.Snapshot.Data.Details.GetString(bundle.RelationKeyName, ""), err)
id[len(id)-4:], pbtypes.GetString(snapshot.Snapshot.Data.Details, bundle.RelationKeyName.String()), err)
}
return nil
}
func insertAnalyticsData(s *pb.ChangeSnapshot, info *useCaseInfo) {
root := s.Data.Blocks[0]
id := s.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Data.Details, bundle.RelationKeyId.String())
f := root.GetFields().GetFields()
if f == nil {
@ -469,8 +469,8 @@ func removeAccountRelatedDetails(s *pb.ChangeSnapshot) {
}
func insertCreatorInfo(s *pb.ChangeSnapshot) {
s.Data.Details.Set(bundle.RelationKeyCreator, pbtypes.String(addr.AnytypeProfileId))
s.Data.Details.Set(bundle.RelationKeyLastModifiedBy, pbtypes.String(addr.AnytypeProfileId))
s.Data.Details.Fields[bundle.RelationKeyCreator.String()] = pbtypes.String(addr.AnytypeProfileId)
s.Data.Details.Fields[bundle.RelationKeyLastModifiedBy.String()] = pbtypes.String(addr.AnytypeProfileId)
}
func processProfile(data []byte, info *useCaseInfo, spaceDashboardId string) ([]byte, error) {

View file

@ -15,6 +15,7 @@ import (
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
"github.com/anyproto/anytype-heart/util/slice"
)
@ -70,7 +71,7 @@ func readRules(fileName string) error {
}
func processRules(s *pb.ChangeSnapshot) {
id := s.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Data.Details, bundle.RelationKeyId.String())
for i, r := range rules {
if r.ObjectID != id && r.ObjectID != "" {
@ -124,7 +125,7 @@ func doDetailRule(s *pb.ChangeSnapshot, r rule) {
case remove:
delete(s.Data.Details.Fields, r.DetailKey)
case change, add:
s.Data.Details.Set(r.DetailKey, r.DetailValue)
s.Data.Details.Fields[r.DetailKey] = r.DetailValue
default:
fmt.Printf(errInvalidAction, r.Action)
}
@ -163,7 +164,7 @@ func doDataViewTargetRule(s *pb.ChangeSnapshot, r rule) {
}
if !ok {
fmt.Println("Failed to process rule as block" + r.BlockID + "of object" +
s.Data.Details.GetString(bundle.RelationKeyId, "") + "is not dataview block")
pbtypes.GetString(s.Data.Details, bundle.RelationKeyId.String()) + "is not dataview block")
return
}
switch r.Action {
@ -192,7 +193,7 @@ func doLinkTargetRule(s *pb.ChangeSnapshot, r rule) {
}
if !ok {
fmt.Println("Failed to process rule as block" + r.BlockID + "of object" +
s.Data.Details.GetString(bundle.RelationKeyId, "") + "is not link block")
pbtypes.GetString(s.Data.Details, bundle.RelationKeyId.String()) + "is not link block")
return
}
switch r.Action {

View file

@ -7,6 +7,7 @@ import (
"strings"
"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
"github.com/samber/lo"
"github.com/anyproto/anytype-heart/core/block/editor/widget"
@ -41,10 +42,10 @@ var validators = []validator{
}
func validateRelationLinks(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
linksToDelete := make([]keyWithIndex, 0)
for i, rel := range s.Snapshot.Data.RelationLinks {
if bundle.HasRelation(rel.Key) {
if bundle.HasRelation(domain.RelationKey(rel.Key)) {
continue
}
if _, found := info.customTypesAndRelations[rel.Key]; found {
@ -61,7 +62,7 @@ func validateRelationLinks(s *pb.SnapshotWithType, info *useCaseInfo) (err error
}
func validateRelationBlocks(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
var relKeys []string
for _, b := range s.Snapshot.Data.Blocks {
if rel := simple.New(b).Model().GetRelation(); rel != nil {
@ -92,7 +93,7 @@ func validateRelationBlocks(s *pb.SnapshotWithType, info *useCaseInfo) (err erro
}
func validateDetails(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
for k, v := range s.Snapshot.Data.Details.Fields {
if isLinkRelation(k) {
@ -123,7 +124,7 @@ func validateDetails(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
values := pbtypes.GetStringListValue(v)
for _, val := range values {
if bundle.HasRelation(strings.TrimPrefix(val, addr.RelationKeyToIdPrefix)) ||
if bundle.HasRelation(domain.RelationKey(strings.TrimPrefix(val, addr.RelationKeyToIdPrefix))) ||
bundle.HasObjectTypeByKey(domain.TypeKey(strings.TrimPrefix(val, addr.ObjectTypeKeyToIdPrefix))) || val == addr.AnytypeProfileId {
continue
}
@ -153,7 +154,7 @@ func validateDetails(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
}
func validateObjectTypes(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
for _, ot := range s.Snapshot.Data.ObjectTypes {
typeId := strings.TrimPrefix(ot, addr.ObjectTypeKeyToIdPrefix)
if !bundle.HasObjectTypeByKey(domain.TypeKey(typeId)) {
@ -166,7 +167,7 @@ func validateObjectTypes(s *pb.SnapshotWithType, info *useCaseInfo) (err error)
}
func validateBlockLinks(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
for _, b := range s.Snapshot.Data.Blocks {
switch a := simple.New(b).(type) {
case link.Block:
@ -213,20 +214,53 @@ func validateBlockLinks(s *pb.SnapshotWithType, info *useCaseInfo) (err error) {
return err
}
func validateDeleted(s *pb.SnapshotWithType, _ *useCaseInfo) error {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
func validateFileKeys(s *pb.SnapshotWithType, _ *useCaseInfo) (err error) {
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
for _, r := range s.Snapshot.Data.RelationLinks {
if r.Format == model.RelationFormat_file || r.Key == bundle.RelationKeyCoverId.String() {
for _, hash := range pbtypes.GetStringList(s.Snapshot.GetData().GetDetails(), r.Key) {
if r.Format != model.RelationFormat_file {
_, err := cid.Parse(hash)
if err != nil {
continue
}
}
if !snapshotHasKeyForHash(s, hash) {
err = multierror.Append(err, fmt.Errorf("object '%s' has file detail '%s' has hash '%s' which keys are not in the snapshot", id, r.Key, hash))
}
}
}
}
for _, b := range s.Snapshot.Data.Blocks {
if v, ok := simple.New(b).(simple.FileHashes); ok {
hashes := v.FillFileHashes([]string{})
if len(hashes) == 0 {
continue
}
for _, hash := range hashes {
if !snapshotHasKeyForHash(s, hash) {
err = multierror.Append(err, fmt.Errorf("file block '%s' of object '%s' has hash '%s' which keys are not in the snapshot", b.Id, id, hash))
}
}
}
}
return err
}
if s.Snapshot.Data.Details.GetBoolOrDefault(bundle.RelationKeyIsArchived, false) {
func validateDeleted(s *pb.SnapshotWithType, _ *useCaseInfo) error {
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
if pbtypes.GetBool(s.Snapshot.Data.Details, bundle.RelationKeyIsArchived.String()) {
fmt.Println("WARNING: object", id, " is archived, so it will be skipped")
return errSkipObject
}
if s.Snapshot.Data.Details.GetBoolOrDefault(bundle.RelationKeyIsDeleted, false) {
if pbtypes.GetBool(s.Snapshot.Data.Details, bundle.RelationKeyIsDeleted.String()) {
fmt.Println("WARNING: object", id, " is deleted, so it will be skipped")
return errSkipObject
}
if s.Snapshot.Data.Details.GetBoolOrDefault(bundle.RelationKeyIsUninstalled, false) {
if pbtypes.GetBool(s.Snapshot.Data.Details, bundle.RelationKeyIsUninstalled.String()) {
fmt.Println("WARNING: object", id, " is uninstalled, so it will be skipped")
return errSkipObject
}
@ -239,13 +273,13 @@ func validateRelationOption(s *pb.SnapshotWithType, info *useCaseInfo) error {
return nil
}
key := s.Snapshot.Data.Details.GetString(bundle.RelationKeyRelationKey, "")
if bundle.HasRelation(key) {
key := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyRelationKey.String())
if bundle.HasRelation(domain.RelationKey(key)) {
return nil
}
if _, found := info.customTypesAndRelations[key]; !found {
id := s.Snapshot.Data.Details.GetString(bundle.RelationKeyId, "")
id := pbtypes.GetString(s.Snapshot.Data.Details, bundle.RelationKeyId.String())
return fmt.Errorf("failed to find relation key %s of relation option %s", key, id)
}
return nil
@ -260,6 +294,15 @@ func getRelationLinkByKey(links []*model.RelationLink, key string) *model.Relati
return nil
}
func snapshotHasKeyForHash(s *pb.SnapshotWithType, hash string) bool {
for _, k := range s.Snapshot.FileKeys {
if k.Hash == hash && len(k.Keys) > 0 {
return true
}
}
return false
}
func isLinkRelation(k string) bool {
return k == bundle.RelationKeyLinks.String() || k == bundle.RelationKeySourceObject.String() || k == bundle.RelationKeyBacklinks.String()
}

View file

@ -14,7 +14,6 @@ import (
"github.com/anyproto/any-sync/app"
"github.com/globalsign/mgo/bson"
"github.com/gogo/protobuf/types"
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/block/import/markdown/anymark"
@ -32,7 +31,6 @@ import (
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/space"
"github.com/anyproto/anytype-heart/util/linkpreview"
"github.com/anyproto/anytype-heart/util/pbtypes"
"github.com/anyproto/anytype-heart/util/uri"
)
@ -58,7 +56,7 @@ type ObjectCreator interface {
}
type DetailsSetter interface {
SetDetails(ctx session.Context, objectId string, details []*model.Detail) (err error)
SetDetails(ctx session.Context, objectId string, details []domain.Detail) (err error)
}
type service struct {
@ -181,25 +179,13 @@ func (s *service) CreateBookmarkObject(ctx context.Context, spaceID string, deta
return objectId, objectDetails, nil
}
func detailsFromContent(content *bookmark.ObjectContent) map[string]*types.Value {
return map[string]*types.Value{
bundle.RelationKeyName.String(): pbtypes.String(content.BookmarkContent.Title),
bundle.RelationKeyDescription.String(): pbtypes.String(content.BookmarkContent.Description),
bundle.RelationKeySource.String(): pbtypes.String(content.BookmarkContent.Url),
bundle.RelationKeyPicture.String(): pbtypes.String(content.BookmarkContent.ImageHash),
bundle.RelationKeyIconImage.String(): pbtypes.String(content.BookmarkContent.FaviconHash),
}
}
func (s *service) UpdateObject(objectId string, getContent *bookmark.ObjectContent) error {
detailsMap := detailsFromContent(getContent)
details := make([]*model.Detail, 0, len(detailsMap))
for k, v := range detailsMap {
details = append(details, &model.Detail{
Key: k,
Value: v,
})
func (s *service) UpdateObject(objectId string, content *bookmark.ObjectContent) error {
details := []domain.Detail{
{Key: bundle.RelationKeyName, Value: domain.String(content.BookmarkContent.Title)},
{Key: bundle.RelationKeyDescription, Value: domain.String(content.BookmarkContent.Description)},
{Key: bundle.RelationKeySource, Value: domain.String(content.BookmarkContent.Url)},
{Key: bundle.RelationKeyPicture, Value: domain.String(content.BookmarkContent.ImageHash)},
{Key: bundle.RelationKeyIconImage, Value: domain.String(content.BookmarkContent.FaviconHash)},
}
return s.detailsSetter.SetDetails(nil, objectId, details)

View file

@ -903,7 +903,7 @@ func TestProfileMigrationExtractCustomState(t *testing.T) {
})
originalState.Details().Iterate(func(k domain.RelationKey, v domain.Value) bool {
require.Contains(t, whitelistedDetailKeys, k, "old state should not contain %s", k)
require.Contains(t, whitelistedDetailKeys, k.String(), "old state should not contain %s", k)
return true
})
require.Equal(t, bundle.TypeKeyPage, extractedState.ObjectTypeKey())

View file

@ -374,8 +374,7 @@ func TestState_SetParent(t *testing.T) {
st2 := orig.Copy()
require.NoError(t, st2.ApplyChange(st.GetChanges()...))
t.Fatal("FIX")
// assert.Equal(t, st.StringDebug(), st2.StringDebug())
assert.Equal(t, st.StringDebug(), st2.StringDebug())
}
func TestStateNormalizeMerge(t *testing.T) {

View file

@ -9,6 +9,7 @@ import (
"github.com/gogo/protobuf/types"
"github.com/ipfs/go-cid"
"github.com/valyala/fastjson"
"github.com/anyproto/anytype-heart/core/block/simple"
"github.com/anyproto/anytype-heart/core/block/undo"
@ -890,6 +891,42 @@ func (s *State) writeString(buf *bytes.Buffer, l int, id string) {
}
}
func (s *State) StringDebug() string {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "RootId: %s\n", s.RootId())
fmt.Fprintf(buf, "ObjectTypeKeys: %v\n", s.ObjectTypeKeys())
fmt.Fprintf(buf, "Relations:\n")
for _, rel := range s.relationLinks {
fmt.Fprintf(buf, "\t%v\n", rel)
}
fmt.Fprintf(buf, "\nDetails:\n")
arena := &fastjson.Arena{}
s.Details().IterateSorted(func(k domain.RelationKey, v domain.Value) bool {
raw := string(v.ToJson(arena).MarshalTo(nil))
fmt.Fprintf(buf, "\t%s:\t%v\n", k, raw)
return true
})
fmt.Fprintf(buf, "\nLocal details:\n")
s.LocalDetails().IterateSorted(func(k domain.RelationKey, v domain.Value) bool {
raw := string(v.ToJson(arena).MarshalTo(nil))
fmt.Fprintf(buf, "\t%s:\t%v\n", k, raw)
return true
})
fmt.Fprintf(buf, "\nBlocks:\n")
s.writeString(buf, 0, s.RootId())
fmt.Fprintf(buf, "\nCollection:\n")
pbtypes.SortedRange(s.Store(), func(k string, v *types.Value) {
fmt.Fprintf(buf, "\t%s\n", k)
if st := v.GetStructValue(); st != nil {
pbtypes.SortedRange(st, func(k string, v *types.Value) {
fmt.Fprintf(buf, "\t\t%s:\t%v\n", k, pbtypes.Sprint(v))
})
}
})
return buf.String()
}
func (s *State) SetDetails(d *domain.Details) *State {
// TODO: GO-2062 Need to refactor details shortening, as it could cut string incorrectly
// if d != nil && d.Fields != nil {

View file

@ -167,7 +167,7 @@ func TestService_CreateTemplateStateWithDetails(t *testing.T) {
// then
assert.NoError(t, err)
assert.Equal(t, BlankTemplateId, st.RootId())
assert.Contains(t, st.Details().GetStringList(bundle.RelationKeyFeaturedRelations), bundle.RelationKeyTag)
assert.Contains(t, st.Details().GetStringList(bundle.RelationKeyFeaturedRelations), bundle.RelationKeyTag.String())
assert.True(t, st.Details().Has(bundle.RelationKeyTag))
})
}
@ -257,7 +257,7 @@ func TestCreateTemplateStateFromSmartBlock(t *testing.T) {
// then
assert.Equal(t, BlankTemplateId, st.RootId())
assert.Contains(t, st.Details().GetStringList(bundle.RelationKeyFeaturedRelations), bundle.RelationKeyTag)
assert.Contains(t, st.Details().GetStringList(bundle.RelationKeyFeaturedRelations), bundle.RelationKeyTag.String())
assert.True(t, st.Details().Has(bundle.RelationKeyTag))
})

View file

@ -2,6 +2,7 @@ package domain
import (
"fmt"
"sort"
"strings"
"github.com/gogo/protobuf/types"
@ -79,6 +80,27 @@ func (d *GenericMap[K]) Iterate(proc func(key K, value Value) bool) {
}
}
func (d *GenericMap[K]) IterateSorted(proc func(key K, value Value) bool) {
if d == nil {
return
}
keys := make([]K, 0, len(d.data))
for k := range d.data {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, k := range keys {
v := d.data[k]
if !proc(k, v) {
return
}
}
}
func (d *GenericMap[K]) GetRaw(key K) (any, bool) {
v, ok := d.data[key]
return v, ok