package mapatest import ( "context" "net/http" "net/url" "strings" "testing" "time" "amadan.net/oficina/mapa/internal/mapa" ) // Pins are the viewer's own (level-up §3.2, §11 Q1 proposed): Bo's pin // is in Bo's sidebar and nobody else's, and a page cannot be pinned by // someone who may not open it. func TestPinsAreYoursAlone(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.") rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"1"}}) bo, ana := rig.page(t, Bo, "/pages"), rig.page(t, Ana, "/pages") if !strings.Contains(between(bo, "Pinned", ""), "Acme") { t.Fatal("Bo's pin is not in his sidebar") } if strings.Contains(ana, ">Pinned<") { t.Fatal("Bo's pin showed in Ana's sidebar") } if !strings.Contains(rig.page(t, Bo, acme), "Unpin from sidebar") { t.Fatal("a pinned page does not offer Unpin") } rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"0"}}) if strings.Contains(rig.page(t, Bo, "/pages"), ">Pinned<") { t.Fatal("Unpin left the page pinned") } scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}) private := scratch.Header().Get("Location") if got := rig.post(t, Bo, private+"/pin", url.Values{"pin": {"1"}}); got.Code != http.StatusNotFound { t.Fatalf("Bo pinned Ana's scratch page: %d", got.Code) } } // Recently edited is the pages you last wrote in, newest first, and // "Update links" is not writing (level-up §3.2). func TestRecentlyEditedIsWhatYouWroteIn(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Bo, "Acme", "One.") rig.createPage(t, Bo, "Beta", "Two.") recent := between(rig.page(t, Bo, acme), "Recently edited", "") if strings.Index(recent, "Beta") > strings.Index(recent, "Acme") || !strings.Contains(recent, "Acme") { t.Fatalf("Recently edited is not newest first:\n%s", recent) } if strings.Contains(rig.page(t, Ana, acme), "Recently edited") { t.Fatal("Ana, who wrote nothing, has Recently edited") } } // Recently edited is yours only (review L2): what a teammate wrote, and // a teammate's later edit to a page you wrote, change nothing in it. func TestRecentlyEditedIsYoursOnly(t *testing.T) { rig := newRig(t) mine := rig.createPage(t, Bo, "Mine", "Bo's.") rig.createPage(t, Ana, "Theirs", "Ana's.") revision := revisionAs(t, rig, Ana, mine) rig.post(t, Ana, mine, url.Values{"revision": {revision}, "body": {"Ana was here."}}) recent := between(rig.page(t, Bo, "/pages"), "Recently edited", "") if strings.Contains(recent, "Theirs") || !strings.Contains(recent, "Mine") { t.Fatalf("Bo's Recently edited:\n%s", recent) } anas := between(rig.page(t, Ana, "/pages"), "Recently edited", "") if strings.Index(anas, "Mine") > strings.Index(anas, "Theirs") { t.Fatalf("Ana's edit to Mine is not her newest:\n%s", anas) } } // The sealed pins are one length however many pages are pinned (review // L11), so the file does not count them. func TestSealedPinsAreOneLength(t *testing.T) { rig := newRig(t) one := rig.createPage(t, Ana, "One", "1.") two := rig.createPage(t, Ana, "Two", "2.") rig.post(t, Ana, one+"/pin", url.Values{"pin": {"1"}}) rig.post(t, Bo, one+"/pin", url.Values{"pin": {"1"}}) rig.post(t, Bo, two+"/pin", url.Values{"pin": {"1"}}) var lengths []int err := rig.db.G.Raw("SELECT length(sealed_pins) FROM members " + "WHERE sealed_pins IS NOT NULL").Scan(&lengths).Error if err != nil || len(lengths) != 2 || lengths[0] != lengths[1] { t.Fatalf("sealed pins lengths %v (%v)", lengths, err) } } // The scoped accessor's own guard counts (review L1): asked by id for a // page outside the scope, it answers nothing, even when the caller's // list of ids was wrong. Without the guard this test fails. func TestTheScopedAccessorGuardsEveryRead(t *testing.T) { rig := newRig(t) rig.createPage(t, Bo, "Bo's", "So Bo is a member.") rig.post(t, Ana, "/scratch", url.Values{"body": {"Ana alone."}}) var private []int64 err := rig.db.G.Raw("SELECT id FROM pages WHERE rung = ?", "private").Scan(&private).Error if err != nil || len(private) != 1 { t.Fatalf("Ana's private page: %v (%v)", private, err) } ctx := context.Background() bo, err := rig.built.Store.Viewer(ctx, Bo, time.UTC) if err != nil { t.Fatal(err) } if got := rig.built.Index.SideLinks(ctx, mapa.ScopeOf(bo), private, 0, ""); len(got) != 0 { t.Fatal("the accessor read a page outside its scope") } } // Pin and Unpin from a sidebar row's ⋯ come back to the screen the row // was on, never off the site; the bar's Pin still goes to the page // (build note 88). func TestPinFromARowComesBack(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.") cases := map[string]string{"/pages": "/pages", "/d/2026-11-14?x=1": "/d/2026-11-14?x=1", "": acme, "//evil.example/": acme, "https://evil.example": acme} for back, want := range cases { got := rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"1"}, "return": {back}}) if where := got.Header().Get("Location"); where != want { t.Errorf("return %q: went to %q, want %q", back, where, want) } } }