this repo has no description
1import assert from 'node:assert/strict'
2import { access, mkdir, rm, unlink, writeFile } from 'node:fs/promises'
3import { join } from 'node:path'
4import { describe, test } from 'node:test'
5import { extractArchive } from '../src/extract.js'
6
7describe('Archive Extraction', () => {
8 test('extractArchive identifies tar.gz files', async () => {
9 const testDir = './test-extract'
10 const archivePath = join(testDir, 'test.tar.gz')
11
12 await mkdir(testDir, { recursive: true })
13
14 // Create a minimal tar.gz file (this is a mock test)
15 await writeFile(archivePath, Buffer.from('mock-tar-content'))
16
17 try {
18 // This will fail because it's not a real tar.gz, but we're testing the path detection
19 await extractArchive(archivePath, testDir)
20 } catch (error) {
21 // Expected to fail with tar error from our native implementation
22 assert.ok(error instanceof Error, 'Should throw an error')
23 }
24
25 // Clean up
26 await rm(testDir, { recursive: true })
27 })
28
29 test('extractArchive identifies zip files', async () => {
30 const testDir = './test-extract-zip'
31 const archivePath = join(testDir, 'test.zip')
32
33 await mkdir(testDir, { recursive: true })
34
35 // Create a minimal zip file (this is a mock test)
36 await writeFile(archivePath, Buffer.from('mock-zip-content'))
37
38 try {
39 // This will fail because it's not a real zip, but we're testing the path detection
40 await extractArchive(archivePath, testDir)
41 } catch (error) {
42 // Expected to fail with unzip command error
43 assert.match((error as Error).message, /unzip command failed|Failed to extract/)
44 }
45
46 // Clean up
47 await rm(testDir, { recursive: true })
48 })
49
50 test('extractArchive rejects unsupported formats', async () => {
51 const testDir = './test-extract-unsupported'
52 const archivePath = join(testDir, 'test.rar')
53
54 await mkdir(testDir, { recursive: true })
55 await writeFile(archivePath, Buffer.from('mock-content'))
56
57 await assert.rejects(async () => await extractArchive(archivePath, testDir), /Unsupported archive format/)
58
59 // Clean up
60 await rm(testDir, { recursive: true })
61 })
62
63 test('extractArchive creates output directory', async () => {
64 const testDir = './test-extract-mkdir'
65 const archivePath = './test-mkdir.tar.gz'
66
67 // Create a mock archive file in current directory
68 await writeFile(archivePath, Buffer.from('mock-content'))
69
70 try {
71 await extractArchive(archivePath, testDir)
72 } catch {
73 // Expected to fail, but directory should be created
74 try {
75 await access(testDir)
76 // Directory exists, test passed
77 } catch {
78 assert.fail('Output directory was not created')
79 }
80 }
81
82 // Clean up
83 try {
84 await rm(testDir, { recursive: true })
85 } catch {}
86 try {
87 await unlink(archivePath)
88 } catch {}
89 })
90})