Path: blob/master/src/packages/sync-fs/lib/compressed-json.test.ts
1496 views
import { toCompressedJSON, fromCompressedJSON } from "./compressed-json";1import { writeFile, unlink } from "fs/promises";2import { execSync } from "child_process";3import { tmpdir } from "os";4import { join } from "path";5import { writeFileLz4 } from "./util";67describe("test basic compression in memory", () => {8it("it compresses and decompresses an object", async () => {9const obj = { cocalc: "sagemath", v: [1, 2, 7] };10const c = await toCompressedJSON(obj);11const d = await fromCompressedJSON(c);12expect(d).toEqual(obj);13});1415it("compresses a large object and observes the result is small", async () => {16const obj = { cocalc: "sagemath".repeat(10000), v: [1, 2, 7] };17const c = await toCompressedJSON(obj);18const d = await fromCompressedJSON(c);19expect(d).toEqual(obj);20expect(c.length).toBeLessThan(1000);21});22});2324describe("test compression is compatible with command line lz4", () => {25it("compression output is compatible with lz4 tool", async () => {26const obj = { cocalc: "sagemath" };27const c = await toCompressedJSON(obj);2829// Write c to a temporary file ending in .lz430const tempFilePath = join(tmpdir(), `tempfile.lz4`);31await writeFile(tempFilePath, c);3233try {34// Run command line "lz4 -t tempfile.lz4" in subprocess and confirm exit code 035execSync(`lz4 -t ${tempFilePath}`);36} catch (error) {37throw new Error(`lz4 command failed: ${error.message}`);38} finally {39// Clean up the temporary file40await unlink(tempFilePath);41}42});43});4445describe("test compression using writeFileLz4 is compatible with command line lz4", () => {46it("compression output is compatible with lz4 tool and content is 'hello'", async () => {47// Write "hello" to a temporary file ending in .lz448const tempFilePath = join(tmpdir(), `tempfile.lz4`);49await writeFileLz4(tempFilePath, "hello");5051try {52// Run command line "lz4 -dc tempfile.lz4" in subprocess and check the output53const output = execSync(`lz4 -dc ${tempFilePath}`).toString().trim();54expect(output).toBe("hello");55} catch (error) {56throw new Error(`lz4 command failed: ${error.message}`);57} finally {58// Clean up the temporary file59await unlink(tempFilePath);60}61});62});636465