Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

5.9 KB
Raw Blame
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { execFileSync } from "node:child_process";
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { detectProjectConvex, projectGitDiff, projectGitEdits, projectGitSnapshot, readProjectKnowledge } from "./project-experience";

const root = await mkdtemp(path.join(os.tmpdir(), "mso-project-exp-"));
vi.stubEnv("OS_FS_READ_ROOTS", root);
const project = path.join(root, "app");

beforeAll(async () => {
  await mkdir(project, { recursive: true });
  await writeFile(path.join(project, "package.json"), JSON.stringify({ name: "fixture", version: "1.0.0", dependencies: { convex: "1.0.0" } }));
  await mkdir(path.join(project, "convex"));
  execFileSync("git", ["init", "-b", "main"], { cwd: project });
  execFileSync("git", ["config", "user.email", "[email protected]"], { cwd: project });
  execFileSync("git", ["config", "user.name", "MSO Test"], { cwd: project });
  await writeFile(path.join(project, "a.txt"), "one\n");
  execFileSync("git", ["add", "."], { cwd: project });
  execFileSync("git", ["commit", "-m", "initial"], { cwd: project });
  await writeFile(path.join(project, "a.txt"), "one\ntwo\n");
  execFileSync("git", ["add", "a.txt"], { cwd: project });
  execFileSync("git", ["commit", "-m", "second"], { cwd: project });
  await writeFile(path.join(project, "a.txt"), "one\ntwo\nworking\n");
});

afterAll(async () => { vi.unstubAllEnvs(); await rm(root, { recursive: true, force: true }); });

describe("project experience host helpers", () => {
  it("returns bounded Git snapshot, history and working-tree diff", async () => {
    const snapshot = await projectGitSnapshot(project);
    expect(snapshot).toMatchObject({ available: true, clean: false });
    expect(snapshot.branch).toContain("main");
    expect(snapshot.changes).toContain(" M a.txt");

    const edits = await projectGitEdits(project, { limit: 1 });
    expect(edits.edits).toHaveLength(1);
    expect(edits.edits[0].subject).toBe("second");
    expect(edits.pagination.hasMore).toBe(true);
    const page2 = await projectGitEdits(project, { limit: 5, cursor: edits.pagination.nextCursor });
    expect(page2.edits.some((row) => row.subject === "initial")).toBe(true);

    const diff = await projectGitDiff(project);
    expect(diff.summary).toMatchObject({ files: 1, additions: 1, deletions: 0 });
    expect(diff.unifiedDiff).toContain("+working");
  });

  it("keeps project knowledge bounded and refuses symlinks", async () => {
    await mkdir(path.join(project, ".mso"), { recursive: true });
    await writeFile(path.join(project, ".mso", "KNOWLEDGE.md"), "# Project knowledge\nUse Convex.\n");
    const knowledge = await readProjectKnowledge(project);
    expect(knowledge).toMatchObject({ exists: true, path: ".mso/KNOWLEDGE.md" });
    expect(knowledge.sha256).toMatch(/^[a-f0-9]{64}$/);

    const target = path.join(root, "outside.md");
    await writeFile(target, "outside");
    await rm(path.join(project, ".mso", "KNOWLEDGE.md"));
    await symlink(target, path.join(project, ".mso", "KNOWLEDGE.md"));
    await expect(readProjectKnowledge(project)).rejects.toThrow(/regular non-symlink/i);
  });

  it("suppresses protected Git patches in staged and historical diffs and redacts ordinary-file credentials", async () => {
    const repo = path.join(root,"private-diff");
    await mkdir(repo);
    const git = (args:string[]) => execFileSync("git",args,{cwd:repo,encoding:"utf8"}).trim();
    git(["init","-b","main"]); git(["config","user.email","[email protected]"]); git(["config","user.name","MSO Test"]);
    await writeFile(path.join(repo,"public.txt"),"public before\n");
    git(["add","."]); git(["commit","-m","before"]);
    const names = [".mcp.json",".netrc",".mcp.json.fixture.tmp",".env.local","private.pem","odd\tdir/.netrc"];
    for (const name of names) {
      await mkdir(path.dirname(path.join(repo,name)),{recursive:true});
      await writeFile(path.join(repo,name),"private-fixture-value\n");
    }
    await writeFile(path.join(repo,"public.txt"),"public after\nAPI_TOKEN=synthetic-private-token\n");
    git(["add","."]);
    const staged = await projectGitDiff(repo,{staged:true});
    expect(staged.unifiedDiff).not.toContain("private-fixture-value");
    expect(staged.unifiedDiff).not.toContain("synthetic-private-token");
    expect(staged.unifiedDiff).toContain("+public after");
    expect(staged.files.find(row=>row.file===".mcp.json")).toMatchObject({redacted:true});
    git(["commit","-m","private changes"]);
    const sha = git(["rev-parse","HEAD"]);
    const historic = await projectGitDiff(repo,{sha});
    expect(historic.unifiedDiff).toBe(staged.unifiedDiff);
    git(["mv",".mcp.json","safe-name.json"]);
    const renamed = await projectGitDiff(repo,{staged:true});
    expect(renamed.files.find(row=>row.file==="safe-name.json")).toMatchObject({redacted:true,previousFile:".mcp.json"});
    expect(renamed.unifiedDiff).not.toContain("private-fixture-value");
  });

  it("refuses a symlinked knowledge directory inside readable roots", async () => {
    const outside = path.join(root, "other-project");
    await mkdir(outside);
    await writeFile(path.join(outside, "KNOWLEDGE.md"), "other project's knowledge");
    await rm(path.join(project, ".mso"), { recursive: true });
    await symlink(outside, path.join(project, ".mso"));
    await expect(readProjectKnowledge(project)).rejects.toThrow(/real directory/i);
  });

  it("detects Convex without returning deployment values", async () => {
    await writeFile(path.join(project, ".env.local"), "CONVEX_SELF_HOSTED_URL=https://secret.example\n");
    const detected = await detectProjectConvex(project);
    expect(detected).toMatchObject({ detected: true, dependency: true, convexDirectory: true, configured: true, mode: "self-hosted" });
    expect(JSON.stringify(detected)).not.toContain("secret.example");
  });
});