console: getting-started facts and view model
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
3 files changed,
+299
−0
internal/console/gettingstarted.go+127 −0internal/console/gettingstarted_test.go+125 −0internal/console/gettingstartedcopy.go+47 −0
diff --git a/internal/console/gettingstarted.go b/internal/console/gettingstarted.go| new file mode 100644 |
| index 000000000..98787f189 |
| --- /dev/null |
| +++ b/internal/console/gettingstarted.go |
| @@ -0,0 +1,127 @@ |
| +package console |
| + |
| +import ( |
| + "strings" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// personConnected: the person has a live CLI token of their own. Person |
| +// tokens always carry an expiry, so a zero expiry never ticks. |
| +func personConnected(tokens []CLIToken, now time.Time) bool { |
| + for _, t := range tokens { |
| + if t.Kind == "" && t.RevokedAt.IsZero() && t.ExpiresAt.After(now) { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// appIsLive is the one definition of "this app is live" for the checklist: |
| +// healthy, and either serving a version from instances or a static site |
| +// whose own channel points at a version. |
| +func appIsLive(row appRow, v AppView, acct string) bool { |
| + if row.Verdict != "ok" { |
| + return false |
| + } |
| + if row.Deployment.UniformVersion() != "" || row.Deployment.Mixed() { |
| + return true |
| + } |
| + for _, rt := range v.Routes { |
| + if rt.Account != acct || rt.Platform || rt.Kind != "static" { |
| + continue |
| + } |
| + // An empty channel never resolves (view.go defaults it to stable |
| + // before it gets here); it does not tick. |
| + if _, ok := release.Rung(rt.Channel); !ok { |
| + continue |
| + } |
| + for _, ch := range v.Channels { |
| + if ch.Name == rt.Channel && ch.Err == "" && ch.Version != "" { |
| + return true |
| + } |
| + } |
| + } |
| + return false |
| +} |
| + |
| +type gsStepState string |
| + |
| +const ( |
| + gsStateDone gsStepState = "done" |
| + gsStateSkipped gsStepState = "skipped" |
| + gsStateOpen gsStepState = "open" |
| + gsStateMuted gsStepState = "muted" |
| +) |
| + |
| +type gsStep struct { |
| + Key string // connect | deploy | secrets | domain |
| + State gsStepState |
| + Title string |
| + Body string |
| + // Prompt is the text to paste to an agent; CLI is the command to run. |
| + Prompt string |
| + CLI string |
| +} |
| + |
| +type gettingStartedView struct { |
| + Steps []gsStep |
| + CanCreate bool |
| + IsOwner bool |
| + DomainApp string |
| + Copy gettingStartedCopy |
| +} |
| + |
| +// buildGettingStarted returns the card, or nil when the person hid it or it |
| +// already completed. complete is true when every step is done or skipped. |
| +func buildGettingStarted(marks map[gsMark]bool, connected, anyLive bool, role string, firstApp string, consoleName string) (view *gettingStartedView, complete bool) { |
| + if marks[gsHidden] || marks[gsCompleted] { |
| + return nil, false |
| + } |
| + c := approvedGettingStartedCopy |
| + for _, p := range []*string{&c.ConnectPrompt, &c.DeployPrompt, &c.SecretsPrompt} { |
| + *p = strings.ReplaceAll(*p, "{console}", consoleName) |
| + } |
| + mark := func(done, skip gsMark) gsStepState { |
| + switch { |
| + case marks[done]: |
| + return gsStateDone |
| + case marks[skip]: |
| + return gsStateSkipped |
| + } |
| + return gsStateOpen |
| + } |
| + fact := func(ok bool) gsStepState { |
| + if ok { |
| + return gsStateDone |
| + } |
| + return gsStateOpen |
| + } |
| + steps := []gsStep{ |
| + {Key: "connect", State: fact(connected), Title: c.ConnectTitle, Body: c.ConnectBody, Prompt: c.ConnectPrompt, CLI: c.ConnectCLI}, |
| + {Key: "deploy", State: fact(anyLive), Title: c.DeployTitle, Body: c.DeployBody, Prompt: c.DeployPrompt}, |
| + {Key: "secrets", State: mark(gsSecretsDone, gsSecretsSkip), Title: c.SecretsTitle, Body: c.SecretsBody, Prompt: c.SecretsPrompt}, |
| + {Key: "domain", State: mark(gsDomainDone, gsDomainSkip), Title: c.DomainTitle, Body: c.DomainBody, CLI: c.DomainCLI}, |
| + } |
| + foundOpen := false |
| + for i := range steps { |
| + if steps[i].State != gsStateOpen { |
| + continue |
| + } |
| + if foundOpen { |
| + steps[i].State = gsStateMuted |
| + } |
| + foundOpen = true |
| + } |
| + if !foundOpen { |
| + return nil, true |
| + } |
| + return &gettingStartedView{ |
| + Steps: steps, |
| + CanCreate: role == RoleOwner || role == RoleMember, |
| + IsOwner: role == RoleOwner, |
| + DomainApp: firstApp, |
| + Copy: c, |
| + }, false |
| +} |
diff --git a/internal/console/gettingstarted_test.go b/internal/console/gettingstarted_test.go| new file mode 100644 |
| index 000000000..fda7e04c1 |
| --- /dev/null |
| +++ b/internal/console/gettingstarted_test.go |
| @@ -0,0 +1,125 @@ |
| +package console |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestPersonConnected(t *testing.T) { |
| + now := time.Date(2026, 11, 8, 12, 0, 0, 0, time.UTC) |
| + live := now.Add(time.Hour) |
| + cases := []struct { |
| + name string |
| + toks []CLIToken |
| + want bool |
| + }{ |
| + {"none", nil, false}, |
| + {"person live", []CLIToken{{ExpiresAt: live}}, true}, |
| + {"person expired", []CLIToken{{ExpiresAt: now.Add(-time.Second)}}, false}, |
| + {"person revoked", []CLIToken{{ExpiresAt: live, RevokedAt: now.Add(-time.Minute)}}, false}, |
| + {"person zero expiry", []CLIToken{{}}, false}, |
| + {"app live", []CLIToken{{Kind: "app", ExpiresAt: live}}, false}, |
| + {"service live", []CLIToken{{Kind: "service", ExpiresAt: live}}, false}, |
| + } |
| + for _, c := range cases { |
| + if got := personConnected(c.toks, now); got != c.want { |
| + t.Errorf("%s: got %v want %v", c.name, got, c.want) |
| + } |
| + } |
| +} |
| + |
| +func TestAppIsLive(t *testing.T) { |
| + uniform := DeploymentStatus{RouteCount: 1, ReportedCount: 1, Versions: []DeploymentVersion{{"v1", 1}}} |
| + mixed := DeploymentStatus{RouteCount: 2, ReportedCount: 2, Versions: []DeploymentVersion{{"v1", 1}, {"v2", 1}}} |
| + unknown := DeploymentStatus{RouteCount: 1, UnknownCount: 1} |
| + static := func(acct, ch string) RouteView { |
| + return RouteView{Host: "s.example.com", Kind: "static", Channel: ch, Account: acct} |
| + } |
| + cases := []struct { |
| + name string |
| + row appRow |
| + v AppView |
| + want bool |
| + }{ |
| + {"uniform ok", appRow{Verdict: "ok", Deployment: uniform}, AppView{}, true}, |
| + {"mixed ok", appRow{Verdict: "ok", Deployment: mixed}, AppView{}, true}, |
| + {"static stable v1", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{static("a", "stable")}, Channels: []ChannelView{{Name: "stable", Version: "v1"}}}, true}, |
| + {"unknown verdict with version", appRow{Verdict: "unknown", Deployment: uniform}, AppView{}, false}, |
| + {"unknown instance", appRow{Verdict: "ok", Deployment: unknown}, AppView{}, false}, |
| + {"static own channel empty, other has version", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{static("a", "stable")}, |
| + Channels: []ChannelView{{Name: "stable"}, {Name: "beta", Version: "v1"}}}, false}, |
| + {"static channel err", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{static("a", "stable")}, Channels: []ChannelView{{Name: "stable", Version: "v1", Err: "boom"}}}, false}, |
| + {"attention with version", appRow{Verdict: "attention", Deployment: uniform}, AppView{}, false}, |
| + {"other account route", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{static("b", "stable")}, Channels: []ChannelView{{Name: "stable", Version: "v1"}}}, false}, |
| + {"platform route", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{{Kind: "static", Channel: "stable", Account: "a", Platform: true}}, |
| + Channels: []ChannelView{{Name: "stable", Version: "v1"}}}, false}, |
| + {"empty route channel", appRow{Verdict: "ok"}, AppView{ |
| + Routes: []RouteView{static("a", "")}, Channels: []ChannelView{{Name: "", Version: "v1"}}}, false}, |
| + } |
| + for _, c := range cases { |
| + if got := appIsLive(c.row, c.v, "a"); got != c.want { |
| + t.Errorf("%s: got %v want %v", c.name, got, c.want) |
| + } |
| + } |
| +} |
| + |
| +func gsStates(v *gettingStartedView) string { |
| + var out []string |
| + for _, s := range v.Steps { |
| + out = append(out, string(s.State)) |
| + } |
| + return strings.Join(out, ",") |
| +} |
| + |
| +func TestBuildGettingStarted(t *testing.T) { |
| + none := map[gsMark]bool{} |
| + v, done := buildGettingStarted(none, false, false, RoleOwner, "shop", "Carloku") |
| + if done || v == nil || gsStates(v) != "open,muted,muted,muted" { |
| + t.Fatalf("fresh: %v %v", v, done) |
| + } |
| + v, _ = buildGettingStarted(none, true, false, RoleOwner, "shop", "Carloku") |
| + if gsStates(v) != "done,open,muted,muted" { |
| + t.Errorf("connected: %s", gsStates(v)) |
| + } |
| + v, _ = buildGettingStarted(map[gsMark]bool{gsSecretsSkip: true}, true, true, RoleOwner, "shop", "Carloku") |
| + if gsStates(v) != "done,done,skipped,open" { |
| + t.Errorf("secrets skip: %s", gsStates(v)) |
| + } |
| + all := map[gsMark]bool{gsSecretsDone: true, gsDomainSkip: true} |
| + if v, done := buildGettingStarted(all, true, true, RoleOwner, "shop", "Carloku"); v != nil || !done { |
| + t.Errorf("all four: %v %v", v, done) |
| + } |
| + for _, m := range []gsMark{gsHidden, gsCompleted} { |
| + if v, done := buildGettingStarted(map[gsMark]bool{m: true}, false, false, RoleOwner, "", "Carloku"); v != nil || done { |
| + t.Errorf("%s: %v %v", m, v, done) |
| + } |
| + } |
| + v, _ = buildGettingStarted(none, false, false, RoleMember, "shop", "Carloku") |
| + if !v.CanCreate || v.IsOwner { |
| + t.Errorf("member: %+v", v) |
| + } |
| + v, _ = buildGettingStarted(none, false, false, RoleOwner, "", "Carloku") |
| + if v.DomainApp != "" || !v.CanCreate || !v.IsOwner { |
| + t.Errorf("no apps: %+v", v) |
| + } |
| + v, _ = buildGettingStarted(none, false, false, RoleOwner, "shop", "Carloku") |
| + if v.DomainApp != "shop" { |
| + t.Errorf("domain app %q", v.DomainApp) |
| + } |
| + all2 := v.Copy.ConnectPrompt + v.Copy.DeployPrompt + v.Copy.SecretsPrompt |
| + for _, s := range v.Steps { |
| + all2 += s.Title + s.Body + s.Prompt + s.CLI |
| + } |
| + if strings.Contains(all2, "{console}") || strings.ContainsAny(all2, "—–") { |
| + t.Errorf("bad copy: %q", all2) |
| + } |
| + if !strings.Contains(v.Copy.ConnectPrompt, "Carloku") || v.Copy.ConnectCLI != "carlos auth login --no-wait" { |
| + t.Errorf("copy: %+v", v.Copy) |
| + } |
| +} |
diff --git a/internal/console/gettingstartedcopy.go b/internal/console/gettingstartedcopy.go| new file mode 100644 |
| index 000000000..d682b7b94 |
| --- /dev/null |
| +++ b/internal/console/gettingstartedcopy.go |
| @@ -0,0 +1,47 @@ |
| +package console |
| + |
| +// gettingStartedCopy is every string on the Getting started card. Fields |
| +// holding {console} are filled in by buildGettingStarted. |
| +type gettingStartedCopy struct { |
| + Title, Hide, Show, Done, Skipped, MarkDone, Skip, Copy, Copied string |
| + |
| + ConnectTitle, ConnectBody, ConnectPrompt, ConnectCLI string |
| + DeployTitle, DeployBody, DeployPrompt, DeployCreate string |
| + SecretsTitle, SecretsBody, SecretsPrompt string |
| + DomainTitle, DomainBody, DomainCLI, DomainLink string |
| + |
| + SaveFailed string |
| +} |
| + |
| +var approvedGettingStartedCopy = gettingStartedCopy{ |
| + Title: "Getting started", |
| + Hide: "Hide", |
| + Show: "Show getting started", |
| + Done: "Done", |
| + Skipped: "Skipped", |
| + MarkDone: "Mark done", |
| + Skip: "Skip", |
| + Copy: "Copy", |
| + Copied: "Copied", |
| + |
| + ConnectTitle: "Connect your agent or terminal", |
| + ConnectBody: "Ask your agent to sign in. It runs one command and gives you a link to approve.", |
| + ConnectPrompt: "Sign in to {console} with the carlos CLI: run carlos auth login --no-wait and give me the link it prints.", |
| + ConnectCLI: "carlos auth login --no-wait", |
| + |
| + DeployTitle: "Put your first app live", |
| + DeployBody: "Ask your agent to deploy your app.", |
| + DeployPrompt: "Deploy this app to {console}. Use the carlos CLI; my account is ready.", |
| + DeployCreate: "Create an app here instead", |
| + |
| + SecretsTitle: "Add your app's secrets", |
| + SecretsBody: "If your app needs API keys or passwords, give them to your agent, or set them yourself with carlos secrets set.", |
| + SecretsPrompt: "Set the secrets my app needs on {console}", |
| + |
| + DomainTitle: "Use your own domain", |
| + DomainBody: "Point a domain you own at your app. Your agent can attach it with the carlos CLI.", |
| + DomainCLI: "carlos domains attach --app <app> <hostname>", |
| + DomainLink: "Attach a domain in settings", |
| + |
| + SaveFailed: "Could not save that. Please try again.", |
| +} |