-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-compress.test.ts
More file actions
431 lines (370 loc) · 15.2 KB
/
Copy pathtext-compress.test.ts
File metadata and controls
431 lines (370 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { afterEach, describe, expect, it } from "vitest"
import {
AUTO_SPLIT_CHARS,
assertDirectory,
compress,
compressFolder,
decompress,
decompressPayload,
decompressToPath,
extractFilenamePrefix,
formatSplitOutputPath,
parseSplitPartPath,
readSplitInput,
readTextFile,
resolveSplitChunkSize,
resolveSplitInputPaths,
SPLIT_MAGIC,
splitEncodedIntoWrappedParts,
splitString,
TAG_TEXT,
unpackDirectory,
wrapSplitChunk,
} from "../src/index.js"
const tempDirs: string[] = []
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true })
}
})
function makeTempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "text-compress-"))
tempDirs.push(dir)
return dir
}
describe("text compression", () => {
it("round-trips text with base64", () => {
const input = "Hello, world! This is a test of brotli compression."
expect(decompress(compress(input, 64), 64)).toBe(input)
})
it("round-trips text with base85", () => {
const input = "Hello, world! This is a test of brotli compression."
expect(decompress(compress(input, 85), 85)).toBe(input)
})
it("round-trips empty string", () => {
expect(decompress(compress("", 64), 64)).toBe("")
})
it("round-trips unicode text", () => {
const input = "日本語 🎉 émojis and spëcial chars"
expect(decompress(compress(input, 64), 64)).toBe(input)
expect(decompress(compress(input, 85), 85)).toBe(input)
})
it("tags text payloads correctly", () => {
const encoded = compress("hello", 64)
const { tag } = decompressPayload(encoded, 64)
expect(tag).toBe(TAG_TEXT)
})
it("rejects folder payload in decompress()", () => {
const dir = makeTempDir()
mkdirSync(join(dir, "sub"))
writeFileSync(join(dir, "sub", "a.txt"), "content")
const { encoded } = compressFolder(dir, 64)
expect(() => decompress(encoded, 64)).toThrow(/compressed folder/)
})
it("round-trips text with a password", () => {
const input = "Secret payload with password protection."
const password = "hunter2"
expect(decompress(compress(input, 64, password), 64, password)).toBe(input)
})
it("rejects password-protected payload without a password", () => {
const encoded = compress("secret", 64, "hunter2")
expect(() => decompress(encoded, 64)).toThrow(/password-protected/)
})
it("rejects password-protected payload with the wrong password", () => {
const encoded = compress("secret", 64, "hunter2")
expect(() => decompress(encoded, 64, "wrong")).toThrow(/Invalid password/)
})
})
describe("folder compression", () => {
it("round-trips a folder tree", () => {
const src = makeTempDir()
mkdirSync(join(src, "nested"))
writeFileSync(join(src, "readme.txt"), "hello")
writeFileSync(join(src, "nested", "data.json"), '{"x":1}')
const { encoded } = compressFolder(src, 64)
const dest = makeTempDir()
const stats = decompressToPath(encoded, dest, 64)
expect(stats.files).toBe(2)
expect(readFileSync(join(dest, "readme.txt"), "utf-8")).toBe("hello")
expect(readFileSync(join(dest, "nested", "data.json"), "utf-8")).toBe('{"x":1}')
})
it("round-trips folder with base85", () => {
const src = makeTempDir()
writeFileSync(join(src, "file.txt"), "z85 test")
const { encoded } = compressFolder(src, 85)
const dest = makeTempDir()
decompressToPath(encoded, dest, 85)
expect(readFileSync(join(dest, "file.txt"), "utf-8")).toBe("z85 test")
})
it("reports file and directory counts", () => {
const src = makeTempDir()
mkdirSync(join(src, "a"))
mkdirSync(join(src, "b"))
writeFileSync(join(src, "a", "1.txt"), "1")
writeFileSync(join(src, "b", "2.txt"), "22")
const result = compressFolder(src, 64)
expect(result.fileCount).toBe(2)
expect(result.dirCount).toBe(2)
expect(result.originalBytes).toBe(3)
})
it("round-trips a folder tree with a password", () => {
const src = makeTempDir()
mkdirSync(join(src, "nested"))
writeFileSync(join(src, "readme.txt"), "hello")
writeFileSync(join(src, "nested", "data.json"), '{"x":1}')
const password = "folder-secret"
const { encoded } = compressFolder(src, 64, password)
const dest = makeTempDir()
const stats = decompressToPath(encoded, dest, 64, password)
expect(stats.files).toBe(2)
expect(readFileSync(join(dest, "readme.txt"), "utf-8")).toBe("hello")
expect(readFileSync(join(dest, "nested", "data.json"), "utf-8")).toBe('{"x":1}')
})
})
describe("unpackDirectory", () => {
it("rejects unsafe archive paths", () => {
const badArchive = Buffer.concat([
Buffer.from([0x46]),
(() => {
const p = Buffer.from("../evil", "utf-8")
const len = Buffer.alloc(4)
len.writeUInt32LE(p.length, 0)
return Buffer.concat([len, p])
})(),
(() => {
const c = Buffer.from("x", "utf-8")
const len = Buffer.alloc(4)
len.writeUInt32LE(c.length, 0)
return Buffer.concat([len, c])
})(),
])
expect(() => unpackDirectory(badArchive, makeTempDir())).toThrow(/Unsafe path/)
})
})
describe("split output", () => {
it("splits a string into fixed-size chunks", () => {
expect(splitString("abcdefghij", 4)).toEqual(["abcd", "efgh", "ij"])
})
it("returns a single empty chunk for empty input", () => {
expect(splitString("", 10)).toEqual([""])
})
it("rejects invalid split sizes", () => {
expect(() => splitString("abc", 0)).toThrow(/positive integer/)
expect(() => splitString("abc", 1.5)).toThrow(/positive integer/)
})
it("formats numbered output paths before the extension", () => {
expect(formatSplitOutputPath("output.txt", 1, 3)).toBe("output.1.txt")
expect(formatSplitOutputPath("output.txt", 2, 12)).toBe("output.02.txt")
expect(formatSplitOutputPath("archive", 5, 5)).toBe("archive.5")
})
it("round-trips split compressed output when concatenated", () => {
const input = "Split test payload.\n".repeat(100)
const encoded = compress(input, 64)
const parts = splitString(encoded, 40)
expect(decompress(parts.join(""), 64)).toBe(input)
})
it("discovers and reads numbered split part files", () => {
const dir = makeTempDir()
const basePath = join(dir, "output.txt")
const encoded = compress("split file round trip", 64)
const chunks = splitString(encoded, 10)
const totalParts = chunks.length
const paths = chunks.map((chunk, index) => {
const partPath = formatSplitOutputPath(basePath, index + 1, totalParts)
writeFileSync(partPath, wrapSplitChunk(index + 1, totalParts, chunk), "utf-8")
return partPath
})
expect(parseSplitPartPath(paths[0])).toEqual({ prefix: "output" })
expect(extractFilenamePrefix(paths[2])).toBe("output")
expect(resolveSplitInputPaths(paths[2])).toEqual(paths)
expect(readSplitInput(paths[1]).content).toBe(encoded)
expect(decompress(readSplitInput(paths[0]).content, 64)).toBe("split file round trip")
})
it("reassembles split output when filenames are shuffled", () => {
const dir = makeTempDir()
const basePath = join(dir, "output.txt")
const encoded = compress("shuffled split parts", 64)
const chunks = splitString(encoded, 12)
const totalParts = chunks.length
for (let index = 0; index < totalParts; index++) {
const shuffledIndex = totalParts - index
const partPath = formatSplitOutputPath(basePath, shuffledIndex, totalParts)
writeFileSync(partPath, wrapSplitChunk(index + 1, totalParts, chunks[index]), "utf-8")
}
expect(readSplitInput(join(dir, "output.1.txt")).content).toBe(encoded)
expect(decompress(readSplitInput(join(dir, "output.1.txt")).content, 64)).toBe(
"shuffled split parts",
)
})
it("reassembles split output merged into fewer files", () => {
const dir = makeTempDir()
const basePath = join(dir, "output.txt")
const encoded = compress("merged split parts", 64)
const chunks = splitString(encoded, 11)
const totalParts = chunks.length
const mergedPath = join(dir, "output.1.txt")
const mergedContent = [
wrapSplitChunk(1, totalParts, chunks[0]),
wrapSplitChunk(2, totalParts, chunks[1]),
].join("")
writeFileSync(mergedPath, mergedContent, "utf-8")
for (let index = 2; index < totalParts; index++) {
const partPath = formatSplitOutputPath(basePath, index + 1, totalParts)
writeFileSync(partPath, wrapSplitChunk(index + 1, totalParts, chunks[index]), "utf-8")
}
expect(readSplitInput(mergedPath).content).toBe(encoded)
expect(decompress(readSplitInput(mergedPath).content, 64)).toBe("merged split parts")
})
it("reassembles split output merged into a single file", () => {
const dir = makeTempDir()
const encoded = compress("single merged file", 64)
const chunks = splitString(encoded, 9)
const totalParts = chunks.length
const mergedPath = join(dir, "all.txt")
writeFileSync(
mergedPath,
chunks.map((chunk, index) => wrapSplitChunk(index + 1, totalParts, chunk)).join(""),
"utf-8",
)
expect(readSplitInput(mergedPath).content).toBe(encoded)
expect(decompress(readSplitInput(mergedPath).content, 64)).toBe("single merged file")
})
it("errors when split parts are missing", () => {
const dir = makeTempDir()
writeFileSync(join(dir, "output.1.txt"), wrapSplitChunk(1, 3, "a"))
writeFileSync(join(dir, "output.3.txt"), wrapSplitChunk(3, 3, "c"))
expect(() => readSplitInput(join(dir, "output.1.txt"))).toThrow(/Missing split part 2/)
})
it("discovers prefix siblings with shuffled names and skips invalid files", () => {
const dir = makeTempDir()
const encoded = compress(`prefix discovery ${"x".repeat(2000)}`, 64)
const chunks = splitString(encoded, 10)
expect(chunks.length).toBeGreaterThanOrEqual(3)
const totalParts = chunks.length
writeFileSync(join(dir, "file.1.md"), wrapSplitChunk(3, totalParts, chunks[2]), "utf-8")
writeFileSync(join(dir, "file.2.md"), "not valid split data", "utf-8")
writeFileSync(join(dir, "file.3.md"), wrapSplitChunk(2, totalParts, chunks[1]), "utf-8")
writeFileSync(join(dir, "file.7.md"), wrapSplitChunk(1, totalParts, chunks[0]), "utf-8")
for (let index = 3; index < totalParts; index++) {
const partPath = join(dir, `file.${index + 10}.md`)
writeFileSync(partPath, wrapSplitChunk(index + 1, totalParts, chunks[index]), "utf-8")
}
for (const entry of ["file.1.md", "file.2.md", "file.7.md"]) {
expect(readSplitInput(join(dir, entry)).content).toBe(encoded)
expect(decompress(readSplitInput(join(dir, entry)).content, 64)).toBe(
`prefix discovery ${"x".repeat(2000)}`,
)
}
})
it("discovers prefix siblings regardless of extension", () => {
const dir = makeTempDir()
const encoded = compress("mixed extensions", 64)
const chunks = splitString(encoded, 10)
const totalParts = chunks.length
const extensions = [".md", ".txt", ".bin", ".dat", ".z"]
for (let index = 0; index < totalParts; index++) {
const ext = extensions[index % extensions.length]
writeFileSync(
join(dir, `blob.${index + 1}${ext}`),
wrapSplitChunk(index + 1, totalParts, chunks[index]),
"utf-8",
)
}
expect(readSplitInput(join(dir, "blob.2.txt")).content).toBe(encoded)
})
it("auto-splits above 30,000 characters when -s is omitted", () => {
expect(resolveSplitChunkSize(30_000)).toBeUndefined()
expect(resolveSplitChunkSize(30_001)).toBe(AUTO_SPLIT_CHARS)
expect(resolveSplitChunkSize(50_000, 4_000)).toBe(4_000)
})
it("disables splitting when explicit size is 0", () => {
expect(resolveSplitChunkSize(50_000, 0)).toBeUndefined()
expect(resolveSplitChunkSize(30_001, 0)).toBeUndefined()
})
it("uses a printable ASCII split header so files stay text-only", () => {
const wrapped = wrapSplitChunk(2, 5, "AwEAAA==")
expect(wrapped.startsWith(";TCP2;2;5;")).toBe(true)
expect(SPLIT_MAGIC).toBe(";TCP2;")
for (const char of wrapped) {
const code = char.charCodeAt(0)
expect(code).toBeGreaterThanOrEqual(32)
expect(code).toBeLessThan(127)
}
})
it("limits each split part file to -s characters including the header", () => {
const dir = makeTempDir()
const encoded = compress("x".repeat(500), 64)
const maxPartChars = 100
const wrappedParts = splitEncodedIntoWrappedParts(encoded, maxPartChars)
for (const part of wrappedParts) {
expect(part.length).toBeLessThanOrEqual(maxPartChars)
}
const totalParts = wrappedParts.length
for (let index = 0; index < totalParts; index++) {
const partPath = formatSplitOutputPath(join(dir, "out.txt"), index + 1, totalParts)
writeFileSync(partPath, wrappedParts[index], "utf-8")
}
expect(readSplitInput(join(dir, "out.1.txt")).content).toBe(encoded)
expect(decompress(readSplitInput(join(dir, "out.1.txt")).content, 64)).toBe("x".repeat(500))
})
it("accounts for wider headers when part counts cross digit boundaries", () => {
const encoded = "a".repeat(950)
const parts = splitEncodedIntoWrappedParts(encoded, 100)
expect(parts.length).toBeGreaterThan(1)
for (const part of parts) {
expect(part.length).toBeLessThanOrEqual(100)
}
const dir = makeTempDir()
const totalParts = parts.length
for (let index = 0; index < totalParts; index++) {
writeFileSync(
formatSplitOutputPath(join(dir, "wide.txt"), index + 1, totalParts),
parts[index],
"utf-8",
)
}
expect(readSplitInput(join(dir, "wide.1.txt")).content).toBe(encoded)
})
it("still reads legacy binary TCP\\x02 split headers", () => {
const dir = makeTempDir()
const legacyHeader = Buffer.alloc(12)
Buffer.from([0x54, 0x43, 0x50, 0x02]).copy(legacyHeader, 0)
legacyHeader.writeUInt32LE(1, 4)
legacyHeader.writeUInt32LE(1, 8)
const encoded = compress("legacy split header", 64)
writeFileSync(
join(dir, "legacy.txt"),
Buffer.concat([legacyHeader, Buffer.from(encoded, "utf-8")]),
)
expect(readSplitInput(join(dir, "legacy.txt")).content).toBe(encoded)
expect(decompress(readSplitInput(join(dir, "legacy.txt")).content, 64)).toBe(
"legacy split header",
)
})
})
describe("input path validation", () => {
it("rejects -f input when the path is a directory", () => {
const dir = makeTempDir()
expect(() => readTextFile(dir, "compress")).toThrow(/is a directory/)
expect(() => readTextFile(dir, "decompress")).toThrow(/compressed .txt file/)
expect(() => resolveSplitInputPaths(dir)).toThrow(/is a directory/)
expect(() => readSplitInput(dir)).toThrow(/is a directory/)
})
it("rejects -d input when the path is a file", () => {
const dir = makeTempDir()
const file = join(dir, "notes.txt")
writeFileSync(file, "hello")
expect(() => assertDirectory(file)).toThrow(/is not a directory/)
})
})
describe("base85 encoding", () => {
it("produces smaller output than base64 for larger payloads", () => {
const input = "The quick brown fox jumps over the lazy dog.\n".repeat(200)
const b64 = compress(input, 64)
const b85 = compress(input, 85)
expect(b85.length).toBeLessThan(b64.length)
})
})