smartbucket/test/test.ts

130 lines
3.8 KiB
TypeScript

import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import { Qenv } from '@push.rocks/qenv';
import * as smartbucket from '../ts/index.js';
const testQenv = new Qenv('./', './.nogit/');
let testSmartbucket: smartbucket.SmartBucket;
let myBucket: smartbucket.Bucket;
let baseDirectory: smartbucket.Directory;
tap.test('should create a valid smartbucket', async () => {
testSmartbucket = new smartbucket.SmartBucket({
accessKey: await testQenv.getEnvVarOnDemandStrict('S3_ACCESSKEY'),
accessSecret: await testQenv.getEnvVarOnDemandStrict('S3_ACCESSSECRET'),
endpoint: await testQenv.getEnvVarOnDemandStrict('S3_ENDPOINT'),
});
expect(testSmartbucket).toBeInstanceOf(smartbucket.SmartBucket);
myBucket = await testSmartbucket.getBucketByNameStrict(await testQenv.getEnvVarOnDemandStrict('S3_BUCKET'),);
expect(myBucket).toBeInstanceOf(smartbucket.Bucket);
expect(myBucket.name).toEqual('test-pushrocks-smartbucket');
});
tap.test('should clean all contents', async () => {
await myBucket.cleanAllContents();
expect(await myBucket.fastExists({ path: 'hithere/socool.txt' })).toBeFalse();
expect(await myBucket.fastExists({ path: 'trashtest/trashme.txt' })).toBeFalse();
});
tap.skip.test('should create testbucket', async () => {
// await testSmartbucket.createBucket('testzone2');
});
tap.skip.test('should remove testbucket', async () => {
// await testSmartbucket.removeBucket('testzone2');
});
// Fast operations
tap.test('should store data in bucket fast', async () => {
await myBucket.fastPut({
path: 'hithere/socool.txt',
contents: 'hi there!',
});
});
tap.test('should get data in bucket', async () => {
const fileString = await myBucket.fastGet({
path: 'hithere/socool.txt',
});
const fileStringStream = await myBucket.fastGetStream(
{
path: 'hithere/socool.txt',
},
'nodestream'
);
console.log(fileString);
});
tap.test('should delete data in bucket', async () => {
await myBucket.fastRemove({
path: 'hithere/socool.txt',
});
});
// fs operations
tap.test('prepare for directory style tests', async () => {
await myBucket.fastPut({
path: 'dir1/file1.txt',
contents: 'dir1/file1.txt content',
});
await myBucket.fastPut({
path: 'dir1/file2.txt',
contents: 'dir1/file2.txt content',
});
await myBucket.fastPut({
path: 'dir2/file1.txt',
contents: 'dir2/file1.txt content',
});
await myBucket.fastPut({
path: 'dir3/file1.txt',
contents: 'dir3/file1.txt content',
});
await myBucket.fastPut({
path: 'dir3/dir4/file1.txt',
contents: 'dir3/dir4/file1.txt content',
});
await myBucket.fastPut({
path: '/file1.txt',
contents: 'file1 content',
});
});
tap.test('should get base directory', async () => {
baseDirectory = await myBucket.getBaseDirectory();
const directories = await baseDirectory.listDirectories();
console.log('Found the following directories:');
console.log(directories);
expect(directories.length).toEqual(3);
const files = await baseDirectory.listFiles();
console.log('Found the following files:');
console.log(files);
expect(files.length).toEqual(1);
});
tap.test('should correctly build paths for sub directories', async () => {
const dir4 = await baseDirectory.getSubDirectoryByName('dir3/dir4');
expect(dir4).toBeInstanceOf(smartbucket.Directory);
const dir4BasePath = dir4?.getBasePath();
console.log(dir4BasePath);
expect(dir4BasePath).toEqual('dir3/dir4/');
});
tap.test('clean up directory style tests', async () => {
await myBucket.fastRemove({
path: 'dir1/file1.txt',
});
await myBucket.fastRemove({
path: 'dir1/file2.txt',
});
await myBucket.fastRemove({
path: 'dir2/file1.txt',
});
await myBucket.fastRemove({ path: 'dir3/file1.txt' });
await myBucket.fastRemove({ path: 'dir3/dir4/file1.txt' });
await myBucket.fastRemove({ path: 'file1.txt' });
});
tap.start();