Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode. - **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V). - **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request. - **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds. - **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person. - **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router. - **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch. - **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away. - **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once. - **Undo.** An action done from a card or a request is undone from its toast. - **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own. - **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name. - **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking. - **Check access and Health know the networks behind routers.** - **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st. - Extra data lasts until that person's month ends; «Reset usage» starts the month anew. - A limit is for a month or for the whole term; the admin and the portal show when it resets. - Charts that said «this month» show the last 30 days. - After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st. - **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped. - **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone. - **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence. - **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
178 lines
5.5 KiB
Go
178 lines
5.5 KiB
Go
package hub
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func doc(s string) any {
|
|
var v any
|
|
if err := json.Unmarshal([]byte(s), &v); err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// A patch holds only what changed, and applying it gives the new document.
|
|
func TestDiffAndMerge(t *testing.T) {
|
|
cases := []struct{ a, b, patch string }{
|
|
{`{"a":1,"b":2}`, `{"a":1,"b":3}`, `{"b":3}`},
|
|
{`{"a":1,"b":2}`, `{"a":1}`, `{"b":null}`},
|
|
{`{"a":1}`, `{"a":1,"c":{"d":true}}`, `{"c":{"d":true}}`},
|
|
{`{"p":{"d1":{"online":false,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true}}}`},
|
|
{`{"l":[1,2,3]}`, `{"l":[1,2]}`, `{"l":[1,2]}`},
|
|
{`{"a":{"b":1}}`, `{"a":"text"}`, `{"a":"text"}`},
|
|
{`{"a":"text"}`, `{"a":{"b":1}}`, `{"a":{"b":1}}`},
|
|
}
|
|
for _, c := range cases {
|
|
patch, changed := Diff(doc(c.a), doc(c.b))
|
|
if !changed {
|
|
t.Fatalf("%s → %s: no change seen", c.a, c.b)
|
|
}
|
|
if !reflect.DeepEqual(patch, doc(c.patch)) {
|
|
got, _ := json.Marshal(patch)
|
|
t.Fatalf("%s → %s: patch %s, want %s", c.a, c.b, got, c.patch)
|
|
}
|
|
if got := Merge(doc(c.a), patch); !reflect.DeepEqual(got, doc(c.b)) {
|
|
t.Fatalf("%s + %s did not give %s", c.a, c.patch, c.b)
|
|
}
|
|
}
|
|
if _, changed := Diff(doc(`{"a":[1,{"b":2}]}`), doc(`{"a":[1,{"b":2}]}`)); changed {
|
|
t.Fatal("equal documents differ")
|
|
}
|
|
}
|
|
|
|
// Nulls never reach a document: a patch could not tell one from a removal.
|
|
func TestPlainDropsNulls(t *testing.T) {
|
|
got, err := plain(map[string]any{"a": nil, "b": map[string]any{"c": nil, "d": 1}, "l": []any{nil, 1}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(got, doc(`{"b":{"d":1},"l":[null,1]}`)) {
|
|
t.Fatalf("got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestPutPublishesOnlyChanges(t *testing.T) {
|
|
h := New()
|
|
sub := h.Follow()
|
|
defer sub.Close()
|
|
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`))
|
|
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`)) // the same: nothing
|
|
h.Put("people", doc(`{"p1":{"name":"Myata","online":true}}`))
|
|
first, second := <-sub.Events(), <-sub.Events()
|
|
if first.Seq != 1 || first.Op != OpPut || second.Seq != 2 || second.Op != OpPatch {
|
|
t.Fatalf("events: %+v %+v", first, second)
|
|
}
|
|
if !reflect.DeepEqual(second.Data, doc(`{"p1":{"online":true}}`)) {
|
|
t.Fatalf("patch: %v", second.Data)
|
|
}
|
|
select {
|
|
case e := <-sub.Events():
|
|
t.Fatalf("an event for nothing: %+v", e)
|
|
default:
|
|
}
|
|
}
|
|
|
|
// A reader gets a snapshot first, then what it missed; from another run or
|
|
// too far behind, a snapshot again.
|
|
func TestSync(t *testing.T) {
|
|
h := New()
|
|
h.Put("a", doc(`{"x":1}`))
|
|
h.Put("b", doc(`{"y":1}`))
|
|
snap := h.Sync("", 0, nil)
|
|
if snap.Seq != 2 || len(snap.Docs) != 2 || snap.Events != nil {
|
|
t.Fatalf("snapshot: %+v", snap)
|
|
}
|
|
h.Put("a", doc(`{"x":2}`))
|
|
h.Put("b", doc(`{"y":2}`))
|
|
only := func(topic string) bool { return topic == "a" }
|
|
got := h.Sync(snap.Epoch, snap.Seq, only)
|
|
if got.Docs != nil || len(got.Events) != 1 || got.Events[0].Topic != "a" || got.Seq != 4 {
|
|
t.Fatalf("catch-up: %+v", got)
|
|
}
|
|
// A snapshot taken before anything was published is followed from the first event.
|
|
if early := h.Sync(snap.Epoch, 0, nil); early.Docs != nil || len(early.Events) != 4 {
|
|
t.Fatalf("from zero: %+v", early)
|
|
}
|
|
if up := h.Sync(snap.Epoch, 4, nil); up.Docs != nil || len(up.Events) != 0 {
|
|
t.Fatalf("up to date: %+v", up)
|
|
}
|
|
if other := h.Sync("another-run", 3, nil); other.Docs == nil {
|
|
t.Fatal("a number of another run was taken for ours")
|
|
}
|
|
if ahead := h.Sync(snap.Epoch, 99, nil); ahead.Docs == nil {
|
|
t.Fatal("a number ahead of the hub was accepted")
|
|
}
|
|
for i := 0; i < journalSize+10; i++ {
|
|
h.Put("a", map[string]any{"x": i + 10})
|
|
}
|
|
if far := h.Sync(snap.Epoch, 4, nil); far.Docs == nil {
|
|
t.Fatal("events no longer kept were promised")
|
|
}
|
|
}
|
|
|
|
func TestAppendKeepsNewest(t *testing.T) {
|
|
h := New()
|
|
h.Append("meter", 3, 1, 2)
|
|
h.Append("meter", 3, 3, 4)
|
|
snap := h.Sync("", 0, nil)
|
|
if !reflect.DeepEqual(snap.Docs["meter"], doc(`[2,3,4]`)) {
|
|
t.Fatalf("list: %v", snap.Docs["meter"])
|
|
}
|
|
ev := h.Sync(snap.Epoch, 1, nil).Events
|
|
if len(ev) != 1 || ev[0].Op != OpAppend || !reflect.DeepEqual(ev[0].Data, doc(`{"items":[3,4],"keep":3}`)) {
|
|
t.Fatalf("event: %+v", ev)
|
|
}
|
|
}
|
|
|
|
// Wait holds until something the reader wants happens, and gives up with its context.
|
|
func TestWait(t *testing.T) {
|
|
h := New()
|
|
h.Put("a", doc(`{"x":1}`))
|
|
at := h.Sync("", 0, nil)
|
|
only := func(topic string) bool { return topic == "a" }
|
|
got := make(chan Answer, 1)
|
|
go func() { got <- h.Wait(context.Background(), at.Epoch, at.Seq, only) }()
|
|
h.Put("b", doc(`{"y":1}`)) // not wanted: the wait goes on
|
|
select {
|
|
case a := <-got:
|
|
t.Fatalf("answered for another topic: %+v", a)
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
h.Put("a", doc(`{"x":2}`))
|
|
select {
|
|
case a := <-got:
|
|
if len(a.Events) != 1 || a.Seq != 3 {
|
|
t.Fatalf("answer: %+v", a)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("never answered")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
|
|
defer cancel()
|
|
if a := h.Wait(ctx, at.Epoch, 3, only); len(a.Events) != 0 || a.Docs != nil || a.Seq != 3 {
|
|
t.Fatalf("timed out with: %+v", a)
|
|
}
|
|
}
|
|
|
|
// A follower that does not read is let go, not waited for.
|
|
func TestSlowFollowerIsDropped(t *testing.T) {
|
|
h := New()
|
|
sub := h.Follow()
|
|
for i := 0; i < 300; i++ {
|
|
h.Put("a", map[string]any{"x": i})
|
|
}
|
|
n := 0
|
|
for range sub.Events() {
|
|
n++
|
|
}
|
|
if n != 256 {
|
|
t.Fatalf("read %d events before the channel closed", n)
|
|
}
|
|
sub.Close() // closing again is harmless
|
|
}
|