| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/home/roster" |
| 13 | "amadan.net/oficina/home/teammode" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | ) |
| 16 | |
| 17 | func TestAgentDeckEditRestart(t *testing.T) { |
| 18 | f := newSlideAgentFixture(t) |
| 19 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 20 | in := updateDeckInput(t, f, sqid, []op{{"op": "add_slide", "layout": "flow"}}) |
| 21 | ack := updatedDeck(t, f.call(t, "slides_update_deck", in)) |
| 22 | var path string |
| 23 | if e := f.a.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); e != nil { |
| 24 | t.Fatal(e) |
| 25 | } |
| 26 | f.a.app.Close() |
| 27 | f.a.app.db.Close() |
| 28 | d, e := db.Open(path, quietLogger()) |
| 29 | if e != nil { |
| 30 | t.Fatal(e) |
| 31 | } |
| 32 | defer d.Close() |
| 33 | _, app, e := New(d, f.a.app.cfg) |
| 34 | if e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | defer app.Close() |
| 38 | f.a.app = app |
| 39 | f.install(t) |
| 40 | if got := updatedDeck(t, f.call(t, "slides_update_deck", in)); !reflect.DeepEqual(got, ack) { |
| 41 | t.Fatal("reopened edit receipt", got, ack) |
| 42 | } |
| 43 | } |
| 44 | func TestAgentDeckEditLimitsAndSchema(t *testing.T) { |
| 45 | f := newSlideAgentFixture(t) |
| 46 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 47 | in := updateDeckInput(t, f, sqid, []op{{"op": "add_slide", "layout": "flow"}}) |
| 48 | for _, rev := range []string{"01", "-1", "1.0", "9223372036854775808"} { |
| 49 | bad := in |
| 50 | bad.ExpectedRevision = rev |
| 51 | if r := f.call(t, "slides_update_deck", bad); r.Code != 422 { |
| 52 | t.Fatal(rev, r.Code) |
| 53 | } |
| 54 | } |
| 55 | for _, ops := range [][]op{nil, {{"op": "register_asset", "id": "image"}}, {{"op": "add_slide", "layout": "nonsense"}}, {{"op": "set_notes", "id": "x", "markdown": 3}}, {{"op": "set_phone", "slide": "s", "id": "b", "phone": map[string]any{"unknown": true}}}} { |
| 56 | bad := updateDeckInput(t, f, sqid, ops) |
| 57 | if r := f.call(t, "slides_update_deck", bad); r.Code != 422 { |
| 58 | t.Fatal("schema", r.Code, r.Body.String()) |
| 59 | } |
| 60 | } |
| 61 | many := in |
| 62 | many.Ops = nil |
| 63 | for range 101 { |
| 64 | many.Ops = append(many.Ops, in.Ops[0]) |
| 65 | } |
| 66 | if r := f.call(t, "slides_update_deck", many); r.Code != 422 { |
| 67 | t.Fatal("op cap", r.Code) |
| 68 | } |
| 69 | raw, _ := json.Marshal(in) |
| 70 | raw = bytes.Replace(raw, []byte(`"flow"`), []byte(`"\ud800"`), 1) |
| 71 | if r := f.raw(t, "slides_update_deck", raw); r.Code != 422 { |
| 72 | t.Fatal("invalid Unicode", r.Code) |
| 73 | } |
| 74 | raw, _ = json.Marshal(in) |
| 75 | raw = bytes.Replace(raw, []byte(`"layout":"flow"`), []byte(`"layout":"flow","layout":"canvas"`), 1) |
| 76 | if r := f.raw(t, "slides_update_deck", raw); r.Code != 422 { |
| 77 | t.Fatal("duplicate keys", r.Code) |
| 78 | } |
| 79 | enormous := updateDeckInput(t, f, sqid, []op{{"op": "set_meta", "title": strings.Repeat("a", slideAgentLimit)}}) |
| 80 | if r := f.call(t, "slides_update_deck", enormous); r.Code != 413 { |
| 81 | t.Fatal("request cap", r.Code) |
| 82 | } |
| 83 | var n int64 |
| 84 | f.a.app.db.G.Model(&slideAgentEdit{}).Count(&n) |
| 85 | if n != 0 { |
| 86 | t.Fatal("invalid request left receipt") |
| 87 | } |
| 88 | } |
| 89 | func TestAgentDeckEditResultRemainsReadable(t *testing.T) { |
| 90 | f := newSlideAgentFixture(t) |
| 91 | initial := initialSlideAgentInput() |
| 92 | initial.Slides[0].Body = strings.Repeat("x", 120<<10) |
| 93 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initial)) |
| 94 | _, v, _ := readEditedDeck(t, f, sqid) |
| 95 | var ops []op |
| 96 | for range 10 { |
| 97 | ops = append(ops, op{"op": "duplicate_slide", "id": v.Slides[0].ID}) |
| 98 | } |
| 99 | in := updateDeckInput(t, f, sqid, ops) |
| 100 | before := editDeckCounts(t, f) |
| 101 | if r := f.call(t, "slides_update_deck", in); r.Code != 413 { |
| 102 | t.Fatal("unreadable result committed", r.Code, r.Body.String()) |
| 103 | } |
| 104 | if !reflect.DeepEqual(before, editDeckCounts(t, f)) { |
| 105 | t.Fatal("oversized result effects") |
| 106 | } |
| 107 | } |
| 108 | func TestAgentDeckEditChecksWholeSharedTypeShape(t *testing.T) { |
| 109 | f := newSlideAgentFixture(t) |
| 110 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 111 | var d Deck |
| 112 | if e := f.a.app.db.G.Where("sqid=?", sqid).Take(&d).Error; e != nil { |
| 113 | t.Fatal(e) |
| 114 | } |
| 115 | // Simulate an old, invalid stored document whose tolerant JSON view still |
| 116 | // parses. A harmless edit must not append on top of an unknown shared mark. |
| 117 | poison, e := os.ReadFile("../slidestest/testdata/poison/unknownmark.yjs") |
| 118 | if e != nil { |
| 119 | t.Fatal(e) |
| 120 | } |
| 121 | if e = f.a.app.db.G.Create(&DeckUpdate{DeckID: d.ID, Seq: d.Seq + 1, Actor: "legacy", Bytes: poison}).Error; e != nil { |
| 122 | t.Fatal(e) |
| 123 | } |
| 124 | if e = f.a.app.db.G.Model(&Deck{}).Where("id=?", d.ID).Update("seq", d.Seq+1).Error; e != nil { |
| 125 | t.Fatal(e) |
| 126 | } |
| 127 | in := updateDeckInput(t, f, sqid, []op{{"op": "set_meta", "title": "Must not commit"}}) |
| 128 | before := editDeckCounts(t, f) |
| 129 | r := f.call(t, "slides_update_deck", in) |
| 130 | if r.Code != 422 { |
| 131 | t.Fatal("shared-type shape gate", r.Code, r.Body.String()) |
| 132 | } |
| 133 | if !reflect.DeepEqual(before, editDeckCounts(t, f)) { |
| 134 | t.Fatal("shape-invalid edit committed") |
| 135 | } |
| 136 | } |
| 137 | func testAgentDeckEditGuardLapse(t *testing.T) { |
| 138 | f, af := newTeamSlideAgent(t) |
| 139 | sqid := slideCreated(t, af.call(t, "slides_create_deck", teamSlideInput(af))) |
| 140 | in := updateDeckInput(t, af, sqid, []op{{"op": "add_slide", "layout": "flow"}}) |
| 141 | counts := editDeckCounts(t, af) |
| 142 | old := teamGuard |
| 143 | t.Cleanup(func() { teamGuard = old }) |
| 144 | teamGuard = func(ctx context.Context, authorize func(roster.State) error, write func() error) error { |
| 145 | return teammode.Guard(ctx, authorize, func() error { |
| 146 | if e := write(); e != nil { |
| 147 | return e |
| 148 | } |
| 149 | f.expired() |
| 150 | return nil |
| 151 | }) |
| 152 | } |
| 153 | if r := af.call(t, "slides_update_deck", in); r.Code != 503 || !reflect.DeepEqual(counts, editDeckCounts(t, af)) { |
| 154 | t.Fatal("last team guard omitted", r.Code, r.Body.String()) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestAgentDeckEditExactNumberRetryFingerprint(t *testing.T) { |
| 159 | f := newSlideAgentFixture(t) |
| 160 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 161 | _, v, _ := readEditedDeck(t, f, sqid) |
| 162 | s := v.Slides[0] |
| 163 | in := updateDeckInput(t, f, sqid, []op{{"op": "set_block", "slide": s.ID, "id": s.Blocks[0].ID, "props": map[string]any{"custom_number": json.Number("9007199254740993")}}}) |
| 164 | ack := updatedDeck(t, f.call(t, "slides_update_deck", in)) |
| 165 | same, _ := json.Marshal(in) |
| 166 | // Object whitespace is irrelevant, but adjacent integers above 2^53 must |
| 167 | // not collapse to the same retry fingerprint through float64 decoding. |
| 168 | if got := updatedDeck(t, f.raw(t, "slides_update_deck", append([]byte(" \n"), same...))); !reflect.DeepEqual(got, ack) { |
| 169 | t.Fatal("whitespace changed retry") |
| 170 | } |
| 171 | changed := bytes.Replace(same, []byte("9007199254740993"), []byte("9007199254740992"), 1) |
| 172 | if r := f.raw(t, "slides_update_deck", changed); r.Code != 409 { |
| 173 | t.Fatal("rounded retry identity", r.Code, r.Body.String()) |
| 174 | } |
| 175 | } |
| 176 | func TestAgentDeckEditRejectsInvalidNativeProjection(t *testing.T) { |
| 177 | f := newSlideAgentFixture(t) |
| 178 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 179 | before, v, raw := readEditedDeck(t, f, sqid) |
| 180 | s := v.Slides[0] |
| 181 | // The native op accepts int64, but Yjs stores a Number: MaxInt64 rounds |
| 182 | // past the host's integer field. Shared rich-text shape remains valid. |
| 183 | in := updateDeckInput(t, f, sqid, []op{{"op": "set_phone", "slide": s.ID, "id": s.Blocks[0].ID, "phone": map[string]any{"order": json.Number("9223372036854775807")}}}) |
| 184 | counts := editDeckCounts(t, f) |
| 185 | r := f.call(t, "slides_update_deck", in) |
| 186 | if r.Code != 503 { |
| 187 | t.Fatal("invalid projection accepted", r.Code, r.Body.String()) |
| 188 | } |
| 189 | after, _, got := readEditedDeck(t, f, sqid) |
| 190 | if after.Seq != before.Seq || !bytes.Equal(raw, got) || !reflect.DeepEqual(counts, editDeckCounts(t, f)) { |
| 191 | t.Fatal("invalid projection persisted") |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func TestAgentDeckEditWriteScopeAndUnboundReceipt(t *testing.T) { |
| 196 | f := newSlideAgentFixture(t) |
| 197 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 198 | in := updateDeckInput(t, f, sqid, []op{{"op": "add_slide", "layout": "flow"}}) |
| 199 | f.p.Scopes = []string{"slides:write"} |
| 200 | updatedDeck(t, f.call(t, "slides_update_deck", in)) |
| 201 | f.p.Scopes = []string{"slides:read"} |
| 202 | if r := f.call(t, "slides_update_deck", in); r.Code != 403 { |
| 203 | t.Fatal("read scope edited", r.Code) |
| 204 | } |
| 205 | f.p.Scopes = []string{"slides:write"} |
| 206 | for _, table := range []string{"slides_agent_receipts", "slides_connections", "slides_agent_state"} { |
| 207 | if e := f.a.app.db.G.Exec("DELETE FROM " + table).Error; e != nil { |
| 208 | t.Fatal(e) |
| 209 | } |
| 210 | } |
| 211 | if r := f.call(t, "slides_update_deck", in); r.Code != 503 { |
| 212 | t.Fatal("unbound edit receipt adopted", r.Code) |
| 213 | } |
| 214 | } |
| 215 | |