Files
Reims-Go/internal/runtime/remote_test.go
T
NwflowerandClaude Opus 5.5 f6f6bfce34 feat: online runtime install, first-launch data folder picker, source packages
- Runtime: list and fetch runtimes from this repo's GitHub Releases
  (stable by default, candidate on request), with SHA256 check and resume.
  CLI gains `runtime available` and `runtime fetch`; the host page gets an
  online install row and a "receive candidates" switch.
- First launch: pick a data folder (non-system drives first, USB never
  recommended); the choice is recorded in %APPDATA%\reims-go\location.json.
  Existing installs in the default folder are adopted without asking.
- pack-runtime.ps1: also builds the matching source package, records every
  patch with its SHA256 and the channel, and refuses undeclared changes in
  the build trees.
- patches: declare reims-vgpu-cargo-lock.patch (Cargo.lock source lines).
- UI: sidebar and VM page restyle, new design tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 04:11:37 -07:00

155 lines
4.9 KiB
Go

package runtime
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
type fakeGitHub struct {
srv *httptest.Server
archive []byte
sums string
hits map[string]int
}
func newFakeGitHub(t *testing.T) *fakeGitHub {
t.Helper()
g := &fakeGitHub{archive: bytes.Repeat([]byte("reims-runtime-"), 50_000), hits: map[string]int{}}
sum := sha256.Sum256(g.archive)
g.sums = hex.EncodeToString(sum[:]) + " reims-runtime-win-x64-abc1234.tar.zst\n"
mux := http.NewServeMux()
mux.HandleFunc("/repos/o/r/releases", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") != "reims-go" {
t.Errorf("User-Agent = %q", r.Header.Get("User-Agent"))
}
base := g.srv.URL
fmt.Fprintf(w, `[
{"tag_name":"runtime-abc1234","prerelease":true,"published_at":"2026-11-28T10:00:00Z","assets":[
{"name":"reims-runtime-win-x64-abc1234.tar.zst","size":%d,"browser_download_url":"%s/a/archive"},
{"name":"reims-runtime-src-abc1234.tar.zst","size":5,"browser_download_url":"%s/a/src"},
{"name":"SHA256SUMS.txt","size":90,"browser_download_url":"%s/a/sums"}]},
{"tag_name":"runtime-0000old","prerelease":false,"published_at":"2026-10-01T00:00:00Z","assets":[
{"name":"reims-runtime-win-x64-0000old.tar.zst","size":1,"browser_download_url":"%s/a/x"},
{"name":"reims-runtime-src-0000old.tar.zst","size":1,"browser_download_url":"%s/a/x"},
{"name":"SHA256SUMS.txt","size":1,"browser_download_url":"%s/a/x"}]},
{"tag_name":"runtime-nosrc00","prerelease":false,"published_at":"2026-11-01T00:00:00Z","assets":[
{"name":"reims-runtime-win-x64-nosrc00.tar.zst","size":1,"browser_download_url":"%s/a/x"},
{"name":"SHA256SUMS.txt","size":1,"browser_download_url":"%s/a/x"}]},
{"tag_name":"runtime-draft00","draft":true,"assets":[]},
{"tag_name":"v0.1.0","prerelease":false,"assets":[]}
]`, len(g.archive), base, base, base, base, base, base, base, base)
})
mux.HandleFunc("/a/archive", func(w http.ResponseWriter, r *http.Request) {
g.hits["archive"]++
if r.Header.Get("Range") != "" {
g.hits["range"]++
}
http.ServeContent(w, r, "a.tar.zst", time.Time{}, bytes.NewReader(g.archive))
})
mux.HandleFunc("/a/sums", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, g.sums) })
g.srv = httptest.NewServer(mux)
t.Cleanup(g.srv.Close)
return g
}
func (g *fakeGitHub) source() Source {
return Source{APIBase: g.srv.URL, Repo: "o/r", Client: g.srv.Client()}
}
func TestListRemoteFiltersChannelsAndIncompleteReleases(t *testing.T) {
g := newFakeGitHub(t)
stable, err := g.source().ListRemote(context.Background(), false)
if err != nil {
t.Fatal(err)
}
if len(stable) != 1 || stable[0].Commit != "0000old" || stable[0].Channel != ChannelStable {
t.Fatalf("stable only = %+v", stable)
}
all, err := g.source().ListRemote(context.Background(), true)
if err != nil {
t.Fatal(err)
}
if len(all) != 2 || all[0].Commit != "abc1234" || all[0].Channel != ChannelCandidate || all[0].SourceName == "" {
t.Fatalf("with candidate = %+v", all)
}
if r, err := Pick(all, "abc"); err != nil || r.Commit != "abc1234" {
t.Fatalf("pick prefix: %v %v", r, err)
}
if r, _ := Pick(all, "latest"); r.Commit != "abc1234" {
t.Fatalf("latest = %v", r)
}
if _, err := Pick(nil, ""); err == nil {
t.Fatal("empty list")
}
}
func TestDownloadResumesAndVerifies(t *testing.T) {
g := newFakeGitHub(t)
src := g.source()
list, _ := src.ListRemote(context.Background(), true)
r := list[0]
dir := t.TempDir()
// A previous run left the first 100 KB.
part := filepath.Join(dir, r.ArchiveName+".part")
os.WriteFile(part, g.archive[:100_000], 0o644)
var last int64
path, err := src.Download(context.Background(), r, dir, func(done, total int64) {
if total != int64(len(g.archive)) {
t.Errorf("total = %d", total)
}
last = done
})
if err != nil {
t.Fatal(err)
}
if g.hits["range"] != 1 {
t.Fatalf("expected a ranged request, hits=%v", g.hits)
}
if last != int64(len(g.archive)) {
t.Fatalf("last progress = %d", last)
}
got, _ := os.ReadFile(path)
if !bytes.Equal(got, g.archive) {
t.Fatal("archive content differs")
}
if _, err := os.Stat(part); !os.IsNotExist(err) {
t.Fatal(".part left behind")
}
// Already there and valid: no new request.
before := g.hits["archive"]
if _, err := src.Download(context.Background(), r, dir, nil); err != nil {
t.Fatal(err)
}
if g.hits["archive"] != before {
t.Fatal("re-downloaded a verified archive")
}
}
func TestDownloadRejectsAWrongChecksum(t *testing.T) {
g := newFakeGitHub(t)
g.sums = strings.Repeat("0", 64) + " reims-runtime-win-x64-abc1234.tar.zst\n"
list, _ := g.source().ListRemote(context.Background(), true)
dir := t.TempDir()
_, err := g.source().Download(context.Background(), list[0], dir, nil)
if err == nil || !strings.Contains(err.Error(), "SHA256 mismatch") {
t.Fatalf("err = %v", err)
}
entries, _ := os.ReadDir(dir)
if len(entries) != 0 {
t.Fatalf("bad download kept: %v", entries)
}
}