Fix Task 7 review: refresh, greeting, tests
The Store's install refresh is now a plain meta refresh in the page head, so it works with scripts on or off. The greeting is a pure function tested in package web and web.SetClock is gone. Copy-pending rows are one per string. Adds a blocking-installer test and Settings tests.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
9 files changed,
+143
−57
docs/copy-pending.md+18 −5internal/look/templates/layout.html+1 −0internal/web/clock.go+0 −14internal/web/home.go+13 −9internal/web/home_internal_test.go+36 −0internal/web/templates/store.html+2 −2internal/webtest/home_test.go+6 −20internal/webtest/settings_test.go+31 −0internal/webtest/store_test.go+36 −7
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index ec202c0..997fc72 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -67,17 +67,30 @@ 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 greeting, 05:00 to 11:59 | Good morning, {name}. | |
| +| internal/web/home.go | Home greeting, 12:00 to 17:59 | Good afternoon, {name}. | |
| +| internal/web/home.go | Home greeting, 18:00 to 23:59 | Good evening, {name}. | |
| +| internal/web/home.go | Home greeting, 00:00 to 04:59 | Hello, {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/home.html | Built-in tile | Store | |
| +| internal/web/templates/home.html | Built-in tile | People | |
| +| internal/web/templates/home.html | Built-in tile | 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 | Store button/label | Install | |
| +| internal/web/templates/store.html | Store button/label | Installing | |
| +| internal/web/templates/store.html | Store button/label | Try again | |
| +| internal/web/templates/store.html | Store button/label | Uninstall | |
| +| internal/web/templates/store.html | Store button/label | 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 | |
| +| internal/web/templates/uninstall.html | Confirmation button | Uninstall | |
| +| internal/web/templates/uninstall.html | Confirmation button | Keep it | |
| +| internal/web/templates/settings.html | Settings heading | Settings | |
| +| internal/web/templates/settings.html | Settings label | Base name | |
| +| internal/web/templates/settings.html | Settings label | Signed in as | |
| +| internal/web/templates/settings.html | Settings button | Sign out | |
diff --git a/internal/look/templates/layout.html b/internal/look/templates/layout.html| index 9e87875..2140a88 100644 |
| --- a/internal/look/templates/layout.html |
| +++ b/internal/look/templates/layout.html |
| @@ -4,6 +4,7 @@ |
| <meta charset="utf-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <title>{{block "title" .}}Base{{end}}</title> |
| +{{block "head" .}}{{end}} |
| <link rel="preload" href="/static/fonts/timeless/1.094/TimelessSansVF.woff2" as="font" type="font/woff2" crossorigin> |
| <link rel="stylesheet" href="{{asset "static/app.css"}}"> |
| </head> |
diff --git a/internal/web/clock.go b/internal/web/clock.go| deleted file mode 100644 |
| index 90273fb..0000000 |
| --- a/internal/web/clock.go |
| +++ /dev/null |
| @@ -1,14 +0,0 @@ |
| -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| index 8d582c1..629c1d9 100644 |
| --- a/internal/web/home.go |
| +++ b/internal/web/home.go |
| @@ -4,6 +4,7 @@ import ( |
| "html/template" |
| "net/http" |
| "strings" |
| + "time" |
| "amadan.net/carlos/base/internal/catalog" |
| "amadan.net/carlos/base/internal/install" |
| @@ -63,19 +64,22 @@ func initials(name string) string { |
| 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) |
| +// greeting says hello by the time of day where the Base is. Between midnight |
| +// and 05:00 "good morning" and "good evening" are both wrong, so it says a |
| +// plain hello. A nil loc uses t as given. |
| +func greeting(t time.Time, loc *time.Location, name string) string { |
| + if loc != nil { |
| + t = t.In(loc) |
| } |
| - part := "evening" |
| switch h := t.Hour(); { |
| + case h < 5: |
| + return "Hello, " + name + "." |
| case h < 12: |
| - part = "morning" |
| + return "Good morning, " + name + "." |
| case h < 18: |
| - part = "afternoon" |
| + return "Good afternoon, " + name + "." |
| } |
| - return "Good " + part + ", " + name + "." |
| + return "Good evening, " + name + "." |
| } |
| func (a *app) home(w http.ResponseWriter, r *http.Request) { |
| @@ -93,7 +97,7 @@ func (a *app) home(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| name := displayName(me) |
| - page := homePage{Name: a.cfg.Name, Greeting: a.greeting(name), Initials: initials(name)} |
| + page := homePage{Name: a.cfg.Name, Greeting: greeting(time.Now(), a.cfg.Timezone, name), Initials: initials(name)} |
| page.Tiles = []tile{ |
| {Name: "Store", Href: "/store", Tint: "teal", Icon: iconStore}, |
| {Name: "People", Href: "/people", Tint: "violet", Icon: iconPeople}, |
diff --git a/internal/web/home_internal_test.go b/internal/web/home_internal_test.go| new file mode 100644 |
| index 0000000..353e91d |
| --- /dev/null |
| +++ b/internal/web/home_internal_test.go |
| @@ -0,0 +1,36 @@ |
| +package web |
| + |
| +import ( |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestGreeting(t *testing.T) { |
| + london, err := time.LoadLocation("Europe/London") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + at := func(h, m int) time.Time { return time.Date(2026, 7, 1, h, m, 0, 0, time.UTC) } |
| + for _, c := range []struct { |
| + now time.Time |
| + loc *time.Location |
| + want string |
| + }{ |
| + {at(0, 0), time.UTC, "Hello, Paul."}, |
| + {at(4, 59), time.UTC, "Hello, Paul."}, |
| + {at(5, 0), time.UTC, "Good morning, Paul."}, |
| + {at(11, 59), time.UTC, "Good morning, Paul."}, |
| + {at(12, 0), time.UTC, "Good afternoon, Paul."}, |
| + {at(17, 59), time.UTC, "Good afternoon, Paul."}, |
| + {at(18, 0), time.UTC, "Good evening, Paul."}, |
| + {at(23, 59), time.UTC, "Good evening, Paul."}, |
| + {at(8, 0), london, "Good morning, Paul."}, // 09:00 BST |
| + {at(17, 0), london, "Good evening, Paul."}, // 18:00 BST |
| + {at(23, 30), london, "Hello, Paul."}, // 00:30 BST next day |
| + {at(8, 0), nil, "Good morning, Paul."}, // no zone: as given |
| + } { |
| + if got := greeting(c.now, c.loc, "Paul"); got != c.want { |
| + t.Errorf("greeting(%s, %v) = %q, want %q", c.now.Format(time.RFC3339), c.loc, got, c.want) |
| + } |
| + } |
| +} |
diff --git a/internal/web/templates/store.html b/internal/web/templates/store.html| index eb93550..3e9227d 100644 |
| --- a/internal/web/templates/store.html |
| +++ b/internal/web/templates/store.html |
| @@ -1,6 +1,6 @@ |
| {{define "title"}}Store · {{.Name}}{{end}} |
| -{{define "main"}}{{if .Installing}}<noscript><meta http-equiv="refresh" content="2"></noscript> |
| -{{end}}<main class="inner"> |
| +{{define "head"}}{{if .Installing}}<meta http-equiv="refresh" content="2">{{end}}{{end}} |
| +{{define "main"}}<main class="inner"> |
| <div class="sheet glass"> |
| <div class="crumb"><div class="wordmark">base</div><span>Store</span></div> |
| <h2>Store</h2> |
diff --git a/internal/webtest/home_test.go b/internal/webtest/home_test.go| index 34d3c02..888aaa8 100644 |
| --- a/internal/webtest/home_test.go |
| +++ b/internal/webtest/home_test.go |
| @@ -5,10 +5,8 @@ import ( |
| "regexp" |
| "strings" |
| "testing" |
| - "time" |
| "amadan.net/carlos/base/internal/store" |
| - "amadan.net/carlos/base/internal/web" |
| ) |
| func (a *testApp) setInstall(app, state, errMsg string) { |
| @@ -32,34 +30,22 @@ func stepDone(t *testing.T, body, label string) bool { |
| 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) |
| +func TestHomeShowsGreetingWithName(t *testing.T) { |
| + a := newApp(t) |
| 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) |
| + if body := a.get("/").Body; !regexp.MustCompile(`(Good (morning|afternoon|evening)|Hello), Paul\.`).MatchString(body) { |
| + t.Fatalf("no 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) |
| + if body := a.get("/").Body; !strings.Contains(body, ", Owner.") { |
| + t.Fatalf("greeting lacks capitalised local part:\n%s", body) |
| } |
| } |
diff --git a/internal/webtest/settings_test.go b/internal/webtest/settings_test.go| new file mode 100644 |
| index 0000000..4810107 |
| --- /dev/null |
| +++ b/internal/webtest/settings_test.go |
| @@ -0,0 +1,31 @@ |
| +package webtest |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestSettingsShowsBaseAndAddress(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + page := a.get("/settings") |
| + if page.Code != 200 { |
| + t.Fatalf("settings: %d", page.Code) |
| + } |
| + for _, want := range []string{"Acme", ownerAddress, "Sign out"} { |
| + if !strings.Contains(page.Body, want) { |
| + t.Errorf("settings lacks %q:\n%s", want, page.Body) |
| + } |
| + } |
| +} |
| + |
| +func TestSettingsSignOut(t *testing.T) { |
| + a := newApp(t) |
| + a.signedInOwner() |
| + if r := a.post("/auth/signout", nil); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { |
| + t.Fatalf("signout: %d %q", r.Code, r.Location()) |
| + } |
| + if r := a.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { |
| + t.Fatalf("still signed in: %d %q", r.Code, r.Location()) |
| + } |
| +} |
diff --git a/internal/webtest/store_test.go b/internal/webtest/store_test.go| index a27b3fc..4a76d0f 100644 |
| --- a/internal/webtest/store_test.go |
| +++ b/internal/webtest/store_test.go |
| @@ -49,24 +49,53 @@ func contains(body, want string) bool { |
| return strings.Contains(body, want) || strings.Contains(body, strings.ReplaceAll(want, "'", "'")) |
| } |
| +// blocker is an Installer whose Install waits for release, and reports whether |
| +// its context was cancelled by then. |
| +type blocker struct { |
| + noInstaller |
| + started chan struct{} |
| + release chan struct{} |
| + done chan error // ctx.Err() as Install returns |
| + gotApp chan string |
| +} |
| + |
| +func (b *blocker) Install(ctx context.Context, app string) error { |
| + b.gotApp <- app |
| + close(b.started) |
| + <-b.release |
| + b.done <- ctx.Err() |
| + return nil |
| +} |
| + |
| func TestStoreInstallButtonPostsAndRedirects(t *testing.T) { |
| - rec := newRecorder() |
| - a := newAppWith(t, rec, time.UTC) |
| + b := &blocker{started: make(chan struct{}), release: make(chan struct{}), done: make(chan error, 1), gotApp: make(chan string, 1)} |
| + a := newAppWith(t, b, 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) |
| } |
| + // Install blocks until release, so a 303 here proves the handler did not wait for it. |
| 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 <-b.started: |
| 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 app := <-b.gotApp; app != "scratchpad" { |
| + t.Fatalf("installed %q", app) |
| + } |
| + time.Sleep(50 * time.Millisecond) // the request has long ended |
| + close(b.release) |
| + select { |
| + case err := <-b.done: |
| + if err != nil { |
| + t.Fatalf("install context was cancelled with the request: %v", err) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("Install did not finish") |
| } |
| if r := a.post("/store/nope/install", nil); r.Code != 404 { |
| t.Fatalf("unknown app: %d", r.Code) |
| @@ -81,8 +110,8 @@ func TestStoreShowsInstallingWithRefresh(t *testing.T) { |
| } |
| 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, `<meta http-equiv="refresh" content="2">`) || strings.Contains(body, "<noscript>") { |
| + t.Fatalf("want a plain meta refresh, not wrapped in noscript:\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) |