mirror of
https://github.com/OpenRouterTeam/spawn.git
synced 2026-04-28 03:49:31 +00:00
test: remove duplicate getSpawnDir/getHistoryPath tests from history.test.ts (#2426)
These path-utility tests were duplicated between history.test.ts and paths.test.ts. Consolidate into paths.test.ts (the canonical location) and move 4 unique test cases (dot-relative path, .. resolution, outside home rejection, home-as-SPAWN_HOME) that only existed in history.test.ts. Removes 64 lines of duplicate test code with zero coverage loss. Co-authored-by: spawn-qa-bot <qa@openrouter.ai>
This commit is contained in:
parent
01263193be
commit
73ab90fb53
2 changed files with 21 additions and 64 deletions
|
|
@ -4,7 +4,6 @@ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { filterHistory, HISTORY_SCHEMA_VERSION, loadHistory, saveSpawnRecord } from "../history.js";
|
||||
import { getHistoryPath, getSpawnDir, getUserHome } from "../shared/paths.js";
|
||||
|
||||
describe("history", () => {
|
||||
let testDir: string;
|
||||
|
|
@ -32,69 +31,6 @@ describe("history", () => {
|
|||
}
|
||||
});
|
||||
|
||||
// ── getSpawnDir ─────────────────────────────────────────────────────────
|
||||
|
||||
describe("getSpawnDir", () => {
|
||||
it("returns SPAWN_HOME when set to valid path within home", () => {
|
||||
const validPath = join(process.env.HOME ?? "", "custom", "spawn", "dir");
|
||||
process.env.SPAWN_HOME = validPath;
|
||||
expect(getSpawnDir()).toBe(validPath);
|
||||
});
|
||||
|
||||
it("falls back to ~/.spawn when SPAWN_HOME is not set", () => {
|
||||
delete process.env.SPAWN_HOME;
|
||||
expect(getSpawnDir()).toBe(join(getUserHome(), ".spawn"));
|
||||
});
|
||||
|
||||
it("throws for relative SPAWN_HOME path", () => {
|
||||
process.env.SPAWN_HOME = "relative/path";
|
||||
expect(() => getSpawnDir()).toThrow("must be an absolute path");
|
||||
});
|
||||
|
||||
it("throws for dot-relative SPAWN_HOME path", () => {
|
||||
process.env.SPAWN_HOME = "./local/dir";
|
||||
expect(() => getSpawnDir()).toThrow("must be an absolute path");
|
||||
});
|
||||
|
||||
it("resolves .. segments in absolute SPAWN_HOME within home", () => {
|
||||
const pathWithDots = join(process.env.HOME ?? "", "foo", "..", "bar");
|
||||
process.env.SPAWN_HOME = pathWithDots;
|
||||
expect(getSpawnDir()).toBe(join(process.env.HOME ?? "", "bar"));
|
||||
});
|
||||
|
||||
it("accepts normal absolute SPAWN_HOME within home", () => {
|
||||
const validPath = join(process.env.HOME ?? "", ".spawn");
|
||||
process.env.SPAWN_HOME = validPath;
|
||||
expect(getSpawnDir()).toBe(validPath);
|
||||
});
|
||||
|
||||
it("throws for SPAWN_HOME outside home directory", () => {
|
||||
process.env.SPAWN_HOME = "/tmp/spawn";
|
||||
expect(() => getSpawnDir()).toThrow("must be within your home directory");
|
||||
});
|
||||
|
||||
it("throws for path traversal attempt to escape home directory", () => {
|
||||
// Attempt to traverse outside home using .. segments
|
||||
// e.g., /home/user/../../etc/.spawn
|
||||
const traversalPath = join(process.env.HOME ?? "", "..", "..", "etc", ".spawn");
|
||||
process.env.SPAWN_HOME = traversalPath;
|
||||
expect(() => getSpawnDir()).toThrow("must be within your home directory");
|
||||
});
|
||||
|
||||
it("accepts home directory itself as SPAWN_HOME", () => {
|
||||
process.env.SPAWN_HOME = process.env.HOME ?? "";
|
||||
expect(getSpawnDir()).toBe(process.env.HOME ?? "");
|
||||
});
|
||||
});
|
||||
|
||||
// ── getHistoryPath ──────────────────────────────────────────────────────
|
||||
|
||||
describe("getHistoryPath", () => {
|
||||
it("returns history.json inside spawn dir", () => {
|
||||
expect(getHistoryPath()).toBe(join(testDir, "history.json"));
|
||||
});
|
||||
});
|
||||
|
||||
// ── loadHistory ─────────────────────────────────────────────────────────
|
||||
|
||||
describe("loadHistory", () => {
|
||||
|
|
|
|||
|
|
@ -55,10 +55,31 @@ describe("paths", () => {
|
|||
expect(() => getSpawnDir()).toThrow("must be an absolute path");
|
||||
});
|
||||
|
||||
it("rejects dot-relative SPAWN_HOME", () => {
|
||||
process.env.SPAWN_HOME = "./local/dir";
|
||||
expect(() => getSpawnDir()).toThrow("must be an absolute path");
|
||||
});
|
||||
|
||||
it("resolves .. segments in absolute SPAWN_HOME within home", () => {
|
||||
const pathWithDots = join(getUserHome(), "foo", "..", "bar");
|
||||
process.env.SPAWN_HOME = pathWithDots;
|
||||
expect(getSpawnDir()).toBe(join(getUserHome(), "bar"));
|
||||
});
|
||||
|
||||
it("rejects SPAWN_HOME outside home directory", () => {
|
||||
process.env.SPAWN_HOME = "/tmp/spawn";
|
||||
expect(() => getSpawnDir()).toThrow("must be within your home directory");
|
||||
});
|
||||
|
||||
it("rejects path traversal outside home directory", () => {
|
||||
process.env.SPAWN_HOME = "/tmp/../../root/.spawn";
|
||||
expect(() => getSpawnDir()).toThrow("must be within your home directory");
|
||||
});
|
||||
|
||||
it("accepts home directory itself as SPAWN_HOME", () => {
|
||||
process.env.SPAWN_HOME = getUserHome();
|
||||
expect(getSpawnDir()).toBe(getUserHome());
|
||||
});
|
||||
});
|
||||
|
||||
describe("getHistoryPath", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue