commit 5628df7f9d1deccf01e2a9448b25a6407584485d
parent d02221090838767d7538a432a8354c922cb9a7fa
Author: Henry Unite <unitehenry@gmail.com>
Date: Sun, 25 Oct 2026 01:37:37 +0000
test(command): add unit tests for clear.ts using vitest
- Import actual `init()` from context (no mocking, per prior pattern)
- 4 tests covering: logging/early path, session dir removal, ignoring non-dirs, graceful error handling
- Setup/teardown uses real fs under the real openpodRoot from init
- Accounts for side-effect of logger always leaving .log dir after clear
- Tests pass via vitest
Diffstat:
1 file changed, 69 insertions(+), 0 deletions(-)
diff --git a/command/clear.test.ts b/command/clear.test.ts
@@ -0,0 +1,69 @@
+import { describe, it, expect, beforeAll, beforeEach, afterEach } from "vitest";
+import * as fs from "node:fs";
+import * as path from "node:path";
+
+import type { OpenpodContext } from "../context";
+import { init } from "../context";
+import { clear } from "./clear";
+
+describe("clear", () => {
+ let ctx: OpenpodContext;
+
+ beforeAll(async () => {
+ ctx = await init();
+ });
+
+ beforeEach(() => {
+ if (fs.existsSync(ctx.openpodRoot)) {
+ fs.rmSync(ctx.openpodRoot, { recursive: true, force: true });
+ }
+ });
+
+ afterEach(() => {
+ if (fs.existsSync(ctx.openpodRoot)) {
+ fs.rmSync(ctx.openpodRoot, { recursive: true, force: true });
+ }
+ });
+
+ it("logs start and leaves only .log dir (due to logging) even if root did not exist", async () => {
+ await clear(ctx);
+ expect(fs.existsSync(ctx.openpodRoot)).toBe(true);
+ const entries = fs.readdirSync(ctx.openpodRoot);
+ expect(entries).toContain(".log");
+ });
+
+ it("removes session directories and leaves only .log after cleanup", async () => {
+ fs.mkdirSync(path.join(ctx.openpodRoot, "session-abc"), {
+ recursive: true,
+ });
+ fs.mkdirSync(path.join(ctx.openpodRoot, "session-xyz", "nested"), {
+ recursive: true,
+ });
+ fs.mkdirSync(path.join(ctx.openpodRoot, ".log"), { recursive: true });
+
+ await clear(ctx);
+
+ expect(fs.existsSync(ctx.openpodRoot)).toBe(true);
+ const entries = fs.readdirSync(ctx.openpodRoot);
+ expect(entries).toEqual([".log"]);
+ });
+
+ it("ignores non-directory entries and cleans sessions", async () => {
+ fs.mkdirSync(ctx.openpodRoot, { recursive: true });
+ fs.writeFileSync(path.join(ctx.openpodRoot, "not-a-dir.txt"), "data");
+ fs.mkdirSync(path.join(ctx.openpodRoot, "real-session"), {
+ recursive: true,
+ });
+
+ await clear(ctx);
+
+ expect(fs.existsSync(ctx.openpodRoot)).toBe(true);
+ const entries = fs.readdirSync(ctx.openpodRoot);
+ expect(entries).toEqual([".log"]);
+ });
+
+ it("handles errors gracefully without throwing", async () => {
+ fs.mkdirSync(ctx.openpodRoot, { recursive: true });
+ await expect(clear(ctx)).resolves.not.toThrow();
+ });
+});