A Claude subscription signs in and answers through the local `claude`, so
on a server or in the Docker image, which has none, it couldn't be used
(Jorben on Discord: could it be downloaded on first use, as Bun is).
- internal/claudecode: Anthropic's native build, as claude.ai/install.sh
takes it: downloads.claude.ai/claude-code-releases/latest names the
version, its manifest.json the platform's SHA-256 and size (linux
glibc/musl, darwin, win32). A build that doesn't match both is not kept.
It goes to <cache>/claude-code/<version>/claude, one version kept. Find
takes the machine's own claude first, then the download.
- magpie claude-code [install [--yes]|remove]: says the version, platform,
size, the URL it comes from, where it goes, that its checksum is checked
and that it is Anthropic's software under Anthropic's terms, then asks
[y/N]. magpie accounts add claude offers it when there is no Claude Code
(not on Windows, where one in WSL may be the one).
- the gateway's claudeBinary and the sign-in's claudeExecutable take
claudecode.Find; their "not installed" errors name the command.
- Docker docs (en, zh, ja): Claude sign-in needs `docker exec -it magpie
magpie claude-code install` first; kept in /config/cache.
The Dockerfile is unchanged: distroless cc-debian12 has glibc.
Run:
- go test -tags nogui ./... under a temp HOME with pinned caches: all ok
- gofmt -l: none; go vet (darwin), GOOS=linux and GOOS=windows go vet
-tags nogui ./...: clean; darwin, linux, windows builds
- TestClaudeSubscriptionRunsTheDownloadedClaudeCode fails on the old
proc.FindTool lookup ("Claude Code is not installed"); taking the
checksum compare out fails TestInstallRefusesABuildTheManifestDoesntName
- real: magpie claude-code install --yes downloaded Claude Code 2.1.296
darwin-arm64 (230 MB) into a sandbox HOME, checked; it ran --version
- real, in gcr.io/distroless/cc-debian12:nonroot with the image's /config
layout: install downloaded 2.1.296 linux-arm64 (245 MB) to
/config/cache/magpie/claude-code, it ran `2.1.296 (Claude Code)` there,
and magpie accounts add claude went on to print the claude.ai sign-in
link and ask for the pasted address
- the claudecode tests built for linux/arm64 pass in debian:12-slim
215 lines
6.6 KiB
Go
215 lines
6.6 KiB
Go
package claudecode
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
func TestMain(m *testing.M) { testenv.Main(m) }
|
|
|
|
// manifest is the shape of downloads.claude.ai's manifest.json for
|
|
// 2.1.287 (fields as Anthropic serves them), with this machine's build's
|
|
// checksum and size those of body.
|
|
func manifest(platform, body string) string {
|
|
sum := sha256.Sum256([]byte(body))
|
|
return fmt.Sprintf(`{
|
|
"version": "2.1.287",
|
|
"manifestSignatureEnforcement": "flag",
|
|
"commit": "3c446a1b98aceb99a6cdee0f84a8bea42f4a8937",
|
|
"buildDate": "2026-10-01T16:26:16Z",
|
|
"platforms": {
|
|
"darwin-arm64": {"binary": "claude", "checksum": "6eab8333fe2121553100d8f40bfada384a3e989b94f947e18ba6677a6fcb41ea", "size": 227827120},
|
|
"linux-x64-musl": {"binary": "claude", "checksum": "ba22ce4b5744c6016e3076ccbd692a059fc4e9f1592f21361859a73938f4e722", "size": 238063704},
|
|
%q: {"binary": %q, "checksum": %q, "size": %d}
|
|
},
|
|
"sdkCompat": {"testedWrapperVersions": ["0.3.247"]}
|
|
}`, platform, exeName(runtime.GOOS), hex.EncodeToString(sum[:]), len(body))
|
|
}
|
|
|
|
// release serves a release as downloads.claude.ai does: latest, the
|
|
// manifest, and the build, served as served (body may differ from what the
|
|
// manifest says).
|
|
func release(t *testing.T, latest, manifestBody, served string) {
|
|
t.Helper()
|
|
platform, err := Platform(runtime.GOOS, runtime.GOARCH, false)
|
|
if err != nil {
|
|
t.Skip(err)
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/latest":
|
|
fmt.Fprint(w, latest)
|
|
case "/2.1.287/manifest.json":
|
|
fmt.Fprint(w, manifestBody)
|
|
case "/2.1.287/" + platform + "/" + exeName(runtime.GOOS):
|
|
fmt.Fprint(w, served)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
old := Base
|
|
Base = srv.URL
|
|
t.Cleanup(func() { Base = old })
|
|
t.Cleanup(func() { _ = Remove() })
|
|
}
|
|
|
|
func thisPlatform(t *testing.T) string {
|
|
p, err := Platform(runtime.GOOS, runtime.GOARCH, false)
|
|
if err != nil {
|
|
t.Skip(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func TestInstallKeepsTheCheckedBuild(t *testing.T) {
|
|
const body = "#!/bin/sh\necho 2.1.287 '(Claude Code)'\n"
|
|
release(t, "2.1.287\n", manifest(thisPlatform(t), body), body)
|
|
// a version downloaded before goes once the new one is in
|
|
old := filepath.Join(Root(), "2.1.200")
|
|
if err := os.MkdirAll(old, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(old, exeName(runtime.GOOS)), []byte("old"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, v := Downloaded(); v != "2.1.200" || p == "" {
|
|
t.Fatalf("Downloaded() = %q, %q; want 2.1.200", p, v)
|
|
}
|
|
|
|
r, err := Latest(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Version != "2.1.287" || r.Size != int64(len(body)) {
|
|
t.Fatalf("Latest() = %+v", r)
|
|
}
|
|
var told int64
|
|
path, err := Install(context.Background(), r, func(n int64) { told = n })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if told != int64(len(body)) {
|
|
t.Errorf("progress told %d bytes, want %d", told, len(body))
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil || string(b) != body {
|
|
t.Fatalf("kept %q (%v), want the build", b, err)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
if st, _ := os.Stat(path); st.Mode()&0o100 == 0 {
|
|
t.Errorf("%s isn't executable: %v", path, st.Mode())
|
|
}
|
|
}
|
|
if p, v := Downloaded(); p != path || v != "2.1.287" {
|
|
t.Errorf("Downloaded() = %q, %q; want %q, 2.1.287", p, v, path)
|
|
}
|
|
if _, err := os.Stat(old); !os.IsNotExist(err) {
|
|
t.Errorf("the older download is still there: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestInstallRefusesABuildTheManifestDoesntName(t *testing.T) {
|
|
const body = "#!/bin/sh\necho 2.1.287\n"
|
|
for name, served := range map[string]string{
|
|
"other bytes": strings.Replace(body, "2.1.287", "2.1.288", 1),
|
|
"cut short": body[:10],
|
|
"longer": body + "x",
|
|
"an error page": "<html>Access denied</html>",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
release(t, "2.1.287", manifest(thisPlatform(t), body), served)
|
|
r, err := Latest(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Install(context.Background(), r, nil); err == nil {
|
|
t.Fatal("a build that isn't the manifest's was kept")
|
|
}
|
|
if p, v := Downloaded(); p != "" {
|
|
t.Errorf("Downloaded() = %q, %q after a failed download", p, v)
|
|
}
|
|
left, _ := filepath.Glob(filepath.Join(Root(), "*", "*"))
|
|
if len(left) > 0 {
|
|
t.Errorf("left behind: %v", left)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLatestSaysWhenNoVersionCame(t *testing.T) {
|
|
// what a region downloads.claude.ai doesn't serve gets in its place
|
|
release(t, "<!doctype html><title>Unavailable</title>", "", "")
|
|
_, err := Latest(context.Background())
|
|
if err == nil || !strings.Contains(err.Error(), "didn't name a version") {
|
|
t.Fatalf("Latest() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLatestWithoutThisPlatformsBuild(t *testing.T) {
|
|
release(t, "2.1.287", `{"version":"2.1.287","platforms":{"plan9-x64":{"binary":"claude","checksum":"`+strings.Repeat("a", 64)+`","size":1}}}`, "")
|
|
if _, err := Latest(context.Background()); err == nil || !strings.Contains(err.Error(), "has no") {
|
|
t.Fatalf("Latest() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPlatformNames(t *testing.T) {
|
|
for _, c := range []struct {
|
|
goos, arch string
|
|
musl bool
|
|
want string
|
|
}{
|
|
{"darwin", "arm64", false, "darwin-arm64"},
|
|
{"darwin", "amd64", false, "darwin-x64"},
|
|
{"linux", "amd64", false, "linux-x64"},
|
|
{"linux", "arm64", false, "linux-arm64"},
|
|
{"linux", "amd64", true, "linux-x64-musl"},
|
|
{"linux", "arm64", true, "linux-arm64-musl"},
|
|
{"windows", "amd64", false, "win32-x64"},
|
|
{"windows", "arm64", false, "win32-arm64"},
|
|
} {
|
|
if got, err := Platform(c.goos, c.arch, c.musl); err != nil || got != c.want {
|
|
t.Errorf("Platform(%s, %s, %v) = %q, %v; want %q", c.goos, c.arch, c.musl, got, err, c.want)
|
|
}
|
|
}
|
|
if _, err := Platform("linux", "386", false); err == nil {
|
|
t.Error("linux/386 named a build")
|
|
}
|
|
}
|
|
|
|
func TestFindTakesTheMachinesOwnFirst(t *testing.T) {
|
|
dir := filepath.Join(Root(), "2.1.287")
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = Remove() })
|
|
got := filepath.Join(dir, exeName(runtime.GOOS))
|
|
if err := os.WriteFile(got, []byte("x"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", t.TempDir())
|
|
if p := Find(); p != got {
|
|
t.Errorf("with none of its own, Find() = %q, want the download %q", p, got)
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
return // a #! script isn't a program there
|
|
}
|
|
own := t.TempDir()
|
|
testenv.Program(t, filepath.Join(own, "claude"), "#!/bin/sh\n")
|
|
t.Setenv("PATH", own)
|
|
if p := Find(); p == got || p == "" {
|
|
t.Errorf("with its own on PATH, Find() = %q, want that one", p)
|
|
}
|
|
}
|