Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/cli/src/commands/_lib/scaffold.test.ts
T
2026-07-08 16:15:37 +02:00

103 lines
3.3 KiB
TypeScript

import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
camelCase,
createScaffoldResult,
kebabCase,
pascalCase,
pathExists,
snakeCase,
validateIdentifier,
writeScaffoldFile,
} from "./scaffold.js";
describe("scaffold helpers / string conversions", () => {
it.each([
["foo bar", "fooBar"],
["foo-bar-baz", "fooBarBaz"],
["FooBarBaz", "fooBarBaz"],
["foo_bar", "fooBar"],
])("camelCase(%s) === %s", (input, expected) => {
expect(camelCase(input)).toBe(expected);
});
it.each([
["foo bar", "FooBar"],
["foo-bar-baz", "FooBarBaz"],
["fooBarBaz", "FooBarBaz"],
])("pascalCase(%s) === %s", (input, expected) => {
expect(pascalCase(input)).toBe(expected);
});
it("kebabCase + snakeCase", () => {
expect(kebabCase("FooBarBaz")).toBe("foo-bar-baz");
expect(snakeCase("FooBarBaz")).toBe("foo_bar_baz");
});
});
describe("scaffold helpers / validateIdentifier", () => {
it("accepts valid identifiers", () => {
expect(validateIdentifier("foo")).toBe("foo");
expect(validateIdentifier("foo-bar")).toBe("foo-bar");
expect(validateIdentifier("foo_bar")).toBe("foo_bar");
});
it("rejects empty / invalid input", () => {
expect(() => validateIdentifier("")).toThrow(/required/);
expect(() => validateIdentifier("1foo")).toThrow(/Invalid/);
expect(() => validateIdentifier("foo!bar")).toThrow(/Invalid/);
});
});
describe("scaffold helpers / writeScaffoldFile", () => {
let tempRoot: string;
beforeEach(async () => {
tempRoot = await mkdtemp(path.join(tmpdir(), "hivegrid-cli-scaffold-"));
});
afterEach(async () => {
await rm(tempRoot, { recursive: true, force: true });
});
it("writes the file in non-dry-run mode and records it on the result", async () => {
const result = createScaffoldResult(false);
const target = path.join(tempRoot, "nested", "out.txt");
await writeScaffoldFile({ targetPath: target, contents: "hello", dryRun: false, result });
expect(result.files).toHaveLength(1);
expect(result.files[0]).toMatchObject({ path: target, contents: "hello" });
const written = await readFile(target, "utf8");
expect(written).toBe("hello");
});
it("records the write but does NOT touch the FS in dry-run mode", async () => {
const result = createScaffoldResult(true);
const target = path.join(tempRoot, "ghost.txt");
await writeScaffoldFile({ targetPath: target, contents: "ghost", dryRun: true, result });
expect(result.files).toHaveLength(1);
await expect(stat(target)).rejects.toThrow();
});
it("refuses to overwrite an existing file", async () => {
const target = path.join(tempRoot, "existing.txt");
await writeFile(target, "old", "utf8");
const result = createScaffoldResult(false);
await expect(
writeScaffoldFile({ targetPath: target, contents: "new", dryRun: false, result }),
).rejects.toThrow(/already exists/);
});
it("pathExists distinguishes existing vs missing paths", async () => {
const target = path.join(tempRoot, "maybe.txt");
expect(await pathExists(target)).toBe(false);
await writeFile(target, "x", "utf8");
expect(await pathExists(target)).toBe(true);
});
});