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.

9.0 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { fileOutline } from './file-outline';
import { fileFactLines, isDataFile } from './file-facts';

/** django's tox.ini, shortened to its first sections. */
const TOX = `# Tox (https://tox.readthedocs.io/) is a tool for running tests in multiple
# virtualenvs.

[tox]
minversion = 4.0
skipsdist = true
envlist =
    py3
    black
    flake8

# Add environment to use the default python3 installation
[testenv:py3]
basepython = python3

[testenv]
usedevelop = true
setenv =
    PYTHONDONTWRITEBYTECODE=1
deps =
    -e .
    py{3,310,311,312,313,py3}: -rtests/requirements/py3.txt
    postgres: -rtests/requirements/postgres.txt
changedir = tests
commands =
    {envpython} runtests.py {posargs}

[testenv:javascript]
usedevelop = false
deps =
changedir = {toxinidir}
`;
const PRE_COMMIT = 'repos:\n  - repo: https://github.com/PyCQA/isort\n    rev: 5.13.2\n    hooks:\n      - id: isort\n  - repo: https://github.com/PyCQA/flake8\n    rev: 7.1.1\n    hooks:\n      - id: flake8\n';
const FLAKE8 = '[flake8]\nexclude = build,.git,.tox,./tests/.env\nextend-ignore = E203\nmax-line-length = 88\nper-file-ignores =\n    django/core/cache/backends/filebased.py:W601\n    tests/cache/tests.py:W601\n';
const EDITORCONFIG = '# https://editorconfig.org/\n\nroot = true\n\n[*]\nindent_style = space\nindent_size = 4\n\n[*.py]\nmax_line_length = 88\n\n[Makefile]\nindent_style = tab\n';

/** "was macht diese datei?" on tox.ini went to the model, which wrote "Testverwendungen" (B6). */
describe('the outline of an INI file, read from it (B6)', () => {
    it('lists each section of tox.ini with its keys and short values', () => {
        const outline = fileOutline('tox.ini', TOX, 'en')!;
        expect(outline).toContain('- `[tox]`: minversion `4.0`; skipsdist `true`; envlist: `py3`, `black`, `flake8`');
        expect(outline).toContain('- `[testenv:py3]`: basepython `python3`');
        expect(outline).toContain('- `[testenv]`: usedevelop `true`; setenv `PYTHONDONTWRITEBYTECODE=1`; deps: `-e .`, `py{3,310,311,312,313,py3}: -rtests/requirements/py3.txt`, '
            + '`postgres: -rtests/requirements/postgres.txt`; changedir `tests`; commands `{envpython} runtests.py {posargs}`');
        expect(outline).toContain('- `[testenv:javascript]`: usedevelop `false`; deps empty; changedir `{toxinidir}`');
        expect(outline).not.toContain('virtualenvs');
        expect(outline).toContain('_Read from the file; not generated by the model._');
        expect(fileOutline('tox.ini', TOX, 'de')).toContain('- `[testenv:javascript]`: usedevelop `false`; deps leer; changedir `{toxinidir}`');
    });

    it('reads .flake8, .editorconfig, setup.cfg, pytest.ini and .coveragerc the same way, keys before the first section included', () => {
        expect(fileOutline('.flake8', FLAKE8, 'en')).toContain('- `[flake8]`: exclude `build,.git,.tox,./tests/.env`; extend-ignore `E203`; max-line-length `88`; '
            + 'per-file-ignores: `django/core/cache/backends/filebased.py:W601`, `tests/cache/tests.py:W601`');
        const editor = fileOutline('.editorconfig', EDITORCONFIG, 'en')!;
        expect(editor).toContain('- `root`: `true`');
        expect(editor).toContain('- `[*]`: indent_style `space`; indent_size `4`');
        expect(editor).toContain('- `[Makefile]`: indent_style `tab`');
        expect(fileOutline('setup.cfg', '[metadata]\nname = demo\n\n[isort]\nprofile = black\n', 'en')).toContain('- `[isort]`: profile `black`');
        expect(fileOutline('pytest.ini', '[pytest]\naddopts = -ra\n', 'en')).toContain('- `[pytest]`: addopts `-ra`');
        expect(fileOutline('.coveragerc', '[run]\nbranch = True\nsource = django\n', 'en')).toContain('- `[run]`: branch `True`; source `django`');
    });

    it('bounds a section with many keys and a file with many sections with "+N more"', () => {
        const wide = `[many]\n${Array.from({ length: 11 }, (_, index) => `key${index} = ${index}`).join('\n')}\n`;
        expect(fileOutline('wide.ini', wide, 'en')).toContain('key7 `7`; +3 more');
        const sections = Array.from({ length: 15 }, (_, index) => `[section${index}]\nkey = ${index}\n`).join('\n');
        expect(fileOutline('many.cfg', sections, 'de')).toContain('+3 weitere');
    });

    it('counts the sections of .flake8 and .coveragerc for the automatic card like those of any INI file', () => {
        expect(isDataFile('.flake8')).toBe(true);
        expect(isDataFile('.coveragerc')).toBe(true);
        expect(fileFactLines('.flake8', FLAKE8)).toContain('Sections (1): `[flake8]` (4 keys).');
        expect(fileFactLines('tox.ini', TOX)).toContain('Sections (4): `[tox]` (3 keys), `[testenv:py3]` (1 key), `[testenv]` (5 keys), `[testenv:javascript]` (3 keys).');
    });
});

