Files
DeepTutor/web/tests/cli-apps-api.test.ts
Bingxi Zhao (Frank) 740ec413a0 release: v1.5.7
Per-account MCP Services and CLI Apps — the two ways a deployment plugs in
tools DeepTutor never shipped — plus the isolation that makes them safe on a
shared install.

- MCP Services: a 45-entry curated store of hosted servers each account
  installs for itself, OAuth 2.1 (PKCE + dynamic client registration) for the
  servers that require it, static keys held by reference and resolved in memory
  at connect time, stdio refused for self-service. Settings no longer
  configures MCP; /settings/mcp redirects to the store.
- CLI Apps: 101 tools snapshotted from the two CLI-Anything registries.
  Installs are admin-only, the registry's install_cmd is parsed into an argv we
  build rather than executed, npm install scripts are forced off, and 66
  first-party entries are pinned to one reviewed commit. Apps land in
  data/cli-apps, one environment each, mounted read-only into the runner.
- Credentials that authorise a person move to data/system, the one branch the
  exec sandbox never mounts: Codex tokens relocate themselves on first use
  (refusing to follow a symlink), per-user MCP configs and keys land beside
  them.
- Grants gain a cli_apps whitelist with the same deny-by-default posture as
  MCP tools; a per-turn registry view keeps one owner's provider tools out of
  the process-global registry and refuses an unauthorised tool at execute, not
  just in the manifest.
- The composer reports what filled this turn's context window, a gutter rail
  jumps back through long conversations, and the workspace collapses to a
  drawer on small screens.
- The composer keeps focus after sending and across tab switches (#720);
  released images carry provenance and SBOM attestations (#722).

README prose, all ten translated READMEs, and the docs-site pages that named
Settings → MCP are updated to match. Release notes:
assets/releases/ver1-5-7.md
2026-07-31 00:25:48 +08:00

164 lines
4.7 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
CliAppError,
getCliApps,
getCliCatalog,
installCliApp,
setCliAppEnabled,
} from "../lib/cli-apps-api";
type Captured = { method: string; url: string; body: unknown };
function stubFetch(
body: unknown,
status = 200,
): { calls: Captured[]; restore: () => void } {
const original = globalThis.fetch;
const calls: Captured[] = [];
(globalThis as { fetch: typeof fetch }).fetch = async (
input: RequestInfo | URL,
init?: RequestInit,
) => {
calls.push({
method: init?.method ?? "GET",
url: String(input),
body: init?.body ? JSON.parse(String(init.body)) : undefined,
});
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
});
};
return {
calls,
restore: () => {
(globalThis as { fetch: typeof fetch }).fetch = original;
},
};
}
test("an app row keeps the two separate facts about availability", async () => {
// `granted` is the administrator's decision, `enabled` the account's own. A
// client that collapsed them could not explain why an app is unavailable.
const stub = stubFetch({
apps: [
{
id: "blender",
display_name: "Blender",
tool_name: "cli_blender",
granted: true,
enabled: false,
trust: "first-party",
pin: "abc123",
},
],
access: { unrestricted: false, exec_denied: false },
catalog_pin: "abc123",
});
try {
const state = await getCliApps();
assert.equal(state.apps[0].granted, true);
assert.equal(state.apps[0].enabled, false);
assert.equal(state.apps[0].tool_name, "cli_blender");
assert.equal(state.catalog_pin, "abc123");
} finally {
stub.restore();
}
});
test("an app the response says nothing about still reads as catalogued", async () => {
// Treating an absent field as "withdrawn upstream" would put a warning badge
// on every working app the moment the field is ever dropped.
const stub = stubFetch({ apps: [{ id: "x" }], access: {} });
try {
assert.equal((await getCliApps()).apps[0].in_catalog, true);
} finally {
stub.restore();
}
});
test("a refused install carries its code and its output", async () => {
// The install log is the only actionable thing about a failed install.
const stub = stubFetch(
{
detail: {
code: "cli.install_failed",
message: "`pip` exited 1",
log: "ERROR: No matching distribution found",
},
},
400,
);
try {
await assert.rejects(
() => installCliApp("blender"),
(error: unknown) => {
assert.ok(error instanceof CliAppError);
assert.equal(error.code, "cli.install_failed");
assert.match(error.log, /No matching distribution/);
assert.match(error.message, /exited 1/);
return true;
},
);
} finally {
stub.restore();
}
});
test("a refusal without a structured detail still surfaces something readable", async () => {
const stub = stubFetch({ detail: "not allowed" }, 403);
try {
await assert.rejects(() => installCliApp("blender"), /not allowed/);
} finally {
stub.restore();
}
});
test("the catalog only sends installable_only when it is being turned off", async () => {
// The backend defaults it to true; sending it every time makes the URL — and
// therefore the cache key — differ for identical requests.
const stub = stubFetch({ entries: [], next_cursor: "", total: 0 });
try {
await getCliCatalog({ q: "blend" });
assert.equal(stub.calls[0].url.includes("installable_only"), false);
await getCliCatalog({ q: "blend", installableOnly: false });
assert.ok(stub.calls[1].url.includes("installable_only=false"));
} finally {
stub.restore();
}
});
test("an app id with URL-significant characters is escaped in the path", async () => {
const stub = stubFetch({ apps: [], access: {} });
try {
await setCliAppEnabled("a b/c", true);
assert.equal(
stub.calls[0].url,
"/api/v1/space/cli-apps/apps/a%20b%2Fc/enabled",
);
assert.deepEqual(stub.calls[0].body, { enabled: true });
} finally {
stub.restore();
}
});
test("a category count of zero is dropped rather than rendered as a chip", async () => {
const stub = stubFetch({
entries: [],
next_cursor: "",
total: 0,
categories: { ai: 3, devops: 0 },
});
try {
const page = await getCliCatalog();
// The client keeps what the backend sent; the component filters. Pinned here
// so a future "helpful" filter in the client cannot double-count.
assert.deepEqual(page.categories, { ai: 3, devops: 0 });
} finally {
stub.restore();
}
});