Path: blob/main/extensions/copilot/src/util/node/test/debounce.spec.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { suite, test } from 'vitest';7import { Debouncer } from '../../common/debounce';89suite('Debouncing', function () {10test('single debounce call', async function () {11const debouncer = new Debouncer();12await debouncer.debounce(1);13});14test('repeated call within time limit', async function () {15const debouncer = new Debouncer();16let result: boolean | undefined;17(async () => {18try {19await debouncer.debounce(10);20result = true;21} catch {22// we should end up here as the debounce call should get cancelled by the subsequent one23result = false;24}25})();26await debouncer.debounce(1);27assert.deepStrictEqual(result, false);28});29test('repeated call outside time limit', async function () {30const debouncer = new Debouncer();31let result: boolean | undefined;32(async () => {33try {34await debouncer.debounce(1);35// we should end up here as the debounce call should have time to finish before the next one36result = true;37} catch {38result = false;39}40})();41await new Promise(resolve => setTimeout(resolve, 5));42await debouncer.debounce(1);43assert.deepStrictEqual(result, true);44});45test('multiple debounce objects are independent', async function () {46const debouncer1 = new Debouncer();47const debouncer2 = new Debouncer();48let result: boolean | undefined;49(async () => {50try {51await debouncer1.debounce(10);52// we should end up here as the debounce call won't be cancelled by the second one even though53// they run in parallel.54result = true;55} catch {56result = false;57}58})();59await debouncer2.debounce(1);60await new Promise(resolve => setTimeout(resolve, 20));61assert.deepStrictEqual(result, true);62});63});646566