Files
skillshare/internal/server/handler_version_test.go
Willie 11add19544 feat(ui): note how Nix updates the CLI in the update dialog (#771)
* feat(ui): show the install's own upgrade command in the update dialog

The update dialog always offered `skillshare upgrade` for the terminal.
On Nix that command only prints advice, and Homebrew users are better
served by `brew upgrade skillshare`, so the right command appeared only
after a click on Update returned an error.

/api/version now detects the install method, reports its command as
upgradeCommand, and looks up the latest version the way the CLI update
notice does for that method. The dialog shows that command, or the
existing "update your Nix configuration and rebuild" text when a Nix
configuration provides skillshare, so no new translations are needed.

* fix(ui): keep `skillshare upgrade` in the update dialog, add only a Nix note

Review of the previous commit found three regressions:

- `skillshare upgrade` already runs `brew upgrade` on Homebrew and also
  updates the built-in skill. Showing `brew upgrade skillshare` (or the
  nix profile command) instead broke the case where only the skill is
  outdated: the shown command updates nothing.
- /api/version looked up the latest version through `brew info` on
  Homebrew, inside the request with no timeout. Homebrew's auto-update
  could stall the dialog, and a server without brew on PATH never showed
  an update.
- The rendered dialog tests only checked shown text, which
  wiki/frontend.md says to leave to screenshots.

The dialog keeps `skillshare upgrade` and the GitHub lookup. /api/version
reports nixUpgradeCommand only for Nix installs, and when a newer CLI is
pending the dialog adds the existing Nix advice under the command. The
decision lives in nixUpdateNote, unit-tested in upgradeError.test.ts.
2026-11-04 16:51:03 +08:00

109 lines
3.7 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
versioncheck "skillshare/internal/version"
)
func TestHandleVersionCheckDevModeExposesSimulatedUpdate(t *testing.T) {
oldVersion := versioncheck.Version
versioncheck.Version = "dev"
defer func() { versioncheck.Version = oldVersion }()
s := &Server{cfg: &config.Config{Source: t.TempDir()}}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/version", nil)
s.handleVersionCheck(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", rec.Code, rec.Body.String())
}
var body map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if body["cliDevMode"] != true || body["cliUpdateAvailable"] != true || body["cliLatest"] == "" {
t.Fatalf("dev version response = %#v", body)
}
}
// versionCheckWithSkill serves /api/version for a source holding the
// built-in skill at local, with remote already in the skill version cache.
func versionCheckWithSkill(t *testing.T, local, remote string) map[string]any {
t.Helper()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
versioncheck.SaveRemoteSkillVersion(remote)
source := t.TempDir()
skillDir := filepath.Join(source, "skillshare")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
skill := "---\nname: skillshare\nmetadata:\n version: " + local + "\n---\n"
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(skill), 0o644); err != nil {
t.Fatal(err)
}
s := &Server{cfg: &config.Config{Source: source}}
rec := httptest.NewRecorder()
s.handleVersionCheck(rec, httptest.NewRequest(http.MethodGet, "/api/version", nil))
var body map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
return body
}
func TestHandleVersionCheckUsesCachedSkillVersion(t *testing.T) {
body := versionCheckWithSkill(t, "1.0.0", "9.9.9")
if body["skillLatest"] != "9.9.9" || body["skillUpdateAvailable"] != true {
t.Fatalf("skill fields = latest %v, update %v; want the cached 9.9.9 offered", body["skillLatest"], body["skillUpdateAvailable"])
}
}
func TestHandleVersionCheckNewerLocalSkillIsNotAnUpdate(t *testing.T) {
body := versionCheckWithSkill(t, "10.0.0", "9.9.9")
if body["skillUpdateAvailable"] != false {
t.Fatalf("skillUpdateAvailable = %v, want false when the local skill is newer", body["skillUpdateAvailable"])
}
}
func TestHandleVersionCheckReportsNixUpgradeCommand(t *testing.T) {
tests := []struct {
method versioncheck.InstallMethod
want any // nil: skillshare upgrade replaces the CLI itself
}{
{method: versioncheck.InstallDirect, want: nil},
{method: versioncheck.InstallBrew, want: nil},
{method: versioncheck.InstallNixProfile, want: "nix profile upgrade skillshare"},
{method: versioncheck.InstallNix, want: ""},
}
oldVersion, oldDetect := versioncheck.Version, detectUIInstallMethod
defer func() { versioncheck.Version, detectUIInstallMethod = oldVersion, oldDetect }()
versioncheck.Version = "dev" // skips the network lookup
for _, tt := range tests {
detectUIInstallMethod = func() versioncheck.InstallMethod { return tt.method }
rec := httptest.NewRecorder()
(&Server{cfg: &config.Config{Source: t.TempDir()}}).handleVersionCheck(rec, httptest.NewRequest(http.MethodGet, "/api/version", nil))
var body map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if body["nixUpgradeCommand"] != tt.want {
t.Errorf("method %v: nixUpgradeCommand = %#v, want %#v", tt.method, body["nixUpgradeCommand"], tt.want)
}
}
}