oficina / suite Public

Clone
git clone https://amadan.net/oficina/suite

Plain git — no account needed to clone.

Download

Download this file

1package suite
2
3import (
4 "errors"
5 "testing"
6)
7
8// S1 rows are (item_id, revision, gone, touched_at) and nothing else
9// (doctrine §6). Draft 1 of memoria's feed carried a visible-to-team field and
10// that was a leak: `visible=false, gone=false` tells a consumer that a
11// guest-only item exists, at what revision and when it was touched — exactly
12// what S3's "404, never 403" exists to hide.
13func TestS1DecodeRows(t *testing.T) {
14 body := []byte(`{"cursor":"c-92","rows":[
15 {"item_id":"doc-1","revision":7,"gone":false,"touched_at":"2026-09-05T09:00:00Z"},
16 {"item_id":"doc-2","revision":2,"gone":true,"touched_at":"2026-09-05T09:01:00Z"}
17 ]}`)
18
19 feed, err := DecodeS1(200, body)
20 if err != nil {
21 t.Fatalf("unexpected error: %v", err)
22 }
23
24 if feed.Cursor != "c-92" {
25 t.Errorf("cursor: got %q, want %q", feed.Cursor, "c-92")
26 }
27 if len(feed.Rows) != 2 {
28 t.Fatalf("got %d rows, want 2", len(feed.Rows))
29 }
30 if feed.Rows[0].ItemID != "doc-1" || feed.Rows[0].Revision != 7 || feed.Rows[0].Gone {
31 t.Errorf("row 0: got %+v", feed.Rows[0])
32 }
33 if !feed.Rows[1].Gone {
34 t.Errorf("row 1: got gone=false, want the tombstone")
35 }
36}
37
38// A feed row carrying anything beyond the four fields is refused, loudly. The
39// contract says a row has no title, no content and no visibility flag; a
40// sibling sending one is either leaking or running a different contract, and
41// silently ignoring the field would let either go unnoticed for as long as
42// nobody grepped the store.
43func TestS1RejectsRowCarryingContent(t *testing.T) {
44 body := []byte(`{"cursor":"c-92","rows":[
45 {"item_id":"doc-1","revision":7,"gone":false,"touched_at":"2026-09-05T09:00:00Z","title":"Q3 plan"}
46 ]}`)
47
48 _, err := DecodeS1(200, body)
49
50 if err == nil {
51 t.Fatal("got nil error for a row carrying a title, want a refusal")
52 }
53}
54
55// Departure from team-visibility and deletion are the same row, and the
56// client must not offer a caller any way to tell them apart (§4.2).
57func TestS1GoneCarriesNoReason(t *testing.T) {
58 body := []byte(`{"cursor":"c-1","rows":[
59 {"item_id":"doc-1","revision":7,"gone":true,"touched_at":"2026-09-05T09:00:00Z","reason":"went_private"}
60 ]}`)
61
62 _, err := DecodeS1(200, body)
63
64 if err == nil {
65 t.Fatal("got nil error for a gone row carrying a reason, want a refusal")
66 }
67}
68
69// An empty feed is legitimate and must keep working: it means nothing changed.
70func TestS1EmptyFeedIsValid(t *testing.T) {
71 feed, err := DecodeS1(200, []byte(`{"cursor":"c-1","rows":[]}`))
72 if err != nil {
73 t.Fatalf("unexpected error: %v", err)
74 }
75 if feed.Cursor != "c-1" || len(feed.Rows) != 0 {
76 t.Errorf("got %+v, want an empty feed at cursor c-1", feed)
77 }
78}
79
80// Bodies that parse but say nothing. `null` and `{}` both decode without
81// error into a zero Feed, which a caller would read as an empty feed at an
82// empty cursor — and committing that cursor loses the consumer's place.
83// Reported by the GPT-6 module review, finding 8.
84func TestS1RejectsBodiesThatSayNothing(t *testing.T) {
85 for _, body := range []string{`null`, `{}`, `{"rows":[]}`} {
86 if _, err := DecodeS1(200, []byte(body)); err == nil {
87 t.Errorf("accepted %s, want a refusal", body)
88 }
89 }
90}
91
92// A row missing its identity, or carrying null where a value belongs, decodes
93// to a live zero-value row: item "" at revision 0, present rather than gone.
94func TestS1RejectsIncompleteRows(t *testing.T) {
95 bodies := []string{
96 `{"cursor":"c-1","rows":[{}]}`,
97 `{"cursor":"c-1","rows":[{"item_id":"","revision":7,"gone":false,"touched_at":"2026-09-05T09:00:00Z"}]}`,
98 `{"cursor":"c-1","rows":[{"item_id":"doc-1","revision":7,"gone":null,"touched_at":null}]}`,
99 }
100 for _, body := range bodies {
101 if _, err := DecodeS1(200, []byte(body)); err == nil {
102 t.Errorf("accepted %s, want a refusal", body)
103 }
104 }
105}
106
107// Trailing content after a complete page, and duplicate keys inside a row.
108// json.Decoder.Decode reads one value and stops, so a second document — one
109// carrying a title, say — rides along unread; and a duplicated `gone` key is
110// resolved silently, so `gone:true,gone:false` loses a tombstone.
111func TestS1RejectsAmbiguousBodies(t *testing.T) {
112 bodies := []string{
113 `{"cursor":"c-1","rows":[]} {"cursor":"c-2","rows":[{"title":"Q3 plan"}]}`,
114 `{"cursor":"c-1","rows":[{"item_id":"doc-1","revision":7,"gone":true,"gone":false,"touched_at":"2026-09-05T09:00:00Z"}]}`,
115 `{"cursor":"c-1","cursor":"c-2","rows":[]}`,
116 }
117 for _, body := range bodies {
118 if _, err := DecodeS1(200, []byte(body)); err == nil {
119 t.Errorf("accepted %s, want a refusal", body)
120 }
121 }
122}
123
124// A non-2xx feed answer is an error, not an empty feed. An empty feed means
125// "nothing changed"; returning that for a failed call would advance a
126// consumer's cursor past changes it never saw.
127func TestS1NonSuccessIsAnError(t *testing.T) {
128 _, err := DecodeS1(503, []byte(`{"cursor":"c-1","rows":[]}`))
129
130 if err == nil {
131 t.Fatal("got nil error for a 503, want a refusal")
132 }
133}
134
135// A cursor the producer's log does not recognise is answered with one fixed
136// 409, and only that exact answer is a reset (doctrine §6.6, revision 11).
137// A reset makes a consumer drop its whole copy of the producer, so a body
138// that merely resembles one — a field more, another code, a second document,
139// the right body under another status — is a plain failure, never a reset.
140//
141// MUTATION-TESTED 2026-11-01, each reverted: the status check removed (any
142// status with the body is a reset) → "the body under 200" and "under 503";
143// the body compared with bytes.Contains instead of equality → "a field
144// more"; the reset check disabled, so a 409 falls to the non-2xx failure →
145// "exact".
146func TestS1CursorReset(t *testing.T) {
147 for name, body := range map[string]string{
148 "exact": `{"code":"CURSOR_RESET"}`,
149 "with json.Encoder's newline": `{"code":"CURSOR_RESET"}` + "\n",
150 } {
151 if _, err := DecodeS1(409, []byte(body)); !errors.Is(err, ErrCursorReset) {
152 t.Errorf("%s: %v, want ErrCursorReset", name, err)
153 }
154 }
155 for name, c := range map[string]struct {
156 status int
157 body string
158 }{
159 "a field more": {409, `{"code":"CURSOR_RESET","at":"c-3"}`},
160 "another code": {409, `{"code":"CURSOR_STALE"}`},
161 "an empty 409": {409, ``},
162 "two documents": {409, `{"code":"CURSOR_RESET"}{}`},
163 "a stray bracket": {409, `{"code":"CURSOR_RESET"}]`},
164 "the body under 200": {200, `{"code":"CURSOR_RESET"}`},
165 "under 503": {503, `{"code":"CURSOR_RESET"}`},
166 } {
167 _, err := DecodeS1(c.status, []byte(c.body))
168 if err == nil || errors.Is(err, ErrCursorReset) {
169 t.Errorf("%s: %v, want a plain failure", name, err)
170 }
171 }
172}
173
174// Trailing content is a real end-of-input check. Decoder.More answers false
175// in front of a stray ']' or '}', so a complete page followed by either was
176// accepted as one document (Fable, review of c38e3ad, L6).
177//
178// MUTATION-TESTED 2026-11-01, reverted: dec.More() restored as the end check
179// in rejectAmbiguousJSON → "]" and "}" accepted.
180func TestS1TrailingDelimiterIsRefused(t *testing.T) {
181 for _, junk := range []string{"]", "}", " ]", "[]", "0"} {
182 body := `{"cursor":"c-1","rows":[]}` + junk
183 if _, err := DecodeS1(200, []byte(body)); err == nil {
184 t.Errorf("accepted %q after a page", junk)
185 }
186 }
187 if _, err := DecodeS1(200, []byte(`{"cursor":"c-1","rows":[]}`+" \n\t")); err != nil {
188 t.Errorf("trailing whitespace is not content: %v", err)
189 }
190}
191