package thin import ( "context" "crypto/sha256" "encoding/json" "fmt" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/render" "amadan.net/oficina/equipo/internal/wire" ) // TestToolHealthReportsTheScannerPin (stuck tasks, protocol 30): the // capability report says whether the preview scanner is installed and is // the pinned build, as the scan itself checks it: one fixed state, never // its path or digest. func TestToolHealthReportsTheScannerPin(t *testing.T) { dir := t.TempDir() binary := filepath.Join(dir, "gitleaks") if err := os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0o700); err != nil { t.Fatal(err) } raw, _ := os.ReadFile(binary) pin := fmt.Sprintf("%x", sha256.Sum256(raw)) other := fmt.Sprintf("%x", sha256.Sum256([]byte("another build"))) old := previewScannerTool t.Cleanup(func() { previewScannerTool = old }) report := func(path, want string) wire.Capabilities { previewScannerTool = func(runner.Config) (string, string) { return path, want } return linuxCapabilities(runner.Config{}, func() *wire.PreviewHost { return nil }, nil, nil, nil)(context.Background()) } for _, c := range []struct{ path, pin, want string }{ {binary, pin, wire.ToolHealthy}, {binary, other, wire.ToolMismatch}, {filepath.Join(dir, "absent"), pin, wire.ToolMissing}, {dir, pin, wire.ToolMissing}, {binary, "", wire.ToolUnpinned}, } { caps := report(c.path, c.pin) if caps.Tools == nil || caps.Tools.Scanner != c.want || caps.Tools.Renderer != "" { t.Fatalf("%s pinned %q: %+v", c.path, c.pin, caps.Tools) } if err := wire.ValidCapabilities(caps); err != nil { t.Fatalf("report out of bounds: %v", err) } out, _ := json.Marshal(caps) if strings.Contains(string(out), dir) || strings.Contains(string(out), pin) || strings.Contains(string(out), "gitleaks") { t.Fatalf("the report names the tool's path or pin: %s", out) } } // Not executable is not installed: the scan would refuse it. if err := os.Chmod(binary, 0o600); err != nil { t.Fatal(err) } if caps := report(binary, pin); caps.Tools.Scanner != wire.ToolMismatch && caps.Tools.Scanner != wire.ToolMissing { t.Fatalf("a scanner the scan refuses reported %q", caps.Tools.Scanner) } } // TestHandshakeHelloWithholdsToolHealth: the first hello goes out before // the instance says its protocol, so it never carries the tool health an // older instance's strict decoder refuses; after a protocol 30 welcome // the next hello, sent at once, does; at 29 none ever does. func TestHandshakeHelloWithholdsToolHealth(t *testing.T) { hello := func() wire.Hello { return wire.Hello{Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagLinux}, Simulators: []wire.Simulator{}, Tools: &wire.ToolHealth{Scanner: wire.ToolMissing}}} } for _, protocol := range []int{wire.ProtocolPinnedRelease, wire.ProtocolStuckTasks} { inst := newFakeInstance(t) link, _, _, _ := startLinkWith(t, inst.url, &stubCoordinator{}, hello) c := <-inst.conns _, raw := read(t, c, wire.FrameHello) if strings.Contains(string(raw), `"tools"`) || !strings.Contains(string(raw), `"linux"`) { t.Fatalf("handshake hello: %s", raw) } welcome, _ := json.Marshal(wire.Frame{ID: "w-t", Type: wire.FrameWelcome, Data: json.RawMessage(`{"generation":3,"runner_id":"RUNNER1","protocol":` + itoa(int64(protocol)) + `,"role":"primary"}`)}) write(t, c, welcome) read(t, c, wire.FrameSync) if protocol < wire.ProtocolStuckTasks { link.HelloSoon() } _, raw = read(t, c, wire.FrameHello) if got := strings.Contains(string(raw), `"tools":{"scanner":"missing"}`); got != (protocol >= wire.ProtocolStuckTasks) { t.Fatalf("protocol %d hello: %s", protocol, raw) } } } // TestToolHealthReportsTheRenderer (task 8): on Linux amd64 the // capability report carries the page renderer's health, ok only once the // tree verified and rendered the fixture page; a runner without one (a // Mac, Linux arm64) sends no renderer field at all. func TestToolHealthReportsTheRenderer(t *testing.T) { for _, p := range [][2]string{{"darwin", "arm64"}, {"linux", "arm64"}} { if h := rendererHealth(runner.Config{Executable: "/opt/equipo/runner"}, p[0], p[1], strings.Repeat("a", 64), nil); h != nil { t.Fatalf("%s/%s has a renderer: %+v", p[0], p[1], h) } } h := rendererHealth(runner.Config{Executable: "/opt/equipo/runner", BwrapPath: "/usr/bin/bwrap"}, "linux", "amd64", strings.Repeat("a", 64), nil) if h == nil || h.Config.Tree != "/opt/equipo/renderer/aaaaaaaaaaaa" || h.Config.Binary != "/opt/equipo/runner" || h.Config.Bwrap != "/usr/bin/bwrap" || h.Scratch != "/opt/equipo/renderer" { t.Fatalf("linux/amd64 renderer: %+v", h) } caps := func(h *render.Health) wire.Capabilities { return linuxCapabilities(runner.Config{}, func() *wire.PreviewHost { return nil }, nil, nil, h)(context.Background()) } none := caps(nil) if out, _ := json.Marshal(none); none.Tools == nil || none.Tools.Renderer != "" || strings.Contains(string(out), "renderer") { t.Fatalf("no renderer reported: %s", out) } tree := filepath.Join(t.TempDir(), "renderer", "aaaaaaaaaaaa") if err := os.MkdirAll(tree, 0o755); err != nil { t.Fatal(err) } for _, c := range []struct { verify, fixture error want string }{ {nil, nil, wire.ToolHealthy}, {nil, fmt.Errorf("no namespaces"), wire.ToolMissing}, {&release.RendererError{Health: wire.ToolMismatch, Reason: "tampered"}, nil, wire.ToolMismatch}, {&release.RendererError{Health: wire.ToolUnpinned, Reason: "no pin"}, nil, wire.ToolUnpinned}, {&release.RendererError{Health: wire.ToolMissing, Reason: "absent"}, nil, wire.ToolMissing}, } { h := &render.Health{Config: render.LaunchConfig{Tree: tree}, Verify: func(string, string) error { return c.verify }, Fixture: func(context.Context, render.LaunchConfig, string) error { return c.fixture }} h.Start(context.Background()) _, _, _ = h.Ready(context.Background()) got := caps(h) if got.Tools == nil || got.Tools.Renderer != c.want || wire.ValidCapabilities(got) != nil { t.Fatalf("verify %v, fixture %v: %+v", c.verify, c.fixture, got.Tools) } if out, _ := json.Marshal(got); strings.Contains(string(out), tree) { t.Fatalf("the report names the tree: %s", out) } } } // TestStatusSaysABadRendererNeedsTheRunnerUpdated: status's description // tells the coordinator what runner.tools.renderer means and that a bad // renderer needs the runner updated, never another try. func TestStatusSaysABadRendererNeedsTheRunnerUpdated(t *testing.T) { for _, tool := range converseTools { m := tool.(map[string]any) if m["name"] != "status" { continue } d := m["description"].(string) if !strings.Contains(d, "runner.tools.renderer") || !strings.Contains(d, "anything but ok needs the runner updated, not another try") { t.Fatalf("status's description: %s", d) } return } t.Fatal("no status tool") }