Files
smartfile/test/test.streamfile.ts

156 lines
5.2 KiB
TypeScript

import * as path from 'path';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartfile from '../ts/index.js';
import { MockSmartFs } from './helpers/mock-smartfs.js';
// Create factory with MockSmartFs
const mockFs = new MockSmartFs();
const factory = new smartfile.SmartFileFactory(mockFs);
// Test assets path
const testAssetsPath = './test/testassets/';
// ---------------------------
// StreamFile Factory Tests
// ---------------------------
tap.test(
'SmartFileFactory.streamFromPath() -> should create a StreamFile from a file path',
async () => {
const streamFile = await factory.streamFromPath(
path.join(testAssetsPath, 'mytest.json'),
);
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
const contentBuffer = await streamFile.getContentAsBuffer();
expect(contentBuffer).toBeInstanceOf(Buffer);
},
);
tap.test(
'SmartFileFactory.streamFromBuffer() -> should create a StreamFile from a Buffer',
async () => {
const buffer = Buffer.from('Some content');
const streamFile = factory.streamFromBuffer(
buffer,
'bufferfile.txt',
);
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
const content = await streamFile.getContentAsBuffer();
expect(content.toString()).toEqual('Some content');
},
);
tap.test(
'SmartFileFactory.streamFromStream() -> should create a StreamFile from a stream',
async () => {
const { Readable } = await import('stream');
const stream = new Readable();
stream.push('stream content');
stream.push(null);
const streamFile = factory.streamFromStream(stream, 'streamfile.txt', false);
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
},
);
// ---------------------------
// StreamFile Instance Tests
// ---------------------------
tap.test('StreamFile -> should write the stream to disk', async () => {
const streamFile = await factory.streamFromPath(
path.join(testAssetsPath, 'mytest.json'),
);
const targetPath = path.join(testAssetsPath, 'temp', 'stream-mytest.json');
await streamFile.writeToDisk(targetPath);
// Verify the file was written by reading it back
const verifyFile = await factory.fromFilePath(targetPath);
expect(verifyFile.contentBuffer).toBeInstanceOf(Buffer);
});
tap.test('StreamFile -> should write to a directory', async () => {
const streamFile = await factory.streamFromPath(
path.join(testAssetsPath, 'mytest.json'),
);
// Set relative path so writeToDir knows where to put it
streamFile.relativeFilePath = 'mytest-fromdir.json';
await streamFile.writeToDir(path.join(testAssetsPath, 'temp'));
// Verify the file was written
const targetPath = path.join(testAssetsPath, 'temp', 'mytest-fromdir.json');
const verifyFile = await factory.fromFilePath(targetPath);
expect(verifyFile.contentBuffer).toBeInstanceOf(Buffer);
});
tap.test('StreamFile -> should return content as a buffer', async () => {
const streamFile = await factory.streamFromPath(
path.join(testAssetsPath, 'mytest.json'),
);
const contentBuffer = await streamFile.getContentAsBuffer();
expect(contentBuffer).toBeInstanceOf(Buffer);
});
tap.test('StreamFile -> should return content as a string', async () => {
const streamFile = await factory.streamFromPath(
path.join(testAssetsPath, 'mytest.json'),
);
const contentString = await streamFile.getContentAsString();
expect(contentString).toBeTypeofString();
// Verify the content matches what's expected
const parsed = JSON.parse(contentString);
expect(parsed.key1).toEqual('this works');
});
tap.test('StreamFile -> should get size', async () => {
const buffer = Buffer.from('test content for size');
const streamFile = factory.streamFromBuffer(buffer, 'sizefile.txt');
const size = await streamFile.getSize();
expect(size).toEqual(buffer.length);
});
tap.test('StreamFile -> should handle multi-use streams', async () => {
const buffer = Buffer.from('multi-use content');
const streamFile = factory.streamFromBuffer(buffer, 'multiuse.txt');
streamFile.multiUse = true;
// Read multiple times
const content1 = await streamFile.getContentAsString();
const content2 = await streamFile.getContentAsString();
expect(content1).toEqual('multi-use content');
expect(content2).toEqual('multi-use content');
});
tap.test('StreamFile -> should convert to SmartFile', async () => {
const buffer = Buffer.from('convert to smartfile');
const streamFile = factory.streamFromBuffer(buffer, 'convert.txt');
const smartFile = await streamFile.toSmartFile();
expect(smartFile).toBeInstanceOf(smartfile.SmartFile);
expect(smartFile.parseContentAsString()).toEqual('convert to smartfile');
});
tap.test('StreamFile -> should create readable stream', async () => {
const buffer = Buffer.from('readable stream content');
const streamFile = factory.streamFromBuffer(buffer, 'readable.txt');
const stream = await streamFile.createReadStream();
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('readable stream content');
});
// Start the test sequence
tap.start();