package testci_test import ( "encoding/json" "os" "regexp" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider/claude" "amadan.net/oficina/equipo/internal/runner/provider/codex" "amadan.net/oficina/equipo/internal/runner/provider/opencode" "amadan.net/oficina/equipo/internal/runner/provider/pi" ) // The CI box fetches the pinned programs by the digests in // scripts/ci-box.json. They must be the ones the tests check: the // certified pins on amd64 and the arm64 pins beside them. func TestBoxPinsMatchTheCode(t *testing.T) { raw, err := os.ReadFile("../../scripts/ci-box.json") if err != nil { t.Fatal(err) } var box struct { Image string Tools map[string]map[string]json.RawMessage } if err = json.Unmarshal(raw, &box); err != nil { t.Fatal(err) } if !regexp.MustCompile(`@sha256:[a-f0-9]{64}$`).MatchString(box.Image) { t.Errorf("the box image %q is not pinned by digest", box.Image) } digest := regexp.MustCompile(`^[a-f0-9]{64}$`) for _, arch := range []string{"amd64", "arm64"} { executable, host, ok := codex.ReleasePins(arch) pin, okClaude := claude.ReleasePin(arch) if !ok || !okClaude { t.Fatalf("no pins for %s", arch) } want := map[string]string{"codex": executable, "codex-code-mode-host": host, "claude": pin, "bwrap": ""} for name, sum := range want { var entry struct{ URL, SHA256 string } if err = json.Unmarshal(box.Tools[name][arch], &entry); err != nil { t.Errorf("%s %s: %v", name, arch, err) continue } if !strings.HasPrefix(entry.URL, "https://") || !digest.MatchString(entry.SHA256) { t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q", name, arch, entry.URL, entry.SHA256) } if sum != "" && entry.SHA256 != sum { t.Errorf("%s %s: the box fetches %s, the code pins %s", name, arch, entry.SHA256, sum) } } } // Every other program the box fetches is pinned for both architectures too. for name, tool := range box.Tools { for _, arch := range []string{"amd64", "arm64"} { var entry struct{ URL, SHA256 string } if err = json.Unmarshal(tool[arch], &entry); err != nil || !strings.HasPrefix(entry.URL, "https://") || !digest.MatchString(entry.SHA256) { t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q (%v)", name, arch, entry.URL, entry.SHA256, err) } } } } // The box's Chromium is a tree of Debian packages (scripts/ci-chromium.json, // written by scripts/ci-chromium-pin.py): every one fetched by an https URL // and checked by SHA-256, on both architectures, and the headless shell // among them at the pinned version. func TestBoxChromiumPinned(t *testing.T) { raw, err := os.ReadFile("../../scripts/ci-chromium.json") if err != nil { t.Fatal(err) } var pinned map[string]json.RawMessage if err = json.Unmarshal(raw, &pinned); err != nil { t.Fatal(err) } var version string if err = json.Unmarshal(pinned["version"], &version); err != nil || version == "" { t.Fatalf("no version: %v", err) } digest := regexp.MustCompile(`^[a-f0-9]{64}$`) for _, arch := range []string{"amd64", "arm64"} { var packages []struct{ Package, Version, URL, SHA256 string } if err = json.Unmarshal(pinned[arch], &packages); err != nil || len(packages) == 0 { t.Fatalf("%s: no packages (%v)", arch, err) } shell := false for _, p := range packages { if !strings.HasPrefix(p.URL, "https://") || !digest.MatchString(p.SHA256) { t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q", arch, p.Package, p.URL, p.SHA256) } shell = shell || (p.Package == "chromium-headless-shell" && p.Version == version) } if !shell { t.Errorf("%s: no chromium-headless-shell %s among the pinned packages", arch, version) } } } // The CI box installs the pinned Pi for the real-Pi tests: Node by the // archive and program digests the code pins, and the tree the lockfile // makes, checked against the committed tree digest. They are the code's. func TestBoxPiMatchesTheCode(t *testing.T) { raw, err := os.ReadFile("../../scripts/ci-box.json") if err != nil { t.Fatal(err) } var box struct { Pi struct { Version, Lockfile, Box string TreeSHA256 string `json:"tree_sha256"` Node map[string]struct { URL, Root, SHA256 string ArchiveSHA256 string `json:"archive_sha256"` } } } if err = json.Unmarshal(raw, &box); err != nil { t.Fatal(err) } if box.Pi.Version != pi.Version || box.Pi.Lockfile != "internal/runner/provider/pi/npm" || box.Pi.Box != "/opt/equipo-pi" { t.Fatalf("pi %+v", box.Pi) } for _, arch := range []string{"amd64", "arm64"} { tree, node, ok := pi.Pins(arch) got := box.Pi.Node[arch] if !ok || box.Pi.TreeSHA256 != tree || got.URL != node.URL || got.ArchiveSHA256 != node.SHA256 || got.SHA256 != node.BinarySHA256 || got.Root != node.Root { t.Errorf("%s: the box installs %+v and tree %s, the code pins %+v and tree %s", arch, got, box.Pi.TreeSHA256, node, tree) } } } // The CI box installs the pinned OpenCode for the real-OpenCode tests: // the two packages the lockfile names, by the lockfile's integrities, the // tree and the binary checked against the committed digests. They are // the code's. func TestBoxOpenCodeMatchesTheCode(t *testing.T) { raw, err := os.ReadFile("../../scripts/ci-box.json") if err != nil { t.Fatal(err) } type pkg struct { Package, URL, Integrity string BinarySHA256 string `json:"binary_sha256"` TreeSHA256 string `json:"tree_sha256"` } var box struct { OpenCode struct { Version, Lockfile, Box string CLI pkg Platforms map[string]pkg } } if err = json.Unmarshal(raw, &box); err != nil { t.Fatal(err) } oc := box.OpenCode cli, ok := opencode.Lock(opencode.PackageName) if oc.Version != opencode.Version || oc.Lockfile != "internal/runner/provider/opencode/npm" || oc.Box != "/opt/equipo-opencode" || !ok || oc.CLI.URL != cli.Resolved || oc.CLI.Integrity != cli.Integrity || cli.Integrity != opencode.Integrity { t.Fatalf("opencode %+v", oc) } for _, arch := range []string{"amd64", "arm64"} { tree, platform, ok := opencode.Pins(arch) lock, locked := opencode.Lock(platform.Package) got := oc.Platforms[arch] if !ok || !locked || got.Package != platform.Package || got.URL != lock.Resolved || got.Integrity != lock.Integrity || got.BinarySHA256 != platform.BinarySHA256 || got.TreeSHA256 != tree { t.Errorf("%s: the box installs %+v, the code pins %+v and tree %s", arch, got, platform, tree) } } }