package fichastest import ( "io" "log/slog" "net/http" "net/http/httptest" "net/url" "path/filepath" "regexp" "strings" "testing" fichas "amadan.net/oficina/fichas/internal/fichas" "amadan.net/rastrillo/rastrillo/db" ) // appWith is newApp with the config adjusted first. func appWith(t *testing.T, adjust func(*fichas.Config)) http.Handler { t.Helper() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) cfg := testConfig(t) adjust(&cfg) mux, err := fichas.AppForTest(d, cfg, logger, TestOwner) if err != nil { t.Fatalf("AppForTest: %v", err) } return mux } // The account is the suite's account menu, estilo's, carrying what every // app's carries. It sat at the rail's foot (Paul, 2026-10-01); the level-up // moves it to the end of the bar, as Docs', Slides' and Mapa's (level-up // §2, §3.1), and the rail's foot holds the fold. func TestTheBarEndsWithTheAccountMenu(t *testing.T) { body := get(t, newApp(t), "/").Body.String() foot := body[strings.Index(body, "
")] for _, want := range []string{ `
`, `aria-label="Account: ` + TestOwner + `"`, `Signed in as`, `
`, ``, } { if !strings.Contains(foot, want) { t.Errorf("the bar lacks %s:\n%s", want, foot) } } if n := strings.Count(body, ">Sign out<"); n != 1 { t.Errorf("Sign out appears %d times; once, in the account menu", n) } if strings.Contains(body, `rst-btn="ghost" type="submit">Sign out`) { t.Error("the old bordered Sign out button is still in the rail") } // Nothing Fichas does not have: no Home connect, no credits page, and // no hub directory configured here. for _, absent := range []string{"Connect your Oficina account", "Open source", "All apps", "estilo-menu-heading>Help"} { if strings.Contains(body, absent) { t.Errorf("the account menu offers %q, which Fichas does not have", absent) } } pressed := regexp.MustCompile(`value="(light|dark|system)" aria-pressed="(true|false)"`).FindAllStringSubmatch(foot, -1) if len(pressed) != 3 { t.Fatalf("want Light, Dark and System, got %v", pressed) } for _, p := range pressed { if (p[1] == "system") != (p[2] == "true") { t.Errorf("%s pressed=%s; with no preference only System is checked", p[1], p[2]) } } } // "All apps" goes to the hub directory when one is configured. func TestAllAppsWhenThereIsAHubDirectory(t *testing.T) { h := appWith(t, func(c *fichas.Config) { c.Directory = "https://oficina.example" }) body := get(t, h, "/").Body.String() if !strings.Contains(body, `All apps`) { t.Errorf("no All apps row to the hub directory:\n%s", body) } } // Appearance is a per-person preference set by a form: estilo's handler, // inside the CSRF check, and every page — the shell and the plain layout // alike — draws the chosen scheme from the first byte. func TestAppearanceIsChosenInTheAccountMenu(t *testing.T) { h := newApp(t) // No same-origin evidence: refused by the CSRF check like any POST. req := httptest.NewRequest(http.MethodPost, "/appearance", strings.NewReader("mode=dark&return_to=%2Fcards%2Fnew")) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") rec := httptest.NewRecorder() h.ServeHTTP(rec, req) if rec.Code != http.StatusForbidden { t.Errorf("a cross-site Appearance POST answered %d; want 403", rec.Code) } rec = post(t, h, "/appearance", url.Values{"mode": {"dark"}, "return_to": {"/cards/new"}}) if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/cards/new" { t.Fatalf("POST /appearance: %d to %q; want 303 back to /cards/new", rec.Code, rec.Header().Get("Location")) } // return_to is the page the menu was opened on, carried in the form as // in every app (spec §8.3), and only ever a path on this origin: // anything else lands on the list. for _, off := range []string{"https://evil.example/", "//evil.example/", "/\\evil.example", "/\t/evil.example", "javascript:alert(1)", ""} { r := post(t, h, "/appearance", url.Values{"mode": {"dark"}, "return_to": {off}}) if loc := r.Header().Get("Location"); r.Code != http.StatusSeeOther || loc != "/" { t.Errorf("return_to %q: %d to %q; want 303 to /", off, r.Code, loc) } } cookies := rec.Result().Cookies() if len(cookies) != 1 || cookies[0].Value != "dark" { t.Fatalf("POST /appearance set %v; want the dark preference", cookies) } for _, path := range []string{"/", "/signin?sent=1", "/signed-out"} { r := httptest.NewRequest(http.MethodGet, path, nil) r.AddCookie(cookies[0]) rec := httptest.NewRecorder() h.ServeHTTP(rec, r) if !strings.Contains(rec.Body.String(), ``) { t.Errorf("%s does not draw the dark scheme the person chose", path) } if path == "/" && !strings.Contains(rec.Body.String(), `value="dark" aria-pressed="true"`) { t.Error("the account menu does not check Dark") } } // No preference, no attribute: the scheme follows the system. if strings.Contains(get(t, h, "/").Body.String(), "data-theme") { t.Error("data-theme drawn without a preference") } }