package slidestest // An agent's scene work through the drive API (scenes and clips spec §4, // §8 calls 4 and 5; internal/slides/scenestills.go): a bundle before the // scene exists, new code for one that does, and a still for each state, // each refused when it was made from a version of the scene that has // since moved on. import ( "bytes" "encoding/binary" "encoding/json" "hash/crc32" "image/png" "net/http" "net/http/httptest" "strings" "testing" slides "amadan.net/oficina/slides/internal/slides" "amadan.net/oficina/slides/internal/slides/deck" ) // agentCall is one raw API request with a body that is not JSON. func agentCall(t *testing.T, a *testApp, method, target, token string, body []byte) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(method, target, bytes.NewReader(body)) req.Header.Set("Authorization", "Bearer "+token) return send(t, a, req) } func decode(t *testing.T, rec *httptest.ResponseRecorder) map[string]any { t.Helper() var out map[string]any if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { t.Fatalf("body is not a JSON object: %v\n%s", err, rec.Body) } return out } // sceneOf reads one scene out of the deck as an agent sees it. func sceneOf(t *testing.T, a *testApp, token, sqid, id string) deck.Scene { t.Helper() rec := agentCall(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, nil) var view struct { Scenes map[string]deck.Scene `json:"scenes"` } if err := json.Unmarshal(rec.Body.Bytes(), &view); err != nil { t.Fatal(err) } s, ok := view.Scenes[id] if !ok { t.Fatalf("the deck has no scene %s: %s", id, rec.Body) } return s } // stillsCurrent is the twin of the editor's stillsCurrent // (fixtures/scenes.mjs): every state has a still, made from the scene's // version now. func stillsCurrent(s deck.Scene) bool { v := deck.SceneVersion(s) for _, st := range s.States { if got, ok := s.Stills[st.ID]; !ok || got.Made != v { return false } } return true } func stillURL(sqid, id, state, made string) string { return "/api/v1/decks/" + sqid + "/scenes/" + id + "/stills/" + state + "?made=" + made } // agentScene is an app, a signed-in person, their agent's token, a deck // and a scene in it whose bundle went up through the API. func agentScene(t *testing.T) (a *testApp, token, sqid, id string) { t.Helper() cfg, mailer := newConfig(t) cfg.AllowedAddresses = []string{allowedAddress, otherAddress} cfg.Blobs = dirStore(t) a = newAppWith(t, cfg, mailer) cookies := signedIn(t, a, allowedAddress) token = mintAgent(t, a, cookies, "scenes") sqid = apiDeck(t, a, token, "Stills") rec := agentCall(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/scene-bundles", token, sceneBundle) if rec.Code != http.StatusOK { t.Fatalf("POST scene-bundles: status %d\n%s", rec.Code, rec.Body) } ref, _ := decode(t, rec)["ref"].(string) if ref != hashOf(sceneBundle) { t.Fatalf("scene-bundles answered ref %q, want the hash of the bytes", ref) } return a, token, sqid, addScene(t, a, token, sqid, ref) } // TestAnAgentUploadsAStillForEveryState: each PUT is checked, stored, // registered to the deck as a picture and set on the scene as the agent; // after one for each state the scene's stills are current, and the // pictures are served by the deck's own picture route. func TestAnAgentUploadsAStillForEveryState(t *testing.T) { a, token, sqid, id := agentScene(t) cookies := signedIn(t, a, allowedAddress) if stillsCurrent(sceneOf(t, a, token, sqid, id)) { t.Fatal("a scene with no stills reads as current") } made := deck.SceneVersion(sceneOf(t, a, token, sqid, id)) for _, state := range []string{"one", "two"} { rec := agentCall(t, a, http.MethodPut, stillURL(sqid, id, state, made), token, onePixelPNG) if rec.Code != http.StatusOK { t.Fatalf("PUT still %s: status %d, want 200\n%s", state, rec.Code, rec.Body) } out := decode(t, rec) if out["ref"] != hashOf(onePixelPNG) || out["made"] != made { t.Errorf("PUT still %s answered %v, want the picture's hash and the version", state, out) } } s := sceneOf(t, a, token, sqid, id) if !stillsCurrent(s) { t.Errorf("a still for each state, and the scene's stills are not current: %+v", s.Stills) } if got := s.Stills["one"]; got.Ref != hashOf(onePixelPNG) || got.Made != made { t.Errorf("the scene's still for one is %+v", got) } // A picture of the deck: registered, verified, served. rec := mustGet(t, a, "/d/"+sqid+"/assets/"+hashOf(onePixelPNG), http.StatusOK, cookies) if !bytes.Equal(rec.Body.Bytes(), onePixelPNG) { t.Error("the still's bytes differ from what went up") } // The change was the agent's, through the op path. var actor string if err := a.db.G.Raw("SELECT actor FROM deck_updates WHERE deck_id = (SELECT id FROM decks WHERE sqid = ?) ORDER BY seq DESC LIMIT 1", sqid).Scan(&actor).Error; err != nil { t.Fatal(err) } if !strings.HasPrefix(actor, "agent:") { t.Errorf("the last update's actor is %q, want an agent's", actor) } } // TestAStillMadeFromOldCodeIsRefused: a stale or malformed made is a // 409 stale and the scene is untouched. func TestAStillMadeFromOldCodeIsRefused(t *testing.T) { a, token, sqid, id := agentScene(t) made := deck.SceneVersion(sceneOf(t, a, token, sqid, id)) for _, bad := range []string{strings.Repeat("0", 64), "", "nonsense"} { rec := agentCall(t, a, http.MethodPut, stillURL(sqid, id, "one", bad), token, onePixelPNG) if rec.Code != http.StatusConflict { t.Errorf("made=%q: status %d, want 409\n%s", bad, rec.Code, rec.Body) continue } if out := decode(t, rec); out["error"] != "stale" { t.Errorf("made=%q: error %v, want stale", bad, out["error"]) } } if s := sceneOf(t, a, token, sqid, id); len(s.Stills) != 0 { t.Errorf("a refused still was set anyway: %+v", s.Stills) } // A hand edit of the words moves the version: stills made before it // are stale to the editor's note, and a new upload against the old // version is refused. for _, state := range []string{"one", "two"} { agentCall(t, a, http.MethodPut, stillURL(sqid, id, state, made), token, onePixelPNG) } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_scene","id":"`+id+`","words":{"one":"Changed"}}]}`, http.StatusOK) if stillsCurrent(sceneOf(t, a, token, sqid, id)) { t.Error("a hand edit of words left the stills current") } if rec := agentCall(t, a, http.MethodPut, stillURL(sqid, id, "one", made), token, onePixelPNG); rec.Code != http.StatusConflict { t.Errorf("a still against the old version: status %d, want 409", rec.Code) } } // TestStillsAreRefusedForWhatIsNotYours: another person's deck, a scene // or a state that is not there. func TestStillsAreRefusedForWhatIsNotYours(t *testing.T) { a, token, sqid, id := agentScene(t) made := deck.SceneVersion(sceneOf(t, a, token, sqid, id)) theirs := mintAgent(t, a, signedIn(t, a, otherAddress), "theirs") for name, call := range map[string]struct{ token, target string }{ "another person's deck": {theirs, stillURL(sqid, id, "one", made)}, "an unknown scene": {token, stillURL(sqid, "zzzzzzzzzzzz", "one", made)}, "an unknown state": {token, stillURL(sqid, id, "three", made)}, } { rec := agentCall(t, a, http.MethodPut, call.target, call.token, onePixelPNG) if rec.Code != http.StatusNotFound { t.Errorf("%s: status %d, want 404\n%s", name, rec.Code, rec.Body) } } // Another person's deck, for their code and a bundle, too. if rec := agentCall(t, a, http.MethodPut, "/api/v1/decks/"+sqid+"/scenes/"+id+"/code", theirs, sceneBundle); rec.Code != http.StatusNotFound { t.Errorf("code for another person's deck: status %d, want 404", rec.Code) } if rec := agentCall(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/scene-bundles", theirs, sceneBundle); rec.Code != http.StatusNotFound { t.Errorf("a bundle for another person's deck: status %d, want 404", rec.Code) } if s := sceneOf(t, a, token, sqid, id); len(s.Stills) != 0 { t.Errorf("a refused still was set anyway: %+v", s.Stills) } } // TestNewCodeChangesTheVersionAndMarksStillsStale: PUT code registers the // bundle, sets it as the scene's code with the agent as its author, and // answers the new version; stills made before it are stale. func TestNewCodeChangesTheVersionAndMarksStillsStale(t *testing.T) { a, token, sqid, id := agentScene(t) cookies := signedIn(t, a, allowedAddress) made := deck.SceneVersion(sceneOf(t, a, token, sqid, id)) for _, state := range []string{"one", "two"} { agentCall(t, a, http.MethodPut, stillURL(sqid, id, state, made), token, onePixelPNG) } if !stillsCurrent(sceneOf(t, a, token, sqid, id)) { t.Fatal("the stills were not current to begin with") } next := append([]byte("\n