package mapatest import ( "net/http" "net/url" "strings" "testing" "time" ) // traceTables is every table a written day would leave a row in. var traceTables = []string{"members", "pages", "drafts", "events", "daily_prints", "revisions", "title_prints"} // Gate 10: opening today's note and leaving without typing creates no // row, no draft, no event, and so no S1 entry (S1 is the event log). func TestGate10AnUnwrittenDayLeavesNoTrace(t *testing.T) { rig := newRig(t) for _, path := range []string{"/", "/d/2026-11-12", "/d/2026-11-12/edit", "/d/1999-01-01", "/settings", "/scratch/new"} { if code := rig.get(t, Ana, path).Code; code != http.StatusOK { t.Fatalf("GET %s: %d", path, code) } } empty := rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {" "}}) if empty.Code != http.StatusUnprocessableEntity { t.Fatalf("an empty save: %d", empty.Code) } for _, table := range traceTables { if rows := rig.count(t, table); rows != 0 { t.Errorf("visiting left %d rows in %s", rows, table) } } } func TestTheFirstSaveCreatesTheDay(t *testing.T) { rig := newRig(t) saved := rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Called Acme."}}) if saved.Header().Get("Location") != "/d/2026-11-12" { t.Fatalf("save: %d %q", saved.Code, saved.Header().Get("Location")) } if !strings.Contains(rig.page(t, Ana, "/d/2026-11-12"), "Called Acme.") { t.Fatal("the note does not show its text") } if strings.Contains(rig.page(t, Bo, "/d/2026-11-12"), "Called Acme.") { t.Fatal("Bo's 12 November shows Ana's note") } } // Date links open the reader's own note (ยง3.4, ruled): the same link in a // team page leads Ana to hers and Bo to his. func TestADateLinkOpensTheReadersOwnNote(t *testing.T) { rig := newRig(t) rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Ana writes here."}}) rig.post(t, Bo, "/d/2026-11-12", url.Values{"body": {"Bo writes here."}}) plan := rig.createPage(t, Ana, "Plan", "See [[2026-11-12]].") for _, who := range []string{Ana, Bo} { drawn := rig.page(t, who, plan) if !strings.Contains(drawn, `href="/d/2026-11-12" mapa-chip="date"`) || !strings.Contains(drawn, `Thursday, 12 November`) { t.Fatal("the date link does not lead to /d/2026-11-12, " + "named in words") } } anaDay := rig.page(t, Ana, "/d/2026-11-12") boDay := rig.page(t, Bo, "/d/2026-11-12") if !strings.Contains(anaDay, "Ana writes here.") || !strings.Contains(boDay, "Bo writes here.") || strings.Contains(boDay, "Ana writes here.") { t.Fatal("a date link reached someone else's note") } if !strings.Contains(boDay, ">Plan") { t.Fatal("the team page linking the day is not a backlink") } } // "Today" is in the person's own time zone, defaulting to the team's. func TestTodayIsInThePersonsTimeZone(t *testing.T) { rig := newRig(t) rig.built.Store.SetClock(func() time.Time { return time.Date(2026, 11, 12, 23, 30, 0, 0, time.UTC) }) if !strings.Contains(rig.page(t, Ana, "/"), "Thursday 12 November") { t.Fatal("the team's zone (Dublin) does not make it the 12th") } set := rig.post(t, Ana, "/settings", url.Values{"zone": {"Pacific/Auckland"}}) if set.Code != http.StatusSeeOther { t.Fatalf("setting a zone: %d", set.Code) } if !strings.Contains(rig.page(t, Ana, "/"), "Friday 13 November") { t.Fatal("Ana's own zone is not used") } if !strings.Contains(rig.page(t, Bo, "/"), "Thursday 12 November") { t.Fatal("Ana's setting changed Bo's day") } bad := rig.post(t, Ana, "/settings", url.Values{"zone": {"Mars/Base"}}) if bad.Code != http.StatusUnprocessableEntity { t.Fatalf("a bad zone: %d", bad.Code) } } func TestMakeTeamPageFromADailyNote(t *testing.T) { rig := newRig(t) rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Mine.\n\nThe team should know this."}}) day := rig.page(t, Ana, "/d/2026-11-12") share := between(day, `href="/p/`, `/share"`) form := rig.page(t, Ana, "/p/"+share+"/share") if !strings.Contains(form, `name="block" value="1"`) { t.Fatal("the share form offers no blocks") } made := rig.post(t, Ana, "/p/"+share+"/share", url.Values{"title": {"Known"}, "block": {"1"}, "revision": {formRevision(form)}}) if made.Code != http.StatusSeeOther { t.Fatalf("share: %d", made.Code) } if !strings.Contains(rig.page(t, Bo, made.Header().Get("Location")), "The team should know this.") { t.Fatal("Bo cannot read the shared page") } again := rig.page(t, Ana, "/p/"+share+"/share") taken := rig.post(t, Ana, "/p/"+share+"/share", url.Values{"title": {"known"}, "block": {"0"}, "revision": {formRevision(again)}}) if taken.Code != http.StatusUnprocessableEntity || !strings.Contains(taken.Body.String(), `value="0" checked`) { t.Fatal("a taken title must ask again, keeping the choice") } } func TestAPrivatePageIsNotThereForAnyoneElse(t *testing.T) { rig := newRig(t) created := rig.post(t, Ana, "/scratch", url.Values{"body": {"secret"}}) path := created.Header().Get("Location") if rig.get(t, Ana, path).Code != http.StatusOK { t.Fatal("Ana cannot open her scratch page") } for _, suffix := range []string{"", "/edit", "/delete", "/share"} { if code := rig.get(t, Bo, path+suffix).Code; code != 404 { t.Errorf("Bo GET %s%s: %d, want 404", path, suffix, code) } } // Ana's private page and a page that does not exist must answer Bo // alike, every write route included (review N3). missing := "/p/zzzzzzzzzz" for _, suffix := range []string{"", "/delete", "/share", "/draft", "/draft/save", "/draft/discard", "/draft/scratch", "/resolve", "/blocks", "/rename", "/links", "/mentions"} { form := url.Values{"body": {"x"}, "revision": {"1"}, "title": {"T"}, "block": {"0"}, "version": {"0"}} private := rig.post(t, Bo, path+suffix, form).Code absent := rig.post(t, Bo, missing+suffix, form).Code if private != http.StatusNotFound || absent != private { t.Errorf("Bo POST %s: %d on Ana's page, %d on none; want 404 "+ "for both", suffix, private, absent) } } } // formRevision is the revision a form names: the one its blocks or its // text were drawn from. func formRevision(form string) string { return between(form, `name="revision" value="`, `"`) }