Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

4.4 KB
Raw Blame
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest';
import { dockHarness, readerOf } from './chat-dock.fixture';

const dock = dockHarness();
const GENERAL = '"""Aggregate functions for PostgreSQL."""\nfrom django.db.models import Aggregate\n\n\n'
    + 'class ArrayAgg(OrderableAggMixin, Aggregate):\n    function = "ARRAY_AGG"\n    allow_distinct = True\n\n\n'
    + 'class JSONBAgg(OrderableAggMixin, Aggregate):\n    function = "JSONB_AGG"\n    allow_distinct = True\n    output_field = JSONField()\n';
const PATH = 'django/contrib/postgres/aggregates/general.py';
const JSONB_AGG = 'class JSONBAgg(OrderableAggMixin, Aggregate):\n    function = "JSONB_AGG"\n    allow_distinct = True\n    output_field = JSONField()';

/** Short general questions about an open code file or marked code got the free model answer
 * the reader had called garbled, while a Galaxy selection got listed facts and one checked
 * sentence (B3). */
describe('short general questions about an open code file or marked code (B3)', () => {
    it('answers "was macht diese datei?" with facts read from the file and one checked model text, in German', async () => {
        const { props, runtime } = dock.setup();
        runtime.chat.mockResolvedValueOnce('Die Datei definiert die Aggregate `ArrayAgg` und `JSONBAgg`.');
        await dock.render({ ...props, selectionScope: 'django-demo:explore', readerContext: readerOf(GENERAL, PATH) }); await dock.load();
        await dock.ask('was macht diese datei?');
        expect(runtime.chat).toHaveBeenCalledOnce();
        const [request, , options] = runtime.chat.mock.calls[0];
        expect(options).toMatchObject({ generationProfile: 'automatic-explanation' });
        expect(request.at(-1)!.content).toContain('Answer in German.');
        expect(request.at(-1)!.content).toContain('class JSONBAgg(OrderableAggMixin, Aggregate):');
        const answer = dock.answerOf(dock.last());
        expect(answer).toContain('general.py: Python-Quelltext, 13 Zeilen.');
        expect(answer).toContain('Docstring des Moduls: "Aggregate functions for PostgreSQL."');
        expect(answer).toContain('Definitionen auf oberster Ebene (2): class ArrayAgg, class JSONBAgg.');
        expect(answer).toContain('Die Datei definiert die Aggregate ArrayAgg und JSONBAgg.');
        expect(answer).toContain('Fakten aus der Datei gelesen; der Text danach ist vom Modell erzeugt.');
        expect(dock.buttonsOf(dock.last())).toEqual(['Modell fragen']);
    });

    it('answers a question about marked code that is one class with its lines and the code facts, in English', async () => {
        const { props, runtime } = dock.setup();
        runtime.chat.mockResolvedValueOnce('It declares the JSONB_AGG aggregate with `json_helper`.');
        await dock.render({ ...props, selectionScope: 'django-demo:explore', readerContext: readerOf(JSONB_AGG, PATH, 'selection', { start: 11, end: 14 }) }); await dock.load();
        await dock.ask('explain this');
        expect(runtime.chat).toHaveBeenCalledOnce();
        const answer = dock.answerOf(dock.last());
        expect(answer).toContain('Marked lines 11-14 of general.py (Python source file).');
        expect(answer).toContain('In the source:');
        expect(dock.last().querySelector('.cbm-chat-answer-text pre')?.textContent).toContain('output_field = JSONField()');
        // The model named something the code does not have: its text is left out.
        expect(answer).not.toContain('It declares the JSONB_AGG aggregate');
        // The note names what the left out text named (W5).
        expect(answer).toContain("Read from the file. The model's text named something the file does not show (here: json_helper) and was left out.");
    });

    it('still sends questions that are not general to the model as before', async () => {
        const { props, runtime } = dock.setup();
        await dock.render({ ...props, selectionScope: 'django-demo:explore', readerContext: readerOf(GENERAL, PATH) }); await dock.load();
        await dock.ask('Which class sets allow_distinct and why?');
        expect(runtime.chat).toHaveBeenCalledOnce();
        expect(runtime.chat.mock.calls[0][2]?.generationProfile).toBeUndefined();
        expect(dock.notesOf(dock.last())).toContain('Generated by the local model; it can be wrong.');
        await dock.ask('Erklär diese Datei Zeile für Zeile');
        expect(runtime.chat.mock.calls[1][2]?.generationProfile).toBeUndefined();
    });
});