| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // IMG1's attach half and IMG7's attaching (images spec §3.2): a commit |
| 9 | // attaches only the committer's own pending images that it introduces; |
| 10 | // Revert, Restore and the bulk verbs attach nothing. |
| 11 | |
| 12 | func page(t *testing.T, store *Store, pageID int64) Page { |
| 13 | t.Helper() |
| 14 | var found Page |
| 15 | if err := store.database.First(&found, pageID).Error; err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | return found |
| 19 | } |
| 20 | |
| 21 | func TestACommitAttachesOnlyItsOwnIntroducedImages(t *testing.T) { |
| 22 | store, _, _ := imageRig(t) |
| 23 | own := upload(t, store, ana, 1) |
| 24 | theirs := upload(t, store, bo, 2) |
| 25 | made := mustCreate(t, store, "Pilot", |
| 26 | own.Markdown("mine")+"\n\n"+theirs.Markdown("Bo's")) |
| 27 | if attachedTo(t, store, own) != made.ID { |
| 28 | t.Error("Ana's own upload was not attached by her create") |
| 29 | } |
| 30 | if attachedTo(t, store, theirs) != 0 { |
| 31 | t.Error("Bo's pending image was attached by Ana's commit") |
| 32 | } |
| 33 | if _, err := store.UpdateLinks(ctx(), bo, made.ID, nil); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if attachedTo(t, store, theirs) != 0 { |
| 37 | t.Error("a bulk verb attached an image") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // plant is IMG1's setup: Ana has a pending upload, and Bo types its id |
| 42 | // into a team page. It returns the image, the page, and the revision |
| 43 | // that held Bo's text. |
| 44 | func plant(t *testing.T, store *Store) (Image, Page, int64) { |
| 45 | t.Helper() |
| 46 | pending := upload(t, store, ana, 3) |
| 47 | made := mustCreate(t, store, "Shared", "start") |
| 48 | _, err := store.Commit(ctx(), bo, made.ID, made.Revision, |
| 49 | "start\n\n"+pending.Markdown("planted")) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | planted := page(t, store, made.ID) |
| 54 | if attachedTo(t, store, pending) != 0 { |
| 55 | t.Fatal("Bo's commit attached Ana's image") |
| 56 | } |
| 57 | return pending, planted, planted.Revision |
| 58 | } |
| 59 | |
| 60 | func TestAnasOwnCommitOfAPlantedIDAttachesNothing(t *testing.T) { |
| 61 | store, _, _ := imageRig(t) |
| 62 | pending, planted, revision := plant(t, store) |
| 63 | body := "start, edited by Ana\n\n" + pending.Markdown("planted") |
| 64 | if _, err := store.Commit(ctx(), ana, planted.ID, revision, |
| 65 | body); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if attachedTo(t, store, pending) != 0 { |
| 69 | t.Error("Ana's commit published an id Bo planted") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // unplant has Bo take his text out again. |
| 74 | func unplant(t *testing.T, store *Store, planted Page, revision int64) { |
| 75 | t.Helper() |
| 76 | if _, err := store.Commit(ctx(), bo, planted.ID, revision, |
| 77 | "start"); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestAnasRevertToBosTextAttachesNothing(t *testing.T) { |
| 83 | store, _, _ := imageRig(t) |
| 84 | pending, planted, revision := plant(t, store) |
| 85 | unplant(t, store, planted, revision) |
| 86 | if err := store.Revert(ctx(), ana, planted.ID, revision); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if attachedTo(t, store, pending) != 0 { |
| 90 | t.Error("Ana's Revert published an id Bo planted") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestAnasRestoreOfBosPageAttachesNothing(t *testing.T) { |
| 95 | store, _, _ := imageRig(t) |
| 96 | pending, planted, _ := plant(t, store) |
| 97 | if err := store.Delete(ctx(), bo, planted.ID); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | restored, err := store.Restore(ctx(), ana, planted.ID, "") |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if attachedTo(t, store, pending) != 0 || restored.ID == 0 { |
| 105 | t.Error("Ana's Restore published an id Bo planted") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestAnasDraftStartedOnBosTextAttachesNothing(t *testing.T) { |
| 110 | for _, resolve := range []bool{false, true} { |
| 111 | store, _, _ := imageRig(t) |
| 112 | pending, planted, revision := plant(t, store) |
| 113 | viewer := Viewer{Address: ana, MemberID: member(t, store, ana)} |
| 114 | key := DraftKey{PageID: planted.ID} |
| 115 | body := "start, Ana's draft\n\n" + pending.Markdown("planted") |
| 116 | version, err := store.SaveDraft(ctx(), ana, key, revision, 0, body) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | unplant(t, store, planted, revision) |
| 121 | if resolve { |
| 122 | current := page(t, store, planted.ID).Revision |
| 123 | _, err = store.Resolve(ctx(), ana, planted.ID, current, version, |
| 124 | body) |
| 125 | } else { |
| 126 | _, _, err = store.CommitDraft(ctx(), viewer, key, version) |
| 127 | } |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if attachedTo(t, store, pending) != 0 { |
| 132 | t.Errorf("resolve %v: Ana's draft published an id Bo planted", |
| 133 | resolve) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Fable M1 on 49d8df3: a pending image a draft keeps past its seven |
| 139 | // days is still its owner's. The window is the sweep's alone: while the |
| 140 | // row exists its owner reads it, sees it drawn, and attaches it. |
| 141 | func TestAPendingImageADraftKeepsStaysItsOwners(t *testing.T) { |
| 142 | store, _, now := imageRig(t) |
| 143 | kept := upload(t, store, ana, 4) |
| 144 | body := "Late, but kept: " + kept.Markdown("kept") |
| 145 | key := DraftKey{Date: "2026-11-17"} |
| 146 | if _, err := store.SaveDraft(ctx(), ana, key, 0, 0, body); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | *now = now.Add(PendingWindow + 24*time.Hour) |
| 150 | if err := store.Sweep(ctx()); err != nil || row(t, store, kept).ID == 0 { |
| 151 | t.Fatalf("the sweep took an image a draft names: %v", err) |
| 152 | } |
| 153 | viewer := Viewer{Address: ana, MemberID: member(t, store, ana)} |
| 154 | if _, err := store.ReadableImage(ctx(), viewer, |
| 155 | kept.PublicID); err != nil { |
| 156 | t.Fatalf("its owner cannot read it: %v", err) |
| 157 | } |
| 158 | // Drawn, listed and removable too (Fable N4 on 4c95863). |
| 159 | states := NewIndex(store).ImageStates(ctx(), ScopeOf(viewer), 0, |
| 160 | []string{kept.PublicID}) |
| 161 | if !states[kept.PublicID].Available { |
| 162 | t.Error("its owner's draft draws it as not available") |
| 163 | } |
| 164 | listed, err := store.PageImages(ctx(), viewer, 0, body) |
| 165 | if err != nil || len(listed) != 1 { |
| 166 | t.Errorf("Images on this page lists %v: %v", listed, err) |
| 167 | } |
| 168 | if _, err := store.RemovableImage(ctx(), viewer, |
| 169 | kept.PublicID); err != nil { |
| 170 | t.Errorf("its owner cannot remove it: %v", err) |
| 171 | } |
| 172 | day, err := store.SaveDaily(ctx(), ana, "2026-11-17", 0, body) |
| 173 | if err != nil { |
| 174 | t.Fatal(err) |
| 175 | } |
| 176 | if attachedTo(t, store, kept) != day.ID { |
| 177 | t.Error("its owner's commit did not attach it") |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestTheStampIsSetOnceAndClearedByRevert(t *testing.T) { |
| 182 | store, _, now := imageRig(t) |
| 183 | shown := upload(t, store, ana, 5) |
| 184 | made := mustCreate(t, store, "Stamps", shown.Markdown("x")) |
| 185 | first := made.Revision |
| 186 | if _, err := store.Commit(ctx(), ana, made.ID, first, |
| 187 | "gone"); err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | stamped := row(t, store, shown).UnreferencedAt |
| 191 | if stamped == nil { |
| 192 | t.Fatal("taking an image out of the text did not stamp it") |
| 193 | } |
| 194 | *now = now.Add(24 * 3600e9) |
| 195 | if _, err := store.Commit(ctx(), ana, made.ID, |
| 196 | page(t, store, made.ID).Revision, "gone again"); err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | if again := row(t, store, shown).UnreferencedAt; !again.Equal(*stamped) { |
| 200 | t.Error("a later edit moved the stamp") |
| 201 | } |
| 202 | if err := store.Revert(ctx(), ana, made.ID, first); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | if row(t, store, shown).UnreferencedAt != nil { |
| 206 | t.Error("Revert did not clear the stamp") |
| 207 | } |
| 208 | } |
| 209 | |