Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync-fs/lib/compressed-json.test.ts
1496 views
1
import { toCompressedJSON, fromCompressedJSON } from "./compressed-json";
2
import { writeFile, unlink } from "fs/promises";
3
import { execSync } from "child_process";
4
import { tmpdir } from "os";
5
import { join } from "path";
6
import { writeFileLz4 } from "./util";
7
8
describe("test basic compression in memory", () => {
9
it("it compresses and decompresses an object", async () => {
10
const obj = { cocalc: "sagemath", v: [1, 2, 7] };
11
const c = await toCompressedJSON(obj);
12
const d = await fromCompressedJSON(c);
13
expect(d).toEqual(obj);
14
});
15
16
it("compresses a large object and observes the result is small", async () => {
17
const obj = { cocalc: "sagemath".repeat(10000), v: [1, 2, 7] };
18
const c = await toCompressedJSON(obj);
19
const d = await fromCompressedJSON(c);
20
expect(d).toEqual(obj);
21
expect(c.length).toBeLessThan(1000);
22
});
23
});
24
25
describe("test compression is compatible with command line lz4", () => {
26
it("compression output is compatible with lz4 tool", async () => {
27
const obj = { cocalc: "sagemath" };
28
const c = await toCompressedJSON(obj);
29
30
// Write c to a temporary file ending in .lz4
31
const tempFilePath = join(tmpdir(), `tempfile.lz4`);
32
await writeFile(tempFilePath, c);
33
34
try {
35
// Run command line "lz4 -t tempfile.lz4" in subprocess and confirm exit code 0
36
execSync(`lz4 -t ${tempFilePath}`);
37
} catch (error) {
38
throw new Error(`lz4 command failed: ${error.message}`);
39
} finally {
40
// Clean up the temporary file
41
await unlink(tempFilePath);
42
}
43
});
44
});
45
46
describe("test compression using writeFileLz4 is compatible with command line lz4", () => {
47
it("compression output is compatible with lz4 tool and content is 'hello'", async () => {
48
// Write "hello" to a temporary file ending in .lz4
49
const tempFilePath = join(tmpdir(), `tempfile.lz4`);
50
await writeFileLz4(tempFilePath, "hello");
51
52
try {
53
// Run command line "lz4 -dc tempfile.lz4" in subprocess and check the output
54
const output = execSync(`lz4 -dc ${tempFilePath}`).toString().trim();
55
expect(output).toBe("hello");
56
} catch (error) {
57
throw new Error(`lz4 command failed: ${error.message}`);
58
} finally {
59
// Clean up the temporary file
60
await unlink(tempFilePath);
61
}
62
});
63
});
64
65