-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathTopUpCreditsDialogContentWorkspace.test.ts
More file actions
300 lines (255 loc) · 8.65 KB
/
Copy pathTopUpCreditsDialogContentWorkspace.test.ts
File metadata and controls
300 lines (255 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createI18n } from 'vue-i18n'
import type { CreateTopupResponse } from '@/platform/workspace/api/workspaceApi'
import TopUpCreditsDialogContentWorkspace from './TopUpCreditsDialogContentWorkspace.vue'
const mockFetchBalance = vi.fn()
const mockFetchStatus = vi.fn()
const mockTopup = vi.fn<(amountCents: number) => Promise<CreateTopupResponse>>()
const mockStartOperation = vi.fn()
const mockShowSettings = vi.fn()
const mockToastAdd = vi.fn()
const mockCloseDialog = vi.fn()
const mockTrackTopUpPurchase = vi.fn()
const mockTrackBillingEvent = vi.fn()
const mockCanTopUp = vi.hoisted(() => ({ value: true }))
const mockShouldUseWorkspaceBilling = vi.hoisted(() => ({ value: true }))
const mockTopupActionOperation = vi.hoisted(() => ({
value: undefined as { actionUrl: string } | undefined
}))
vi.mock('@/composables/billing/useBillingContext', () => ({
useBillingContext: () => ({
fetchBalance: mockFetchBalance,
fetchStatus: mockFetchStatus,
topup: (amountCents: number) => mockTopup(amountCents)
})
}))
vi.mock('@/platform/workspace/stores/billingOperationStore', () => ({
useBillingOperationStore: () => ({
hasPendingOperations: true,
isAddingCredits: false,
get topupActionOperation() {
return mockTopupActionOperation.value
},
startOperation: mockStartOperation
})
}))
vi.mock('@/platform/workspace/composables/useWorkspaceUI', () => ({
useWorkspaceUI: () => ({
permissions: {
__v_isRef: true,
get value() {
return { canTopUp: mockCanTopUp.value }
}
}
})
}))
vi.mock('@/composables/billing/useBillingRouting', () => ({
useBillingRouting: () => ({
shouldUseWorkspaceBilling: {
get value() {
return mockShouldUseWorkspaceBilling.value
}
}
})
}))
vi.mock('@/platform/settings/composables/useSettingsDialog', () => ({
useSettingsDialog: () => ({ show: mockShowSettings })
}))
vi.mock('@/stores/dialogStore', () => ({
useDialogStore: () => ({ closeDialog: mockCloseDialog })
}))
vi.mock('@/platform/telemetry', () => ({
useTelemetry: () => ({
trackApiCreditTopupButtonPurchaseClicked: mockTrackTopUpPurchase,
trackBillingEvent: mockTrackBillingEvent
})
}))
vi.mock('@/platform/telemetry/topupTracker', () => ({
clearTopupTracking: vi.fn()
}))
vi.mock('@/composables/useExternalLink', () => ({
useExternalLink: () => ({
buildDocsUrl: () => 'https://docs.comfy.org',
docsPaths: { partnerNodesPricing: '' }
})
}))
vi.mock('primevue/usetoast', () => ({
useToast: () => ({ add: mockToastAdd })
}))
vi.mock('@/base/credits/comfyCredits', () => ({
creditsToUsd: (credits: number) => credits,
usdToCredits: (usd: number) => usd
}))
const i18n = createI18n({
legacy: false,
locale: 'en',
messages: {
en: {
g: { close: 'Close' },
subscription: {
addCredits: 'Add credits',
preview: { completeVerification: 'Complete verification' }
},
credits: {
topUp: {
addMoreCredits: 'Add more credits',
addMoreCreditsToRun: 'Add more credits to run',
selectAmount: 'Select amount',
youPay: 'You pay',
youGet: 'You get',
purchaseSuccess: 'Credits added successfully!',
purchaseError: 'Purchase Failed',
purchaseErrorDetail: 'Failed to purchase credits: {error}',
unknownError: 'An unknown error occurred',
minRequired: 'Minimum required',
maxAllowed: 'Maximum allowed',
needMore: 'Need more?',
contactUs: 'Contact us',
viewPricing: 'View pricing',
insufficientWorkflowMessage: 'Insufficient credits'
}
}
}
}
})
function topupResponse(
status: CreateTopupResponse['status']
): CreateTopupResponse {
return {
billing_op_id: 'op-1',
topup_id: 'topup-1',
status,
amount_cents: 5000
}
}
function renderDialog() {
return render(TopUpCreditsDialogContentWorkspace, {
global: {
plugins: [i18n],
stubs: {
FormattedNumberStepper: {
name: 'FormattedNumberStepper',
props: ['modelValue'],
template: '<div />'
}
}
}
})
}
async function clickAddCredits() {
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Add credits' }))
}
describe('TopUpCreditsDialogContentWorkspace', () => {
beforeEach(() => {
vi.clearAllMocks()
mockCanTopUp.value = true
mockShouldUseWorkspaceBilling.value = true
mockTopupActionOperation.value = undefined
mockFetchBalance.mockResolvedValue(undefined)
mockFetchStatus.mockResolvedValue(undefined)
})
it('allows a top-up while an unrelated billing operation is pending', () => {
renderDialog()
expect(screen.getByRole('button', { name: 'Add credits' })).toBeEnabled()
})
it('opens topup verification without exposing its URL', async () => {
const actionUrl = 'https://verify.example/sensitive-token'
const open = vi.spyOn(window, 'open').mockReturnValue({} as Window)
mockTopupActionOperation.value = { actionUrl }
const { container } = renderDialog()
expect(container.innerHTML).not.toContain(actionUrl)
await userEvent.click(
screen.getByRole('button', { name: 'Complete verification' })
)
expect(open).toHaveBeenCalledWith(
actionUrl,
'_blank',
'noopener,noreferrer'
)
open.mockRestore()
})
it('hides topup verification after permission is revoked', () => {
mockCanTopUp.value = false
mockTopupActionOperation.value = {
actionUrl: 'https://verify.example/sensitive-token'
}
renderDialog()
expect(
screen.queryByRole('button', { name: 'Complete verification' })
).not.toBeInTheDocument()
})
it('refreshes both balance and status after a completed top-up', async () => {
mockTopup.mockResolvedValue(topupResponse('completed'))
renderDialog()
await clickAddCredits()
expect(mockFetchBalance).toHaveBeenCalledOnce()
expect(mockFetchStatus).toHaveBeenCalledOnce()
expect(mockShowSettings).toHaveBeenCalledWith('workspace')
expect(mockTrackBillingEvent).toHaveBeenCalledWith({
operation: 'topup',
stage: 'succeeded',
outcome: 'success',
billing_op_id: 'op-1'
})
})
it('keeps completed top-up telemetry successful when refresh fails', async () => {
mockTopup.mockResolvedValue(topupResponse('completed'))
mockFetchBalance.mockRejectedValueOnce(new Error('balance unavailable'))
mockFetchStatus.mockRejectedValueOnce(new Error('status unavailable'))
renderDialog()
await clickAddCredits()
expect(mockTrackBillingEvent).toHaveBeenCalledTimes(1)
expect(mockTrackBillingEvent).toHaveBeenCalledWith({
operation: 'topup',
stage: 'succeeded',
outcome: 'success',
billing_op_id: 'op-1'
})
expect(mockShowSettings).toHaveBeenCalledWith('workspace')
})
it('does not refresh balance or status for a pending top-up', async () => {
mockTopup.mockResolvedValue(topupResponse('pending'))
renderDialog()
await clickAddCredits()
expect(mockStartOperation).toHaveBeenCalledWith('op-1', 'topup')
expect(mockFetchBalance).not.toHaveBeenCalled()
expect(mockFetchStatus).not.toHaveBeenCalled()
expect(mockTrackBillingEvent).not.toHaveBeenCalled()
})
it('does not refresh balance or status for a failed top-up', async () => {
mockTopup.mockResolvedValue(topupResponse('failed'))
renderDialog()
await clickAddCredits()
expect(mockFetchBalance).not.toHaveBeenCalled()
expect(mockFetchStatus).not.toHaveBeenCalled()
expect(mockTrackBillingEvent).toHaveBeenCalledWith({
operation: 'topup',
stage: 'failed',
outcome: 'failure',
billing_op_id: 'op-1',
failure_category: 'unknown'
})
})
it('does not top up after the workspace role loses permission', async () => {
renderDialog()
mockCanTopUp.value = false
await clickAddCredits()
expect(mockTopup).not.toHaveBeenCalled()
expect(mockTrackTopUpPurchase).not.toHaveBeenCalled()
expect(mockToastAdd).not.toHaveBeenCalled()
expect(mockCloseDialog).not.toHaveBeenCalled()
})
it('keeps a mounted workspace dialog usable after routing switches to legacy billing', async () => {
mockCanTopUp.value = false
mockShouldUseWorkspaceBilling.value = false
mockTopup.mockResolvedValue(topupResponse('completed'))
renderDialog()
await clickAddCredits()
expect(mockTopup).toHaveBeenCalledWith(5000)
expect(mockFetchBalance).toHaveBeenCalledOnce()
expect(mockFetchStatus).toHaveBeenCalledOnce()
})
})