test: tighten small infra helper coverage

This commit is contained in:
Peter Steinberger 2026-03-13 18:31:59 +00:00
parent 8cef6f2120
commit 9c343fb3db
3 changed files with 40 additions and 26 deletions

View File

@ -3,14 +3,20 @@ import { describe, expect, it } from "vitest";
import { isWithinDir, resolveSafeBaseDir } from "./path-safety.js"; import { isWithinDir, resolveSafeBaseDir } from "./path-safety.js";
describe("path-safety", () => { describe("path-safety", () => {
it("resolves safe base dir with trailing separator", () => { it.each([
const base = resolveSafeBaseDir("/tmp/demo"); { rootDir: "/tmp/demo", expected: `${path.resolve("/tmp/demo")}${path.sep}` },
expect(base.endsWith(path.sep)).toBe(true); { rootDir: `/tmp/demo${path.sep}`, expected: `${path.resolve("/tmp/demo")}${path.sep}` },
])("resolves safe base dir for %j", ({ rootDir, expected }) => {
expect(resolveSafeBaseDir(rootDir)).toBe(expected);
}); });
it("checks directory containment", () => { it.each([
expect(isWithinDir("/tmp/demo", "/tmp/demo")).toBe(true); { rootDir: "/tmp/demo", targetPath: "/tmp/demo", expected: true },
expect(isWithinDir("/tmp/demo", "/tmp/demo/sub/file.txt")).toBe(true); { rootDir: "/tmp/demo", targetPath: "/tmp/demo/sub/file.txt", expected: true },
expect(isWithinDir("/tmp/demo", "/tmp/demo/../escape.txt")).toBe(false); { rootDir: "/tmp/demo", targetPath: "/tmp/demo/../escape.txt", expected: false },
{ rootDir: "/tmp/demo", targetPath: "/tmp/demo-sibling/file.txt", expected: false },
{ rootDir: "/tmp/demo", targetPath: "sub/file.txt", expected: false },
])("checks containment for %j", ({ rootDir, targetPath, expected }) => {
expect(isWithinDir(rootDir, targetPath)).toBe(expected);
}); });
}); });

View File

@ -2,17 +2,17 @@ import { describe, expect, it } from "vitest";
import { isPlainObject } from "./plain-object.js"; import { isPlainObject } from "./plain-object.js";
describe("isPlainObject", () => { describe("isPlainObject", () => {
it("accepts plain objects", () => { it.each([{}, { a: 1 }, Object.create(null), new (class X {})()])(
expect(isPlainObject({})).toBe(true); "accepts object-tag values: %j",
expect(isPlainObject({ a: 1 })).toBe(true); (value) => {
}); expect(isPlainObject(value)).toBe(true);
},
);
it("rejects non-plain values", () => { it.each([null, [], new Date(), /re/, "x", 42, () => null, new Map()])(
expect(isPlainObject(null)).toBe(false); "rejects non-plain values: %j",
expect(isPlainObject([])).toBe(false); (value) => {
expect(isPlainObject(new Date())).toBe(false); expect(isPlainObject(value)).toBe(false);
expect(isPlainObject(/re/)).toBe(false); },
expect(isPlainObject("x")).toBe(false); );
expect(isPlainObject(42)).toBe(false);
});
}); });

View File

@ -2,8 +2,21 @@ import { describe, expect, it } from "vitest";
import { SYSTEM_MARK, hasSystemMark, prefixSystemMessage } from "./system-message.js"; import { SYSTEM_MARK, hasSystemMark, prefixSystemMessage } from "./system-message.js";
describe("system-message", () => { describe("system-message", () => {
it("prepends the system mark once", () => { it.each([
expect(prefixSystemMessage("thread notice")).toBe(`${SYSTEM_MARK} thread notice`); { input: "thread notice", expected: `${SYSTEM_MARK} thread notice` },
{ input: ` thread notice `, expected: `${SYSTEM_MARK} thread notice` },
{ input: " ", expected: "" },
])("prefixes %j", ({ input, expected }) => {
expect(prefixSystemMessage(input)).toBe(expected);
});
it.each([
{ input: `${SYSTEM_MARK} already prefixed`, expected: true },
{ input: ` ${SYSTEM_MARK} hello`, expected: true },
{ input: "", expected: false },
{ input: "hello", expected: false },
])("detects marks for %j", ({ input, expected }) => {
expect(hasSystemMark(input)).toBe(expected);
}); });
it("does not double-prefix messages that already have the mark", () => { it("does not double-prefix messages that already have the mark", () => {
@ -11,9 +24,4 @@ describe("system-message", () => {
`${SYSTEM_MARK} already prefixed`, `${SYSTEM_MARK} already prefixed`,
); );
}); });
it("detects marked system text after trim normalization", () => {
expect(hasSystemMark(` ${SYSTEM_MARK} hello`)).toBe(true);
expect(hasSystemMark("hello")).toBe(false);
});
}); });