Files
wehub-resource-sync 23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:02:19 +08:00

650 lines
18 KiB
TypeScript

/**
* AST Analyzer Tests
*
* Tests for the AST analysis functionality.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { ASTAnalyzer, createASTAnalyzer, type ASTAnalysis } from '../../src/ruvector/ast-analyzer';
// Mock the @ruvector/ast module
vi.mock('@ruvector/ast', () => ({
createASTAnalyzer: vi.fn(() => null),
}));
describe('ASTAnalyzer', () => {
let analyzer: ASTAnalyzer;
beforeEach(() => {
analyzer = new ASTAnalyzer();
});
afterEach(() => {
analyzer.clearCache();
vi.clearAllMocks();
});
describe('constructor', () => {
it('should create analyzer with default config', () => {
const stats = analyzer.getStats();
expect(stats.cacheSize).toBe(0);
});
it('should create analyzer with custom config', () => {
const customAnalyzer = new ASTAnalyzer({
maxFileSize: 500000,
languages: ['typescript'],
includeComments: false,
});
expect(customAnalyzer).toBeInstanceOf(ASTAnalyzer);
});
});
describe('initialize', () => {
it('should initialize without ruvector (fallback mode)', async () => {
await analyzer.initialize();
const stats = analyzer.getStats();
expect(stats.useNative).toBe(0);
});
});
describe('analyze', () => {
const typeScriptCode = `
import { foo } from './foo';
import bar from 'bar';
export interface User {
id: string;
name: string;
}
export class UserService {
private users: User[] = [];
async getUser(id: string): Promise<User | null> {
return this.users.find(u => u.id === id) || null;
}
createUser(name: string): User {
const user = { id: Date.now().toString(), name };
this.users.push(user);
return user;
}
}
export function helper() {
return 'helper';
}
`;
it('should analyze TypeScript code', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis).toHaveProperty('filePath', 'test.ts');
expect(analysis).toHaveProperty('language', 'typescript');
expect(analysis).toHaveProperty('root');
expect(analysis).toHaveProperty('functions');
expect(analysis).toHaveProperty('classes');
expect(analysis).toHaveProperty('imports');
expect(analysis).toHaveProperty('exports');
expect(analysis).toHaveProperty('complexity');
expect(analysis).toHaveProperty('timestamp');
expect(analysis).toHaveProperty('durationMs');
});
it('should extract functions', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis.functions.length).toBeGreaterThan(0);
const funcNames = analysis.functions.map(f => f.name);
expect(funcNames).toContain('helper');
});
it('should extract classes', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis.classes.length).toBeGreaterThan(0);
const classNames = analysis.classes.map(c => c.name);
expect(classNames).toContain('UserService');
});
it('should extract imports', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis.imports).toContain('./foo');
expect(analysis.imports).toContain('bar');
});
it('should extract exports', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis.exports).toContain('User');
expect(analysis.exports).toContain('UserService');
expect(analysis.exports).toContain('helper');
});
it('should calculate complexity metrics', () => {
const analysis = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis.complexity).toHaveProperty('cyclomatic');
expect(analysis.complexity).toHaveProperty('cognitive');
expect(analysis.complexity).toHaveProperty('loc');
expect(analysis.complexity).toHaveProperty('commentDensity');
expect(analysis.complexity.cyclomatic).toBeGreaterThan(0);
expect(analysis.complexity.loc).toBeGreaterThan(0);
});
it('should cache analysis results', () => {
const analysis1 = analyzer.analyze(typeScriptCode, 'test.ts');
const analysis2 = analyzer.analyze(typeScriptCode, 'test.ts');
expect(analysis1.timestamp).toBe(analysis2.timestamp);
expect(analyzer.getStats().cacheSize).toBe(1);
});
it('should throw on file too large', () => {
const smallAnalyzer = new ASTAnalyzer({ maxFileSize: 10 });
expect(() => smallAnalyzer.analyze(typeScriptCode, 'test.ts')).toThrow('File too large');
});
});
describe('language detection', () => {
it('should detect TypeScript by extension', () => {
const analysis = analyzer.analyze('const x = 1;', 'file.ts');
expect(analysis.language).toBe('typescript');
});
it('should detect JavaScript by extension', () => {
const analysis = analyzer.analyze('const x = 1;', 'file.js');
expect(analysis.language).toBe('javascript');
});
it('should detect Python by extension', () => {
const analysis = analyzer.analyze('x = 1', 'file.py');
expect(analysis.language).toBe('python');
});
it('should detect Rust by extension', () => {
const analysis = analyzer.analyze('fn main() {}', 'file.rs');
expect(analysis.language).toBe('rust');
});
it('should detect Go by extension', () => {
const analysis = analyzer.analyze('package main', 'file.go');
expect(analysis.language).toBe('go');
});
it('should detect TypeScript by content', () => {
const analysis = analyzer.analyze('const x: string = "hello";', 'unknown');
expect(analysis.language).toBe('typescript');
});
it('should detect Python by content', () => {
const analysis = analyzer.analyze('def hello():\n print("hi")', 'unknown');
expect(analysis.language).toBe('python');
});
it('should return unknown for undetectable language', () => {
const analysis = analyzer.analyze('random text', 'file.xyz');
expect(analysis.language).toBe('unknown');
});
});
describe('JavaScript analysis', () => {
const jsCode = `
const helper = (x) => x * 2;
function processData(data) {
if (data.length === 0) {
return [];
}
return data.map(helper);
}
class DataProcessor {
process(data) {
return processData(data);
}
}
module.exports = { DataProcessor, processData };
`;
it('should analyze JavaScript code', () => {
const analysis = analyzer.analyze(jsCode, 'processor.js');
expect(analysis.language).toBe('javascript');
expect(analysis.functions.length).toBeGreaterThan(0);
});
it('should extract arrow functions', () => {
const analysis = analyzer.analyze(jsCode, 'processor.js');
const funcNames = analysis.functions.map(f => f.name);
expect(funcNames).toContain('helper');
});
});
describe('Python analysis', () => {
const pythonCode = `
import os
from typing import List
class DataService:
def __init__(self):
self.data = []
def add_item(self, item):
self.data.append(item)
def process(items: List[str]) -> List[str]:
return [item.upper() for item in items]
`;
it('should analyze Python code', () => {
const analysis = analyzer.analyze(pythonCode, 'service.py');
expect(analysis.language).toBe('python');
});
it('should extract Python functions', () => {
const analysis = analyzer.analyze(pythonCode, 'service.py');
const funcNames = analysis.functions.map(f => f.name);
expect(funcNames).toContain('process');
});
it('should extract Python classes', () => {
const analysis = analyzer.analyze(pythonCode, 'service.py');
const classNames = analysis.classes.map(c => c.name);
expect(classNames).toContain('DataService');
});
it('should extract Python imports', () => {
const analysis = analyzer.analyze(pythonCode, 'service.py');
expect(analysis.imports).toContain('os');
});
});
describe('getFunctionAtLine', () => {
const code = `
function foo() {
return 1;
}
function bar() {
return 2;
}
`;
it('should return function containing line', () => {
const analysis = analyzer.analyze(code, 'test.ts');
const func = analyzer.getFunctionAtLine(analysis, 3);
expect(func).not.toBeNull();
expect(func?.name).toBe('foo');
});
it('should return null for line outside functions', () => {
const analysis = analyzer.analyze(code, 'test.ts');
const func = analyzer.getFunctionAtLine(analysis, 1);
expect(func).toBeNull();
});
});
describe('getClassAtLine', () => {
const code = `
class Foo {
method() {}
}
class Bar {
method() {}
}
`;
it('should return class containing line', () => {
const analysis = analyzer.analyze(code, 'test.ts');
const cls = analyzer.getClassAtLine(analysis, 3);
expect(cls).not.toBeNull();
expect(cls?.name).toBe('Foo');
});
it('should return null for line outside classes', () => {
const analysis = analyzer.analyze(code, 'test.ts');
const cls = analyzer.getClassAtLine(analysis, 1);
expect(cls).toBeNull();
});
});
describe('getSymbols', () => {
const code = `
function helper() {}
class Service {}
function util() {}
`;
it('should return all symbols', () => {
const analysis = analyzer.analyze(code, 'test.ts');
const symbols = analyzer.getSymbols(analysis);
expect(symbols).toContain('helper');
expect(symbols).toContain('Service');
expect(symbols).toContain('util');
});
});
describe('clearCache', () => {
it('should clear the analysis cache', () => {
analyzer.analyze('const x = 1;', 'test.ts');
expect(analyzer.getStats().cacheSize).toBe(1);
analyzer.clearCache();
expect(analyzer.getStats().cacheSize).toBe(0);
});
});
describe('complexity calculation', () => {
it('should calculate higher cyclomatic complexity for branching code', () => {
const simpleCode = 'const x = 1;';
const complexCode = `
function complex(x) {
if (x > 0) {
if (x > 10) {
return 'big';
} else {
return 'medium';
}
} else {
return 'small';
}
}
`;
const simpleAnalysis = analyzer.analyze(simpleCode, 'simple.ts');
const complexAnalysis = analyzer.analyze(complexCode, 'complex.ts');
expect(complexAnalysis.complexity.cyclomatic).toBeGreaterThan(
simpleAnalysis.complexity.cyclomatic
);
});
it('should calculate cognitive complexity with nesting', () => {
const nestedCode = `
function nested() {
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
while (true) {
break;
}
}
}
}
`;
const analysis = analyzer.analyze(nestedCode, 'nested.ts');
expect(analysis.complexity.cognitive).toBeGreaterThan(0);
});
it('should calculate comment density', () => {
const commentedCode = `
// This is a comment
function foo() {
// Another comment
return 1;
}
`;
const analysis = analyzer.analyze(commentedCode, 'test.ts');
expect(analysis.complexity.commentDensity).toBeGreaterThan(0);
});
});
describe('edge cases', () => {
it('should handle empty code', () => {
const analysis = analyzer.analyze('', 'empty.ts');
expect(analysis.functions).toHaveLength(0);
expect(analysis.classes).toHaveLength(0);
});
it('should handle code with only comments', () => {
const analysis = analyzer.analyze('// Just a comment\n/* block comment */', 'comments.ts');
// Comments are counted as lines but have low LOC
expect(analysis.complexity.loc).toBeGreaterThanOrEqual(0);
});
it('should handle malformed code', () => {
const analysis = analyzer.analyze('function { broken', 'broken.ts');
expect(analysis).toBeDefined();
});
it('should handle Unicode identifiers', () => {
const analysis = analyzer.analyze('function \u00e9valuate() { return true; }', 'unicode.ts');
expect(analysis.functions.length).toBeGreaterThanOrEqual(0);
});
});
});
describe('createASTAnalyzer', () => {
it('should create analyzer instance', () => {
const analyzer = createASTAnalyzer();
expect(analyzer).toBeInstanceOf(ASTAnalyzer);
});
it('should accept config', () => {
const analyzer = createASTAnalyzer({ maxFileSize: 500000 });
expect(analyzer).toBeInstanceOf(ASTAnalyzer);
});
});
describe('ASTAnalyzer Advanced Scenarios', () => {
let analyzer: ASTAnalyzer;
beforeEach(() => {
analyzer = new ASTAnalyzer();
});
afterEach(() => {
analyzer.clearCache();
});
describe('TypeScript-specific features', () => {
it('should extract interfaces', () => {
// Note: The fallback analyzer extracts exported interfaces
const code = `
export interface User {
id: string;
name: string;
}
export interface Admin {
role: 'admin';
}
`;
const analysis = analyzer.analyze(code, 'interfaces.ts');
// The fallback implementation extracts export names
expect(analysis.exports).toContain('User');
expect(analysis.exports).toContain('Admin');
});
it('should extract type aliases', () => {
const code = `
export type UserId = string;
export type UserRole = 'admin' | 'user';
`;
const analysis = analyzer.analyze(code, 'types.ts');
// The fallback implementation may or may not extract type exports
// depending on regex patterns - just check it doesn't throw
expect(analysis).toBeDefined();
});
it('should extract generic types', () => {
const code = `
export function identity<T>(value: T): T {
return value;
}
export class Container<T> {
private value: T;
constructor(value: T) {
this.value = value;
}
}
`;
const analysis = analyzer.analyze(code, 'generics.ts');
// Fallback analyzer extracts functions and classes - check at least one exists
expect(analysis.functions.length + analysis.classes.length).toBeGreaterThan(0);
});
it('should extract decorators', () => {
const code = `
@Injectable()
export class UserService {
@Inject('config')
private config: Config;
@Get('/users')
async getUsers(): Promise<User[]> {
return [];
}
}
`;
const analysis = analyzer.analyze(code, 'service.ts');
expect(analysis.classes.some(c => c.name === 'UserService')).toBe(true);
});
it('should handle async/await functions', () => {
const code = `
export async function fetchUser(id: string): Promise<User> {
const response = await fetch(\`/api/users/\${id}\`);
return response.json();
}
export const asyncArrow = async () => {
return await Promise.resolve('done');
};
`;
const analysis = analyzer.analyze(code, 'async.ts');
expect(analysis.functions.some(f => f.name === 'fetchUser')).toBe(true);
expect(analysis.functions.some(f => f.name === 'asyncArrow')).toBe(true);
});
});
describe('JavaScript analysis edge cases', () => {
it('should handle CommonJS require', () => {
const code = `
const fs = require('fs');
const { join } = require('path');
const utils = require('./utils');
module.exports = { processFile };
function processFile(path) {
return fs.readFileSync(path);
}
`;
const analysis = analyzer.analyze(code, 'cjs.js');
expect(analysis.imports).toContain('fs');
expect(analysis.imports).toContain('path');
expect(analysis.imports).toContain('./utils');
});
it('should handle mixed import styles', () => {
const code = `
import defaultExport from 'module1';
import { named1, named2 } from 'module2';
import * as namespace from 'module3';
export { defaultExport, named1 };
`;
const analysis = analyzer.analyze(code, 'mixed.js');
expect(analysis.imports).toContain('module1');
expect(analysis.imports).toContain('module2');
expect(analysis.imports).toContain('module3');
// Note: side-effect imports may not be captured by fallback regex
});
});
describe('complexity edge cases', () => {
it('should calculate high complexity for deeply nested code', () => {
const deeplyNested = `
function deepNest() {
if (a) {
if (b) {
if (c) {
if (d) {
while (e) {
for (let i = 0; i < 10; i++) {
try {
switch (f) {
case 1: break;
case 2: break;
default: throw new Error();
}
} catch (err) {
console.error(err);
}
}
}
}
}
}
}
}
`;
const analysis = analyzer.analyze(deeplyNested, 'nested.ts');
expect(analysis.complexity.cognitive).toBeGreaterThan(5);
expect(analysis.complexity.cyclomatic).toBeGreaterThan(5);
});
it('should calculate low complexity for simple code', () => {
const simple = `
const x = 1;
const y = 2;
const z = x + y;
export { x, y, z };
`;
const analysis = analyzer.analyze(simple, 'simple.ts');
expect(analysis.complexity.cyclomatic).toBeLessThanOrEqual(2);
});
});
describe('method extraction', () => {
it('should extract class methods with parameters', () => {
const code = `
export class Calculator {
add(a: number, b: number): number {
return a + b;
}
subtract(a: number, b: number): number {
return a - b;
}
private multiply(a: number, b: number): number {
return a * b;
}
static divide(a: number, b: number): number {
return a / b;
}
}
`;
const analysis = analyzer.analyze(code, 'calculator.ts');
expect(analysis.classes.some(c => c.name === 'Calculator')).toBe(true);
// Note: The fallback analyzer may not extract method details
const calcClass = analysis.classes.find(c => c.name === 'Calculator');
expect(calcClass).toBeDefined();
});
});
describe('symbol extraction from different constructs', () => {
it('should extract exported constants', () => {
const code = `
export const API_URL = 'https://api.example.com';
export const MAX_RETRIES = 3;
export const DEFAULT_CONFIG = { timeout: 5000 };
`;
const analysis = analyzer.analyze(code, 'constants.ts');
expect(analysis.exports).toContain('API_URL');
expect(analysis.exports).toContain('MAX_RETRIES');
expect(analysis.exports).toContain('DEFAULT_CONFIG');
});
it('should extract re-exports', () => {
const code = `
export { foo } from './foo';
export { bar as baz } from './bar';
export * from './utils';
`;
const analysis = analyzer.analyze(code, 'reexports.ts');
// The fallback implementation extracts export sources as imports
// At minimum it should not throw
expect(analysis).toBeDefined();
});
});
});