/** The outline named what the file holds but not what it is for, and said "Read from the file" twice (B7). */
describe('the purpose of a well-known file before its outline (B7)', () => {
    it('says what a pre-commit file is for, and "Read from the file" only in the note', () => {
        const english = fileOutline('.pre-commit-config.yaml', PRE_COMMIT, 'en')!;
        expect(english.split('\n\n').slice(0, 2)).toEqual(['`.pre-commit-config.yaml`: YAML configuration, 9 lines.', 'pre-commit configuration: hooks that run before each commit.']);
        expect(english.match(/Read from the file/g)).toHaveLength(1);
        const german = fileOutline('.pre-commit-config.yaml', PRE_COMMIT, 'de')!;
        expect(german.split('\n\n').slice(0, 2)).toEqual(['`.pre-commit-config.yaml`: YAML-Konfiguration, 9 Zeilen.', 'pre-commit-Konfiguration: Hooks, die vor jedem Commit laufen.']);
        expect(german.match(/Aus der Datei gelesen/g)).toHaveLength(1);
    });

    it.each([
        ['.github/workflows/new_contributor_pr.yml', 'name: New contributor message\non:\n  pull_request_target:\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/first-interaction@v1\n',
            'GitHub runs the jobs of this workflow when one of its triggers occurs.', 'GitHub führt die Jobs dieses Workflows aus, wenn einer seiner Auslöser eintritt.'],
        ['package.json', '{"name": "Django", "scripts": {"test": "grunt test"}}', 'npm package manifest: the name, scripts and dependencies of a JavaScript package.',
            'npm-Paketmanifest: Name, Skripte und Abhängigkeiten eines JavaScript-Pakets.'],
        ['pyproject.toml', '[build-system]\nrequires = ["setuptools"]\n\n[project]\nname = "Django"\n', 'Python project configuration: how the package is built, its metadata and the settings of tools.',
            'Python-Projektkonfiguration: wie das Paket gebaut wird, seine Metadaten und die Einstellungen von Werkzeugen.'],
        ['tox.ini', TOX, 'tox configuration: the test environments tox creates and the commands it runs in each.', 'tox-Konfiguration: die Testumgebungen, die tox anlegt, und die Befehle, die es in jeder ausführt.'],
        ['setup.cfg', '[metadata]\nname = demo\n', 'setuptools configuration: package metadata and options, often also settings of other tools.',
            'setuptools-Konfiguration: Paketmetadaten und Optionen, oft auch Einstellungen anderer Werkzeuge.'],
        ['docker-compose.yml', 'services:\n  web:\n    image: nginx\n', 'Docker Compose file: the services that Docker Compose starts together.', 'Docker-Compose-Datei: die Services, die Docker Compose zusammen startet.'],
        ['.readthedocs.yml', 'version: 2\nbuild:\n  os: ubuntu-24.04\n', 'Read the Docs configuration: how readthedocs.org builds the documentation.', 'Read-the-Docs-Konfiguration: wie readthedocs.org die Dokumentation baut.'],
        ['.editorconfig', EDITORCONFIG, 'EditorConfig: indentation, line endings and similar editor settings per file pattern.', 'EditorConfig: Einrückung, Zeilenenden und ähnliche Editor-Einstellungen je Dateimuster.'],
        ['graph-ui/tsconfig.json', '{"compilerOptions": {"strict": true}, "include": ["src"]}', 'TypeScript compiler configuration: which files are compiled and with which options.',
            'TypeScript-Compilerkonfiguration: welche Dateien mit welchen Optionen kompiliert werden.'],
        ['.flake8', FLAKE8, 'flake8 configuration: the rules of the Python linter flake8.', 'flake8-Konfiguration: die Regeln des Python-Linters flake8.'],
    ])('gives %s its purpose in both languages', (path, text, english, german) => {
        expect(fileOutline(path, text, 'en')!.split('\n\n')[1]).toBe(english);
        expect(fileOutline(path, text, 'de')!.split('\n\n')[1]).toBe(german);
    });

    it('gives an unknown file, or a known name with another structure, no purpose line', () => {
        const unknown = fileOutline('config/settings.yaml', 'debug: true\n', 'en')!;
        expect(unknown.split('\n\n')).toEqual(['`settings.yaml`: YAML configuration, 1 line.', '- `debug`: `true`', '_Read from the file; not generated by the model._']);
        expect(fileOutline('docker-compose.yml', 'version: "3"\n', 'en')!.split('\n\n')[1]).toBe('- `version`: `3`');
        expect(fileOutline('.pre-commit-config.yaml', 'default_language_version:\n  python: python3\n', 'en')).not.toContain('hooks that run before each commit');
    });
});