a reactive (signals based) hypermedia web framework (wip)
stormlightlabs.github.io/volt/
hypermedia
frontend
signals
1import { createFile, isEmptyOrMissing } from "$utils/files.js";
2import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
3import { tmpdir } from "node:os";
4import path from "node:path";
5import { afterEach, beforeEach, describe, expect, it } from "vitest";
6
7describe("files utilities", () => {
8 let tempDir: string;
9
10 beforeEach(async () => {
11 tempDir = await mkdtemp(path.join(tmpdir(), "voltx-test-"));
12 });
13
14 afterEach(async () => {
15 await rm(tempDir, { recursive: true, force: true });
16 });
17
18 describe("createFile", () => {
19 it("should create a file with content", async () => {
20 const filePath = path.join(tempDir, "test.txt");
21 const content = "Hello VoltX!";
22
23 await createFile(filePath, content);
24
25 const fileContent = await readFile(filePath, "utf8");
26 expect(fileContent).toBe(content);
27 });
28
29 it("should create parent directories if they don't exist", async () => {
30 const filePath = path.join(tempDir, "nested", "deep", "test.txt");
31 const content = "Nested file";
32
33 await createFile(filePath, content);
34
35 const fileContent = await readFile(filePath, "utf8");
36 expect(fileContent).toBe(content);
37 });
38 });
39
40 describe("isEmptyOrMissing", () => {
41 it("should return true for non-existent directory", async () => {
42 const nonExistentDir = path.join(tempDir, "does-not-exist");
43 const result = await isEmptyOrMissing(nonExistentDir);
44 expect(result).toBe(true);
45 });
46
47 it("should return true for empty directory", async () => {
48 const emptyDir = path.join(tempDir, "empty");
49 await mkdir(emptyDir);
50
51 const result = await isEmptyOrMissing(emptyDir);
52 expect(result).toBe(true);
53 });
54
55 it("should return false for directory with files", async () => {
56 const dirWithFiles = path.join(tempDir, "with-files");
57 await mkdir(dirWithFiles);
58 await createFile(path.join(dirWithFiles, "test.txt"), "content");
59
60 const result = await isEmptyOrMissing(dirWithFiles);
61 expect(result).toBe(false);
62 });
63 });
64});