77 lines
3.0 KiB
TypeScript
77 lines
3.0 KiB
TypeScript
|
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||
|
|
|
||
|
|
import * as plugins from '../ts/plugins.js';
|
||
|
|
import * as smartbucket from '../ts/index.js';
|
||
|
|
|
||
|
|
class FakeS3Client {
|
||
|
|
private callIndex = 0;
|
||
|
|
|
||
|
|
constructor(private readonly pages: Array<Partial<plugins.s3.ListObjectsV2Output>>) {}
|
||
|
|
|
||
|
|
public async send(_command: any) {
|
||
|
|
const page = this.pages[this.callIndex] || { Contents: [], CommonPrefixes: [], IsTruncated: false };
|
||
|
|
this.callIndex += 1;
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
tap.test('MetaData.hasMetaData should return false when metadata file does not exist', async () => {
|
||
|
|
const fakeFile = {
|
||
|
|
name: 'file.txt',
|
||
|
|
parentDirectoryRef: {
|
||
|
|
async getFile() {
|
||
|
|
throw new Error(`File not found at path 'file.txt.metadata'`);
|
||
|
|
},
|
||
|
|
},
|
||
|
|
} as unknown as smartbucket.File;
|
||
|
|
|
||
|
|
const hasMetaData = await smartbucket.MetaData.hasMetaData({ file: fakeFile });
|
||
|
|
expect(hasMetaData).toBeFalse();
|
||
|
|
});
|
||
|
|
|
||
|
|
tap.test('getSubDirectoryByName should create correct parent chain for new nested directories', async () => {
|
||
|
|
const fakeSmartbucket = { s3Client: new FakeS3Client([{ Contents: [], CommonPrefixes: [] }]) } as unknown as smartbucket.SmartBucket;
|
||
|
|
const bucket = new smartbucket.Bucket(fakeSmartbucket, 'test-bucket');
|
||
|
|
const baseDirectory = new smartbucket.Directory(bucket, null as any, '');
|
||
|
|
|
||
|
|
const nestedDirectory = await baseDirectory.getSubDirectoryByName('level1/level2', { getEmptyDirectory: true });
|
||
|
|
|
||
|
|
expect(nestedDirectory.name).toEqual('level2');
|
||
|
|
expect(nestedDirectory.parentDirectoryRef.name).toEqual('level1');
|
||
|
|
expect(nestedDirectory.getBasePath()).toEqual('level1/level2/');
|
||
|
|
});
|
||
|
|
|
||
|
|
tap.test('listFiles should aggregate results across paginated ListObjectsV2 responses', async () => {
|
||
|
|
const firstPage = {
|
||
|
|
Contents: Array.from({ length: 1000 }, (_, index) => ({ Key: `file-${index}` })),
|
||
|
|
IsTruncated: true,
|
||
|
|
NextContinuationToken: 'token-1',
|
||
|
|
};
|
||
|
|
const secondPage = {
|
||
|
|
Contents: Array.from({ length: 200 }, (_, index) => ({ Key: `file-${1000 + index}` })),
|
||
|
|
IsTruncated: false,
|
||
|
|
};
|
||
|
|
const fakeSmartbucket = { s3Client: new FakeS3Client([firstPage, secondPage]) } as unknown as smartbucket.SmartBucket;
|
||
|
|
const bucket = new smartbucket.Bucket(fakeSmartbucket, 'test-bucket');
|
||
|
|
const baseDirectory = new smartbucket.Directory(bucket, null as any, '');
|
||
|
|
|
||
|
|
const files = await baseDirectory.listFiles();
|
||
|
|
expect(files.length).toEqual(1200);
|
||
|
|
});
|
||
|
|
|
||
|
|
tap.test('listDirectories should aggregate CommonPrefixes across pagination', async () => {
|
||
|
|
const fakeSmartbucket = {
|
||
|
|
s3Client: new FakeS3Client([
|
||
|
|
{ CommonPrefixes: [{ Prefix: 'dirA/' }], IsTruncated: true, NextContinuationToken: 'token-1' },
|
||
|
|
{ CommonPrefixes: [{ Prefix: 'dirB/' }], IsTruncated: false },
|
||
|
|
]),
|
||
|
|
} as unknown as smartbucket.SmartBucket;
|
||
|
|
const bucket = new smartbucket.Bucket(fakeSmartbucket, 'test-bucket');
|
||
|
|
const baseDirectory = new smartbucket.Directory(bucket, null as any, '');
|
||
|
|
|
||
|
|
const directories = await baseDirectory.listDirectories();
|
||
|
|
expect(directories.map((d) => d.name)).toEqual(['dirA', 'dirB']);
|
||
|
|
});
|
||
|
|
|
||
|
|
export default tap.start();
|