package webui import ( "encoding/json" "slices" "testing" ) // TestManifestKeepsTheFlatWebJSONShape pins the on-disk contract: the TUI and a // later albedo read web.json written by an earlier one, and embedding the // options must not nest them under a key or lose a renamed field, or pid and // url would read as zero and --stop would leave a server and its tailscale // handler behind when web.json was written by an older albedo. func TestManifestKeepsTheFlatWebJSONShape(t *testing.T) { const earlier = `{"allow":["10.0.0.0/8"],"local":true,"public":"https://albedo.example.ts.net","tailnet_https":true,"names":["albedo.example.ts.net"],"pid":42,"port":4317,"url":"http://127.0.0.1:4317"}` var manifest Manifest if err := json.Unmarshal([]byte(earlier), &manifest); err != nil { t.Fatal(err) } if manifest.PID != 42 || manifest.Port != 4317 || !manifest.Local || !manifest.TailnetHTTPS || manifest.Public != "https://albedo.example.ts.net" || manifest.URL != "http://127.0.0.1:4317" || !slices.Equal(manifest.Allow, []string{"10.0.0.0/8"}) || !slices.Equal(manifest.Names, []string{"albedo.example.ts.net"}) { t.Fatalf("read %+v", manifest) } if origin := manifest.PublicOrigin(); origin == nil || origin.String() != "https://albedo.example.ts.net" { t.Fatalf("public origin %v", origin) } encoded, err := json.Marshal(Manifest{Options: Options{Port: 4317, Public: "https://albedo.example.ts.net"}, PID: 42, URL: "http://127.0.0.1:4317"}) if err != nil { t.Fatal(err) } var fields map[string]json.RawMessage if err := json.Unmarshal(encoded, &fields); err != nil { t.Fatal(err) } keys := make([]string, 0, len(fields)) for key := range fields { keys = append(keys, key) } slices.Sort(keys) if want := []string{"pid", "port", "public", "url"}; !slices.Equal(keys, want) { t.Errorf("web.json keys = %q, want %q", keys, want) } } // TestSameAllowComparesNetworksAsASet pins the conflict check's rule: the // flags are a set, and an address with trailing zeros is its CIDR, so a // second albedo web with the same networks in another order or spelling may // attach. func TestSameAllowComparesNetworksAsASet(t *testing.T) { one := Options{Allow: []string{"10.0.0.0/8", "100.64.0.0/10"}} other := Options{Allow: []string{"100.64.0.0/10", "10.0.0.0", "10.0.0.0/8"}} if !one.SameAllow(other) { t.Errorf("%v and %v allow the same networks", one.Allow, other.Allow) } if one.SameAllow(Options{Allow: []string{"192.168.0.0"}}) { t.Error("a different network matched") } }