-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: deck separate file, rename Hint.Hint to Hint.Value
- Loading branch information
1 parent
7f48508
commit c30d3a0
Showing
12 changed files
with
261 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package hintview | ||
|
||
import ( | ||
"fmt" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
|
||
"github.com/six78/2-story-points-cli/internal/config" | ||
"github.com/six78/2-story-points-cli/internal/view/components/voteview" | ||
"github.com/six78/2-story-points-cli/internal/view/messages" | ||
"github.com/six78/2-story-points-cli/pkg/protocol" | ||
) | ||
|
||
var ( | ||
headerStyle = lipgloss.NewStyle() // .Foreground(lipgloss.Color("#FAFAFA")) | ||
acceptableStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00FF00")) | ||
unacceptableStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF0000")) | ||
textStyle = lipgloss.NewStyle() // .Foreground(lipgloss.Color("#FAFAFA")) | ||
MentionStyle = textStyle.Copy().Italic(true).Foreground(config.UserColor) | ||
) | ||
|
||
type Model struct { | ||
hint *protocol.Hint | ||
} | ||
|
||
func New() Model { | ||
return Model{ | ||
hint: nil, | ||
} | ||
} | ||
|
||
func (m Model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case messages.GameStateMessage: | ||
if msg.State == nil || !msg.State.VotesRevealed { | ||
m.hint = nil | ||
break | ||
} | ||
|
||
issue := msg.State.Issues.Get(msg.State.ActiveIssue) | ||
if issue != nil { | ||
m.hint = issue.Hint | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m Model) View() string { | ||
if m.hint == nil { | ||
return "" | ||
} | ||
|
||
verdictStyle := unacceptableStyle | ||
verdictText := "x" | ||
if m.hint.Acceptable { | ||
verdictStyle = acceptableStyle | ||
verdictText = "✓" | ||
} | ||
|
||
rejectionReason := "" | ||
if !m.hint.Acceptable { | ||
rejectionReason = fmt.Sprintf(" (%s)", textStyle.Render(m.hint.RejectReason)) | ||
} | ||
|
||
return lipgloss.JoinVertical(lipgloss.Top, | ||
"", | ||
headerStyle.Render("Recommended:")+""+voteview.Render(m.hint.Value), | ||
headerStyle.Render("Acceptable:")+" "+verdictStyle.Render(verdictText)+rejectionReason, | ||
headerStyle.Render("What to do:")+" "+textStyle.Render(m.hint.Advice), | ||
"", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package hintview | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/six78/2-story-points-cli/internal/view/messages" | ||
"github.com/six78/2-story-points-cli/pkg/protocol" | ||
) | ||
|
||
func TestInit(t *testing.T) { | ||
model := New() | ||
cmd := model.Init() | ||
require.Nil(t, cmd) | ||
require.Nil(t, model.hint) | ||
} | ||
|
||
func TestUpdateNilState(t *testing.T) { | ||
model := New() | ||
_ = model.Init() | ||
model, cmd := model.Update(messages.GameStateMessage{State: nil}) | ||
require.Nil(t, cmd) | ||
require.Nil(t, model.hint) | ||
require.Empty(t, model.View()) | ||
} | ||
|
||
func TestUpdateVotesNotRevealed(t *testing.T) { | ||
model := New() | ||
_ = model.Init() | ||
model, cmd := model.Update(messages.GameStateMessage{ | ||
State: &protocol.State{ | ||
VotesRevealed: false, | ||
}, | ||
}) | ||
require.Nil(t, cmd) | ||
require.Nil(t, model.hint) | ||
require.Empty(t, model.View()) | ||
} | ||
|
||
func TestUpdateAcceptableVote(t *testing.T) { | ||
model := New() | ||
|
||
cmd := model.Init() | ||
require.Nil(t, cmd) | ||
require.Nil(t, model.hint) | ||
|
||
// Test acceptable vote | ||
|
||
testCases := []struct { | ||
name string | ||
acceptable bool | ||
}{ | ||
{ | ||
name: "acceptable vote", | ||
acceptable: true, | ||
}, | ||
{ | ||
name: "non-acceptable vote", | ||
acceptable: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
issue := protocol.Issue{ | ||
ID: protocol.IssueID(gofakeit.UUID()), | ||
Hint: &protocol.Hint{ | ||
Acceptable: tc.acceptable, | ||
Value: protocol.VoteValue(gofakeit.LetterN(5)), | ||
RejectReason: gofakeit.LetterN(10), | ||
}, | ||
} | ||
model, cmd = model.Update(messages.GameStateMessage{ | ||
State: &protocol.State{ | ||
Issues: protocol.IssuesList{&issue}, | ||
ActiveIssue: issue.ID, | ||
VotesRevealed: true, | ||
}, | ||
}) | ||
require.Nil(t, cmd) | ||
require.NotNil(t, model.hint) | ||
require.Equal(t, *issue.Hint, *model.hint) | ||
|
||
expectedVerdict := "✓" | ||
if !tc.acceptable { | ||
expectedVerdict = "x" + fmt.Sprintf(" (%s)", issue.Hint.RejectReason) | ||
} | ||
|
||
expectedLines := []string{ | ||
"", | ||
"Recommended: " + string(issue.Hint.Value), | ||
"Acceptable: " + expectedVerdict, | ||
"What to do:", | ||
"", | ||
} | ||
|
||
lines := strings.Split(model.View(), "\n") | ||
require.Len(t, lines, len(expectedLines)) | ||
|
||
for i, line := range lines { | ||
trimmedLine := strings.Trim(line, " ") | ||
require.Equal(t, expectedLines[i], trimmedLine) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.