diff --git a/internal/terminal/handler/handler.go b/internal/terminal/handler/handler.go index 6bb387f..94262bc 100644 --- a/internal/terminal/handler/handler.go +++ b/internal/terminal/handler/handler.go @@ -230,9 +230,8 @@ func (m *model) syncTerminal(msg tea.Msg) { if !m.terminalSizeReady { m.viewport = viewport.New(msg.Width, msg.Height) - m.viewport.YPosition = headerHeight - m.terminalSizeReady = true m.viewport.YPosition = headerHeight + 1 + m.terminalSizeReady = true } else { m.viewport.Width = msg.Width m.viewport.Height = msg.Height diff --git a/internal/terminal/handler/information/information.go b/internal/terminal/handler/information/information.go index d035e82..ebf488e 100644 --- a/internal/terminal/handler/information/information.go +++ b/internal/terminal/handler/information/information.go @@ -48,7 +48,7 @@ const ( ) var ( - gamaVersion string + currentVersion string newVersionAvailableMsg string applicationDescription string ) @@ -72,8 +72,8 @@ func SetupModelInfo(githubUseCase gu.UseCase, versionUseCase vu.UseCase, lockTab } func (m *ModelInfo) Init() tea.Cmd { - gamaVersion = m.versionUseCase.CurrentVersion() - applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", gamaVersion) + currentVersion = m.versionUseCase.CurrentVersion() + applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", currentVersion) go m.testConnection(context.Background()) go m.checkUpdates(context.Background()) @@ -81,7 +81,7 @@ func (m *ModelInfo) Init() tea.Cmd { } func (m *ModelInfo) checkUpdates(ctx context.Context) { - isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable() + isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable(ctx) if err != nil { m.modelError.SetError(err) m.modelError.SetErrorMessage("failed to check updates") diff --git a/internal/version/repository/ports.go b/internal/version/repository/ports.go index b980393..9562eb4 100644 --- a/internal/version/repository/ports.go +++ b/internal/version/repository/ports.go @@ -1,6 +1,8 @@ package repository +import "context" + type Repository interface { CurrentVersion() string - LatestVersion() (string, error) + LatestVersion(ctx context.Context) (string, error) } diff --git a/internal/version/repository/repository.go b/internal/version/repository/repository.go index c7b5655..8dcf9d2 100644 --- a/internal/version/repository/repository.go +++ b/internal/version/repository/repository.go @@ -36,18 +36,19 @@ func (r *Repo) CurrentVersion() string { return r.currentVersion } -func (r *Repo) LatestVersion() (string, error) { +func (r *Repo) LatestVersion(ctx context.Context) (string, error) { var result struct { TagName string `json:"tag_name"` } - err := r.do(context.Background(), nil, &result, requestOptions{ + err := r.do(ctx, nil, &result, requestOptions{ method: "GET", path: fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo), accept: "application/vnd.github+json", }) // client time out error - if errors.As(err, &context.DeadlineExceeded) { + var deadlineExceededError *url.Error + if errors.As(err, &deadlineExceededError) && deadlineExceededError.Timeout() { return "", errors.New("request timed out") } else if err != nil { return "", err diff --git a/internal/version/repository/repository_test.go b/internal/version/repository/repository_test.go index 8fa041f..b70f3e0 100644 --- a/internal/version/repository/repository_test.go +++ b/internal/version/repository/repository_test.go @@ -1,7 +1,10 @@ package repository import ( + "context" "testing" + + "github.com/stretchr/testify/assert" ) func NewRepository() Repository { @@ -9,10 +12,12 @@ func NewRepository() Repository { } func TestRepo_LatestVersion(t *testing.T) { + ctx := context.Background() repo := NewRepository() - _, err := repo.LatestVersion() - if err != nil { - t.Error(err) - } + t.Run("Get latest version", func(t *testing.T) { + res, err := repo.LatestVersion(ctx) + assert.NoError(t, err) + assert.NotEmpty(t, res) + }) } diff --git a/internal/version/usecase/ports.go b/internal/version/usecase/ports.go index 63b07aa..e282386 100644 --- a/internal/version/usecase/ports.go +++ b/internal/version/usecase/ports.go @@ -1,6 +1,8 @@ package usecase +import "context" + type UseCase interface { CurrentVersion() string - IsUpdateAvailable() (isAvailable bool, version string, err error) + IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) } diff --git a/internal/version/usecase/usecase.go b/internal/version/usecase/usecase.go index 2e537f8..a84bebf 100644 --- a/internal/version/usecase/usecase.go +++ b/internal/version/usecase/usecase.go @@ -1,6 +1,7 @@ package usecase import ( + "context" "github.com/Masterminds/semver/v3" vr "github.com/termkit/gama/internal/version/repository" @@ -20,13 +21,13 @@ func (u *useCase) CurrentVersion() string { return u.versionRepository.CurrentVersion() } -func (u *useCase) IsUpdateAvailable() (isAvailable bool, version string, err error) { +func (u *useCase) IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) { currentVersion := u.versionRepository.CurrentVersion() if currentVersion == "under development" { return false, currentVersion, nil } - latestVersion, err := u.versionRepository.LatestVersion() + latestVersion, err := u.versionRepository.LatestVersion(ctx) if err != nil { return false, currentVersion, err }