okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufdocs(test): explain coverage and manual checks2f6697a

main
1.5 KiB37 linesraw
1// Collection streams must validate whole batches before changing the client view.
2// A controlled peer supplies malformed rows and overflowing activity batches.
3package daemon
4
5import (
6	"fmt"
7	"net/http"
8	"testing"
9)
10
11func TestCollectionReadyOverflowAndAtomicValidation(t *testing.T) {
12	for _, bad := range []bool{false, true} {
13		t.Run(fmt.Sprint(bad), func(t *testing.T) {
14			conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) {
15				if r.URL.Path != "/sessions" || r.Header.Get("Accept") != "text/event-stream" {
16					t.Errorf("wrong collection route %s", r.URL)
17				}
18				w.Header().Set("Content-Type", "text/event-stream")
19				writeInitialStream(w, "agents")
20				if bad {
21					writeBatch(w, map[string]any{"events": []any{map[string]any{"type": "mail", "data": map[string]any{"mail_id": "m", "sender_session_id": nil, "sender_label": nil, "receiver_session_id": "s", "kind": "message", "bytes": 1}}, map[string]any{"type": "invalidate", "data": map[string]any{"urls": []any{}, "session_ids": []any{}, "scope_dirty": "false"}}}})
22				} else {
23					writeBatch(w, map[string]any{"events": []any{map[string]any{"type": "overflow", "data": map[string]any{}}}})
24				}
25			})
26			batches := 0
27			err := StreamAgents(t.Context(), conn, func([]AgentEvent) error { batches++; return nil })
28			expected := 2
29			if bad {
30				expected = 1
31			}
32			if err == nil || batches != expected {
33				t.Fatalf("collection delivery changed: %v batches=%d", err, batches)
34			}
35		})
36	}
37}