Archived
Offer "quit and update" in the footer, check for updates on every start
The update check ran at most once a day and only printed a line after the TUI exited. Now the registry is asked on every start (in the background, 3 s timeout, no cache) and the result reaches the TUI as a message. With a newer release, the footer shows `q quit and update to vX` right of help and quit, on a highlighted background (Theme.Highlight). `q` quits and `main` installs the release once the terminal is restored, sharing the code of `submarine update`; plain quit moves to `Q`, ctrl+c never updates. Drop LatestCached and CheckInterval. Add D27 and F58. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"submarine/internal/submodule/mock"
|
||||
)
|
||||
|
||||
func newUpdateModel(version string) tea.Model {
|
||||
check := func(context.Context) string { return version }
|
||||
var m tea.Model = New(context.Background(), mock.New(), WithUpdateCheck(check))
|
||||
m, _ = m.Update(tea.WindowSizeMsg{Width: 120, Height: 30})
|
||||
return m
|
||||
}
|
||||
|
||||
func isQuit(cmd tea.Cmd) bool {
|
||||
if cmd == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := cmd().(tea.QuitMsg)
|
||||
return ok
|
||||
}
|
||||
|
||||
func TestUpdateCheckDeliversMessage(t *testing.T) {
|
||||
m := New(context.Background(), mock.New(), WithUpdateCheck(func(context.Context) string { return "v9.9.9" }))
|
||||
if m.lookForUpdate() == nil {
|
||||
t.Fatal("no update check was started")
|
||||
}
|
||||
if msg := m.lookForUpdate()(); msg != (updateMsg{"v9.9.9"}) {
|
||||
t.Errorf("got %v", msg)
|
||||
}
|
||||
if New(context.Background(), mock.New()).lookForUpdate() != nil {
|
||||
t.Error("an update check was started without the option")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuitWithoutUpdate(t *testing.T) {
|
||||
m := newUpdateModel("")
|
||||
m, _ = m.Update(updateMsg{""})
|
||||
m, cmd := press(m, "q")
|
||||
if !isQuit(cmd) || m.(Model).UpdateTo() != "" {
|
||||
t.Errorf("q must quit without updating, got update to %q", m.(Model).UpdateTo())
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuitAndUpdate(t *testing.T) {
|
||||
m := newUpdateModel("v9.9.9")
|
||||
m, _ = m.Update(updateMsg{"v9.9.9"})
|
||||
if bar := m.(Model).footer.View(); !strings.Contains(bar, "q quit and update to v9.9.9") {
|
||||
t.Errorf("the footer does not offer the update:\n%s", bar)
|
||||
}
|
||||
m2, cmd := press(m, "q")
|
||||
if !isQuit(cmd) || m2.(Model).UpdateTo() != "v9.9.9" {
|
||||
t.Errorf("q must quit and update, got update to %q", m2.(Model).UpdateTo())
|
||||
}
|
||||
m2, cmd = press(m, "Q")
|
||||
if !isQuit(cmd) || m2.(Model).UpdateTo() != "" {
|
||||
t.Errorf("Q must quit without updating, got update to %q", m2.(Model).UpdateTo())
|
||||
}
|
||||
m2, cmd = pressKey(m, tea.KeyCtrlC)
|
||||
if !isQuit(cmd) || m2.(Model).UpdateTo() != "" {
|
||||
t.Errorf("ctrl+c must quit without updating, got update to %q", m2.(Model).UpdateTo())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user