@@ -2,6 +2,7 @@ package e2e
22
33import (
44 "net/http"
5+ "slices"
56 "testing"
67 "time"
78
@@ -20,7 +21,7 @@ import (
2021// - Create a meter and feature for usage-based billing (v3)
2122// - Create and publish a plan with a unit rate card backed by the feature (v3)
2223// - Create a subscription for the customer (v3)
23- // - Advance the gathering invoice via the v1 InvoicePendingLinesAction to create a standard invoice
24+ // - Create a charge-backed pending line and wait for charges to advance it into a standard invoice
2425// - List the customer's invoices via the v1 SDK; separate standard from gathering
2526// - GET the standard invoice via the v3 endpoint and assert the response shape
2627// - GET the gathering invoice via the v3 endpoint → 404 (gathering invoices are not exposed)
@@ -135,15 +136,16 @@ func TestV3GetBillingInvoice(t *testing.T) {
135136 PaymentTerm : lo .ToPtr (api .PricePaymentTermInAdvance ),
136137 }))
137138
139+ invoiceAt := now .Add (time .Hour )
138140 lineResp , err := v1 .CreatePendingInvoiceLineWithResponse (t .Context (), customerID , api.InvoicePendingLineCreateInput {
139141 Currency : "USD" ,
140142 Lines : []api.InvoicePendingLineCreate {
141143 {
142144 Name : uniqueKey ("inv_gathering_line_name" ),
143- InvoiceAt : now . Add ( - 10 * time . Hour ) ,
145+ InvoiceAt : invoiceAt ,
144146 Period : api.Period {
145147 From : now .Add (- 24 * time .Hour ),
146- To : now . Add ( time . Hour ) ,
148+ To : invoiceAt ,
147149 },
148150 Price : & price ,
149151 },
@@ -176,7 +178,7 @@ func TestV3GetBillingInvoice(t *testing.T) {
176178 InvoiceAt : now .Add (- 10 * time .Hour ),
177179 Period : api.Period {
178180 From : now .Add (- 24 * time .Hour ),
179- To : now .Add (time .Hour ),
181+ To : now .Add (- 2 * time .Hour ),
180182 },
181183 Price : & price ,
182184 },
@@ -186,17 +188,24 @@ func TestV3GetBillingInvoice(t *testing.T) {
186188 require .Equal (t , http .StatusCreated , lineResp .StatusCode (), "line: %s" , string (lineResp .Body ))
187189 require .NotNil (t , lineResp .JSON201 )
188190
189- resp , err := v1 .InvoicePendingLinesActionWithResponse (t .Context (), api.InvoicePendingLinesActionInput {
190- CustomerId : customerID ,
191- ProgressiveBillingOverride : lo .ToPtr (true ),
192- AsOf : lo .ToPtr (now .Add (- 1 * time .Hour )),
193- })
194- require .NoError (t , err )
195- require .Equal (t , http .StatusCreated , resp .StatusCode (), "advance: %s" , string (resp .Body ))
196- require .NotNil (t , resp .JSON201 )
197- require .NotEmpty (t , * resp .JSON201 , "expected at least one standard invoice to be created" )
198-
199- invoiceID = (* resp .JSON201 )[0 ].Id
191+ ctx := t .Context ()
192+ customers := api.InvoiceListParamsCustomers {customerID }
193+ assert .EventuallyWithT (t , func (c * assert.CollectT ) {
194+ listResp , err := v1 .ListInvoicesWithResponse (ctx , & api.ListInvoicesParams {
195+ Customers : & customers ,
196+ PageSize : lo .ToPtr (api .PaginationPageSize (100 )),
197+ })
198+ require .NoError (c , err )
199+ require .Equal (c , http .StatusOK , listResp .StatusCode (), "list invoices: %s" , string (listResp .Body ))
200+ require .NotNil (c , listResp .JSON200 )
201+
202+ standardInvoiceIdx := slices .IndexFunc (listResp .JSON200 .Items , func (inv api.Invoice ) bool {
203+ return inv .Status != api .InvoiceStatusGathering
204+ })
205+ require .NotEqual (c , - 1 , standardInvoiceIdx , "expected charges to advance a pending line into a standard invoice" )
206+
207+ invoiceID = listResp .JSON200 .Items [standardInvoiceIdx ].Id
208+ }, time .Minute , time .Second )
200209 require .NotEmpty (t , invoiceID )
201210 })
202211
@@ -212,10 +221,10 @@ func TestV3GetBillingInvoice(t *testing.T) {
212221 require .NotNil (t , listResp .JSON200 )
213222 require .NotEmpty (t , listResp .JSON200 .Items , "expected at least one invoice for customer %s (key: %s)" , customerID , customerKey )
214223
215- _ , foundStandard := lo . Find (listResp .JSON200 .Items , func (inv api.Invoice ) bool {
224+ standardInvoiceIdx := slices . IndexFunc (listResp .JSON200 .Items , func (inv api.Invoice ) bool {
216225 return inv .Status != api .InvoiceStatusGathering
217226 })
218- require .True (t , foundStandard , "expected at least one non-gathering invoice in the list" )
227+ require .NotEqual (t , - 1 , standardInvoiceIdx , "expected at least one non-gathering invoice in the list" )
219228 })
220229
221230 t .Run ("Should return the invoice via v3 GET" , func (t * testing.T ) {
0 commit comments