import fs from 'fs'; import path from 'path'; import { expect } from 'chai'; import { createIPFS, FileObject } from '../dist'; // Helper to create a directory if it doesn't exist function ensureDirExists(dir: string) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } } function createTempFile(dir: string, content: string, fileName: string): string { ensureDirExists(dir); const filePath = path.join(dir, fileName); fs.writeFileSync(filePath, content); return filePath; } interface TestData { singleFile: fs.ReadStream; flatFiles: FileObject[]; nestedFiles: FileObject[]; cleanup: () => void; } async function filePathToFileObject(filePath: string, basePath: string): Promise { const stream = fs.createReadStream(filePath); const relativePath = path.relative(basePath, filePath); return { path: relativePath, content: stream }; } async function generateTestData(): Promise { // Directories for the test data const baseDir = path.join(process.cwd(), 'tests', 'temp'); const singleFileDir = path.join(baseDir, 'single'); const flatFilesDir = path.join(baseDir, 'flat'); const nestedFilesDir = path.join(baseDir, 'nested'); // Create temporary directories [singleFileDir, flatFilesDir, nestedFilesDir].forEach(ensureDirExists); // Create test data const singleFilePath = createTempFile(singleFileDir, 'Test content for single file', 'singleFile.txt'); const singleFile = fs.createReadStream(singleFilePath); const flatFilesPaths = [ createTempFile(flatFilesDir, 'Content of flat file 1', 'file1.txt'), createTempFile(flatFilesDir, 'Content of flat file 2', 'file2.txt'), ]; const flatFiles = await Promise.all(flatFilesPaths.map(filePath => filePathToFileObject(filePath, baseDir))); const nestedFilePaths = [ createTempFile(path.join(nestedFilesDir, 'folder1'), 'Content of nested file 1', 'nestedFile1.txt'), createTempFile(path.join(nestedFilesDir, 'folder1', 'folder2'), 'Content of nested file 2', 'nestedFile2.txt'), ]; const nestedFiles = await Promise.all(nestedFilePaths.map(filePath => filePathToFileObject(filePath, baseDir))); return { singleFile, flatFiles, nestedFiles, cleanup: () => { fs.rmSync(baseDir, { recursive: true, force: true }); }, }; } describe('IPFS Client', () => { const ipfs = createIPFS({ url: 'http://127.0.0.1:8081' }); let testData: TestData; before(async () => { testData = await generateTestData(); }); after(() => { testData.cleanup(); }); it('should add a single file and return its CID', async () => { const result = await ipfs.add(testData.singleFile, {}); expect(result).to.have.property('Hash'); expect(result.Hash).to.eq("QmPfkmwii1Cz3vH8eWncjzV9DcaWGxpWeyTmW2nHDsgMni") }); it('should add multiple files in a flat structure and return their CIDs', async () => { const result = await ipfs.addAll(testData.flatFiles, {}); const lastItem = result[result.length - 1]; expect(result).to.be.an('array').that.has.lengthOf(testData.flatFiles.length + 1); expect(lastItem).to.have.property('Hash'); expect(lastItem.Hash).to.eq('QmZYpFhXH8TDikp4QqEsmYAHfJaB3H7eXJ7emoNTUCavvo'); }); it('should add files with nested folders and return their CIDs', async () => { const result = await ipfs.addAll(testData.nestedFiles); const lastItem = result[result.length - 1]; expect(result).to.be.an('array'); expect(lastItem.Hash).to.eq('QmcwHom8m1AxsUdJdDZjv2oHTCXtdycdyfCUfQoAYbWcXG'); }); });