Skip to content

Commit

Permalink
test: allow mock api in internal
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 20, 2024
1 parent 4fc1270 commit a39d905
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 97 deletions.
2 changes: 1 addition & 1 deletion internal/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package internal

import (
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/test/mock"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"github.com/stretchr/testify/assert"
"testing"
Expand Down
22 changes: 11 additions & 11 deletions test/client.go → internal/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import (
"context"
"errors"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"net/http"
)

func GetClient(throws bool) api.ClientWithResponsesInterface {
res, _ := NewTestClient(throws)
return res
return NewClient(throws)
}

type TestClient struct {
type Client struct {
api.ClientWithResponsesInterface
Errors bool
}

func NewTestClient(throws bool) (api.ClientWithResponsesInterface, error) {
client := new(TestClient)
func NewClient(throws bool) api.ClientWithResponsesInterface {
client := new(Client)
if throws {
client.Errors = true
}
return client, nil
return client
}
func (c *TestClient) GetParticipationKeysWithResponse(ctx context.Context, reqEditors ...api.RequestEditorFn) (*api.GetParticipationKeysResponse, error) {
func (c *Client) GetParticipationKeysWithResponse(ctx context.Context, reqEditors ...api.RequestEditorFn) (*api.GetParticipationKeysResponse, error) {
httpResponse := http.Response{StatusCode: 200}
clone := Keys
clone := mock.Keys
res := api.GetParticipationKeysResponse{
Body: nil,
HTTPResponse: &httpResponse,
Expand All @@ -42,7 +42,7 @@ func (c *TestClient) GetParticipationKeysWithResponse(ctx context.Context, reqEd
return &res, nil
}

func (c *TestClient) DeleteParticipationKeyByIDWithResponse(ctx context.Context, participationId string, reqEditors ...api.RequestEditorFn) (*api.DeleteParticipationKeyByIDResponse, error) {
func (c *Client) DeleteParticipationKeyByIDWithResponse(ctx context.Context, participationId string, reqEditors ...api.RequestEditorFn) (*api.DeleteParticipationKeyByIDResponse, error) {
httpResponse := http.Response{StatusCode: 200}
res := api.DeleteParticipationKeyByIDResponse{
Body: nil,
Expand All @@ -59,8 +59,8 @@ func (c *TestClient) DeleteParticipationKeyByIDWithResponse(ctx context.Context,
return &res, nil
}

func (c *TestClient) GenerateParticipationKeysWithResponse(ctx context.Context, address string, params *api.GenerateParticipationKeysParams, reqEditors ...api.RequestEditorFn) (*api.GenerateParticipationKeysResponse, error) {
Keys = append(Keys, api.ParticipationKey{
func (c *Client) GenerateParticipationKeysWithResponse(ctx context.Context, address string, params *api.GenerateParticipationKeysParams, reqEditors ...api.RequestEditorFn) (*api.GenerateParticipationKeysResponse, error) {
mock.Keys = append(mock.Keys, api.ParticipationKey{
Address: "",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions internal/test/mock/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mock

import (
"github.com/algorandfoundation/hack-tui/api"
)

var VoteKey = []byte("TESTKEY")
var SelectionKey = []byte("TESTKEY")
var StateProofKey = []byte("TESTKEY")
var Keys = []api.ParticipationKey{
{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "123",
Key: api.AccountParticipation{
SelectionParticipationKey: SelectionKey,
StateProofKey: &StateProofKey,
VoteFirstValid: 0,
VoteKeyDilution: 100,
VoteLastValid: 30000,
VoteParticipationKey: VoteKey,
},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
},
{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "1234",
Key: api.AccountParticipation{
SelectionParticipationKey: nil,
StateProofKey: nil,
VoteFirstValid: 0,
VoteKeyDilution: 100,
VoteLastValid: 30000,
VoteParticipationKey: nil,
},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
},
}
15 changes: 8 additions & 7 deletions ui/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package app

import (
"context"
test2 "github.com/algorandfoundation/hack-tui/test"
"github.com/algorandfoundation/hack-tui/internal/test"
uitest "github.com/algorandfoundation/hack-tui/ui/internal/test"
"net/http"
"testing"
"time"
Expand All @@ -14,8 +15,8 @@ func Intercept(ctx context.Context, req *http.Request) error {
}

func Test_GenerateCmd(t *testing.T) {
client := test2.GetClient(false)
fn := GenerateCmd("ABC", time.Second*60, test2.GetState(client))
client := test.GetClient(false)
fn := GenerateCmd("ABC", time.Second*60, uitest.GetState(client))
res := fn()
evt, ok := res.(ModalEvent)
if !ok {
Expand All @@ -25,8 +26,8 @@ func Test_GenerateCmd(t *testing.T) {
t.Error("Expected InfoModal")
}

client = test2.GetClient(true)
fn = GenerateCmd("ABC", time.Second*60, test2.GetState(client))
client = test.GetClient(true)
fn = GenerateCmd("ABC", time.Second*60, uitest.GetState(client))
res = fn()
evt, ok = res.(ModalEvent)
if !ok {
Expand All @@ -39,7 +40,7 @@ func Test_GenerateCmd(t *testing.T) {
}

func Test_EmitDeleteKey(t *testing.T) {
client := test2.GetClient(false)
client := test.GetClient(false)
fn := EmitDeleteKey(context.Background(), client, "ABC")
res := fn()
evt, ok := res.(DeleteFinished)
Expand All @@ -53,7 +54,7 @@ func Test_EmitDeleteKey(t *testing.T) {
t.Error("Expected no errors")
}

client = test2.GetClient(true)
client = test.GetClient(true)
fn = EmitDeleteKey(context.Background(), client, "ABC")
res = fn()
evt, ok = res.(DeleteFinished)
Expand Down
46 changes: 3 additions & 43 deletions test/fixtures.go → ui/internal/test/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,10 @@ import (
"context"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/test/mock"
mock2 "github.com/algorandfoundation/hack-tui/internal/test/mock"
"time"
)

var VoteKey = []byte("TESTKEY")
var SelectionKey = []byte("TESTKEY")
var StateProofKey = []byte("TESTKEY")
var Keys = []api.ParticipationKey{
{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "123",
Key: api.AccountParticipation{
SelectionParticipationKey: SelectionKey,
StateProofKey: &StateProofKey,
VoteFirstValid: 0,
VoteKeyDilution: 100,
VoteLastValid: 30000,
VoteParticipationKey: VoteKey,
},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
},
{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "1234",
Key: api.AccountParticipation{
SelectionParticipationKey: nil,
StateProofKey: nil,
VoteFirstValid: 0,
VoteKeyDilution: 100,
VoteLastValid: 30000,
VoteParticipationKey: nil,
},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
},
}

func GetState(client api.ClientWithResponsesInterface) *internal.StateModel {
sm := &internal.StateModel{
Status: internal.StatusModel{
Expand All @@ -70,14 +30,14 @@ func GetState(client api.ClientWithResponsesInterface) *internal.StateModel {
LastTX: 0,
},
Accounts: nil,
ParticipationKeys: &Keys,
ParticipationKeys: &mock2.Keys,
Admin: false,
Watching: false,
Client: client,
Context: context.Background(),
}
values := make(map[string]internal.Account)
clock := new(mock.Clock)
clock := new(mock2.Clock)
for _, key := range *sm.ParticipationKeys {
val, ok := values[key.Address]
if !ok {
Expand Down
17 changes: 9 additions & 8 deletions ui/modal/modal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package modal
import (
"bytes"
"errors"
"github.com/algorandfoundation/hack-tui/test"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"github.com/algorandfoundation/hack-tui/ui/app"
"github.com/algorandfoundation/hack-tui/ui/internal/test"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
Expand All @@ -24,31 +25,31 @@ func Test_Snapshot(t *testing.T) {
})
t.Run("InfoModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetType(app.InfoModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("ConfirmModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetType(app.ConfirmModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("ExceptionModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetType(app.ExceptionModal)
model, _ = model.HandleMessage(errors.New("test error"))
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("GenerateModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetAddress("ABC")
model.SetType(app.GenerateModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
Expand All @@ -58,7 +59,7 @@ func Test_Snapshot(t *testing.T) {

t.Run("TransactionModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetActive(true)
model.SetType(app.TransactionModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
Expand All @@ -69,7 +70,7 @@ func Test_Snapshot(t *testing.T) {

func Test_Messages(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&test.Keys[0])
model.SetKey(&mock.Keys[0])
model.SetAddress("ABC")
model.SetType(app.InfoModal)
tm := teatest.NewTestModel(
Expand Down Expand Up @@ -113,7 +114,7 @@ func Test_Messages(t *testing.T) {

tm.Send(app.DeleteFinished{
Err: nil,
Id: test.Keys[0].Id,
Id: mock.Keys[0].Id,
})

delError := errors.New("Something went wrong")
Expand Down
9 changes: 5 additions & 4 deletions ui/modals/confirm/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package confirm

import (
"bytes"
"github.com/algorandfoundation/hack-tui/test"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"github.com/algorandfoundation/hack-tui/ui/internal/test"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
Expand All @@ -16,7 +17,7 @@ func Test_New(t *testing.T) {
if m.ActiveKey != nil {
t.Errorf("expected ActiveKey to be nil")
}
m.ActiveKey = &test.Keys[0]
m.ActiveKey = &mock.Keys[0]
// Handle Delete
m, cmd := m.HandleMessage(tea.KeyMsg{
Type: tea.KeyRunes,
Expand All @@ -35,7 +36,7 @@ func Test_Snapshot(t *testing.T) {
})
t.Run("Visible", func(t *testing.T) {
model := New(test.GetState(nil))
model.ActiveKey = &test.Keys[0]
model.ActiveKey = &mock.Keys[0]
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
Expand All @@ -45,7 +46,7 @@ func Test_Snapshot(t *testing.T) {
func Test_Messages(t *testing.T) {
// Create the Model
m := New(test.GetState(nil))
m.ActiveKey = &test.Keys[0]
m.ActiveKey = &mock.Keys[0]
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(80, 40),
Expand Down
2 changes: 1 addition & 1 deletion ui/modals/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generate

import (
"bytes"
"github.com/algorandfoundation/hack-tui/test"
"github.com/algorandfoundation/hack-tui/ui/internal/test"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
Expand Down
13 changes: 7 additions & 6 deletions ui/modals/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package info

import (
"bytes"
"github.com/algorandfoundation/hack-tui/test"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"github.com/algorandfoundation/hack-tui/ui/internal/test"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
Expand All @@ -16,10 +17,10 @@ func Test_New(t *testing.T) {
if m == nil {
t.Fatal("New returned nil")
}
m.Participation = &test.Keys[0]
account := m.State.Accounts[test.Keys[0].Address]
m.Participation = &mock.Keys[0]
account := m.State.Accounts[mock.Keys[0].Address]
account.Status = "Online"
m.State.Accounts[test.Keys[0].Address] = account
m.State.Accounts[mock.Keys[0].Address] = account
m.Active = true
m.UpdateState()
if m.BorderColor != "1" {
Expand All @@ -32,7 +33,7 @@ func Test_New(t *testing.T) {
func Test_Snapshot(t *testing.T) {
t.Run("Visible", func(t *testing.T) {
model := New(test.GetState(nil))
model.Participation = &test.Keys[0]
model.Participation = &mock.Keys[0]
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
Expand All @@ -46,7 +47,7 @@ func Test_Snapshot(t *testing.T) {
func Test_Messages(t *testing.T) {
// Create the Model
m := New(test.GetState(nil))
m.Participation = &test.Keys[0]
m.Participation = &mock.Keys[0]

tm := teatest.NewTestModel(
t, m,
Expand Down
Loading

0 comments on commit a39d905

Please sign in to comment.