Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: host ordering #31

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion TODO.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
This is a running list of things that need to taken care of at some point:

- [ ] Ordering in the native messaging host
- [ ] Long subtract function too complex
12 changes: 12 additions & 0 deletions cmd/host/internal/messaging/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ func updateBookHandler(in NativeMessage) (NativeMessage, error) {
if payload.URL.Valid {
options.WithURL(payload.URL.String)
}
if payload.NextBook.Valid {
options.WithOrderBefore(payload.NextBook.String)
}
if payload.PreviousBook.Valid {
options.WithOrderAfter(payload.PreviousBook.String)
}

book, err := armariaapi.UpdateBook(payload.ID, options)
if err != nil {
Expand Down Expand Up @@ -340,6 +346,12 @@ func updateFolderHandler(in NativeMessage) (NativeMessage, error) {
if payload.Name.Valid {
options.WithName(payload.Name.String)
}
if payload.NextBook.Valid {
options.WithOrderBefore(payload.NextBook.String)
}
if payload.PreviousBook.Valid {
options.WithOrderAfter(payload.PreviousBook.String)
}

book, err := armariaapi.UpdateFolder(payload.ID, options)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/host/internal/messaging/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type UpdateBookPayload struct {
ParentID null.NullString `json:"parentId"`
RemoveDescription bool `json:"removeDescription"`
RemoveParentID bool `json:"removeParentID"`
PreviousBook null.NullString `json:"previousBook"`
NextBook null.NullString `json:"nextBook"`
}

// UpdateBookPayload is a payload for a request to update a folder.
Expand All @@ -101,6 +103,8 @@ type UpdateFolderPayload struct {
Name null.NullString `json:"name"`
ParentID null.NullString `json:"parentId"`
RemoveParentID bool `json:"removeParentID"`
PreviousBook null.NullString `json:"previousBook"`
NextBook null.NullString `json:"nextBook"`
}

// ErrorPayload is a payload for a response with an error in it.
Expand Down
165 changes: 165 additions & 0 deletions cmd/host/internal/test/update_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/jonathanhope/armaria/cmd/host/internal/messaging"
"github.com/jonathanhope/armaria/internal/null"
"github.com/jonathanhope/armaria/pkg/api"
"github.com/jonathanhope/armaria/pkg/model"
"github.com/samber/lo"
)

func TestUpdateBookURL(t *testing.T) {
Expand Down Expand Up @@ -258,3 +260,166 @@ func TestUpdateBookRemoveParentID(t *testing.T) {
t.Errorf("Expected and actual message different:\n%s", diff)
}
}

func TestUpdateBookOrderStart(t *testing.T) {
db := fmt.Sprintf("%s.db", uuid.New().String())
defer func() { os.Remove(db) }()

addOptions := armariaapi.DefaultAddBookOptions()
addOptions.WithDB(db)
book1, err := armariaapi.AddBook("https://one.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book2, err := armariaapi.AddBook("https://two.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book3, err := armariaapi.AddBook("https://three.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

_, err = nativeMessageLoop(messaging.MessageKindUpdateBook, messaging.UpdateBookPayload{
DB: null.NullStringFrom(db),
ID: book2.ID,
NextBook: null.NullStringFrom(book1.ID),
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}

listOptions := armariaapi.DefaultListBooksOptions()
listOptions.WithBooks(true)
listOptions.WithDB(db)
books, err := armariaapi.ListBooks(listOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

got := lo.Map(books, func(x armaria.Book, _ int) string {
return x.ID
})

want := []string{
book2.ID,
book1.ID,
book3.ID,
}

diff := cmp.Diff(got, want)
if diff != "" {
t.Errorf("Expected and actual orders different:\n%s", diff)
}
}

func TestUpdateBookOrderEnd(t *testing.T) {
db := fmt.Sprintf("%s.db", uuid.New().String())
defer func() { os.Remove(db) }()

addOptions := armariaapi.DefaultAddBookOptions()
addOptions.WithDB(db)
book1, err := armariaapi.AddBook("https://one.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book2, err := armariaapi.AddBook("https://two.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book3, err := armariaapi.AddBook("https://three.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

_, err = nativeMessageLoop(messaging.MessageKindUpdateBook, messaging.UpdateBookPayload{
DB: null.NullStringFrom(db),
ID: book2.ID,
PreviousBook: null.NullStringFrom(book3.ID),
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}

listOptions := armariaapi.DefaultListBooksOptions()
listOptions.WithBooks(true)
listOptions.WithDB(db)
books, err := armariaapi.ListBooks(listOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

got := lo.Map(books, func(x armaria.Book, _ int) string {
return x.ID
})

want := []string{
book1.ID,
book3.ID,
book2.ID,
}

diff := cmp.Diff(got, want)
if diff != "" {
t.Errorf("Expected and actual orders different:\n%s", diff)
}
}

func TestUpdateBookOrderBetween(t *testing.T) {
db := fmt.Sprintf("%s.db", uuid.New().String())
defer func() { os.Remove(db) }()

addOptions := armariaapi.DefaultAddBookOptions()
addOptions.WithDB(db)
book1, err := armariaapi.AddBook("https://one.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book2, err := armariaapi.AddBook("https://two.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

book3, err := armariaapi.AddBook("https://three.com", addOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

_, err = nativeMessageLoop(messaging.MessageKindUpdateBook, messaging.UpdateBookPayload{
DB: null.NullStringFrom(db),
ID: book3.ID,
PreviousBook: null.NullStringFrom(book1.ID),
NextBook: null.NullStringFrom(book2.ID),
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}

listOptions := armariaapi.DefaultListBooksOptions()
listOptions.WithBooks(true)
listOptions.WithDB(db)
books, err := armariaapi.ListBooks(listOptions)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

got := lo.Map(books, func(x armaria.Book, _ int) string {
return x.ID
})

want := []string{
book1.ID,
book3.ID,
book2.ID,
}

diff := cmp.Diff(got, want)
if diff != "" {
t.Errorf("Expected and actual orders different:\n%s", diff)
}
}
Loading