import { getCompletions } from '../completion/completions'; import type { Situation } from '../completion/situation'; import type { DataProvider } from '../completion/DataProvider'; // Minimal mock DataProvider that returns empty data function createMockDataProvider(overrides: Partial = {}): DataProvider { return { getAllMetricNames: () => [], metricNamesToMetrics: (names: string[]) => names.map((name) => ({ name, help: '', type: '' })), fetchLabels: async () => [], fetchSeries: async () => [], fetchLabelValues: async () => [], getVariablesNames: () => [], durationVariablesCompletion: false, ...overrides, } as unknown as DataProvider; } describe('getCompletions', () => { const mockDataProvider = createMockDataProvider(); describe('with keyword completion', () => { it('includes "with" in EMPTY completions', async () => { const situation: Situation = { type: 'EMPTY' }; const completions = await getCompletions(situation, mockDataProvider); const withCompletion = completions.find((c) => c.label === 'with'); expect(withCompletion).toBeDefined(); expect(withCompletion!.insertText).toBe('with ('); expect(withCompletion!.detail).toContain('cte_name'); }); it('includes "with" in AT_ROOT completions', async () => { const situation: Situation = { type: 'AT_ROOT' }; const completions = await getCompletions(situation, mockDataProvider); const withCompletion = completions.find((c) => c.label === 'with'); expect(withCompletion).toBeDefined(); }); it('includes "with" in IN_WITH_BODY completions', async () => { const situation: Situation = { type: 'IN_WITH_BODY' }; const completions = await getCompletions(situation, mockDataProvider); const withCompletion = completions.find((c) => c.label === 'with'); expect(withCompletion).toBeDefined(); }); it('includes "with" in IN_FUNCTION completions', async () => { const situation: Situation = { type: 'IN_FUNCTION' }; const completions = await getCompletions(situation, mockDataProvider); const withCompletion = completions.find((c) => c.label === 'with'); expect(withCompletion).toBeDefined(); }); }); describe('Function completions', () => { it('includes standard PromQL functions', async () => { const situation: Situation = { type: 'AT_ROOT' }; const completions = await getCompletions(situation, mockDataProvider); const sumCompletion = completions.find((c) => c.label === 'sum'); expect(sumCompletion).toBeDefined(); expect(sumCompletion!.type).toBe('FUNCTION'); }); }); describe('Duration completions', () => { it('returns duration values for IN_DURATION', async () => { const situation: Situation = { type: 'IN_DURATION' }; const completions = await getCompletions(situation, mockDataProvider); expect(completions.length).toBeGreaterThan(0); expect(completions.every((c) => c.type === 'DURATION')).toBe(true); }); }); });