import { tap, expect } from '@git.zone/tstest/tapbundle'; import * as plugins from './plugins.js'; const testPaths = { nogitDir: plugins.path.join( plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../.nogit/', ), remoteDir: plugins.path.join( plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../.nogit/remote', ), }; import * as smartarchive from '../ts/index.js'; tap.preTask('should prepare .nogit dir', async () => { await plugins.fsPromises.mkdir(testPaths.remoteDir, { recursive: true }); }); tap.preTask('should prepare downloads', async (tools) => { const response = await plugins.smartrequest.SmartRequest.create() .url( 'https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz', ) .get(); const downloadedFile: Buffer = Buffer.from(await response.arrayBuffer()); await plugins.fsPromises.writeFile( plugins.path.join(testPaths.nogitDir, 'test.tgz'), downloadedFile, ); }); tap.test('should extract existing files on disk using fluent API', async () => { await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .extract(testPaths.nogitDir); }); tap.test('should extract from file using fluent API', async () => { const extractPath = plugins.path.join(testPaths.nogitDir, 'from-file-test'); await plugins.fsPromises.mkdir(extractPath, { recursive: true }); await smartarchive.SmartArchive.create() .file(plugins.path.join(testPaths.nogitDir, 'test.tgz')) .extract(extractPath); const files = await plugins.listFileTree(extractPath, '**/*'); expect(files.length).toBeGreaterThan(0); }); tap.test('should extract with stripComponents using fluent API', async () => { const extractPath = plugins.path.join(testPaths.nogitDir, 'strip-test'); await plugins.fsPromises.mkdir(extractPath, { recursive: true }); await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .stripComponents(1) .extract(extractPath); const files = await plugins.listFileTree(extractPath, '**/*'); expect(files.length).toBeGreaterThan(0); // Files should not have 'package/' prefix const hasPackagePrefix = files.some(f => f.startsWith('package/')); expect(hasPackagePrefix).toBeFalse(); }); tap.test('should extract with filter using fluent API', async () => { const extractPath = plugins.path.join(testPaths.nogitDir, 'filter-test'); await plugins.fsPromises.mkdir(extractPath, { recursive: true }); await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .filter(entry => entry.path.endsWith('.json')) .extract(extractPath); const files = await plugins.listFileTree(extractPath, '**/*'); // All extracted files should be JSON for (const file of files) { expect(file.endsWith('.json')).toBeTrue(); } }); tap.test('should list archive entries using fluent API', async () => { const entries = await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .list(); expect(entries.length).toBeGreaterThan(0); const hasPackageJson = entries.some(e => e.path.includes('package.json')); expect(hasPackageJson).toBeTrue(); }); tap.test('should create archive using fluent API', async () => { const archive = await smartarchive.SmartArchive.create() .format('tar.gz') .compression(9) .entry('hello.txt', 'Hello World!') .entry('config.json', JSON.stringify({ name: 'test', version: '1.0.0' })); expect(archive).toBeInstanceOf(smartarchive.SmartArchive); const buffer = await archive.toBuffer(); expect(buffer.length).toBeGreaterThan(0); }); tap.test('should create and write archive to file using fluent API', async () => { const outputPath = plugins.path.join(testPaths.nogitDir, 'created-archive.tar.gz'); await smartarchive.SmartArchive.create() .format('tar.gz') .entry('readme.txt', 'This is a test archive') .entry('data/info.json', JSON.stringify({ created: new Date().toISOString() })) .toFile(outputPath); // Verify file was created const stats = await plugins.fsPromises.stat(outputPath); expect(stats.size).toBeGreaterThan(0); // Verify we can extract it const extractPath = plugins.path.join(testPaths.nogitDir, 'verify-created'); await smartarchive.SmartArchive.create() .file(outputPath) .extract(extractPath); const files = await plugins.listFileTree(extractPath, '**/*'); expect(files).toContain('readme.txt'); }); tap.test('should create ZIP archive using fluent API', async () => { const outputPath = plugins.path.join(testPaths.nogitDir, 'created-archive.zip'); await smartarchive.SmartArchive.create() .format('zip') .entry('file1.txt', 'Content 1') .entry('file2.txt', 'Content 2') .toFile(outputPath); // Verify file was created const stats = await plugins.fsPromises.stat(outputPath); expect(stats.size).toBeGreaterThan(0); }); tap.test('should extract to SmartFiles using fluent API', async () => { const smartFiles = await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .toSmartFiles(); expect(smartFiles.length).toBeGreaterThan(0); const packageJson = smartFiles.find(f => f.relative.includes('package.json')); expect(packageJson).toBeDefined(); }); tap.test('should analyze archive using fluent API', async () => { const info = await smartarchive.SmartArchive.create() .file(plugins.path.join(testPaths.nogitDir, 'test.tgz')) .analyze(); expect(info.isArchive).toBeTrue(); expect(info.isCompressed).toBeTrue(); expect(info.format).toEqual('gz'); }); tap.test('should check if file exists in archive using fluent API', async () => { const hasPackageJson = await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .hasFile('package.json'); expect(hasPackageJson).toBeTrue(); }); tap.test('should extract single file using fluent API', async () => { const packageJson = await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .extractFile('package.json'); expect(packageJson).toBeDefined(); expect(packageJson!.contents.toString()).toContain('websetup'); }); tap.test('should handle include/exclude patterns', async () => { const smartFiles = await smartarchive.SmartArchive.create() .url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz') .include(/\.json$/) .toSmartFiles(); expect(smartFiles.length).toBeGreaterThan(0); for (const file of smartFiles) { expect(file.relative.endsWith('.json')).toBeTrue(); } }); tap.test('should throw error when mixing modes', async () => { let threw = false; try { smartarchive.SmartArchive.create() .url('https://example.com/archive.tgz') .entry('file.txt', 'content'); // This should throw } catch (e) { threw = true; expect((e as Error).message).toContain('extraction mode'); } expect(threw).toBeTrue(); }); tap.test('should throw error when no source configured', async () => { let threw = false; try { await smartarchive.SmartArchive.create().extract('./output'); } catch (e) { threw = true; expect((e as Error).message).toContain('No source configured'); } expect(threw).toBeTrue(); }); tap.test('should throw error when no format configured', async () => { let threw = false; try { await smartarchive.SmartArchive.create() .entry('file.txt', 'content') .toBuffer(); } catch (e) { threw = true; expect((e as Error).message).toContain('No format specified'); } expect(threw).toBeTrue(); }); tap.skip.test('should extract a b2zip', async () => { const dataUrl = 'https://daten.offeneregister.de/de_companies_ocdata.jsonl.bz2'; await smartarchive.SmartArchive.create() .url(dataUrl) .extract(plugins.path.join(testPaths.nogitDir, 'de_companies_ocdata.jsonl')); }); export default tap.start();