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.

6.5 KB
Raw Blame
import { afterEach, expect, it, vi } from "vitest";
import { promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
vi.mock("@/lib/infra/connection-service", () => ({ directConnectionValues: async (_provider: string, selector: { connection?: string }) => selector.connection === "batonly-private"
  ? { endpoint: "https://site.batonly.site/mcp", accessToken: "batonly-private-value" }
  : { endpoint: "https://example.test/mcp", accessToken: "private-value" } }));
let root = "";
it("preserves private literal configuration only through the dedicated manager", async () => {
  root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-mcp-literals-"));
  vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
  const file = path.join(root, ".mcp.json"), secret = "fixture-private-literal";
  await fs.writeFile(file, JSON.stringify({ mcpServers: { private: { command: "node", env: { ACCESS_TOKEN: secret } } } }), { mode: 0o600 });
  const { inspectProjectMcp, manageProjectMcp } = await import("./project-mcp-manage");
  const inventory = await inspectProjectMcp(root);
  expect(JSON.stringify(inventory)).not.toContain(secret);
  await manageProjectMcp(root, { action: "upsert", server: "public", revision: inventory.revision, url: "https://example.test/mcp" });
  expect(await fs.readFile(file, "utf8")).toContain(secret);
  const { readFile } = await import("./fs");
  await expect(readFile(file)).rejects.toThrow(/credential/);
  const temporary = file + ".fixture.tmp"; await fs.writeFile(temporary, secret);
  await expect(readFile(temporary)).rejects.toThrow(/credential/);
  await fs.writeFile(file, '{"mcpServers":"' + secret);
  await expect(inspectProjectMcp(root)).rejects.toThrow("invalid MCP manifest JSON");
  await fs.unlink(file); await fs.symlink(temporary, file);
  await expect(inspectProjectMcp(root)).rejects.toThrow("unsafe project MCP manifest");
});
afterEach(async () => { vi.unstubAllEnvs(); if (root) await fs.rm(root, { recursive: true, force: true }); });
it("preserves existing servers, refuses stale revisions and stores only private connection references", async () => {
  root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-mcp-manage-"));
  vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
  const { inspectProjectMcp, manageProjectMcp } = await import("./project-mcp-manage");
  const first = await inspectProjectMcp(root);
  expect(first).toEqual({ revision: "new", servers: [] });
  const saved = await manageProjectMcp(root, { action: "upsert", server: "one", revision: first.revision, url: "https://example.test/mcp", user: "owner", connection: "private" });
  await expect(manageProjectMcp(root, { action: "delete", server: "one", revision: "new" })).rejects.toThrow("revision changed");
  await manageProjectMcp(root, { action: "upsert", server: "two", revision: saved.revision, url: "https://public.test/mcp" });
  const current = await inspectProjectMcp(root);
  expect(current.servers.map(s => s.name)).toEqual(["one", "two"]);
  expect(await fs.readFile(path.join(root, ".mcp.json"), "utf8")).not.toContain("private-value");
  await expect(manageProjectMcp(root, { action: "upsert", server: "bad", revision: current.revision, url: "https://other.test/mcp", user: "owner", connection: "private" })).rejects.toThrow("does not match");
  await manageProjectMcp(root, { action: "delete", server: "one", revision: current.revision });
  expect((await inspectProjectMcp(root)).servers.map(s => s.name)).toEqual(["two"]);
});

it("stores managed SC without a command or copied account and permits removal when SC is absent", async () => {
  root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-managed-sc-"));
  vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
  const { inspectProjectMcp, manageProjectMcp } = await import("./project-mcp-manage");
  const saved = await manageProjectMcp(root, { action: "upsert", server: "si-coder", revision: "new", plugin: "si-coder" });
  expect(JSON.parse(await fs.readFile(path.join(root, ".mcp.json"), "utf8"))).toEqual({ mcpServers: { "si-coder": { plugin: "si-coder", credentialAuthority: "mso" } } });
  expect((await inspectProjectMcp(root)).servers[0]).toMatchObject({ name: "si-coder", transport: "stdio", plugin: "si-coder", credentialAuthority: "mso" });
  await expect(manageProjectMcp(root, { action: "upsert", server: "si-coder", revision: saved.revision, plugin: "si-coder", user: "other" })).rejects.toThrow("no copied account");
  await manageProjectMcp(root, { action: "delete", server: "si-coder", revision: saved.revision });
  expect((await inspectProjectMcp(root)).servers).toHaveLength(0);
});


it("installs Batonly only in the exact project without storing endpoint or token", async () => {
  root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-managed-batonly-"));
  vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
  const { inspectProjectMcp, manageProjectMcp } = await import("./project-mcp-manage");
  const saved = await manageProjectMcp(root, { action: "upsert", server: "batonly", revision: "new", plugin: "batonly", user: "owner", connection: "batonly-private" });
  const raw = await fs.readFile(path.join(root, ".mcp.json"), "utf8");
  expect(JSON.parse(raw)).toEqual({ mcpServers: { batonly: { plugin: "batonly", credentialAuthority: "mso", integration: { user: "owner", connection: "batonly-private" } } } });
  expect(raw).not.toContain("site.batonly.site"); expect(raw).not.toContain("batonly-private-value");
  expect((await inspectProjectMcp(root)).servers[0]).toMatchObject({ name: "batonly", plugin: "batonly", transport: "http", auth: "integration", credentialAuthority: "mso" });
  await manageProjectMcp(root, { action: "delete", server: "batonly", revision: saved.revision });
  expect((await inspectProjectMcp(root)).servers).toEqual([]);
});

it("refuses Batonly installation through a connection for another endpoint", async () => {
  root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-managed-batonly-wrong-"));
  vi.stubEnv("OS_FS_WRITE_ROOTS", root); vi.stubEnv("OS_FS_READ_ROOTS", root); vi.resetModules();
  const { manageProjectMcp } = await import("./project-mcp-manage");
  await expect(manageProjectMcp(root, { action: "upsert", server: "batonly", revision: "new", plugin: "batonly", user: "owner", connection: "private" })).rejects.toThrow("reviewed Batonly endpoint");
  await expect(fs.stat(path.join(root, ".mcp.json"))).rejects.toMatchObject({ code: "ENOENT" });
});