package runner import ( "amadan.net/oficina/equipo/internal/runner/provider" "context" "encoding/json" "os" "path/filepath" "testing" ) func policyInputsFixture(t *testing.T) (provider.Binding, provider.PolicyInputs) { t.Helper() root := t.TempDir() home, work, scratch := filepath.Join(root, "account"), filepath.Join(root, "work"), filepath.Join(root, "scratch") for _, p := range []string{home, work, scratch} { mustJournal(t, os.Mkdir(p, 0700)) } id := provider.Identity{AccountID: "fixture", HomeID: "fixture", ProviderVersion: "0.165.0"} mustJournal(t, os.WriteFile(filepath.Join(home, provider.IdentityFile), marshal(id), 0600)) mustJournal(t, os.WriteFile(filepath.Join(home, "auth.json"), []byte(`{"fixture":true}`), 0600)) l, err := provider.OpenAccount(context.Background(), provider.Binding{ID: "fixture", Provider: "codex", AccountID: id.AccountID, Expected: id, Home: home, RuntimeRoot: filepath.Join(root, "homes")}) mustJournal(t, err) t.Cleanup(func() { l.Close() }) in := provider.PolicyInputs{Workspace: work, Scratch: scratch, ProviderVersion: id.ProviderVersion, LauncherVersion: "direct-v2", BwrapVersion: "0.11.0", Argv: []string{"/opt/codex"}, EgressHosts: []string{"chatgpt.com"}} for _, name := range []string{"codex", "equipo-tool-proxy"} { p := filepath.Join(root, name) mustJournal(t, os.WriteFile(p, []byte(name), 0700)) in.RuntimeFiles = append(in.RuntimeFiles, provider.Mount{Source: p, Target: "/opt/" + name, ReadOnly: true}) } return l.Binding(), in } func TestRuntimeMountsStandaloneToolProxy(t *testing.T) { b, in := policyInputsFixture(t) lease, err := b.AccountLease() mustJournal(t, err) mustJournal(t, lease.Close()) c := configFixture(t) c.GoRoot, c.ModuleCache = t.TempDir(), t.TempDir() c.CodexPath = in.RuntimeFiles[0].Source c.ToolProxyPath = in.RuntimeFiles[1].Source c.CodexCodeModeHostPath = filepath.Join(filepath.Dir(c.ToolProxyPath), "code-mode-host") c.Executable = filepath.Join(filepath.Dir(c.ToolProxyPath), "runner") for _, path := range []string{c.CodexCodeModeHostPath, c.Executable} { mustJournal(t, os.WriteFile(path, []byte(path), 0700)) } for _, path := range []string{c.WorkspaceRoot, c.ScratchRoot} { mustJournal(t, os.Mkdir(path, 0700)) } binding := AccountBinding{ID: b.ID, LocalID: b.ID, Provider: b.Provider, Family: "gpt", Model: "model", AccountID: b.AccountID, ProviderHome: b.CredentialHome, ExpectedIdentity: b.Expected, Kind: "subscription"} mustJournal(t, os.WriteFile(c.AccountBindingsPath, marshal([]AccountBinding{binding}), 0600)) r := Runtime{cfg: c} build := func(t *testing.T) provider.Policy { r.cfg = c source, err := c.DirectBinding(binding) mustJournal(t, err) account, err := provider.OpenAccount(context.Background(), source) mustJournal(t, err) l, cleanup, err := r.constructDirectLease(context.Background(), provider.Selection{Provider: b.Provider, Family: "gpt", Model: "model", AccountID: b.AccountID}, c.WorkspaceRoot, "implement", account) if cleanup != nil { defer cleanup() } mustJournal(t, err) for _, m := range l.Policy.Mounts { if m.Target == "/opt/equipo-tool-proxy" && m.Source != c.ToolProxyPath { t.Fatal("runner executable entered provider policy") } } var concrete struct{ Scratch string } mustJournal(t, json.Unmarshal(l.Policy.Manifest, &concrete)) inputs, err := PolicyInputs(context.Background(), c, l.Selection, l.Binding, c.WorkspaceRoot, "implement", concrete.Scratch) mustJournal(t, err) rebuilt, err := provider.BuildPolicy(l.Binding, inputs) mustJournal(t, err) if rebuilt.Hash != l.Policy.Hash { t.Fatal("the policy built from PolicyInputs differs from the runtime policy") } return l.Policy } for _, project := range []string{"go", "non-go"} { t.Run("missing-modules-"+project, func(t *testing.T) { saved := c.ModuleCache c.ModuleCache = filepath.Join(t.TempDir(), "absent") defer func() { c.ModuleCache = saved }() if project == "go" { mustJournal(t, os.WriteFile(filepath.Join(c.WorkspaceRoot, "go.mod"), []byte("module fixture\n\ngo 1.26\n"), 0600)) defer os.Remove(filepath.Join(c.WorkspaceRoot, "go.mod")) } for _, m := range build(t).Mounts { if m.Target == "/opt/go-mod" { t.Fatal("missing cache mounted") } } }) } first := build(t) mustJournal(t, os.WriteFile(c.Executable, []byte("another runner build"), 0700)) if build(t).Hash != first.Hash { t.Fatal("the runner executable changed the provider policy hash") } mustJournal(t, os.WriteFile(c.ToolProxyPath, []byte("changed helper"), 0700)) if build(t).Hash == first.Hash { t.Fatal("helper digest not bound to policy") } }