diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-05-07 09:39:30 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-05-07 09:39:30 -0400 |
| commit | 4ee72fe36dd513bcbad227f7bdd6ed0079d2c95a (patch) | |
| tree | c743fb3b71f46d701ea8db34220adb8cdb3074d8 /lulu_test.go | |
| parent | 2990789d98405291bfbb3e0dce4efca1153120ab (diff) | |
| download | lulu-4ee72fe36dd513bcbad227f7bdd6ed0079d2c95a.zip | |
implement GET /validate-interior
Diffstat (limited to 'lulu_test.go')
| -rw-r--r-- | lulu_test.go | 58 |
1 files changed, 51 insertions, 7 deletions
diff --git a/lulu_test.go b/lulu_test.go index b44427a..5f11e99 100644 --- a/lulu_test.go +++ b/lulu_test.go @@ -1,11 +1,13 @@ package lulu import ( + "context" "encoding/json" "fmt" "os" "strings" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -63,12 +65,12 @@ func TestMarshalValidateInteriorReq(t *testing.T) { GoldFoil}, } - requireJsonEq(t, want, req) + requireMarshalJsonEq(t, want, req) } func TestMarshalValidateInteriorBasicReq(t *testing.T) { t.Parallel() - requireJsonEq(t, + requireMarshalJsonEq(t, `{"source_url": "https://example.com/interior.pdf"}`, validateInteriorBasicReq{"https://example.com/interior.pdf"}) } @@ -99,7 +101,7 @@ func TestUnmarshalInteriorValidationRecord(t *testing.T) { func TestValidateInterior(t *testing.T) { c := newClient(t) - pkg := PkgId{ + mfg := PkgId{ UsTrade, Mono, Standard, @@ -109,9 +111,11 @@ func TestValidateInterior(t *testing.T) { NoLinen, NoFoil, } - id, err := c.ValidateInterior(interiorUrl, pkg) + id, err := c.ValidateInterior(interiorUrl, mfg) require.NoError(t, err) require.NotZero(t, id) + // It seems the server doesn't populate most of the response + // fields, but we just need the ID anyway. } func TestValidateInteriorBasic(t *testing.T) { @@ -119,10 +123,43 @@ func TestValidateInteriorBasic(t *testing.T) { id, err := c.ValidateInteriorBasic(interiorUrl) require.NoError(t, err) require.NotZero(t, id) + // It seems the server doesn't populate most of the response + // fields, but we just need the ID anyway. } func TestGetInteriorValidation(t *testing.T) { - t.Fail() // TODO + c := newClient(t) + + // Start validation job + id, err := c.ValidateInteriorBasic(interiorUrl) + require.NoError(t, err) + + // Poll until status is non-null + timeout := 15 * time.Second + period := time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + timer := time.NewTimer(period) + for { + select { + case <-timer.C: + rec, err := c.GetInteriorValidation(id) + require.NoError(t, err) + if rec.Status.IsFinal() { + require.Equal(t, id, rec.Id) + require.Equal(t, interiorUrl, rec.SrcUrl) + require.Equal(t, uint(210), rec.NPages) + require.Empty(t, rec.Errors) + require.Equal(t, StatusValidated, rec.Status) + require.NotEmpty(t, rec.ValidPkgIds) + return + } + timer.Reset(period) + case <-ctx.Done(): + t.Errorf("status still not finalized after %v", timeout) + return + } + } } func newClient(t *testing.T) *Client { @@ -132,9 +169,16 @@ func newClient(t *testing.T) *Client { return c } -func requireJsonEq(t *testing.T, expected string, actual any) { +func requireMarshalJsonEq(t *testing.T, expected string, marshaler any) { t.Helper() - jactual, err := json.Marshal(actual) + 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) +} |