| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | access "amadan.net/oficina/agent-access.git" |
| 5 | "encoding/json" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestAgentDeckVisibilityAndExactCursor(t *testing.T) { |
| 12 | f := newSlideAgentFixture(t) |
| 13 | const base = int64(9007199254740992) |
| 14 | rows := []Deck{{ID: base + 1, Sqid: "23456789ab", Title: "owned", OwnerID: f.u.ID, Rung: rungPrivate, Theme: "paper"}, {ID: base + 2, Sqid: "23456789ac", Title: "team", OwnerID: 99, Rung: rungTeam, Theme: "paper"}, {ID: base + 3, Sqid: "23456789ad", Title: "granted", OwnerID: 99, Rung: rungGranted, Theme: "paper"}, {ID: base + 4, Sqid: "23456789ae", Title: "PRIVATE_DENIED", OwnerID: 99, Rung: rungPrivate, Theme: "paper"}} |
| 15 | if e := f.a.app.db.G.Create(&rows).Error; e != nil { |
| 16 | t.Fatal(e) |
| 17 | } |
| 18 | if e := f.a.app.db.G.Exec("INSERT INTO deck_grants(deck_id,user_id) VALUES(?,?)", base+3, f.u.ID).Error; e != nil { |
| 19 | t.Fatal(e) |
| 20 | } |
| 21 | var page struct { |
| 22 | Decks []struct { |
| 23 | Sqid string `json:"sqid"` |
| 24 | } |
| 25 | Next string `json:"next_cursor"` |
| 26 | } |
| 27 | r := f.call(t, "slides_list_decks", map[string]any{"team": "blue", "limit": 1}) |
| 28 | if e := json.Unmarshal(r.Body.Bytes(), &page); e != nil || r.Code != 200 || len(page.Decks) != 1 || page.Decks[0].Sqid != rows[2].Sqid || page.Next != strconv.FormatInt(base+3, 10) { |
| 29 | t.Fatal(r.Code, r.Body.String(), e) |
| 30 | } |
| 31 | r = f.call(t, "slides_list_decks", map[string]any{"team": "blue", "limit": 1, "cursor": page.Next}) |
| 32 | if e := json.Unmarshal(r.Body.Bytes(), &page); e != nil || r.Code != 200 || len(page.Decks) != 1 || page.Decks[0].Sqid != rows[1].Sqid || page.Next != strconv.FormatInt(base+2, 10) { |
| 33 | t.Fatal(r.Code, r.Body.String(), e) |
| 34 | } |
| 35 | if r := f.call(t, "slides_get_deck", map[string]any{"team": "blue", "sqid": rows[3].Sqid}); r.Code != 404 { |
| 36 | t.Fatal(r.Code) |
| 37 | } |
| 38 | for _, cursor := range []string{"09223372036854775807", "9223372036854775808"} { |
| 39 | if r := f.call(t, "slides_list_decks", map[string]any{"team": "blue", "cursor": cursor}); r.Code != 422 { |
| 40 | t.Fatal(r.Code) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | func TestAgentDeckRawCorePrecision(t *testing.T) { |
| 45 | f := newSlideAgentFixture(t) |
| 46 | sqid := slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 47 | var d Deck |
| 48 | f.a.app.db.G.Where("sqid=?", sqid).Take(&d) |
| 49 | h, view, e := f.a.app.openDeck(t.Context(), d) |
| 50 | if e != nil { |
| 51 | t.Fatal(e) |
| 52 | } |
| 53 | h.Close(t.Context()) |
| 54 | ops, _ := json.Marshal([]op{{"op": "set_slide", "id": view.Slides[0].ID, "layout": "canvas"}, {"op": "set_block", "slide": view.Slides[0].ID, "id": view.Slides[0].Blocks[0].ID, "z": 1.75}}) |
| 55 | if e = f.a.app.editDeck(t.Context(), &d, actorFor(f.u), ops, nil, nil); e != nil { |
| 56 | t.Fatal(e) |
| 57 | } |
| 58 | r := f.call(t, "slides_get_deck", map[string]any{"team": "blue", "sqid": sqid}) |
| 59 | if r.Code != 200 || !strings.Contains(r.Body.String(), `"z":1.75`) { |
| 60 | t.Fatal("core precision lost", r.Code, r.Body.String()) |
| 61 | } |
| 62 | } |
| 63 | func TestAgentDeckLimitsAndAuthorization(t *testing.T) { |
| 64 | f := newSlideAgentFixture(t) |
| 65 | for _, mode := range []string{"team", "scope", "uuid", "unknown", "title", "text", "input"} { |
| 66 | t.Run(mode, func(t *testing.T) { |
| 67 | in := initialSlideAgentInput() |
| 68 | want := 422 |
| 69 | switch mode { |
| 70 | case "team": |
| 71 | in.Team = "other" |
| 72 | f.p.Teams = []string{"blue", "other"} |
| 73 | defer func() { f.p.Teams = []string{"blue"} }() |
| 74 | want = 403 |
| 75 | case "scope": |
| 76 | f.p.Scopes = []string{"slides:read"} |
| 77 | defer func() { f.p.Scopes = []string{"slides:read", "slides:write"} }() |
| 78 | want = 403 |
| 79 | case "uuid": |
| 80 | in.ClientID = strings.ReplaceAll(in.ClientID, "-", "") |
| 81 | case "title": |
| 82 | in.Title = strings.Repeat("界", 200) |
| 83 | case "text": |
| 84 | in.Slides[0].Body = strings.Repeat("x", 128<<10) |
| 85 | in.Slides[0].Heading = "x" |
| 86 | want = 413 |
| 87 | case "input": |
| 88 | in.Slides[0].Body = strings.Repeat("x", slideAgentLimit) |
| 89 | want = 413 |
| 90 | } |
| 91 | raw, _ := json.Marshal(in) |
| 92 | if mode == "unknown" { |
| 93 | raw = append(raw[:len(raw)-1], []byte(`,"unrecognized":true}`)...) |
| 94 | } |
| 95 | if r := f.raw(t, "slides_create_deck", raw); r.Code != want { |
| 96 | t.Fatal(mode, r.Code, r.Body.String()) |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | // A committed creation always has a small result, even if a later read is too large. |
| 101 | in := initialSlideAgentInput() |
| 102 | in.Slides[0] = slideAgentInitial{Heading: "Large", Body: strings.Repeat("&", 100000)} |
| 103 | for i := 0; i < 3; i++ { |
| 104 | in.Slides = append(in.Slides, slideAgentInitial{Heading: "More", Body: "Small initial text"}) |
| 105 | } |
| 106 | sqid := slideCreated(t, f.call(t, "slides_create_deck", in)) |
| 107 | slideCreated(t, f.call(t, "slides_create_deck", in)) |
| 108 | var d Deck |
| 109 | f.a.app.db.G.Where("sqid=?", sqid).Take(&d) |
| 110 | h, view, e := f.a.app.openDeck(t.Context(), d) |
| 111 | if e != nil { |
| 112 | t.Fatal(e) |
| 113 | } |
| 114 | h.Close(t.Context()) |
| 115 | // Grow the aggregate result through bounded ordinary edits. One 1.1MB |
| 116 | // Markdown parse hits the unchanged one-second guest budget under -race, |
| 117 | // testing the CPU limit instead of the response-size limit asserted below. |
| 118 | for _, slide := range view.Slides { |
| 119 | ops, _ := json.Marshal([]op{{"op": "set_text", "slide": slide.ID, "id": slide.Blocks[1].ID, "markdown": strings.Repeat("z", 280000)}}) |
| 120 | if e = f.a.app.editDeck(t.Context(), &d, actorFor(f.u), ops, nil, nil); e != nil { |
| 121 | t.Fatal(e) |
| 122 | } |
| 123 | } |
| 124 | h, _, e = f.a.app.openDeck(t.Context(), d) |
| 125 | if e != nil { |
| 126 | t.Fatal(e) |
| 127 | } |
| 128 | raw, e := h.JSON(t.Context()) |
| 129 | h.Close(t.Context()) |
| 130 | if e != nil || len(raw) <= slideAgentLimit { |
| 131 | t.Fatal("oversize fixture is not large enough", len(raw), e) |
| 132 | } |
| 133 | if _, e := f.a.get(t.Context(), f.u, sqid); e == nil || access.SafeFailure(e).Code != "too_large" { |
| 134 | t.Fatal("operation itself did not bound canonical get", e) |
| 135 | } |
| 136 | r := f.call(t, "slides_get_deck", map[string]any{"team": "blue", "sqid": sqid}) |
| 137 | if r.Code != 413 { |
| 138 | t.Fatal("oversize canonical get", r.Code) |
| 139 | } |
| 140 | |
| 141 | // Legacy content can be larger than the new creation contract. |
| 142 | if e := f.a.app.db.G.Model(&Deck{}).Where("sqid=?", sqid).Update("title", strings.Repeat("x", slideAgentLimit)).Error; e != nil { |
| 143 | t.Fatal(e) |
| 144 | } |
| 145 | if r := f.call(t, "slides_list_decks", map[string]any{"team": "blue"}); r.Code != 413 { |
| 146 | t.Fatal("oversize result", r.Code) |
| 147 | } |
| 148 | } |
| 149 | |