Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

2.9 KB
Raw Blame
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { checkInput, checkOutput, isWithin, jobWorkspace, normalize, PathError } from "../src/paths";

const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "qls-mcp-"));
const proj = path.join(tmp, "proj");
fs.mkdirSync(path.join(proj, "assets"), { recursive: true });
fs.writeFileSync(path.join(proj, "assets", "header.png"), "x");
fs.writeFileSync(path.join(proj, "notes.txt"), "x");
const outside = path.join(tmp, "proj2");
fs.mkdirSync(outside);
fs.writeFileSync(path.join(outside, "x.png"), "x");
const roots = [proj];
const WIN = process.platform === "win32";

describe("normalize", () => {
  it.each([["\\\\server\\share\\a.png"], ["\\\\?\\C:\\a.png"], ["//server/a.png"], [""], ["a\u0000b"]])("rejects %s", (p) => {
    expect(() => normalize(p, proj)).toThrow(PathError);
  });
  it.runIf(WIN).each([["C:\\a.png:ads"], ["C:\\x\\CON.png"], ["C:rel.png"]])("rejects %s (Windows)", (p) => {
    expect(() => normalize(p, proj)).toThrow(PathError);
  });
  it("resolves relative paths against the project", () => {
    expect(normalize("assets/header.png", proj)).toBe(path.join(proj, "assets", "header.png"));
  });
});

describe("inputs", () => {
  it("accepts a relative image in the project", () => {
    expect(checkInput("assets\\header.png", proj, roots)).toBe(path.join(proj, "assets", "header.png"));
  });
  it("rejects traversal outside the project", () => {
    expect(() => checkInput("..\\proj2\\x.png", proj, roots)).toThrow(/outside/);
    expect(() => checkInput(path.join(outside, "x.png"), proj, roots)).toThrow(/outside/);
  });
  it("rejects non-images and missing files", () => {
    expect(() => checkInput("notes.txt", proj, roots)).toThrow(/Only PNG/);
    expect(() => checkInput("missing.png", proj, roots)).toThrow(/not found/);
  });
  it("sibling prefix is not inside", () => {
    expect(isWithin(path.join(outside, "x.png"), proj)).toBe(false);
  });
});

describe("outputs", () => {
  it("adds .png and stays inside roots", () => {
    expect(checkOutput("assets/hero", proj, roots)).toBe(path.join(proj, "assets", "hero.png"));
    expect(() => checkOutput("../escape.png", proj, roots)).toThrow(/outside/);
    expect(() => checkOutput("x.exe", proj, roots)).toThrow(/png/);
  });
  it("says how to allow another folder (#39)", () => {
    expect(() => checkOutput(path.join(outside, "y.png"), proj, roots)).toThrow(/QLS_MCP_ALLOWED_DIRS \(separated by semicolons\)/);
    expect(() => checkInput(path.join(outside, "x.png"), proj, roots)).toThrow(/QLS_MCP_ALLOWED_DIRS/);
  });
  it("sends the allowed folder an output goes to as the job's workspace", () => {
    const extra = [proj, outside];
    expect(jobWorkspace(path.join(proj, "a.png"), proj, extra)).toBe(proj);
    expect(jobWorkspace(path.join(outside, "sub", "a.png"), proj, extra)).toBe(outside);
    expect(jobWorkspace(undefined, proj, extra)).toBe(proj);
  });
});