2022-06-07 15:43:28 +02:00
|
|
|
import * as smartfile from '../ts/index.js';
|
2025-05-21 13:24:41 +00:00
|
|
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
2025-11-22 13:18:32 +00:00
|
|
|
import { MockSmartFs } from './helpers/mock-smartfs.js';
|
|
|
|
|
|
|
|
|
|
// Create factory with MockSmartFs
|
|
|
|
|
const mockFs = new MockSmartFs();
|
|
|
|
|
const factory = new smartfile.SmartFileFactory(mockFs);
|
2017-01-21 00:47:48 +01:00
|
|
|
|
2017-04-29 16:50:06 +02:00
|
|
|
// ---------------------------
|
2025-11-22 13:18:32 +00:00
|
|
|
// SmartFileFactory Tests
|
2017-04-29 16:50:06 +02:00
|
|
|
// ---------------------------
|
|
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
tap.test('SmartFileFactory.nodeFs() -> should create a default factory', async () => {
|
|
|
|
|
const defaultFactory = smartfile.SmartFileFactory.nodeFs();
|
|
|
|
|
expect(defaultFactory).toBeInstanceOf(smartfile.SmartFileFactory);
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-03-04 21:10:46 +01:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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);
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-27 16:48:08 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-27 16:48:08 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2016-03-18 17:34:31 +01:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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);
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
// ---------------------------
|
|
|
|
|
// SmartFile Instance Tests
|
|
|
|
|
// ---------------------------
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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();
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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);
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
// Read it back
|
|
|
|
|
const smartFile2 = await factory.fromFilePath(filePath);
|
|
|
|
|
const retrievedString = smartFile2.parseContentAsString();
|
|
|
|
|
expect(retrievedString).toEqual(fileString);
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2016-03-18 17:34:31 +01:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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);
|
|
|
|
|
});
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-28 11:28:11 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
|
|
|
|
});
|
2017-04-30 18:13:17 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
tap.test('SmartFile -> should get stream', async () => {
|
|
|
|
|
const smartFile = factory.fromString('./test.txt', 'stream content');
|
|
|
|
|
const stream = smartFile.getStream();
|
|
|
|
|
expect(stream).toHaveProperty('pipe');
|
2017-04-30 18:13:17 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
// Read from stream
|
|
|
|
|
const chunks: Buffer[] = [];
|
|
|
|
|
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
2017-04-30 18:13:17 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
await new Promise((resolve) => {
|
|
|
|
|
stream.on('end', resolve);
|
|
|
|
|
});
|
2018-07-03 08:55:09 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
const content = Buffer.concat(chunks).toString();
|
|
|
|
|
expect(content).toEqual('stream content');
|
|
|
|
|
});
|
2017-04-30 18:13:17 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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);
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-04-30 18:13:17 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2018-07-03 08:55:09 +02:00
|
|
|
});
|
2017-05-26 14:47:41 +02:00
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2020-08-10 20:58:22 +00:00
|
|
|
});
|
|
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2023-01-09 15:32:37 +01:00
|
|
|
});
|
|
|
|
|
|
2025-11-22 13:18:32 +00:00
|
|
|
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');
|
2022-03-11 09:46:54 +01:00
|
|
|
});
|
2021-12-22 19:08:53 +01:00
|
|
|
|
2018-07-03 08:55:09 +02:00
|
|
|
tap.start();
|