This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/gitnexus/test/unit/parser-loader.test.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

170 lines
6.0 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { loadParser, loadLanguage } from '../../src/core/tree-sitter/parser-loader.js';
import { SupportedLanguages } from '../../src/config/supported-languages.js';
describe('parser-loader', () => {
describe('loadParser', () => {
it('returns a Parser instance', async () => {
const parser = await loadParser();
expect(parser).toBeDefined();
expect(typeof parser.parse).toBe('function');
});
it('returns the same singleton instance', async () => {
const parser1 = await loadParser();
const parser2 = await loadParser();
expect(parser1).toBe(parser2);
});
});
describe('loadLanguage', () => {
it('loads TypeScript language', async () => {
await expect(loadLanguage(SupportedLanguages.TypeScript)).resolves.not.toThrow();
});
it('loads JavaScript language', async () => {
await expect(loadLanguage(SupportedLanguages.JavaScript)).resolves.not.toThrow();
});
it('loads Python language', async () => {
await expect(loadLanguage(SupportedLanguages.Python)).resolves.not.toThrow();
});
it('loads Java language', async () => {
await expect(loadLanguage(SupportedLanguages.Java)).resolves.not.toThrow();
});
it('loads C language', async () => {
await expect(loadLanguage(SupportedLanguages.C)).resolves.not.toThrow();
});
it('loads C++ language', async () => {
await expect(loadLanguage(SupportedLanguages.CPlusPlus)).resolves.not.toThrow();
});
it('loads C# language', async () => {
await expect(loadLanguage(SupportedLanguages.CSharp)).resolves.not.toThrow();
});
it('loads Go language', async () => {
await expect(loadLanguage(SupportedLanguages.Go)).resolves.not.toThrow();
});
it('loads Rust language', async () => {
await expect(loadLanguage(SupportedLanguages.Rust)).resolves.not.toThrow();
});
it('loads PHP language', async () => {
await expect(loadLanguage(SupportedLanguages.PHP)).resolves.not.toThrow();
});
it('loads TSX grammar for .tsx files', async () => {
// TSX uses a different grammar (TypeScript.tsx vs TypeScript.typescript)
await expect(
loadLanguage(SupportedLanguages.TypeScript, 'Component.tsx'),
).resolves.not.toThrow();
});
it('loads TS grammar for .ts files', async () => {
await expect(loadLanguage(SupportedLanguages.TypeScript, 'utils.ts')).resolves.not.toThrow();
});
it('loads Ruby language', async () => {
await expect(loadLanguage(SupportedLanguages.Ruby)).resolves.not.toThrow();
});
it('throws for unsupported language', async () => {
await expect(loadLanguage('erlang' as SupportedLanguages)).rejects.toThrow(
'Unsupported language',
);
});
});
// #1242: regression coverage for the Windows tree-sitter@0.21.1 + tree-sitter-c
// ABI mismatch. setLanguage alone could pass while the first non-trivial
// traversal/query produced "Cannot read properties of undefined (reading
// '161')" inside unmarshalNode (or a native segfault under the worker).
describe('C parser ABI compatibility (#1242)', () => {
const C_SOURCE = `#include <stdio.h>
struct Foo { int a; int b; };
typedef struct Foo Bar;
static int helper(int x) { return x * 2; }
int add(int a, int b) { return a + b; }
int main(void) {
Bar b = {1, 2};
return add(b.a, helper(b.b));
}
`;
it('parses a non-trivial C translation unit and walks the tree', async () => {
const parser = await loadParser();
await loadLanguage(SupportedLanguages.C);
const tree = parser.parse(C_SOURCE);
expect(tree.rootNode.type).toBe('translation_unit');
let nodeCount = 0;
const walk = (node: { type: string; children: any[] }): void => {
nodeCount += 1;
// Touching `.type` here is what triggered the original
// unmarshalNode crash on incompatible ABIs.
expect(typeof node.type).toBe('string');
for (const child of node.children) walk(child);
};
walk(tree.rootNode as any);
expect(nodeCount).toBeGreaterThan(20);
});
it('extracts function definitions and call expressions via a query', async () => {
const Parser = (await import('tree-sitter')).default;
const parser = await loadParser();
await loadLanguage(SupportedLanguages.C);
const tree = parser.parse(C_SOURCE);
const language = parser.getLanguage();
const query = new (Parser as any).Query(
language,
'(function_definition declarator: (function_declarator declarator: (identifier) @name)) ' +
'(call_expression function: (identifier) @callee)',
);
const captures = query.captures(tree.rootNode);
const names = captures.filter((c: any) => c.name === 'name').map((c: any) => c.node.text);
const callees = captures.filter((c: any) => c.name === 'callee').map((c: any) => c.node.text);
expect(names).toEqual(expect.arrayContaining(['helper', 'add', 'main']));
expect(callees).toEqual(expect.arrayContaining(['add', 'helper']));
});
it('walks a TreeCursor without throwing (catches unmarshalNode regressions)', async () => {
const parser = await loadParser();
await loadLanguage(SupportedLanguages.C);
const tree = parser.parse(C_SOURCE);
const cursor = tree.walk();
let visited = 0;
const descend = (): void => {
visited += 1;
if (cursor.gotoFirstChild()) {
do {
descend();
} while (cursor.gotoNextSibling());
cursor.gotoParent();
}
};
descend();
expect(visited).toBeGreaterThan(20);
});
});
describe('Swift optional dependency', () => {
it('loads Swift from the default optional dependency and parses source', async () => {
const parser = await loadParser();
await loadLanguage(SupportedLanguages.Swift);
const tree = parser.parse('class Foo { func bar() {} }');
expect(tree.rootNode.type).toBe('source_file');
expect(tree.rootNode.namedChildCount).toBe(1);
});
});
});