11package cmd
22
33import (
4+ "encoding/json"
45 "net/http"
56 "testing"
67
78 "github.com/loops-so/cli/internal/api"
89)
910
1011func TestRunContactsFind (t * testing.T ) {
11- body := `[{"id":"cnt_abc123","email":"bob@example.com","firstName":"Bob","lastName":"Smith","source":"api","subscribed":true,"userGroup":"default","userId":"user_123","mailingLists":{},"optInStatus":"accepted"}]`
12+ body := `[{"id":"cnt_abc123","email":"bob@example.com","firstName":"Bob","lastName":"Smith","source":"api","subscribed":true,"userGroup":"default","userId":"user_123","mailingLists":{},"optInStatus":"accepted","company":"Loops","plan":"pro" }]`
1213
1314 assertContact := func (t * testing.T , got api.Contact ) {
1415 t .Helper ()
@@ -23,6 +24,9 @@ func TestRunContactsFind(t *testing.T) {
2324 deref (got .OptInStatus ) != "accepted" {
2425 t .Errorf ("unexpected contact: %+v" , got )
2526 }
27+ if got .Custom ["company" ] != "Loops" || got .Custom ["plan" ] != "pro" {
28+ t .Errorf ("unexpected custom properties: %v" , got .Custom )
29+ }
2630 }
2731
2832 t .Run ("finds by email" , func (t * testing.T ) {
@@ -67,4 +71,47 @@ func TestRunContactsFind(t *testing.T) {
6771 t .Fatal ("expected error, got nil" )
6872 }
6973 })
74+
75+ t .Run ("marshal preserves custom properties" , func (t * testing.T ) {
76+ serveJSON (t , http .StatusOK , body )
77+ contacts , err := runContactsFind (cfg (t ), "bob@example.com" , "" )
78+ if err != nil {
79+ t .Fatalf ("unexpected error: %v" , err )
80+ }
81+ b , err := json .Marshal (contacts [0 ])
82+ if err != nil {
83+ t .Fatalf ("marshal error: %v" , err )
84+ }
85+ var raw map [string ]json.RawMessage
86+ if err := json .Unmarshal (b , & raw ); err != nil {
87+ t .Fatalf ("unmarshal error: %v" , err )
88+ }
89+ for _ , key := range []string {"company" , "plan" } {
90+ if _ , ok := raw [key ]; ! ok {
91+ t .Errorf ("expected custom property %q in marshaled JSON" , key )
92+ }
93+ }
94+ })
95+ }
96+
97+ func TestFormatMailingLists (t * testing.T ) {
98+ tests := []struct {
99+ name string
100+ in map [string ]bool
101+ want string
102+ }{
103+ {"nil" , nil , "" },
104+ {"empty" , map [string ]bool {}, "" },
105+ {"one subscribed" , map [string ]bool {"list_a" : true }, "list_a" },
106+ {"skips unsubscribed" , map [string ]bool {"list_a" : true , "list_b" : false }, "list_a" },
107+ {"sorted" , map [string ]bool {"list_c" : true , "list_a" : true , "list_b" : true }, "list_a, list_b, list_c" },
108+ }
109+ for _ , tt := range tests {
110+ t .Run (tt .name , func (t * testing.T ) {
111+ got := formatMailingLists (tt .in )
112+ if got != tt .want {
113+ t .Errorf ("got %q, want %q" , got , tt .want )
114+ }
115+ })
116+ }
70117}
0 commit comments