Add Home, Store and Settings
Home greets by time of day in the Base's zone, shows the three setup steps and a tile per installed app linking to its origin. The Store installs in the background (303 at once), shows progress with a noscript refresh, and uninstalls only after a confirmation page. Owner-only writes; members can look. Moves the web.Installer assertion to an external install test to avoid an import cycle.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
16 files changed,
+675
−14
docs/copy-pending.md+14 −0internal/install/install_test.go+0 −3internal/install/web_test.go+9 −0internal/look/static/app.css+5 −0internal/web/app.go+7 −8internal/web/clock.go+14 −0internal/web/home.go+135 −0internal/web/settings.go+14 −0internal/web/store.go+118 −0internal/web/templates/home.html+17 −0internal/web/templates/settings.html+12 −0internal/web/templates/store.html+18 −0internal/web/templates/uninstall.html+14 −0internal/webtest/harness_test.go+8 −3internal/webtest/home_test.go+119 −0internal/webtest/store_test.go+171 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index 623cca4..ec202c0 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -67,3 +67,17 @@ People and invitations: |
| | internal/install/install.go | Uninstall failed | Platform refused to remove the instance | |
| | internal/install/install.go | Uninstall while the app is still installing | This app is still installing. Try again in a moment. | |
| | internal/store/installs.go | Install cut short by a restart, shown on the app's card | Interrupted. Try again. | |
| +| internal/web/home.go | Home greeting | Good morning / afternoon / evening, {name}. | |
| +| internal/web/home.go | Home lede, two steps left | Two steps until {Base name}'s Base is ready. | |
| +| internal/web/home.go | Home lede, one step left | One step until {Base name}'s Base is ready. | |
| +| internal/web/home.go | Home lede, all done | {Base name}'s Base is ready. | |
| +| internal/web/templates/home.html | Ask field placeholder | Ask Base anything | |
| +| internal/web/templates/home.html | Ask field tooltip (disabled) | Asking Base arrives in a later slice | |
| +| internal/web/templates/home.html | Built-in tile names | Store, People, Settings | |
| +| internal/web/templates/store.html | Store heading and lead | Store: Apps that run on {Base name}'s Base. Install one and it's on Home for everyone at {Base name}. | |
| +| internal/web/templates/store.html | Buttons and labels | Install, Installing, Try again, Uninstall, Installed | |
| +| internal/web/templates/store.html | Failed install with no reason | The install didn't finish. | |
| +| internal/web/templates/store.html | Coming soon line | Coming in later slices: Keymail, Sporran and the Oficina apps. | |
| +| internal/web/templates/uninstall.html | Confirmation heading | Uninstall {App}? | |
| +| internal/web/templates/uninstall.html | Confirmation buttons | Uninstall / Keep it | |
| +| internal/web/templates/settings.html | Settings labels | Settings; Base name; Signed in as; Sign out | |
diff --git a/internal/install/install_test.go b/internal/install/install_test.go| index a1397ed..485e9a1 100644 |
| --- a/internal/install/install_test.go |
| +++ b/internal/install/install_test.go |
| @@ -24,11 +24,8 @@ import ( |
| "amadan.net/carlos/base/internal/config" |
| "amadan.net/carlos/base/internal/platform" |
| "amadan.net/carlos/base/internal/store" |
| - "amadan.net/carlos/base/internal/web" |
| ) |
| -var _ web.Installer = (*Installer)(nil) |
| - |
| var template = dbtest.FromSet(store.BootSchema) |
| func TestMain(m *testing.M) { |
diff --git a/internal/install/web_test.go b/internal/install/web_test.go| new file mode 100644 |
| index 0000000..2e968ee |
| --- /dev/null |
| +++ b/internal/install/web_test.go |
| @@ -0,0 +1,9 @@ |
| +package install_test |
| + |
| +import ( |
| + "amadan.net/carlos/base/internal/install" |
| + "amadan.net/carlos/base/internal/web" |
| +) |
| + |
| +// web imports install, so this check lives in the external test package. |
| +var _ web.Installer = (*install.Installer)(nil) |
diff --git a/internal/look/static/app.css b/internal/look/static/app.css| index 6500c89..42b38be 100644 |
| --- a/internal/look/static/app.css |
| +++ b/internal/look/static/app.css |
| @@ -152,3 +152,8 @@ button, input { font: inherit; } |
| .fog, .fog i, .light, .light::after, .app.new, .app.leaving, .installing { animation: none; } |
| .light { transform: skewX(-14deg); } |
| } |
| + |
| +/* Links drawn as tiles and buttons. */ |
| +a.app, a.me { color: inherit; text-decoration: none; } |
| +a.btn { display: inline-flex; align-items: center; text-decoration: none; } |
| +.ask:disabled { cursor: not-allowed; font: inherit; font-size: 14px; } |
diff --git a/internal/web/app.go b/internal/web/app.go| index f12ec0a..1305400 100644 |
| --- a/internal/web/app.go |
| +++ b/internal/web/app.go |
| @@ -1,5 +1,5 @@ |
| -// Package web is Base's HTTP surface: sign-in, the member gate and, in later |
| -// tasks, Home and the Store. |
| +// Package web is Base's HTTP surface: sign-in, the member gate, Home, People, |
| +// the Store and Settings. |
| package web |
| import ( |
| @@ -103,6 +103,11 @@ func App(d *db.DB, cfg config.Config, mailer mail.Sender, inst Installer, logger |
| r.Get("/", a.home) |
| r.Get("/people", a.people) |
| r.Post("/people", a.invite) |
| + r.Get("/store", a.storePage) |
| + r.Post("/store/{app}/install", a.storeInstall) |
| + r.Get("/store/{app}/uninstall", a.uninstallPage) |
| + r.Post("/store/{app}/uninstall", a.storeUninstall) |
| + r.Get("/settings", a.settings) |
| }) |
| return r, nil |
| } |
| @@ -158,9 +163,3 @@ func (a *app) requireMember(next http.Handler) http.Handler { |
| } |
| })) |
| } |
| - |
| -// home is a stand-in until Home lands. |
| -func (a *app) home(w http.ResponseWriter, r *http.Request) { |
| - w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| - _, _ = w.Write([]byte(a.cfg.Name + "\n")) |
| -} |
diff --git a/internal/web/clock.go b/internal/web/clock.go| new file mode 100644 |
| index 0000000..90273fb |
| --- /dev/null |
| +++ b/internal/web/clock.go |
| @@ -0,0 +1,14 @@ |
| +package web |
| + |
| +import "time" |
| + |
| +// now is the clock Home greets by. Tests replace it through SetClock. |
| +var now = time.Now |
| + |
| +// SetClock replaces the clock and returns a function that restores it. It |
| +// exists for tests and is not safe to call while requests are in flight. |
| +func SetClock(f func() time.Time) (restore func()) { |
| + prev := now |
| + now = f |
| + return func() { now = prev } |
| +} |
diff --git a/internal/web/home.go b/internal/web/home.go| new file mode 100644 |
| index 0000000..8d582c1 |
| --- /dev/null |
| +++ b/internal/web/home.go |
| @@ -0,0 +1,135 @@ |
| +package web |
| + |
| +import ( |
| + "html/template" |
| + "net/http" |
| + "strings" |
| + |
| + "amadan.net/carlos/base/internal/catalog" |
| + "amadan.net/carlos/base/internal/install" |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +type step struct { |
| + Label string |
| + Done bool |
| +} |
| + |
| +type tile struct { |
| + Name string |
| + Href string |
| + Tint string |
| + Icon template.HTML |
| +} |
| + |
| +type homePage struct { |
| + Name string // the Base's name |
| + Greeting string |
| + Initials string |
| + Lede string |
| + Steps []step |
| + Tiles []tile |
| +} |
| + |
| +// Icons for the built-in tiles, in the mockup's style. |
| +const ( |
| + iconStore = `<svg viewBox="0 0 24 24"><path d="M5 8h14l-1.2 11.2a1 1 0 0 1-1 .8H7.2a1 1 0 0 1-1-.8z"/><path d="M9 8V6.5a3 3 0 0 1 6 0V8"/></svg>` |
| + iconPeople = `<svg viewBox="0 0 24 24"><circle cx="9" cy="9" r="3"/><circle cx="16.5" cy="10.5" r="2.5"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0M14 19a4 4 0 0 1 7 0"/></svg>` |
| + iconSettings = `<svg viewBox="0 0 24 24"><path d="M4 7h10M18 7h2M4 17h2M10 17h10"/><circle cx="16" cy="7" r="2"/><circle cx="8" cy="17" r="2"/></svg>` |
| +) |
| + |
| +// displayName is the member's name, or the capitalised local part of their |
| +// email address. |
| +func displayName(m store.Member) string { |
| + if m.Name != "" { |
| + return m.Name |
| + } |
| + local, _, _ := strings.Cut(m.Email, "@") |
| + if local == "" { |
| + return m.Email |
| + } |
| + r := []rune(local) |
| + return strings.ToUpper(string(r[:1])) + string(r[1:]) |
| +} |
| + |
| +func initials(name string) string { |
| + f := strings.Fields(name) |
| + switch len(f) { |
| + case 0: |
| + return "" |
| + case 1: |
| + return strings.ToUpper(string([]rune(f[0])[:1])) |
| + } |
| + return strings.ToUpper(string([]rune(f[0])[:1]) + string([]rune(f[len(f)-1])[:1])) |
| +} |
| + |
| +func (a *app) greeting(name string) string { |
| + t := now() |
| + if a.cfg.Timezone != nil { |
| + t = t.In(a.cfg.Timezone) |
| + } |
| + part := "evening" |
| + switch h := t.Hour(); { |
| + case h < 12: |
| + part = "morning" |
| + case h < 18: |
| + part = "afternoon" |
| + } |
| + return "Good " + part + ", " + name + "." |
| +} |
| + |
| +func (a *app) home(w http.ResponseWriter, r *http.Request) { |
| + me, _ := CurrentMember(r) |
| + ms, err := store.Members(r.Context(), a.db) |
| + if err != nil { |
| + a.logger.Error("home: members", "err", err) |
| + http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| + return |
| + } |
| + installs, err := store.Installs(r.Context(), a.db) |
| + if err != nil { |
| + a.logger.Error("home: installs", "err", err) |
| + http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| + return |
| + } |
| + name := displayName(me) |
| + page := homePage{Name: a.cfg.Name, Greeting: a.greeting(name), Initials: initials(name)} |
| + page.Tiles = []tile{ |
| + {Name: "Store", Href: "/store", Tint: "teal", Icon: iconStore}, |
| + {Name: "People", Href: "/people", Tint: "violet", Icon: iconPeople}, |
| + {Name: "Settings", Href: "/settings", Tint: "mist", Icon: iconSettings}, |
| + } |
| + installedOwn := false |
| + for _, in := range installs { |
| + e, ok := catalog.Get(in.App) |
| + if !ok || in.State != "installed" { |
| + continue |
| + } |
| + _, host := install.HostFor(a.cfg, in.App) |
| + page.Tiles = append(page.Tiles, tile{Name: e.Name, Href: "https://" + host + "/", Tint: e.Tint, Icon: template.HTML(e.Icon)}) |
| + if !e.Default { |
| + installedOwn = true |
| + } |
| + } |
| + page.Steps = []step{ |
| + {"Signed in", true}, |
| + {"Invite your team", len(ms) > 1}, |
| + {"Install your first app", installedOwn}, |
| + } |
| + left := 0 |
| + for _, s := range page.Steps { |
| + if !s.Done { |
| + left++ |
| + } |
| + } |
| + switch left { |
| + case 0: |
| + page.Lede = a.cfg.Name + "'s Base is ready." |
| + case 1: |
| + page.Lede = "One step until " + a.cfg.Name + "'s Base is ready." |
| + default: |
| + page.Lede = "Two steps until " + a.cfg.Name + "'s Base is ready." |
| + } |
| + w.Header().Set("Cache-Control", "no-store") |
| + a.render(w, "home", page) |
| +} |
diff --git a/internal/web/settings.go b/internal/web/settings.go| new file mode 100644 |
| index 0000000..00457ad |
| --- /dev/null |
| +++ b/internal/web/settings.go |
| @@ -0,0 +1,14 @@ |
| +package web |
| + |
| +import "net/http" |
| + |
| +type settingsPage struct { |
| + Name string |
| + Address string |
| +} |
| + |
| +func (a *app) settings(w http.ResponseWriter, r *http.Request) { |
| + me, _ := CurrentMember(r) |
| + w.Header().Set("Cache-Control", "no-store") |
| + a.render(w, "settings", settingsPage{Name: a.cfg.Name, Address: me.Email}) |
| +} |
diff --git a/internal/web/store.go b/internal/web/store.go| new file mode 100644 |
| index 0000000..6c5dfc6 |
| --- /dev/null |
| +++ b/internal/web/store.go |
| @@ -0,0 +1,118 @@ |
| +package web |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "html/template" |
| + "net/http" |
| + |
| + "github.com/go-chi/chi/v5" |
| + |
| + "amadan.net/carlos/base/internal/catalog" |
| + "amadan.net/carlos/base/internal/install" |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +type storeRow struct { |
| + catalog.Entry |
| + State string // "", "installing", "installed" or "failed" |
| + Error string |
| +} |
| + |
| +// IconHTML is the catalogue's own SVG, which is trusted markup. |
| +func (s storeRow) IconHTML() template.HTML { return template.HTML(s.Icon) } |
| + |
| +type storePage struct { |
| + Name string |
| + IsOwner bool |
| + Busy bool |
| + Installing bool |
| + Rows []storeRow |
| +} |
| + |
| +func (a *app) storePage(w http.ResponseWriter, r *http.Request) { |
| + me, _ := CurrentMember(r) |
| + installs, err := store.Installs(r.Context(), a.db) |
| + if err != nil { |
| + a.logger.Error("store: installs", "err", err) |
| + http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| + return |
| + } |
| + state := map[string]store.Install{} |
| + for _, in := range installs { |
| + state[in.App] = in |
| + } |
| + page := storePage{Name: a.cfg.Name, IsOwner: me.Role == "owner", Busy: r.URL.Query().Get("problem") == "busy"} |
| + for _, e := range catalog.All() { |
| + in := state[e.ID] |
| + page.Rows = append(page.Rows, storeRow{Entry: e, State: in.State, Error: in.Error}) |
| + if in.State == "installing" { |
| + page.Installing = true |
| + } |
| + } |
| + w.Header().Set("Cache-Control", "no-store") |
| + a.render(w, "store", page) |
| +} |
| + |
| +// owner returns the catalogue entry named in the URL when the signed-in |
| +// member is the owner, and otherwise answers 403 or 404 and reports false. |
| +func (a *app) owner(w http.ResponseWriter, r *http.Request) (catalog.Entry, bool) { |
| + me, _ := CurrentMember(r) |
| + if me.Role != "owner" { |
| + http.Error(w, "forbidden", http.StatusForbidden) |
| + return catalog.Entry{}, false |
| + } |
| + e, ok := catalog.Get(chi.URLParam(r, "app")) |
| + if !ok { |
| + http.NotFound(w, r) |
| + return catalog.Entry{}, false |
| + } |
| + return e, true |
| +} |
| + |
| +// storeInstall starts an install and answers at once; the Store page shows |
| +// progress. The install outlives the request. |
| +func (a *app) storeInstall(w http.ResponseWriter, r *http.Request) { |
| + e, ok := a.owner(w, r) |
| + if !ok { |
| + return |
| + } |
| + ctx := context.WithoutCancel(r.Context()) |
| + go func() { |
| + if err := a.inst.Install(ctx, e.ID); err != nil { |
| + a.logger.Error("install", "app", e.ID, "err", err) |
| + } |
| + }() |
| + http.Redirect(w, r, "/store", http.StatusSeeOther) |
| +} |
| + |
| +func (a *app) uninstallPage(w http.ResponseWriter, r *http.Request) { |
| + e, ok := a.owner(w, r) |
| + if !ok { |
| + return |
| + } |
| + w.Header().Set("Cache-Control", "no-store") |
| + a.render(w, "uninstall", struct { |
| + Name string |
| + App catalog.Entry |
| + }{a.cfg.Name, e}) |
| +} |
| + |
| +func (a *app) storeUninstall(w http.ResponseWriter, r *http.Request) { |
| + e, ok := a.owner(w, r) |
| + if !ok { |
| + return |
| + } |
| + switch err := a.inst.Uninstall(r.Context(), e.ID); { |
| + case errors.Is(err, install.ErrBusy): |
| + http.Redirect(w, r, "/store?problem=busy", http.StatusSeeOther) |
| + return |
| + case errors.Is(err, store.ErrNoInstall): |
| + // Already uninstalled. |
| + case err != nil: |
| + a.logger.Error("uninstall", "app", e.ID, "err", err) |
| + http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| + return |
| + } |
| + http.Redirect(w, r, "/store", http.StatusSeeOther) |
| +} |
diff --git a/internal/web/templates/home.html b/internal/web/templates/home.html| new file mode 100644 |
| index 0000000..363f882 |
| --- /dev/null |
| +++ b/internal/web/templates/home.html |
| @@ -0,0 +1,17 @@ |
| +{{define "title"}}{{.Name}} · Base{{end}} |
| +{{define "main"}}<main class="screen home"> |
| +<div class="topbar glass"> |
| +<div class="wordmark">base</div> |
| +<div class="team">{{.Name}}</div> |
| +<input class="ask" type="text" disabled placeholder="Ask Base anything" title="Asking Base arrives in a later slice" aria-label="Ask Base anything"> |
| +<a class="me" href="/settings" aria-label="Settings">{{.Initials}}</a> |
| +</div> |
| +<h2 class="hello">{{.Greeting}}</h2> |
| +<p class="lede">{{.Lede}}</p> |
| +<ul class="steps"> |
| +{{range .Steps}}<li class="glass{{if .Done}} done{{end}}"><span class="mark">{{if .Done}}✓{{end}}</span>{{.Label}}</li> |
| +{{end}}</ul> |
| +<div class="dock"> |
| +{{range .Tiles}}<a class="app" href="{{.Href}}"><div class="tile glass t-{{.Tint}}">{{.Icon}}</div>{{.Name}}</a> |
| +{{end}}</div> |
| +</main>{{end}} |
diff --git a/internal/web/templates/settings.html b/internal/web/templates/settings.html| new file mode 100644 |
| index 0000000..97b8fa0 |
| --- /dev/null |
| +++ b/internal/web/templates/settings.html |
| @@ -0,0 +1,12 @@ |
| +{{define "title"}}Settings · {{.Name}}{{end}} |
| +{{define "main"}}<main class="inner"> |
| +<div class="sheet glass"> |
| +<div class="crumb"><div class="wordmark">base</div><span>Settings</span></div> |
| +<h2>Settings</h2> |
| +<ul class="people"> |
| +<li><div class="who">Base name<small>{{.Name}}</small></div></li> |
| +<li><div class="who">Signed in as<small>{{.Address}}</small></div></li> |
| +</ul> |
| +<form method="post" action="/auth/signout"><button class="btn ghost" type="submit">Sign out</button></form> |
| +</div> |
| +</main>{{end}} |
diff --git a/internal/web/templates/store.html b/internal/web/templates/store.html| new file mode 100644 |
| index 0000000..eb93550 |
| --- /dev/null |
| +++ b/internal/web/templates/store.html |
| @@ -0,0 +1,18 @@ |
| +{{define "title"}}Store · {{.Name}}{{end}} |
| +{{define "main"}}{{if .Installing}}<noscript><meta http-equiv="refresh" content="2"></noscript> |
| +{{end}}<main class="inner"> |
| +<div class="sheet glass"> |
| +<div class="crumb"><div class="wordmark">base</div><span>Store</span></div> |
| +<h2>Store</h2> |
| +<p class="lead">Apps that run on {{.Name}}'s Base. Install one and it's on Home for everyone at {{.Name}}.</p> |
| +{{if .Busy}}<p role="status">This app is still installing. Try again in a moment.</p> |
| +{{end}}{{range .Rows}}<div class="listing"> |
| +<div class="tile glass t-{{.Tint}}">{{.IconHTML}}</div> |
| +<div class="txt"><b>{{.Name}}</b><span>{{.Summary}}</span>{{if eq .State "failed"}}<span role="status">{{if .Error}}{{.Error}}{{else}}The install didn't finish.{{end}}</span>{{end}}</div> |
| +{{if eq .State "installing"}}<div class="actions"><button class="btn installing" type="button" disabled>Installing</button></div> |
| +{{else if eq .State "installed"}}<div class="actions">{{if $.IsOwner}}<a class="btn ghost" href="/store/{{.ID}}/uninstall">Uninstall</a>{{else}}<span class="tag">Installed</span>{{end}}</div> |
| +{{else if $.IsOwner}}<form class="actions" method="post" action="/store/{{.ID}}/install"><button class="btn" type="submit">{{if eq .State "failed"}}Try again{{else}}Install{{end}}</button></form> |
| +{{end}}</div> |
| +{{end}}<p class="soon">Coming in later slices: <b>Keymail</b>, <b>Sporran</b> and the <b>Oficina</b> apps.</p> |
| +</div> |
| +</main>{{end}} |
diff --git a/internal/web/templates/uninstall.html b/internal/web/templates/uninstall.html| new file mode 100644 |
| index 0000000..1f04b54 |
| --- /dev/null |
| +++ b/internal/web/templates/uninstall.html |
| @@ -0,0 +1,14 @@ |
| +{{define "title"}}Uninstall {{.App.Name}} · {{.Name}}{{end}} |
| +{{define "main"}}<main class="inner"> |
| +<div class="sheet glass"> |
| +<div class="crumb"><div class="wordmark">base</div><span>Store</span></div> |
| +<h2>Uninstall {{.App.Name}}?</h2> |
| +<div class="confirm"> |
| +<p>Uninstall {{.App.Name}}? It's removed from Home for everyone at {{.Name}}. Its data is kept, and comes back if you install it again.</p> |
| +<form class="actions" method="post" action="/store/{{.App.ID}}/uninstall"> |
| +<button class="btn danger" type="submit">Uninstall</button> |
| +<a class="btn ghost" href="/store">Keep it</a> |
| +</form> |
| +</div> |
| +</div> |
| +</main>{{end}} |
diff --git a/internal/webtest/harness_test.go b/internal/webtest/harness_test.go| index 716fab3..ed56f8e 100644 |
| --- a/internal/webtest/harness_test.go |
| +++ b/internal/webtest/harness_test.go |
| @@ -18,6 +18,7 @@ import ( |
| "strings" |
| "sync" |
| "testing" |
| + "time" |
| "amadan.net/rastrillo/rastrillo/budget" |
| "amadan.net/rastrillo/rastrillo/db" |
| @@ -87,7 +88,10 @@ type testApp struct { |
| // newApp builds the whole app per test over its own copy of the migrated |
| // database, exactly as main.go does, with the owner bootstrapped. |
| -func newApp(t *testing.T) *testApp { |
| +func newApp(t *testing.T) *testApp { return newAppWith(t, noInstaller{}, time.UTC) } |
| + |
| +// newAppWith is newApp with the Installer and time zone of the caller's choice. |
| +func newAppWith(t *testing.T, inst web.Installer, tz *time.Location) *testApp { |
| t.Helper() |
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| d, err := db.Open(schema.Path(t), logger) |
| @@ -98,9 +102,10 @@ func newApp(t *testing.T) *testApp { |
| if err := store.Bootstrap(context.Background(), d.Writer(), ownerAddress); err != nil { |
| t.Fatal(err) |
| } |
| - cfg := config.Config{Origin: testOrigin, Name: "Acme", Owner: ownerAddress, InstanceKey: "test-instance-key", AllowLoggedMail: true} |
| + cfg := config.Config{Origin: testOrigin, Name: "Acme", Owner: ownerAddress, InstanceKey: "test-instance-key", AllowLoggedMail: true, |
| + Slug: "acme", AppsDomain: "bab.oncarlos.com", Timezone: tz} |
| mailer := &capturingMailer{} |
| - h, err := web.App(d, cfg, mailer, noInstaller{}, logger) |
| + h, err := web.App(d, cfg, mailer, inst, logger) |
| if err != nil { |
| t.Fatalf("web.App: %v", err) |
| } |
diff --git a/internal/webtest/home_test.go b/internal/webtest/home_test.go| new file mode 100644 |
| index 0000000..34d3c02 |
| --- /dev/null |
| +++ b/internal/webtest/home_test.go |
| @@ -0,0 +1,119 @@ |
| +package webtest |
| + |
| +import ( |
| + "context" |
| + "regexp" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/carlos/base/internal/store" |
| + "amadan.net/carlos/base/internal/web" |
| +) |
| + |
| +func (a *testApp) setInstall(app, state, errMsg string) { |
| + a.t.Helper() |
| + ctx := context.Background() |
| + if _, err := store.ClaimInstall(ctx, a.DB, app, "acme-"+app+".bab.oncarlos.com"); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + if err := store.SetInstallState(ctx, a.DB, app, state, errMsg); err != nil { |
| + a.t.Fatal(err) |
| + } |
| +} |
| + |
| +// stepDone reports whether the Home step with this label is ticked. |
| +func stepDone(t *testing.T, body, label string) bool { |
| + t.Helper() |
| + m := regexp.MustCompile(`<li class="glass( done)?"[^>]*>(?:<span class="mark">[^<]*</span>)?` + regexp.QuoteMeta(label) + `</li>`).FindStringSubmatch(body) |
| + if m == nil { |
| + t.Fatalf("no step %q in:\n%s", label, body) |
| + } |
| + return m[1] != "" |
| +} |
| + |
| +func TestHomeGreetsByTimeOfDay(t *testing.T) { |
| + london, err := time.LoadLocation("Europe/London") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + _ = london |
| + // 08:00 UTC on 1 July is 09:00 in London, so the clock is read in the Base's zone. |
| + defer web.SetClock(func() time.Time { return time.Date(2026, 7, 1, 8, 0, 0, 0, time.UTC) })() |
| + a := newAppWith(t, noInstaller{}, london) |
| + if _, err := a.DB.Exec(`UPDATE members SET name = 'Paul'`); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.signedInOwner() |
| + if body := a.get("/").Body; !strings.Contains(body, "Good morning, Paul.") { |
| + t.Fatalf("no morning greeting:\n%s", body) |
| + } |
| + web.SetClock(func() time.Time { return time.Date(2026, 7, 1, 17, 0, 0, 0, time.UTC) }) // 18:00 BST |
| + if body := a.get("/").Body; !strings.Contains(body, "Good evening, Paul.") { |
| + t.Fatalf("no evening greeting:\n%s", body) |
| + } |
| +} |
| + |
| +func TestHomeNameFallsBackToEmailLocalPart(t *testing.T) { |
| + defer web.SetClock(func() time.Time { return time.Date(2026, 7, 1, 13, 0, 0, 0, time.UTC) })() |
| + a := newApp(t) |
| + a.signedInOwner() |
| + if body := a.get("/").Body; !strings.Contains(body, "Good afternoon, Owner.") { |
| + t.Fatalf("no afternoon greeting with capitalised local part:\n%s", body) |
| + } |
| +} |
| + |
| +func TestHomeSteps(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + body := a.get("/").Body |
| + if !stepDone(t, body, "Signed in") || stepDone(t, body, "Invite your team") || stepDone(t, body, "Install your first app") { |
| + t.Fatalf("fresh Base steps wrong:\n%s", body) |
| + } |
| + a.setInstall("welcome", "installed", "") // a default app does not count |
| + if body := a.get("/").Body; stepDone(t, body, "Install your first app") { |
| + t.Fatalf("default app ticked the install step:\n%s", body) |
| + } |
| + if r := a.post("/people", invite("maria@acme.co")); r.Code != 303 { |
| + t.Fatal(r.Code) |
| + } |
| + a.setInstall("scratchpad", "installed", "") |
| + body = a.get("/").Body |
| + for _, s := range []string{"Signed in", "Invite your team", "Install your first app"} { |
| + if !stepDone(t, body, s) { |
| + t.Errorf("step %q not done:\n%s", s, body) |
| + } |
| + } |
| +} |
| + |
| +func TestHomeTilesLinkToOrigins(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + body := a.get("/").Body |
| + for _, h := range []string{`href="/store"`, `href="/people"`, `href="/settings"`} { |
| + if !strings.Contains(body, h) { |
| + t.Errorf("built-in tile %s missing", h) |
| + } |
| + } |
| + if strings.Contains(body, "acme-scratchpad") { |
| + t.Fatal("uninstalled app has a tile") |
| + } |
| + a.setInstall("scratchpad", "installing", "") |
| + if strings.Contains(a.get("/").Body, "acme-scratchpad") { |
| + t.Fatal("installing app has a tile") |
| + } |
| + a.setInstall("scratchpad", "failed", "x") |
| + if strings.Contains(a.get("/").Body, "acme-scratchpad") { |
| + t.Fatal("failed app has a tile") |
| + } |
| + a.setInstall("scratchpad", "installing", "") |
| + a.setInstall("scratchpad", "installed", "") |
| + body = a.get("/").Body |
| + re := regexp.MustCompile(`<a class="app" href="https://acme-scratchpad\.bab\.oncarlos\.com/"><div class="tile glass t-rose">`) |
| + if !re.MatchString(body) { |
| + t.Fatalf("no tile for scratchpad:\n%s", body) |
| + } |
| + if !strings.Contains(body, "disabled") || !strings.Contains(body, "Ask Base anything") { |
| + t.Errorf("ask field missing or not disabled") |
| + } |
| +} |
diff --git a/internal/webtest/store_test.go b/internal/webtest/store_test.go| new file mode 100644 |
| index 0000000..a27b3fc |
| --- /dev/null |
| +++ b/internal/webtest/store_test.go |
| @@ -0,0 +1,171 @@ |
| +package webtest |
| + |
| +import ( |
| + "context" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/carlos/base/internal/install" |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +// recorder is a fake Installer that records its calls. |
| +type recorder struct { |
| + mu sync.Mutex |
| + installs []string |
| + uninstalls []string |
| + uninstallErr error |
| + called chan struct{} |
| +} |
| + |
| +func newRecorder() *recorder { return &recorder{called: make(chan struct{}, 8)} } |
| + |
| +func (r *recorder) Install(_ context.Context, app string) error { |
| + r.mu.Lock() |
| + r.installs = append(r.installs, app) |
| + r.mu.Unlock() |
| + r.called <- struct{}{} |
| + return nil |
| +} |
| + |
| +func (r *recorder) Uninstall(_ context.Context, app string) error { |
| + r.mu.Lock() |
| + r.uninstalls = append(r.uninstalls, app) |
| + r.mu.Unlock() |
| + return r.uninstallErr |
| +} |
| + |
| +func (r *recorder) counts() (int, int) { |
| + r.mu.Lock() |
| + defer r.mu.Unlock() |
| + return len(r.installs), len(r.uninstalls) |
| +} |
| + |
| +const confirmCopy = "Uninstall Scratchpad? It's removed from Home for everyone at Acme. Its data is kept, and comes back if you install it again." |
| + |
| +func contains(body, want string) bool { |
| + return strings.Contains(body, want) || strings.Contains(body, strings.ReplaceAll(want, "'", "'")) |
| +} |
| + |
| +func TestStoreInstallButtonPostsAndRedirects(t *testing.T) { |
| + rec := newRecorder() |
| + a := newAppWith(t, rec, time.UTC) |
| + a.signedInOwner() |
| + page := a.get("/store") |
| + if page.Code != 200 || !strings.Contains(page.Body, `action="/store/scratchpad/install"`) { |
| + t.Fatalf("store page lacks an install form: %d\n%s", page.Code, page.Body) |
| + } |
| + if r := a.post("/store/scratchpad/install", nil); r.Code != 303 || r.Location() != "/store" { |
| + t.Fatalf("install: %d %q", r.Code, r.Location()) |
| + } |
| + select { |
| + case <-rec.called: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("Installer.Install was not called") |
| + } |
| + if rec.installs[0] != "scratchpad" { |
| + t.Fatalf("installed %v", rec.installs) |
| + } |
| + if r := a.post("/store/nope/install", nil); r.Code != 404 { |
| + t.Fatalf("unknown app: %d", r.Code) |
| + } |
| +} |
| + |
| +func TestStoreShowsInstallingWithRefresh(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + if strings.Contains(a.get("/store").Body, `http-equiv="refresh"`) { |
| + t.Fatal("refresh with nothing installing") |
| + } |
| + a.setInstall("scratchpad", "installing", "") |
| + body := a.get("/store").Body |
| + if !strings.Contains(body, `<noscript><meta http-equiv="refresh" content="2"></noscript>`) { |
| + t.Fatalf("no noscript refresh:\n%s", body) |
| + } |
| + if !strings.Contains(body, `disabled`) || strings.Contains(body, `action="/store/scratchpad/install"`) { |
| + t.Fatalf("installing app should show a disabled button, not an install form:\n%s", body) |
| + } |
| +} |
| + |
| +func TestStoreShowsFailedWithRetry(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + a.setInstall("scratchpad", "failed", "The app did not start answering") |
| + body := a.get("/store").Body |
| + if !strings.Contains(body, "The app did not start answering") { |
| + t.Errorf("no failure reason:\n%s", body) |
| + } |
| + if !strings.Contains(body, `action="/store/scratchpad/install"`) || !strings.Contains(body, "Try again") { |
| + t.Errorf("no retry:\n%s", body) |
| + } |
| +} |
| + |
| +func TestUninstallNeedsConfirm(t *testing.T) { |
| + rec := newRecorder() |
| + a := newAppWith(t, rec, time.UTC) |
| + a.signedInOwner() |
| + a.setInstall("scratchpad", "installed", "") |
| + if !strings.Contains(a.get("/store").Body, `href="/store/scratchpad/uninstall"`) { |
| + t.Fatal("no uninstall link on store") |
| + } |
| + page := a.get("/store/scratchpad/uninstall") |
| + if page.Code != 200 || !contains(page.Body, confirmCopy) { |
| + t.Fatalf("confirmation: %d\n%s", page.Code, page.Body) |
| + } |
| + if _, u := rec.counts(); u != 0 { |
| + t.Fatal("GET uninstalled") |
| + } |
| + if r := a.post("/store/scratchpad/uninstall", nil); r.Code != 303 || r.Location() != "/store" { |
| + t.Fatalf("uninstall: %d %q", r.Code, r.Location()) |
| + } |
| + if _, u := rec.counts(); u != 1 { |
| + t.Fatal("POST did not uninstall") |
| + } |
| +} |
| + |
| +func TestUninstallBusyAndGone(t *testing.T) { |
| + rec := newRecorder() |
| + a := newAppWith(t, rec, time.UTC) |
| + a.signedInOwner() |
| + rec.uninstallErr = install.ErrBusy |
| + r := a.post("/store/scratchpad/uninstall", nil) |
| + if r.Code != 303 || r.Location() != "/store?problem=busy" { |
| + t.Fatalf("busy: %d %q", r.Code, r.Location()) |
| + } |
| + if !strings.Contains(a.get("/store?problem=busy").Body, install.ErrBusy.Error()) { |
| + t.Error("busy message not shown") |
| + } |
| + rec.uninstallErr = store.ErrNoInstall |
| + if r := a.post("/store/scratchpad/uninstall", nil); r.Code != 303 || r.Location() != "/store" { |
| + t.Fatalf("gone: %d %q", r.Code, r.Location()) |
| + } |
| +} |
| + |
| +func TestMemberSeesStoreButCannotInstall(t *testing.T) { |
| + rec := newRecorder() |
| + a := newAppWith(t, rec, time.UTC) |
| + if _, _, err := store.Invite(context.Background(), a.DB, "maria@acme.co"); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.setInstall("scratchpad", "installed", "") |
| + if r := a.signIn("maria@acme.co"); r.Code != 303 { |
| + t.Fatalf("member sign-in: %d", r.Code) |
| + } |
| + page := a.get("/store") |
| + if page.Code != 200 || strings.Contains(page.Body, "/install") || strings.Contains(page.Body, "/uninstall") { |
| + t.Fatalf("member store page: %d\n%s", page.Code, page.Body) |
| + } |
| + for _, p := range []string{"/store/scratchpad/install", "/store/scratchpad/uninstall"} { |
| + if r := a.post(p, nil); r.Code != 403 { |
| + t.Errorf("POST %s: %d", p, r.Code) |
| + } |
| + } |
| + if r := a.get("/store/scratchpad/uninstall"); r.Code != 403 { |
| + t.Errorf("GET uninstall: %d", r.Code) |
| + } |
| + if i, u := rec.counts(); i != 0 || u != 0 { |
| + t.Fatal("member reached the installer") |
| + } |
| +} |