




















@@ -0,0 +1,79 @@
1+import fs from "node:fs";
2+import os from "node:os";
3+import path from "node:path";
4+import { afterEach, describe, expect, it, vi } from "vitest";
5+import { createLowDiskSpaceWarning, formatDiskSpaceBytes, tryReadDiskSpace } from "./disk-space.js";
6+7+function statfsFixture(params: {
8+bavail: number;
9+bsize?: number;
10+blocks?: number;
11+}): ReturnType<typeof fs.statfsSync> {
12+return {
13+type: 0,
14+bsize: params.bsize ?? 1024,
15+blocks: params.blocks ?? 2_000_000,
16+bfree: params.bavail,
17+bavail: params.bavail,
18+files: 0,
19+ffree: 0,
20+};
21+}
22+23+describe("disk-space helpers", () => {
24+afterEach(() => {
25+vi.restoreAllMocks();
26+});
27+28+it("reads disk space from the nearest existing ancestor", () => {
29+const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-disk-space-"));
30+try {
31+const statfs = vi.spyOn(fs, "statfsSync").mockReturnValue(
32+statfsFixture({
33+bavail: 512,
34+bsize: 1024,
35+blocks: 4096,
36+}),
37+);
38+39+const snapshot = tryReadDiskSpace(path.join(tempDir, "missing", "child"));
40+41+expect(snapshot).toEqual({
42+targetPath: path.join(tempDir, "missing", "child"),
43+checkedPath: tempDir,
44+availableBytes: 512 * 1024,
45+totalBytes: 4096 * 1024,
46+});
47+expect(statfs).toHaveBeenCalledWith(tempDir);
48+} finally {
49+fs.rmSync(tempDir, { recursive: true, force: true });
50+}
51+});
52+53+it("formats low disk warnings without making them hard errors", () => {
54+const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-disk-space-"));
55+try {
56+vi.spyOn(fs, "statfsSync").mockReturnValue(
57+statfsFixture({
58+bavail: 256,
59+bsize: 1024 * 1024,
60+}),
61+);
62+63+expect(
64+createLowDiskSpaceWarning({
65+targetPath: tempDir,
66+purpose: "test staging",
67+thresholdBytes: 512 * 1024 * 1024,
68+}),
69+).toBe(`Low disk space near ${tempDir}: 256 MiB available; test staging may fail.`);
70+} finally {
71+fs.rmSync(tempDir, { recursive: true, force: true });
72+}
73+});
74+75+it("keeps byte formatting compact", () => {
76+expect(formatDiskSpaceBytes(420 * 1024 * 1024)).toBe("420 MiB");
77+expect(formatDiskSpaceBytes(1536 * 1024 * 1024)).toBe("1.5 GiB");
78+});
79+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。