oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package calendartest
4
5import (
6 "context"
7 "io"
8 "log/slog"
9 "net/http"
10 "path/filepath"
11 "testing"
12 "time"
13
14 "amadan.net/rastrillo/rastrillo"
15 "amadan.net/rastrillo/rastrillo/harness"
16 "github.com/chromedp/chromedp"
17
18 calendar "amadan.net/oficina/calendar/internal/calendar"
19)
20
21// TestPlacedChipsSurviveTheProductionCSP: Calendar in Chromium behind the
22// production serving stack (App.ServeOptions through rastrillo.Handler:
23// the security headers and Calendar's CSP), standalone. Two events on
24// one day, at 09:00 for an hour and at 14:00 for two, paint where the
25// packer put them: the later one lower, twice as tall, and a team
26// calendar's chip in a different colour from a personal one.
27//
28// Every one of those is a style attribute. Under rastrillo fb806a2's
29// default style-src the browser drops them all — both chips stack at the
30// top of the column at their content height, and every chip falls back
31// to the stylesheet's default hue — while the other browser checks,
32// which serve App.Handler() with no CSP at all, stay green.
33func TestPlacedChipsSurviveTheProductionCSP(t *testing.T) {
34 var app *calendar.App
35 rig := harness.New(t, func(origin string) http.Handler {
36 a, err := calendar.New(calendar.Config{
37 DBPath: filepath.Join(t.TempDir(), "calendar.db"),
38 Env: "development",
39 Origin: origin,
40 Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
41 })
42 if err != nil {
43 t.Fatalf("calendar.New: %v", err)
44 }
45 t.Cleanup(func() { a.Close() })
46 app = a
47 h, closeFn, err := rastrillo.Handler(a.ServeOptions(rastrillo.Options{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}))
48 if err != nil {
49 t.Fatalf("rastrillo.Handler: %v", err)
50 }
51 if closeFn != nil {
52 t.Cleanup(func() { _ = closeFn() })
53 }
54 return h
55 })
56 session := signInOverHTTP(t, app, rig.Origin, "host@example.com")
57
58 ctx := context.Background()
59 st := app.StoreForTest()
60 host := app.PersonByAddressForTest(t, "host@example.com")
61 personal, err := st.DefaultCalendarID(ctx, host.ID)
62 if err != nil {
63 t.Fatal(err)
64 }
65 cals, err := st.Calendars(ctx, host.ID)
66 if err != nil {
67 t.Fatal(err)
68 }
69 var team int64
70 for _, c := range cals {
71 if c.Kind == "team" {
72 team = c.ID
73 }
74 }
75 if team == 0 || team == personal {
76 t.Fatalf("no team calendar beside the personal one: %+v", cals)
77 }
78 for _, e := range []calendar.EventInput{
79 {CalendarID: personal, Title: "Morning", StartLocal: "2026-09-10T09:00", EndLocal: "2026-09-10T10:00", TZ: "UTC"},
80 {CalendarID: team, Title: "Afternoon", StartLocal: "2026-09-10T14:00", EndLocal: "2026-09-10T16:00", TZ: "UTC"},
81 } {
82 if _, err := st.CreateEvent(ctx, host.ID, e); err != nil {
83 t.Fatal(err)
84 }
85 }
86
87 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
88 t.Helper()
89 c, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
90 defer cancel()
91 if err := chromedp.Do(c, actions...); err != nil {
92 t.Fatalf("%s: %v", what, err)
93 }
94 }
95 run("sign in", setCookie(rig.Origin, session))
96
97 var day struct {
98 MorningTop, MorningHeight, AfternoonTop, AfternoonHeight float64
99 MorningBorder, AfternoonBorder string
100 }
101 run("day view",
102 chromedp.Navigate(rig.Origin+"/day/2026-09-10"),
103 chromedp.WaitVisible(chromedp.CSS(`[cal-placed]`)),
104 evalInto(&day, `(() => {
105 const placed = (title) => [...document.querySelectorAll('[cal-placed]')].find(p => p.textContent.includes(title));
106 const box = (title) => placed(title).getBoundingClientRect();
107 const chip = (title) => getComputedStyle(placed(title).querySelector('[cal-chip]'));
108 return {
109 MorningTop: box('Morning').top, MorningHeight: box('Morning').height,
110 AfternoonTop: box('Afternoon').top, AfternoonHeight: box('Afternoon').height,
111 MorningBorder: chip('Morning').borderInlineStartColor,
112 AfternoonBorder: chip('Afternoon').borderInlineStartColor,
113 };
114 })()`),
115 )
116 if day.AfternoonTop-day.MorningTop < 100 {
117 t.Errorf("the 14:00 chip sits %.0fpx below the 09:00 one: its position was refused", day.AfternoonTop-day.MorningTop)
118 }
119 if ratio := day.AfternoonHeight / day.MorningHeight; ratio < 1.8 || ratio > 2.2 {
120 t.Errorf("a two-hour chip is %.0fpx and a one-hour chip %.0fpx: their heights were refused", day.AfternoonHeight, day.MorningHeight)
121 }
122 if day.MorningBorder == day.AfternoonBorder {
123 t.Errorf("the personal and team chips paint the same hue %s: --cal-hue was refused", day.MorningBorder)
124 }
125}
126