import * as smartfile from '../ts/index.js'; import { expect, tap } from '@git.zone/tstest/tapbundle'; import { MockSmartFs } from './helpers/mock-smartfs.js'; // Create factory with MockSmartFs const mockFs = new MockSmartFs(); const factory = new smartfile.SmartFileFactory(mockFs); // --------------------------- // SmartFileFactory Tests // --------------------------- tap.test('SmartFileFactory.nodeFs() -> should create a default factory', async () => { const defaultFactory = smartfile.SmartFileFactory.nodeFs(); expect(defaultFactory).toBeInstanceOf(smartfile.SmartFileFactory); }); tap.test('SmartFileFactory.fromFilePath() -> should create a SmartFile from file path', async () => { const smartFile = await factory.fromFilePath('./test/testassets/mytest.json', process.cwd()); expect(smartFile).toBeInstanceOf(smartfile.SmartFile); expect(smartFile.path).toEqual('test/testassets/mytest.json'); expect(smartFile.contentBuffer).toBeInstanceOf(Buffer); }); tap.test('SmartFileFactory.fromBuffer() -> should create a SmartFile from buffer', async () => { const buffer = Buffer.from('test content'); const smartFile = factory.fromBuffer('./test.txt', buffer); expect(smartFile).toBeInstanceOf(smartfile.SmartFile); expect(smartFile.contentBuffer.toString()).toEqual('test content'); }); tap.test('SmartFileFactory.fromString() -> should create a SmartFile from string', async () => { const smartFile = factory.fromString('./test.txt', 'test content'); expect(smartFile).toBeInstanceOf(smartfile.SmartFile); expect(smartFile.parseContentAsString()).toEqual('test content'); }); tap.test('SmartFileFactory.fromUrl() -> should create a SmartFile from URL', async () => { // Note: This test would need a real HTTP endpoint or mock // For now, we'll skip it or test with a known URL // const smartFile = await factory.fromUrl('https://example.com/test.json'); // expect(smartFile).toBeInstanceOf(smartfile.SmartFile); }); // --------------------------- // SmartFile Instance Tests // --------------------------- tap.test('SmartFile -> should produce vinyl compatible files', async () => { const smartFile = await factory.fromFilePath('./test/testassets/mytest.json'); expect(smartFile).toBeInstanceOf(smartfile.SmartFile); expect(smartFile.contents).toBeInstanceOf(Buffer); expect(smartFile.isBuffer()).toBeTrue(); expect(smartFile.isDirectory()).toBeFalse(); expect(smartFile.isNull()).toBeFalse(); }); tap.test('SmartFile -> should write to disk', async () => { const fileString = 'hi there'; const filePath = './test/testassets/temp/utf8.txt'; const smartFile = factory.fromString(filePath, fileString, 'utf8'); await smartFile.writeToDiskAtPath(filePath); // Read it back const smartFile2 = await factory.fromFilePath(filePath); const retrievedString = smartFile2.parseContentAsString(); expect(retrievedString).toEqual(fileString); }); tap.test('SmartFile -> should get a hash', async () => { const fileString = 'hi there'; const smartFile = factory.fromString('./test/testassets/utf8.txt', fileString, 'utf8'); const hash = await smartFile.getHash(); expect(hash).toBeTypeofString(); expect(hash.length).toBeGreaterThan(0); }); tap.test('SmartFile -> should update file name', async () => { const smartFile = factory.fromString('./test/oldname.txt', 'content'); smartFile.updateFileName('newname.txt'); expect(smartFile.parsedPath.base).toEqual('newname.txt'); }); tap.test('SmartFile -> should edit content as string', async () => { const smartFile = factory.fromString('./test.txt', 'original content'); await smartFile.editContentAsString(async (content) => { return content.replace('original', 'modified'); }); expect(smartFile.parseContentAsString()).toEqual('modified content'); }); tap.test('SmartFile -> should get stream', async () => { const smartFile = factory.fromString('./test.txt', 'stream content'); const stream = smartFile.getStream(); expect(stream).toHaveProperty('pipe'); // Read from stream const chunks: Buffer[] = []; stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))); await new Promise((resolve) => { stream.on('end', resolve); }); const content = Buffer.concat(chunks).toString(); expect(content).toEqual('stream content'); }); tap.test('SmartFile -> should get size', async () => { const content = 'test content with some length'; const smartFile = factory.fromString('./test.txt', content); const size = await smartFile.getSize(); expect(size).toEqual(Buffer.from(content).length); }); tap.test('SmartFile -> should parse content as buffer', async () => { const buffer = Buffer.from('buffer content'); const smartFile = factory.fromBuffer('./test.txt', buffer); const parsedBuffer = smartFile.parseContentAsBuffer(); expect(parsedBuffer).toBeInstanceOf(Buffer); expect(parsedBuffer.toString()).toEqual('buffer content'); }); tap.test('SmartFile -> should write to directory', async () => { const smartFile = factory.fromString('subdir/test.txt', 'directory test content'); const writtenPath = await smartFile.writeToDir('./test/testassets/temp'); expect(writtenPath).toContain('subdir/test.txt'); }); tap.test('SmartFile -> should get parsed path', async () => { const smartFile = factory.fromString('./path/to/file.txt', 'content'); expect(smartFile.parsedPath.base).toEqual('file.txt'); expect(smartFile.parsedPath.ext).toEqual('.txt'); expect(smartFile.parsedPath.name).toEqual('file'); }); tap.test('SmartFile -> should get absolute path', async () => { const smartFile = factory.fromString('relative/path.txt', 'content', 'utf8', '/base'); expect(smartFile.absolutePath).toEqual('/base/relative/path.txt'); }); tap.start();