oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "errors"
5 "strings"
6 "testing"
7)
8
9func counts(t *testing.T, store *Store) map[string]int64 {
10 t.Helper()
11 out := map[string]int64{}
12 for _, table := range []string{"members", "pages", "events",
13 "daily_prints", "drafts", "revisions"} {
14 var rows int64
15 store.database.Table(table).Count(&rows)
16 out[table] = rows
17 }
18 return out
19}
20
21func TestAnEmptyFirstSaveLeavesNoTrace(t *testing.T) {
22 store := testStore(t)
23 for _, body := range []string{"", " \n\n "} {
24 _, err := store.SaveDaily(ctx(), ana, "2026-11-12", 0, body)
25 if !errors.Is(err, ErrEmpty) {
26 t.Fatalf("%q: %v", body, err)
27 }
28 }
29 for table, rows := range counts(t, store) {
30 if rows != 0 {
31 t.Errorf("an empty save left %d rows in %s", rows, table)
32 }
33 }
34}
35
36func TestOneNotePerPersonPerDay(t *testing.T) {
37 store := testStore(t)
38 index := NewIndex(store)
39 first, err := store.SaveDaily(ctx(), ana, "2026-11-12", 0, "one")
40 if err != nil {
41 t.Fatal(err)
42 }
43 again, err := store.SaveDaily(ctx(), ana, "2026-11-12", first.Revision,
44 "two")
45 if err != nil || again.ID != first.ID {
46 t.Fatalf("a second save made another note: %v", err)
47 }
48 if _, err := store.SaveDaily(ctx(), ana, "2026-11-12", 0,
49 "from a stale tab"); !errors.Is(err, ErrStale) {
50 t.Fatalf("a save over an unseen note: %v", err)
51 }
52 bos, _ := store.SaveDaily(ctx(), bo, "2026-11-12", 0, "Bo's")
53 if bos.ID == first.ID {
54 t.Fatal("two people share one daily note")
55 }
56 anaScope := scopeOf(t, store, ana)
57 day, written, _ := index.Day(ctx(), anaScope, "2026-11-12")
58 if !written || day.Body != "two" || day.Rung != RungPrivate {
59 t.Fatalf("Ana's day: %+v", day)
60 }
61 if _, err := index.View(ctx(), anaScope, bos.ID); !errors.Is(err,
62 ErrNotFound) {
63 t.Fatal("Ana can open Bo's daily note")
64 }
65}
66
67func scopeOf(t *testing.T, store *Store, who string) Scope {
68 t.Helper()
69 viewer, err := store.Viewer(ctx(), who, nil)
70 if err != nil {
71 t.Fatal(err)
72 }
73 return ScopeOf(viewer)
74}
75
76func TestMakeTeamPageMovesTheChosenBlocksOnly(t *testing.T) {
77 store := testStore(t)
78 index := NewIndex(store)
79 note, _ := store.SaveDaily(ctx(), ana, "2026-11-12", 0,
80 "Private thought.\n\n- Acme renews in March\n- Ask Tom\n\nMore.")
81 before := loadPage(t, store, note.ID)
82 shared, err := store.MakeTeamPage(ctx(), ana, note.ID,
83 note.Revision, []int{1, 2},
84 "Acme renewal")
85 if err != nil {
86 t.Fatal(err)
87 }
88 page := seen(t, index, Scope{}, shared.ID)
89 if page.Body != "- Acme renews in March\n- Ask Tom" ||
90 page.Rung != RungTeam {
91 t.Fatalf("the team page: %+v", page)
92 }
93 kept := seen(t, index, scopeOf(t, store, ana), note.ID).Body
94 if kept != "Private thought.\n\n[[Acme renewal]]\n\nMore." {
95 t.Fatalf("the note kept %q", kept)
96 }
97 var revisions int64
98 store.database.Model(&Revision{}).Where("page_id = ?", shared.ID).
99 Count(&revisions)
100 after := loadPage(t, store, note.ID)
101 if revisions != 1 || after.Rung != RungPrivate ||
102 after.Revision == before.Revision {
103 t.Fatal("the shared page is not a new page at its first revision")
104 }
105}
106
107// Gate 2, for "Make team page": a taken or date-shaped title asks for
108// another, and nothing is written.
109func TestGate2MakeTeamPageRefusesTakenAndDateTitles(t *testing.T) {
110 store := testStore(t)
111 mustCreate(t, store, "Acme", "")
112 note, _ := store.SaveDaily(ctx(), ana, "2026-11-12", 0, "a\n\nb")
113 before := counts(t, store)
114 for title, want := range map[string]error{"ACME": ErrTitleTaken,
115 "2026-11-13": ErrTitleDate, "2026-11-13": ErrTitleDate} {
116 if _, err := store.MakeTeamPage(ctx(), ana, note.ID,
117 note.Revision, []int{0},
118 title); !errors.Is(err, want) {
119 t.Errorf("%q: %v", title, err)
120 }
121 }
122 if _, err := store.MakeTeamPage(ctx(), ana, note.ID,
123 note.Revision, nil,
124 "Fresh"); !errors.Is(err, ErrNothingChosen) {
125 t.Errorf("nothing chosen: %v", err)
126 }
127 if after := counts(t, store); after["pages"] != before["pages"] ||
128 after["events"] != before["events"] {
129 t.Fatal("a refused share wrote something")
130 }
131}
132
133func TestOnlyTheOwnerSharesAndOnlyPrivateNotes(t *testing.T) {
134 store := testStore(t)
135 note, _ := store.SaveDaily(ctx(), ana, "2026-11-12", 0, "a")
136 if _, err := store.MakeTeamPage(ctx(), bo, note.ID,
137 note.Revision, []int{0},
138 "Stolen"); !errors.Is(err, ErrNotFound) {
139 t.Fatalf("Bo shared Ana's note: %v", err)
140 }
141 team := mustCreate(t, store, "Team", "x")
142 if _, err := store.MakeTeamPage(ctx(), ana, team.ID,
143 team.Revision, []int{0},
144 "Again"); !errors.Is(err, ErrNotFound) {
145 t.Fatalf("a team page was shared again: %v", err)
146 }
147}
148
149func TestADeletedDailyNoteComesBackAsItsDayOrAsScratch(t *testing.T) {
150 store := testStore(t)
151 note, _ := store.SaveDaily(ctx(), ana, "2026-11-12", 0, "first")
152 store.Delete(ctx(), ana, note.ID)
153 store.SaveDaily(ctx(), ana, "2026-11-12", 0, "second")
154 restored, err := store.Restore(ctx(), ana, note.ID, "")
155 if err != nil || restored.Kind != KindScratch {
156 t.Fatalf("restored %+v, %v", restored, err)
157 }
158 record, _ := store.OpenPage(loadPage(t, store, restored.ID))
159 if !strings.HasPrefix(record.Body, "# 2026-11-12\n\nfirst") {
160 t.Fatalf("restored body %q", record.Body)
161 }
162}
163
164func TestMayReadIsTheS2Rule(t *testing.T) {
165 store := testStore(t)
166 note, _ := store.SaveDaily(ctx(), ana, "2026-11-12", 0, "mine")
167 team := mustCreate(t, store, "Team", "")
168 nobody := func(string) bool { return false }
169 everyone := func(string) bool { return true }
170 for _, check := range []struct {
171 who string
172 page int64
173 admit func(string) bool
174 answer bool
175 }{
176 {ana, note.ID, nobody, true}, {bo, note.ID, everyone, false},
177 {bo, team.ID, everyone, true}, {bo, team.ID, nobody, false},
178 } {
179 got, err := store.MayRead(ctx(), check.who, check.page, check.admit)
180 if err != nil || got != check.answer {
181 t.Errorf("%s reading %d: %v, %v", check.who, check.page, got, err)
182 }
183 }
184}
185