dist-mcp / mcp / scratchpadResolution.test.js
dist-mcp / mcp / scratchpadResolution.test.js
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { resolveEffectiveScratchpadPath } from "./scratchpadResolution.js";
async function withBaseDirectory(run) {
const baseDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "process-image-mcp-fallback-"));
try {
await run(baseDirectory);
}
finally {
await fs.rm(baseDirectory, { recursive: true, force: true });
}
}
test("falls back to chatWorkingDirectories itself when scratchpadFolder is omitted, and provisions chat_media_state.json there", async () => {
await withBaseDirectory(async (baseDirectory) => {
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, undefined);
assert.equal(resolved, path.resolve(baseDirectory));
await fs.access(path.join(baseDirectory, "chat_media_state.json"));
});
});
test("falls back the same way for an empty or whitespace-only scratchpadFolder", async () => {
await withBaseDirectory(async (baseDirectory) => {
assert.equal(await resolveEffectiveScratchpadPath(baseDirectory, ""), path.resolve(baseDirectory));
assert.equal(await resolveEffectiveScratchpadPath(baseDirectory, " "), path.resolve(baseDirectory));
});
});
test("resolves an explicit scratchpadFolder to its child path, as before", async () => {
await withBaseDirectory(async (baseDirectory) => {
await fs.mkdir(path.join(baseDirectory, "session-1"));
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, "session-1");
assert.equal(resolved, path.join(baseDirectory, "session-1"));
});
});
test("rejects a missing explicit scratchpadFolder", async () => {
await withBaseDirectory(async (baseDirectory) => {
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, "missing-session"));
});
});
test("rejects when CHAT_WORKING_DIRECTORIES itself is missing and no scratchpadFolder was given", async () => {
await withBaseDirectory(async (baseDirectory) => {
const missingBase = path.join(baseDirectory, "does-not-exist");
await assert.rejects(() => resolveEffectiveScratchpadPath(missingBase, undefined), /CHAT_WORKING_DIRECTORIES/);
});
});
test("requireFolder=true rejects an omitted scratchpadFolder instead of falling back to chatWorkingDirectories", async () => {
await withBaseDirectory(async (baseDirectory) => {
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, undefined, true), /scratchpadFolder is required/);
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, " ", true), /scratchpadFolder is required/);
});
});
test("requireFolder=true still resolves an explicit scratchpadFolder normally", async () => {
await withBaseDirectory(async (baseDirectory) => {
await fs.mkdir(path.join(baseDirectory, "session-1"));
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, "session-1", true);
assert.equal(resolved, path.join(baseDirectory, "session-1"));
});
});
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { resolveEffectiveScratchpadPath } from "./scratchpadResolution.js";
async function withBaseDirectory(run) {
const baseDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "process-image-mcp-fallback-"));
try {
await run(baseDirectory);
}
finally {
await fs.rm(baseDirectory, { recursive: true, force: true });
}
}
test("falls back to chatWorkingDirectories itself when scratchpadFolder is omitted, and provisions chat_media_state.json there", async () => {
await withBaseDirectory(async (baseDirectory) => {
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, undefined);
assert.equal(resolved, path.resolve(baseDirectory));
await fs.access(path.join(baseDirectory, "chat_media_state.json"));
});
});
test("falls back the same way for an empty or whitespace-only scratchpadFolder", async () => {
await withBaseDirectory(async (baseDirectory) => {
assert.equal(await resolveEffectiveScratchpadPath(baseDirectory, ""), path.resolve(baseDirectory));
assert.equal(await resolveEffectiveScratchpadPath(baseDirectory, " "), path.resolve(baseDirectory));
});
});
test("resolves an explicit scratchpadFolder to its child path, as before", async () => {
await withBaseDirectory(async (baseDirectory) => {
await fs.mkdir(path.join(baseDirectory, "session-1"));
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, "session-1");
assert.equal(resolved, path.join(baseDirectory, "session-1"));
});
});
test("rejects a missing explicit scratchpadFolder", async () => {
await withBaseDirectory(async (baseDirectory) => {
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, "missing-session"));
});
});
test("rejects when CHAT_WORKING_DIRECTORIES itself is missing and no scratchpadFolder was given", async () => {
await withBaseDirectory(async (baseDirectory) => {
const missingBase = path.join(baseDirectory, "does-not-exist");
await assert.rejects(() => resolveEffectiveScratchpadPath(missingBase, undefined), /CHAT_WORKING_DIRECTORIES/);
});
});
test("requireFolder=true rejects an omitted scratchpadFolder instead of falling back to chatWorkingDirectories", async () => {
await withBaseDirectory(async (baseDirectory) => {
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, undefined, true), /scratchpadFolder is required/);
await assert.rejects(() => resolveEffectiveScratchpadPath(baseDirectory, " ", true), /scratchpadFolder is required/);
});
});
test("requireFolder=true still resolves an explicit scratchpadFolder normally", async () => {
await withBaseDirectory(async (baseDirectory) => {
await fs.mkdir(path.join(baseDirectory, "session-1"));
const resolved = await resolveEffectiveScratchpadPath(baseDirectory, "session-1", true);
assert.equal(resolved, path.join(baseDirectory, "session-1"));
});
});