Add tests for durationWithUnitRegexp functionality

Added tests for durationWithUnitRegexp to validate matching of complete durations with units and ensure non-matching cases are correctly identified.

Signed-off-by: ADITYA TIWARI <142050150+ADITYATIWARI342005@users.noreply.github.com>
This commit is contained in:
ADITYA TIWARI 2025-11-25 18:30:30 +05:30 committed by GitHub
parent bf76fde0c8
commit 137f846527
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { analyzeCompletion, computeStartCompletePosition, ContextKind } from './hybrid';
import { analyzeCompletion, computeStartCompletePosition, ContextKind, durationWithUnitRegexp } from './hybrid';
import { createEditorState, mockedMetricsTerms, mockPrometheusServer } from '../test/utils-test';
import { Completion, CompletionContext } from '@codemirror/autocomplete';
import {
@ -642,6 +642,32 @@ describe('analyzeCompletion test', () => {
});
});
describe('durationWithUnitRegexp test', () => {
it('should match complete durations with units', () => {
const testCases = [
{ input: '5m', expected: true },
{ input: '30s', expected: true },
{ input: '1h', expected: true },
{ input: '500ms', expected: true },
{ input: '2d', expected: true },
{ input: '1w', expected: true },
{ input: '1y', expected: true },
];
testCases.forEach(({ input, expected }) => {
expect(durationWithUnitRegexp.test(input)).toBe(expected);
});
});
it('should not match durations without units or partial units', () => {
const testCases = ['5', '30', '100', '5m5', 'm', 'd'];
testCases.forEach((input) => {
expect(durationWithUnitRegexp.test(input)).toBe(false);
});
});
});
describe('computeStartCompletePosition test', () => {
const testCases = [
{