Files
smartfile/test/test.ts

143 lines
5.6 KiB
TypeScript
Raw Permalink Normal View History

2022-06-07 15:43:28 +02:00
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);
2017-01-21 00:47:48 +01:00
2017-04-29 16:50:06 +02:00
// ---------------------------
// SmartFileFactory Tests
2017-04-29 16:50:06 +02:00
// ---------------------------
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);
});
2017-04-27 16:48:08 +02: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');
});
2017-04-27 16:48:08 +02: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');
});
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);
});
2017-04-28 11:28:11 +02:00
// ---------------------------
// SmartFile Instance Tests
// ---------------------------
2017-04-28 11:28:11 +02: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();
});
2017-04-28 11:28:11 +02: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
// 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);
});
2017-04-28 11:28:11 +02: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');
});
2017-04-28 11:28:11 +02: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
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
// Read from stream
const chunks: Buffer[] = [];
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
2017-04-30 18:13:17 +02:00
await new Promise((resolve) => {
stream.on('end', resolve);
});
const content = Buffer.concat(chunks).toString();
expect(content).toEqual('stream content');
});
2017-04-30 18:13:17 +02: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);
});
2017-04-30 18:13:17 +02: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');
});
2017-05-26 14:47:41 +02: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');
});
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
});
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
tap.start();