package main import ( "encoding/json" "net/url" "os" "path/filepath" "strings" "testing" ) const testRepository = "https://git.example.org/me/nixos-router" // One check, one preview: the page shows nixos-router and the packages // together, and the preview marks the router's own row. func TestUpgradeShowsRouterAndPackages(t *testing.T) { a, sys := newTestApp(t) dir := t.TempDir() sys.ReleaseFile = filepath.Join(dir, "router-release") sys.RepositoryFile = filepath.Join(dir, "router-update-repository") os.WriteFile(sys.ReleaseFile, []byte(`{"version":"1.0.0","serial":1000}`), 0o600) os.WriteFile(sys.RepositoryFile, []byte(testRepository+"\n"), 0o644) a.sys = sys h := a.routes() cookie := login(t, h) body := do(h, "GET", "/update", nil, cookie).Body.String() for _, want := range []string{testRepository, "Check for upgrades"} { if !strings.Contains(body, want) { t.Fatalf("the page lacks %q:\n%s", want, body) } } if strings.Contains(body, `name="repository"`) { t.Fatalf("the page has a field for the repository:\n%s", body) } if rec := do(h, "POST", "/update/repository", nil, cookie); rec.Code != 404 && rec.Code != 405 { t.Fatalf("the repository can be set from the page: %d", rec.Code) } token := csrfFrom(t, body) do(h, "POST", "/upgrade/channel", url.Values{"csrf": {token}, "channel": {"26.05"}}, cookie) write := func(repo, installed string, newer bool) { os.MkdirAll(sys.upgradeDir(), 0o755) data, _ := json.Marshal(map[string]any{"channel": "26.05", "running": map[string]string{"version": a.sys.runningVersion()}, "time": "2026-11-26T08:00:00Z", "upToDate": !newer, "packagesUpToDate": true, "router": map[string]any{"repository": repo, "installed": installed, "newer": newer, "newest": map[string]string{"tag": "v1.1.0", "version": "1.1.0", "rev": "0123456789abcdef0123456789abcdef01234567"}}}) os.WriteFile(filepath.Join(sys.upgradeDir(), "check.json"), data, 0o644) } write(testRepository, "0.9.0", true) if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "is out") { t.Fatalf("a notice from before the last update:\n%s", body) } write(testRepository, "1.0.0", true) for _, path := range []string{"/", "/update"} { if body := do(h, "GET", path, nil, cookie).Body.String(); !strings.Contains(body, "1.1.0") || (path == "/" && !strings.Contains(body, "nixos-router 1.1.0 is out")) { t.Fatalf("%s lacks the release:\n%s", path, body) } } write(testRepository, "1.0.0", false) if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "nixos-router 1.1.0 is out") { t.Fatalf("a notice when up to date") } // A preview that includes the router: one table, the router's row marked. preview, _ := json.Marshal(map[string]any{"channel": "26.05", "release": "1.0.0", "rev": "", "version": "26.05", "time": "t", "router": map[string]string{"from": "1.0.0", "to": "1.1.0", "tag": "v1.1.0", "rev": "abc"}, "changes": []map[string]string{{"name": "curl", "from": "8.0", "to": "8.1"}}}) os.WriteFile(filepath.Join(sys.upgradeDir(), "preview.json"), preview, 0o644) body = do(h, "GET", "/update", nil, cookie).Body.String() for _, want := range []string{`class="highlight"`, "nixos-router", "curl", "Upgrade nixos-router to 1.1.0 and"} { if !strings.Contains(body, want) { t.Fatalf("the preview lacks %q:\n%s", want, body) } } }