Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160//go:build unix
// A private home isolates fake OAuth accounts and cached capabilities from the// shared suite (including its no-accounts scenarios). No Codex inference is sent.package e2e
import ( "bytes" "context" "crypto/sha256" "encoding/json" "fmt" "os" "os/exec" "path/filepath" "strings" "testing" "time"
"albedo/cli/internal/daemon")
type blueFixture struct { connection *daemon.Connection home string env []string}
func writeBlueJSON(t *testing.T, home, name string, value any) { t.Helper() data, err := json.Marshal(value) if err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(home, name), data, 0600); err != nil { t.Fatal(err) }}
func newBlueFixture(t *testing.T) *blueFixture { t.Helper() f := &blueFixture{home: t.TempDir()} for _, entry := range suite.env { if strings.HasPrefix(entry, "ALBEDO_HOME=") { entry = "ALBEDO_HOME=" + f.home } f.env = append(f.env, entry) } now := time.Now().UnixMilli() lists := filepath.Join(f.home, "model-lists") if err := os.MkdirAll(lists, 0700); err != nil { t.Fatal(err) } for _, account := range []string{"acct-one", "acct-two"} { key := fmt.Sprintf("%x", sha256.Sum256([]byte("codex\x00"+account))) writeBlueJSON(t, lists, key+".json", map[string]any{"owner": "codex", "fetched": now, "ids": []string{"fixture-codex", "fixture-standard"}}) } accounts := []any{} cache := map[string]any{} for _, label := range []string{"one", "two"} { access := "fixture-blue-" + label accounts = append(accounts, map[string]any{"type": "oauth", "_albedo_account_id": label, "access": access, "refresh": "fixture-refresh", "accountId": "acct-" + label, "expires": 9999999999999}) programs := []string{} if label == "one" { programs = []string{"daybreak_blue"} } cache["acct-"+label] = map[string]any{"fetchedAt": now, "clientVersion": "1.2.3", "accessHash": fmt.Sprintf("%X", sha256.Sum256([]byte(access))), "models": []any{ map[string]any{"slug": "fixture-codex", "name": "Fixture Codex", "efforts": []string{"low", "medium", "high"}, "cyber_programs": programs, "service_tiers": []string{}}, map[string]any{"slug": "fixture-standard", "name": "Standard Only", "efforts": []string{}, "cyber_programs": []string{}, "service_tiers": []string{}}, }} } writeBlueJSON(t, f.home, "creds.json", map[string]any{"accounts": map[string]any{"openai-codex": accounts}}) writeBlueJSON(t, f.home, "codex-models.json", map[string]any{"clientVersion": "1.2.3", "versionCheckedAt": now, "accounts": cache}) writeBlueJSON(t, f.home, "config.json", map[string]any{"active": "blue", "providers": map[string]any{ "blue": map[string]any{"extension": "codex", "model": "fixture-codex", "protocol": "responses", "accountId": "one"}, "ordinary": map[string]any{"extension": "codex", "model": "fixture-codex", "protocol": "responses", "accountId": "two"}, "unknown": map[string]any{"extension": "codex", "model": "fixture-codex", "protocol": "responses", "accountId": "missing"}, "unpinned": map[string]any{"extension": "codex", "model": "fixture-codex", "protocol": "responses"}, }}) writeBlueJSON(t, f.home, "models.json", map[string]any{}) writeBlueJSON(t, f.home, "extensions.json", map[string]any{"models": map[string]any{"refreshHours": 0, "url": suite.provider.server.URL + "/models-dev"}, "cacheTtl": map[string]any{"url": nil}}) if _, stderr, err := f.run("sessions"); err != nil { t.Fatalf("boot private Blue fixture: %v %s", err, stderr) } snap, err := readDaemonSnapshot(f.home) if err != nil { t.Fatal(err) } t.Cleanup(func() { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() current := daemon.NewConnection(snap, nil) defer current.HTTPClient().CloseIdleConnections() if stopErr := daemon.StopDaemon(ctx, current, false); stopErr != nil { t.Error(stopErr) } if !awaitExit(snap.Pid, 15*time.Second) { t.Errorf("private Blue daemon %d survived shutdown", snap.Pid) } }) f.connection, err = daemon.Attach(t.Context(), snap, func(context.Context) (daemon.ConnectionSnapshot, error) { return snap, nil }) if err != nil { t.Fatal(err) } t.Cleanup(f.connection.HTTPClient().CloseIdleConnections) return f}
// removeBlueAccount models credential removal after a saved Blue selection.func (f *blueFixture) removeBlueAccount(t *testing.T) { t.Helper() data, err := os.ReadFile(filepath.Join(f.home, "creds.json")) if err != nil { t.Fatal(err) } var creds map[string]any if err := json.Unmarshal(data, &creds); err != nil { t.Fatal(err) } accounts := creds["accounts"].(map[string]any) kept := []any{} for _, item := range accounts["openai-codex"].([]any) { if item.(map[string]any)["_albedo_account_id"] != "one" { kept = append(kept, item) } } accounts["openai-codex"] = kept writeBlueJSON(t, f.home, "creds.json", creds)}
func (f *blueFixture) run(args ...string) (string, string, error) { ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) defer cancel() command := exec.CommandContext(ctx, suite.cli, args...) command.Dir = suite.root command.Env = f.env var stdout, stderr bytes.Buffer command.Stdout, command.Stderr = &stdout, &stderr err := command.Run() return stdout.String(), stderr.String(), err}
func (f *blueFixture) session(t *testing.T, id string) daemon.Session { t.Helper() session, err := daemon.GetSession(t.Context(), f.connection, id) if err != nil { t.Fatal(err) } return session}
func (f *blueFixture) create(t *testing.T, access string) daemon.Session { t.Helper() session, err := daemon.CreateSession(t.Context(), f.connection, daemon.CreateSessionRequest{Workspace: t.TempDir(), Provider: "blue", CyberAccess: access}) if err != nil { t.Fatal(err) } return session}