@@ -38,32 +38,91 @@ import { Container, ContainerModule } from "inversify";
38
38
import { suite , test , timeout } from "mocha-typescript" ;
39
39
import Stripe from "stripe" ;
40
40
import { Config } from "../../../src/config" ;
41
- import { StripeService } from "../user/stripe-service" ;
42
41
import { BillingModes , BillingModesImpl } from "./billing-mode" ;
43
42
import * as deepEqualInAnyOrder from "deep-equal-in-any-order" ;
43
+ import {
44
+ UsageServiceDefinition ,
45
+ UsageServiceClient ,
46
+ GetCostCenterResponse ,
47
+ CostCenter_BillingStrategy ,
48
+ GetBalanceResponse ,
49
+ SetCostCenterResponse ,
50
+ ReconcileUsageResponse ,
51
+ ListUsageRequest_Ordering ,
52
+ ListUsageResponse ,
53
+ } from "@gitpod/usage-api/lib/usage/v1/usage.pb" ;
54
+ import { CallOptions } from "nice-grpc-common" ;
44
55
chai . use ( deepEqualInAnyOrder ) ;
45
56
const expect = chai . expect ;
46
57
47
58
type StripeSubscription = Pick < Stripe . Subscription , "id" > & { customer : string } ;
48
- class StripeServiceMock extends StripeService {
49
- constructor ( protected readonly subscription ?: StripeSubscription ) {
50
- super ( ) ;
51
- }
59
+ class UsageServiceClientMock implements UsageServiceClient {
60
+ constructor ( protected readonly subscription ?: StripeSubscription ) { }
52
61
53
- async findUncancelledSubscriptionByAttributionId ( attributionId : string ) : Promise < string | undefined > {
54
- const customerId = await this . findCustomerByAttributionId ( attributionId ) ;
55
- if ( ! ! customerId && this . subscription ?. customer === customerId ) {
56
- return this . subscription . id ;
62
+ async getCostCenter (
63
+ request : { attributionId ?: string | undefined } ,
64
+ options ?: CallOptions | undefined ,
65
+ ) : Promise < GetCostCenterResponse > {
66
+ if ( ! request . attributionId ) {
67
+ return { costCenter : undefined } ;
57
68
}
58
- return undefined ;
59
- }
60
69
61
- async findCustomerByAttributionId ( attributionId : string ) : Promise < string | undefined > {
62
- const customerId = this . subscription ?. customer ;
63
- if ( ! customerId ) {
64
- return undefined ;
70
+ let billingStrategy = CostCenter_BillingStrategy . BILLING_STRATEGY_OTHER ;
71
+ if ( this . subscription !== undefined ) {
72
+ billingStrategy = CostCenter_BillingStrategy . BILLING_STRATEGY_STRIPE ;
65
73
}
66
- return customerId ;
74
+
75
+ return {
76
+ costCenter : {
77
+ attributionId : request . attributionId ,
78
+ billingStrategy,
79
+ nextBillingTime : new Date ( ) , // does not matter here.
80
+ spendingLimit : 1234 ,
81
+ } ,
82
+ } ;
83
+ }
84
+
85
+ async getBalance (
86
+ request : { attributionId ?: string | undefined } ,
87
+ options ?: CallOptions | undefined ,
88
+ ) : Promise < GetBalanceResponse > {
89
+ throw new Error ( "Mock: not implemented" ) ;
90
+ }
91
+
92
+ async setCostCenter (
93
+ request : {
94
+ costCenter ?:
95
+ | {
96
+ attributionId ?: string | undefined ;
97
+ spendingLimit ?: number | undefined ;
98
+ billingStrategy ?: CostCenter_BillingStrategy | undefined ;
99
+ nextBillingTime ?: Date | undefined ;
100
+ }
101
+ | undefined ;
102
+ } ,
103
+ options ?: CallOptions | undefined ,
104
+ ) : Promise < SetCostCenterResponse > {
105
+ throw new Error ( "Mock: not implemented" ) ;
106
+ }
107
+
108
+ async reconcileUsage (
109
+ request : { from ?: Date | undefined ; to ?: Date | undefined } ,
110
+ options ?: CallOptions | undefined ,
111
+ ) : Promise < ReconcileUsageResponse > {
112
+ throw new Error ( "Mock: not implemented" ) ;
113
+ }
114
+
115
+ async listUsage (
116
+ request : {
117
+ attributionId ?: string | undefined ;
118
+ from ?: Date | undefined ;
119
+ to ?: Date | undefined ;
120
+ order ?: ListUsageRequest_Ordering | undefined ;
121
+ pagination ?: { perPage ?: number | undefined ; page ?: number | undefined } | undefined ;
122
+ } ,
123
+ options ?: CallOptions | undefined ,
124
+ ) : Promise < ListUsageResponse > {
125
+ throw new Error ( "Mock: not implemented" ) ;
67
126
}
68
127
}
69
128
@@ -486,7 +545,9 @@ class BillingModeSpec {
486
545
bind ( SubscriptionService ) . toSelf ( ) . inSingletonScope ( ) ;
487
546
bind ( BillingModes ) . to ( BillingModesImpl ) . inSingletonScope ( ) ;
488
547
489
- bind ( StripeService ) . toConstantValue ( new StripeServiceMock ( test . config . stripeSubscription ) ) ;
548
+ bind ( UsageServiceDefinition . name ) . toConstantValue (
549
+ new UsageServiceClientMock ( test . config . stripeSubscription ) ,
550
+ ) ;
490
551
bind ( ConfigCatClientFactory ) . toConstantValue (
491
552
( ) =>
492
553
new ConfigCatClientMock ( {
0 commit comments