719032b19f
Update Schema / Update configuration json schema (push) Has been cancelled
Memory Benchmark / Memory Test (Full Analysis) (push) Has been cancelled
CI Quality / Lint GitHub Actions (actionlint) (push) Has been cancelled
CI Quality / Lint GitHub Actions (zizmor) (push) Has been cancelled
CI Quality / Check typos (push) Has been cancelled
CI / Test coverage (push) Has been cancelled
CI / Test (22.x, windows-latest) (push) Has been cancelled
CI / Test (24.x, macos-latest) (push) Has been cancelled
CI / Test (24.x, ubuntu-latest) (push) Has been cancelled
CI / Test (24.x, windows-latest) (push) Has been cancelled
CI / Test (26.x, macos-latest) (push) Has been cancelled
CI / Test (26.x, ubuntu-latest) (push) Has been cancelled
CI / Test (26.x, windows-latest) (push) Has been cancelled
CI / Test with Bun (latest, macos-latest) (push) Has been cancelled
CI / Test with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Test with Bun (latest, windows-latest) (push) Has been cancelled
CI / Build and run (22.x, macos-latest) (push) Has been cancelled
CI / Build and run (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (22.x, windows-latest) (push) Has been cancelled
autofix.ci / autofix (push) Has been cancelled
CI Browser Extension / Lint Browser Extension (push) Has been cancelled
CI Browser Extension / Test Browser Extension (push) Has been cancelled
Memory Benchmark / Memory Test (push) Has been cancelled
CI Website / Lint Website Client (push) Has been cancelled
CI Website / Lint Website Server (push) Has been cancelled
CI Website / Test Website Server (push) Has been cancelled
CI Website / Bundle Website Server (push) Has been cancelled
CI / Lint Biome (push) Has been cancelled
CI / Lint oxlint (push) Has been cancelled
CI / Lint TypeScript (push) Has been cancelled
CI / Lint Secretlint (push) Has been cancelled
CI / Test (22.x, macos-latest) (push) Has been cancelled
CI / Test (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, macos-latest) (push) Has been cancelled
CI / Build and run (24.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, windows-latest) (push) Has been cancelled
CI / Build and run (26.x, macos-latest) (push) Has been cancelled
CI / Build and run (26.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (26.x, windows-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, macos-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Docker / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Docker / build (linux/arm/v7, ubuntu-24.04-arm) (push) Has been cancelled
Docker / build (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Docker / merge (push) Has been cancelled
Pack repository with Repomix / pack-repo (push) Has been cancelled
Performance Benchmark History / Benchmark (macos-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (ubuntu-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (windows-latest) (push) Has been cancelled
Performance Benchmark History / Store Results (push) Has been cancelled
Test Repomix Action / Test Node.js 22 (push) Has been cancelled
Test Repomix Action / Test Node.js 24 (push) Has been cancelled
Test Repomix Action / Test Node.js 26 (push) Has been cancelled
80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
|
import { Parser } from 'web-tree-sitter';
|
|
import { LanguageParser } from '../../../src/core/treeSitter/languageParser.js';
|
|
import { RepomixError } from '../../../src/shared/errorHandle.js';
|
|
|
|
describe('LanguageParser', () => {
|
|
let parser: LanguageParser;
|
|
|
|
beforeAll(() => {
|
|
parser = new LanguageParser();
|
|
});
|
|
|
|
describe('guessTheLang', () => {
|
|
it('should return the correct language based on file extension', () => {
|
|
const testCases = [
|
|
{ filePath: 'file.js', expected: 'javascript' },
|
|
{ filePath: 'file.ts', expected: 'typescript' },
|
|
{ filePath: 'file.sol', expected: 'solidity' },
|
|
{ filePath: 'Contract.sol', expected: 'solidity' },
|
|
{ filePath: 'path/to/MyContract.sol', expected: 'solidity' },
|
|
];
|
|
|
|
for (const { filePath, expected } of testCases) {
|
|
const lang = parser.guessTheLang(filePath);
|
|
expect(lang).toBe(expected);
|
|
}
|
|
});
|
|
|
|
it('should return undefined for unsupported extensions', () => {
|
|
const filePath = 'file.txt';
|
|
const lang = parser.guessTheLang(filePath);
|
|
|
|
expect(lang).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('init / dispose', () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('throws RepomixError when used before init', async () => {
|
|
const fresh = new LanguageParser();
|
|
const error = await fresh.getParserForLang('javascript').catch((e) => e);
|
|
expect(error).toBeInstanceOf(RepomixError);
|
|
expect((error as Error).message).toMatch(/not initialized/);
|
|
});
|
|
|
|
it('init() is idempotent — second call short-circuits', async () => {
|
|
const initSpy = vi.spyOn(Parser, 'init').mockResolvedValue(undefined);
|
|
const target = new LanguageParser();
|
|
|
|
await target.init();
|
|
await target.init();
|
|
|
|
expect(initSpy).toHaveBeenCalledTimes(1);
|
|
await target.dispose();
|
|
});
|
|
|
|
it('wraps Parser.init() failures as RepomixError', async () => {
|
|
vi.spyOn(Parser, 'init').mockRejectedValue(new Error('wasm load failed'));
|
|
const target = new LanguageParser();
|
|
|
|
await expect(target.init()).rejects.toBeInstanceOf(RepomixError);
|
|
await expect(target.init()).rejects.toThrow(/Failed to initialize parser.*wasm load failed/);
|
|
});
|
|
|
|
it('dispose() resets state so subsequent calls require re-init', async () => {
|
|
vi.spyOn(Parser, 'init').mockResolvedValue(undefined);
|
|
const target = new LanguageParser();
|
|
await target.init();
|
|
|
|
await target.dispose();
|
|
|
|
// After dispose, the parser should look fresh again.
|
|
await expect(target.getParserForLang('javascript')).rejects.toThrow(/not initialized/);
|
|
});
|
|
});
|
|
});
|