package plan3test import ( "amadan.net/oficina/equipo/internal/equipo/personapi" "fmt" "testing" ) func TestFinalFixDeviceExchangeRevision(t *testing.T) { f := newFixture(t) c, key := deviceChallenge(t, f) expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/auth/devices/"+c.ID+"/approve", fmt.Sprintf(`{"typed_code":%q}`, c.UserCode), "approve", `"1"`), 200) f.exec(t, "UPDATE person_devices SET revision=17 WHERE id=?", c.ID) w := deviceRequest(t, f, "", "POST", "/api/v1/auth/devices/exchange", signedDevice(t, f, c, key), "exchange", "") expectHTTP(t, w, 200) if w.Header().Get("ETag") != `"18"` { t.Fatal("synthetic exchange revision", w.Header().Get("ETag")) } } func TestFinalFixAckRevision(t *testing.T) { f := newFixture(t) c := approvedDevice(t, f) path := "/api/v1/auth/devices/" + c.ID f.exec(t, "CREATE TRIGGER advance_device_revision AFTER UPDATE ON person_devices WHEN NEW.revoked_at IS NOT NULL AND OLD.revoked_at IS NULL BEGIN UPDATE person_devices SET revision=revision+5 WHERE id=NEW.id; END") w := apiRequest(t, f, owner, "DELETE", path, `{}`, "revoke", `"3"`) expectHTTP(t, w, 200) actual := f.count(t, "SELECT revision FROM person_devices WHERE id=?", c.ID) if w.Header().Get("ETag") != fmt.Sprintf(`"%d"`, actual) || apiValue[personapi.Ack](t, w).Revision != actual { t.Fatal("synthetic ack revision", w.Header(), w.Body.String(), actual) } } func TestFinalFixAckSelfDemotion(t *testing.T) { f := newFixture(t) revision := f.count(t, "SELECT revision FROM members WHERE address=?", owner.Address) w := apiRequest(t, f, owner, "PUT", "/api/v1/roster/"+owner.Address, `{"role":"member","active":true}`, "demote", fmt.Sprintf(`"%d"`, revision)) expectHTTP(t, w, 200) actual := f.count(t, "SELECT revision FROM members WHERE address=?", owner.Address) if apiValue[personapi.Ack](t, w).Revision != actual { t.Fatal("wrong self-demotion revision") } } func TestFinalFixAckReadback(t *testing.T) { f, _ := modelChoiceFixture(t) f.exec(t, "CREATE TRIGGER connection_revision_jump AFTER UPDATE ON connections WHEN NEW.label IS NOT OLD.label BEGIN UPDATE connections SET revision=revision+5 WHERE id=NEW.id; END") w := apiRequest(t, f, owner, "PATCH", "/api/v1/connections/claude", `{"label":"Renamed account"}`, "rename", `"1"`) expectHTTP(t, w, 200) actual := f.count(t, "SELECT revision FROM connections WHERE id='claude'") if apiValue[personapi.Ack](t, w).Revision != actual || w.Header().Get("ETag") != fmt.Sprintf(`"%d"`, actual) { t.Fatal("ack did not read the stored revision", w.Body.String(), actual) } }