package db import ( "encoding/json" "testing" ) // attrTruthy replaces a previous strings.Contains check over raw JSONB text. // The key regression it guards: a literal attribute like // {"backups_verified": false} must NOT satisfy the "backups-verified" // precondition, even though the key text is present in the column. func TestAttrTruthy(t *testing.T) { cases := []struct { name string attrs map[string]any key string want bool }{ {"absent", map[string]any{}, "backups_verified", false}, {"nil map", nil, "backups_verified", false}, {"explicit nil value", map[string]any{"backups_verified": nil}, "backups_verified", false}, {"bool true", map[string]any{"backups_verified": true}, "backups_verified", true}, {"bool false is the regression case", map[string]any{"backups_verified": false}, "backups_verified", false}, {"nonempty string age pubkey", map[string]any{"age_pubkey": "age1abc"}, "age_pubkey", true}, {"empty string is falsy", map[string]any{"mesh_ip": ""}, "mesh_ip", false}, {"number counts as present", map[string]any{"port": float64(22)}, "port", true}, {"other keys present", map[string]any{"backups_verified": true, "unrelated": "x"}, "backups_verified", true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := attrTruthy(tc.attrs, tc.key); got != tc.want { t.Fatalf("attrTruthy(%v, %q) = %v, want %v", tc.attrs, tc.key, got, tc.want) } }) } } // fetchAttrs decodes the JSONB column text; verify the decode shape that // attrTruthy then evaluates (the DB round-trip itself is covered by make test-db). func TestAttrTruthyAfterDecode(t *testing.T) { raw := `{"backups_verified": true, "mesh_ip": "10.0.0.5", "secrets_revoked": false}` var got map[string]any if err := json.Unmarshal([]byte(raw), &got); err != nil { t.Fatalf("unmarshal: %v", err) } if !attrTruthy(got, "backups_verified") { t.Error("backups_verified should be truthy after decode") } if !attrTruthy(got, "mesh_ip") { t.Error("mesh_ip should be truthy after decode") } if attrTruthy(got, "secrets_revoked") { t.Error("secrets_revoked:false is the regression — must be falsy") } }