package lulu import ( "context" "encoding/json" "fmt" "os" "strings" "testing" "time" "github.com/stretchr/testify/require" ) const ( clientKeyPath = "testdata/clientkey" clientSecretPath = "testdata/clientsecret" apiKeyPage = "https://developers.sandbox.com/user-profile/api-keys" interiorUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1" coverUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AADP367j0bTWlt8fCu-_tm2ia/161025/139056_cover.pdf?dl=1" pollTimeout = 15 * time.Second pollPeriod = time.Second ) var ( clientKey string clientSecret string ) func TestMain(m *testing.M) { if b, err := os.ReadFile(clientKeyPath); err == nil { clientKey = strings.TrimSpace(string(b)) } else { fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client key\" from the API Keys page into %s\n%s\n", err, clientKeyPath, apiKeyPage) os.Exit(1) } if b, err := os.ReadFile(clientSecretPath); err == nil { clientSecret = strings.TrimSpace(string(b)) } else { fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client secret\" from the API Keys page into %s\n%s\n", err, clientSecretPath, apiKeyPage) os.Exit(1) } m.Run() } func newClient(t *testing.T) *Client { t.Helper() c, err := NewClient(t.Context(), clientKey, clientSecret) require.NoError(t, err) return c } func requireMarshalJsonEq(t *testing.T, expected string, marshaler any) { t.Helper() jactual, err := json.Marshal(marshaler) require.NoError(t, err) require.JSONEq(t, expected, string(jactual)) } func requireUnmarshalJsonEq[T any](t *testing.T, expected T, j string) { t.Helper() var actual T require.NoError(t, json.Unmarshal([]byte(j), &actual)) require.Equal(t, expected, actual) } // poll periodically calls f() until it returns true or the deadline is exceeded. func poll(t *testing.T, f func() bool) { t.Helper() ctx, cancel := context.WithTimeout(t.Context(), pollTimeout) defer cancel() timer := time.NewTimer(pollPeriod) for { select { case <-timer.C: if f() { return } timer.Reset(pollPeriod) case <-ctx.Done(): t.Errorf("timed out after %v", pollTimeout) } } }