|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import { assert, expect } from 'chai'; |
8 | | -import sinon, { SinonFakeTimers } from 'sinon'; |
| 8 | +import sinon, { SinonFakeTimers, SinonStubbedFunction } from 'sinon'; |
| 9 | +import { telemetry } from '../../telemetry'; |
| 10 | +import { LowBalanceSeverity } from '../../telemetry/api'; |
9 | 11 | import { TestEventEmitter } from '../../test/helpers/events'; |
10 | 12 | import { newVsCodeStub, VsCodeStub } from '../../test/helpers/vscode'; |
11 | 13 | import { SubscriptionTier, ConsumptionUserInfo } from '../api'; |
@@ -426,6 +428,111 @@ describe('ConsumptionNotifier', () => { |
426 | 428 | }); |
427 | 429 | } |
428 | 430 |
|
| 431 | + describe('telemetry', () => { |
| 432 | + let logStub: SinonStubbedFunction<typeof telemetry.logLowCcuNotification>; |
| 433 | + |
| 434 | + beforeEach(() => { |
| 435 | + logStub = sinon.stub(telemetry, 'logLowCcuNotification'); |
| 436 | + }); |
| 437 | + |
| 438 | + /** |
| 439 | + * Stubs out the given severity's notification API to immediately resolve |
| 440 | + * with the given action (or `undefined` to simulate a dismiss). |
| 441 | + * |
| 442 | + * @param severity - The severity of the notification to stub ("warn" or |
| 443 | + * "error"). |
| 444 | + * @param respondWith - The action to resolve with, or `undefined` to |
| 445 | + * simulate a dismiss. |
| 446 | + */ |
| 447 | + function autoRespond( |
| 448 | + severity: NotificationSeverity, |
| 449 | + respondWith: string | undefined, |
| 450 | + ): void { |
| 451 | + const stub = |
| 452 | + severity === 'warn' |
| 453 | + ? vs.window.showWarningMessage |
| 454 | + : vs.window.showErrorMessage; |
| 455 | + // Type assertion needed due to overloading. |
| 456 | + (stub as sinon.SinonStub).callsFake(() => Promise.resolve(respondWith)); |
| 457 | + } |
| 458 | + |
| 459 | + it('logs SEVERITY_LOW with clicked=false when warning is dismissed', async () => { |
| 460 | + const ccuInfo = createCcuInfo( |
| 461 | + { paidMinutes: 0, freeMinutes: 1 }, |
| 462 | + SubscriptionTier.NONE, |
| 463 | + ); |
| 464 | + autoRespond('warn', undefined); |
| 465 | + |
| 466 | + const noOp = consumptionNotifier.nextConsumptionCalculation(); |
| 467 | + ccuEmitter.fire(ccuInfo); |
| 468 | + await noOp; |
| 469 | + |
| 470 | + sinon.assert.calledOnceWithExactly( |
| 471 | + logStub, |
| 472 | + LowBalanceSeverity.SEVERITY_LOW, |
| 473 | + SubscriptionTier.NONE, |
| 474 | + false, |
| 475 | + ); |
| 476 | + }); |
| 477 | + |
| 478 | + it('logs SEVERITY_DEPLETED with clicked=true when error action is clicked', async () => { |
| 479 | + const ccuInfo = createCcuInfo( |
| 480 | + { paidMinutes: 0, freeMinutes: 0 }, |
| 481 | + SubscriptionTier.NONE, |
| 482 | + ); |
| 483 | + autoRespond('error', 'Sign Up for Colab'); |
| 484 | + |
| 485 | + const noOp = consumptionNotifier.nextConsumptionCalculation(); |
| 486 | + ccuEmitter.fire(ccuInfo); |
| 487 | + await noOp; |
| 488 | + |
| 489 | + sinon.assert.calledOnceWithExactly( |
| 490 | + logStub, |
| 491 | + LowBalanceSeverity.SEVERITY_DEPLETED, |
| 492 | + SubscriptionTier.NONE, |
| 493 | + true, |
| 494 | + ); |
| 495 | + }); |
| 496 | + |
| 497 | + it('plumbs the Pro subscription tier through unchanged', async () => { |
| 498 | + const ccuInfo = createCcuInfo( |
| 499 | + { paidMinutes: 0, freeMinutes: 0 }, |
| 500 | + SubscriptionTier.PRO, |
| 501 | + ); |
| 502 | + autoRespond('error', 'Upgrade to Pro+'); |
| 503 | + |
| 504 | + const noOp = consumptionNotifier.nextConsumptionCalculation(); |
| 505 | + ccuEmitter.fire(ccuInfo); |
| 506 | + await noOp; |
| 507 | + |
| 508 | + sinon.assert.calledOnceWithExactly( |
| 509 | + logStub, |
| 510 | + LowBalanceSeverity.SEVERITY_DEPLETED, |
| 511 | + SubscriptionTier.PRO, |
| 512 | + true, |
| 513 | + ); |
| 514 | + }); |
| 515 | + |
| 516 | + it('plumbs the Pro+ subscription tier through unchanged', async () => { |
| 517 | + const ccuInfo = createCcuInfo( |
| 518 | + { paidMinutes: 0, freeMinutes: 0 }, |
| 519 | + SubscriptionTier.PRO_PLUS, |
| 520 | + ); |
| 521 | + autoRespond('error', 'Purchase More CCUs'); |
| 522 | + |
| 523 | + const noOp = consumptionNotifier.nextConsumptionCalculation(); |
| 524 | + ccuEmitter.fire(ccuInfo); |
| 525 | + await noOp; |
| 526 | + |
| 527 | + sinon.assert.calledOnceWithExactly( |
| 528 | + logStub, |
| 529 | + LowBalanceSeverity.SEVERITY_DEPLETED, |
| 530 | + SubscriptionTier.PRO_PLUS, |
| 531 | + true, |
| 532 | + ); |
| 533 | + }); |
| 534 | + }); |
| 535 | + |
429 | 536 | describe('snooze', () => { |
430 | 537 | let fakeClock: SinonFakeTimers; |
431 | 538 |
|
|
0 commit comments