import fs from 'fs'; import path from 'path'; import { RustGenerator } from '../src/server-targets/rust/rust-generator'; import { OpenRPCSpec } from '../src/types'; describe('Integration Tests for IComprehensiveTypes', () => { const tempOutputDir = path.join(__dirname, 'temp-output'); let testSpec: OpenRPCSpec; beforeAll(async () => { // Clean up any previous test output if (fs.existsSync(tempOutputDir)) { fs.rmSync(tempOutputDir, { recursive: true, force: true }); } // Load OpenRPC spec for testing const specPath = path.join( __dirname, 'fixtures/IComprehensiveTypes.openrpc.json' ); if (!fs.existsSync(specPath)) { throw new Error('Test spec not found.'); } testSpec = JSON.parse(fs.readFileSync(specPath, 'utf8')); // Generate the Rust server for all tests const rustGenerator = new RustGenerator(); await rustGenerator.generateServer(testSpec, specPath, tempOutputDir); }); afterAll(() => { // Clean up test output if (fs.existsSync(tempOutputDir)) { fs.rmSync(tempOutputDir, { recursive: true, force: true }); } }); describe('Full Rust code generation pipeline', () => { it('should generate complete Rust project from OpenRPC spec', async () => { // Generation happens in beforeAll, just check files exist expect(fs.existsSync(path.join(tempOutputDir, 'Cargo.toml'))).toBe(true); expect(fs.existsSync(path.join(tempOutputDir, 'src', 'main.rs'))).toBe( true ); expect(fs.existsSync(path.join(tempOutputDir, 'src', 'lib.rs'))).toBe( true ); expect( fs.existsSync(path.join(tempOutputDir, 'src', 'gen', 'types.rs')) ).toBe(true); expect(fs.existsSync(path.join(tempOutputDir, 'Dockerfile'))).toBe(true); expect(fs.existsSync(path.join(tempOutputDir, 'README.md'))).toBe(true); }); it('should generate types.rs with proper content', async () => { const typesPath = path.join(tempOutputDir, 'src', 'gen', 'types.rs'); expect(fs.existsSync(typesPath)).toBe(true); const typesContent = fs.readFileSync(typesPath, 'utf8'); // Check imports expect(typesContent).toMatch(/use serde::\{Deserialize, Serialize\};/); expect(typesContent).toMatch( /use alloy_primitives::\{[^}]*Address[^}]*\};/ ); // Check that we have struct definitions expect(typesContent).toMatch(/pub struct \w+ \{/); // Check for specific struct types expect(typesContent).toMatch(/pub struct BasicStruct/); expect(typesContent).toMatch(/pub struct NestedStruct/); expect(typesContent).toMatch(/pub struct ComplexStruct/); }); it('should generate Cargo.toml with correct dependencies', async () => { const cargoPath = path.join(tempOutputDir, 'Cargo.toml'); expect(fs.existsSync(cargoPath)).toBe(true); const cargoContent = fs.readFileSync(cargoPath, 'utf8'); // Check for required dependencies expect(cargoContent).toMatch(/alloy-primitives/); expect(cargoContent).toMatch(/serde/); expect(cargoContent).toMatch(/jsonrpsee/); expect(cargoContent).toMatch(/tokio/); }); it('should generate main.rs with proper trait and implementation structure', async () => { const mainPath = path.join(tempOutputDir, 'src', 'main.rs'); expect(fs.existsSync(mainPath)).toBe(true); const mainContent = fs.readFileSync(mainPath, 'utf8'); // Check for server struct implementation expect(mainContent).toMatch(/pub struct ServerImpl/); // Check for trait implementation expect(mainContent).toMatch(/impl \w+Server for ServerImpl/); // Check for main function expect(mainContent).toMatch(/#\[tokio::main\]/); expect(mainContent).toMatch(/async fn main/); // Check that it references the server trait from gen module const serverPath = path.join(tempOutputDir, 'src', 'gen', 'server.rs'); expect(fs.existsSync(serverPath)).toBe(true); const serverContent = fs.readFileSync(serverPath, 'utf8'); // Check for trait definition in server.rs expect(serverContent).toMatch(/pub trait \w+/); expect(serverContent).toMatch(/#\[rpc\(server\)\]/); }); it('should handle all method types from comprehensive interface', async () => { const mainPath = path.join(tempOutputDir, 'src', 'main.rs'); const mainContent = fs.readFileSync(mainPath, 'utf8'); // Check for some expected method signatures from IComprehensiveTypes const EXPECTED_METHODS = [ 'boolean_function', 'unsigned_integer_function', 'signed_integer_function', 'address_function', 'bytes_function', 'string_function', 'dynamic_array_function', 'fixed_array_function', 'struct_function', 'kitchen_sink_function', ]; for (const method of EXPECTED_METHODS) { expect(mainContent).toMatch(new RegExp(`async fn ${method}`)); } }); }); describe('Generated code quality checks', () => { it('should generate properly formatted Rust code', async () => { const typesPath = path.join(tempOutputDir, 'src', 'gen', 'types.rs'); const typesContent = fs.readFileSync(typesPath, 'utf8'); // Should not have obvious syntax errors expect(typesContent).not.toMatch(/;;\s*$/m); // No double semicolons // Brackets should be balanced const openBrackets = (typesContent.match(/\[/g) || []).length; const closeBrackets = (typesContent.match(/\]/g) || []).length; expect(openBrackets).toBe(closeBrackets); // Angle brackets should be balanced (for generics) const openAngles = (typesContent.match(//g) || []).length; expect(openAngles).toBe(closeAngles); }); it('should not have duplicate type definitions', async () => { const typesPath = path.join(tempOutputDir, 'src', 'gen', 'types.rs'); const typesContent = fs.readFileSync(typesPath, 'utf8'); const lines = typesContent.split('\n'); const typeDeclarations = lines.filter((line) => line.startsWith('pub type ') ); const typeNames = typeDeclarations .map((line) => { const match = line.match(/pub type (\w+) =/); return match ? match[1] : null; }) .filter(Boolean); // Should not have duplicate type names expect(new Set(typeNames).size).toBe(typeNames.length); }); it('should import only necessary alloy primitives', async () => { const typesPath = path.join(tempOutputDir, 'src', 'gen', 'types.rs'); const typesContent = fs.readFileSync(typesPath, 'utf8'); const importMatch = typesContent.match( /use alloy_primitives::\{([^}]+)\};/ ); expect(importMatch).toBeTruthy(); if (importMatch) { const imports = importMatch[1].split(',').map((s) => s.trim()); // Should not contain duplicates expect(new Set(imports).size).toBe(imports.length); // Most imported types should be used directly in struct fields // Since we use direct imports, they appear without the alloy_primitives:: prefix const usedImports = imports.filter( (imp) => typesContent.includes(`: ${imp}`) || typesContent.includes(`<${imp}>`) || typesContent.includes(`[${imp};`) ); expect(usedImports.length).toBeGreaterThan(0); } }); }); });