Files
magpie/internal/update/rosetta_test.go
T
yetone 6abacd46c3 update: an Intel magpie under Rosetta updates to the Apple silicon build
cdredfox on Discord saw magpie at 150% of a core. Their goroutine dump
shows AstrLink Guard running in Go (guard.geluRow) in a darwin/amd64
build on an 8-core Mac. That is the Intel build under Rosetta: no
CoreML, and Go's kernels are translated, several times slower than
Apple silicon's build. AppAsset and BinaryAsset named runtime.GOARCH,
so every update fetched the Intel build again, with no way off it.

- internal/rosetta: Translated (sysctl.proc_translated), moved out of
  pii so the updater reads the same thing. pii's translated is now that
  var; its tests still replace it.
- update: arch() is arm64 for darwin/amd64 under Rosetta. AppAsset and
  BinaryAsset (the darwin CLI) use it. goos/goarch/translated are vars
  that tests set.
  Checked before switching: Stage downloads the zip, ditto unpacks it,
  sameSigner wants the same team, and Install renames it over the
  bundle. Nothing there reads the architecture, and the CLI's
  InstallBinary is a rename too. An arm64 build runs natively on every
  Mac that runs Rosetta.
- pii: Status.Rosetta, set on AstrLink Guard's Now() only. Privacy
  Filter's pf-serve is already Apple silicon's there (target).
- GUI: Settings > Privacy's local model line, while AstrLink Guard runs
  under Rosetta, says "On, slowly under Rosetta: the next update
  installs magpie for Apple silicon" in place of "On, on the CPU". It
  is the same one sub-line, with no new element. en/zh/zh-TW/ja/de.

Not done, as decided: skipping tool outputs for the model, or capping
its cores.

Run (on mote unless named):
- go test -count=1 ./internal/update ./internal/rosetta ./internal/pii:
  ok. On WSL magpie-desktop: the same three, plus go vet of them, ok.
  On Windows (xiaomi-gpu): -run "Rosetta|Asset|GuardNow" on update and
  pii, ok.
- go vet ./... (darwin) and CGO_ENABLED=0 GOOS=windows go vet ./...: ok.
  A Linux cross vet needs cgo for Wails, and WSL has no gtk4, so Linux
  vet covered the changed packages only. gofmt -l: clean.
- node --test privacy-model, i18n-keys, i18n-en-no-cjk (chromium +
  webkit, 5 languages, 1100/640px): 91 of 92. The new rosetta case
  passed in every engine, language and width. The one red is "a
  download is cancelled" (chromium). It passes alone, 1/1 at origin/main
  8eb219354 and 2/2 with this change. Filed as #5292 (wheelTo/viewOf
  return "" after a reload).
- go test -run "Budget|I18n|Lang" ./internal/gui: ok. Core scripts are
  1,530,017, under 1,533,802; no raise.

Broken by hand:
- arch() always the build's: TestRosettaUpdatesToAppleSilicon red (4
  cases), and TestRosettaStagesAppleSiliconsApp red, with Intel's
  2.0-amd64 app installed.
- No Rosetta in guardModel.Now(): TestGuardNowSaysRosetta red.
- The app.js line off: privacy-model's rosetta case red ("On, on the
  CPU").

Not tried:
- A real Rosetta process. mote has no Rosetta (no oahd); an x86_64 test
  binary built there didn't start. The translated check is the sysctl
  pii has used since the pf-serve fix, but this commit never saw it
  return 1.
- A real update from an Intel release to an arm64 one. The stage test
  swaps ad-hoc signed fake bundles through the real
  Stage/sameSigner/Install.
2026-11-05 08:25:53 +08:00

105 lines
3.5 KiB
Go

package update
import (
"context"
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// onSystem runs the test as a magpie built for sys/arch, translated or
// not.
func onSystem(t *testing.T, sys, arch string, tr bool) {
t.Helper()
wasOS, wasArch, wasTr := goos, goarch, translated
t.Cleanup(func() { goos, goarch, translated = wasOS, wasArch, wasTr })
goos, goarch, translated = sys, arch, func() bool { return tr }
}
// An Intel magpie under Rosetta updates to Apple silicon's build, the app
// and the terminal build alike; anywhere else an update is the build's
// own architecture.
func TestRosettaUpdatesToAppleSilicon(t *testing.T) {
was := GUI
t.Cleanup(func() { GUI = was })
for _, c := range []struct {
os, arch string
tr, gui bool
app, bin string
}{
{"darwin", "amd64", true, true, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
{"darwin", "amd64", true, false, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
{"darwin", "amd64", false, true, "magpie-darwin-amd64.zip", "magpie-cli-darwin-amd64"},
{"darwin", "arm64", false, true, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
// only macOS has Rosetta: a translated answer elsewhere changes nothing
{"windows", "amd64", true, true, "magpie-darwin-amd64.zip", "magpie-windows-amd64.exe"},
{"linux", "amd64", true, false, "magpie-darwin-amd64.zip", "magpie-cli-linux-amd64"},
{"linux", "arm64", false, true, "magpie-darwin-arm64.zip", "magpie-linux-arm64"},
} {
onSystem(t, c.os, c.arch, c.tr)
GUI = c.gui
if got := AppAsset(); got != c.app {
t.Errorf("%s/%s translated=%v: AppAsset = %s, want %s", c.os, c.arch, c.tr, got, c.app)
}
if got := BinaryAsset(); got != c.bin {
t.Errorf("%s/%s translated=%v gui=%v: BinaryAsset = %s, want %s", c.os, c.arch, c.tr, c.gui, got, c.bin)
}
}
}
// Under Rosetta the update downloads Apple silicon's zip, not Intel's,
// and swaps it in for the Intel app as any update is: signed by the same
// team, renamed over the bundle.
func TestRosettaStagesAppleSiliconsApp(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("ditto and codesign")
}
stubUnregister(t)
onSystem(t, "darwin", "amd64", true)
root := t.TempDir()
zips := map[string][]byte{}
assets := map[string]Asset{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(zips[strings.TrimPrefix(r.URL.Path, "/")])
}))
defer srv.Close()
for _, a := range []string{"amd64", "arm64"} {
src := filepath.Join(root, "src-"+a)
fakeApp(t, filepath.Join(src, "magpie.app"), "2.0-"+a)
name := "magpie-darwin-" + a + ".zip"
zip := filepath.Join(root, name)
cmd := exec.Command("ditto", "-c", "-k", "--keepParent", "magpie.app", zip)
cmd.Dir = src
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("ditto: %v: %s", err, b)
}
body, err := os.ReadFile(zip)
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(body)
zips[name] = body
assets[name] = Asset{URL: srv.URL + "/" + name, SHA256: hex.EncodeToString(sum[:])}
}
bundle := filepath.Join(root, "Applications", "magpie.app")
fakeApp(t, bundle, "1.0-amd64")
staged, err := Stage(context.Background(), &Release{Version: "2.0", Assets: assets}, bundle)
if err != nil {
t.Fatal(err)
}
if err := Install(staged, bundle); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(filepath.Join(bundle, "Contents", "Info.plist"))
if !strings.Contains(string(b), "<string>2.0-arm64</string>") {
t.Errorf("Apple silicon's app isn't in place: %s", b)
}
}