Keep seal-use probe's stdout to a single JSON document
`runProbe` wrote its progress notes and three separate JSON blobs to stdout, so every consumer parsing the output — `seal-use probe | jq`, the Agent Harness — got prose interleaved with data. The command is also in the help registry, so this was a documented command breaking its own contract. The probe now returns one `ProbeResult` and the command prints it through `printJson`; the `[probe] …` narration moves to stderr, which is the diagnostics channel. The dev-script entry point keeps the same payload and its exit code. Classified read-only too: `probe` reads tenant state and writes nothing, but the allow-list had no entry, so the MCP read-only tool refused it. Its trailing token is an enterprise selector rather than a verb, so the matcher accepts the bare form and bare-plus-selector while still refusing a third token.
This commit is contained in:
1 parent
289ef664da
commit
97bf3301a3
5 files changed
+190
-19
No files matched your search
@@ -0,0 +1,124 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { runCli } from "./cli/runner.js";
|
||||
|
||||
const ENV_KEYS = ["SEAL_USE_ENTERPRISES_DIR", "SEAL_USE_CONFIG_FILE", "SEAL_USE_TOKEN_STORE", "SEAL_DISABLE_BG_UPDATE"] as const;
|
||||
const ORIGINAL_ENV: Record<string, string | undefined> = Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]));
|
||||
const ORIGINAL_FETCH = globalThis.fetch;
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = ORIGINAL_FETCH;
|
||||
for (const key of ENV_KEYS) {
|
||||
if (ORIGINAL_ENV[key] === undefined) delete process.env[key];
|
||||
else process.env[key] = ORIGINAL_ENV[key]!;
|
||||
}
|
||||
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function makeProbeEnv(corpId = "demo-corp"): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-use-probe-test-"));
|
||||
tempDirs.push(dir);
|
||||
writeFileSync(
|
||||
join(dir, `${corpId}.json`),
|
||||
JSON.stringify({
|
||||
id: corpId,
|
||||
name: "Demo Corp",
|
||||
source: { type: "seal", token: "seal-token", sealUrl: `https://${corpId}.sealai.cc` }
|
||||
})
|
||||
);
|
||||
|
||||
process.env.SEAL_USE_ENTERPRISES_DIR = dir;
|
||||
process.env.SEAL_USE_CONFIG_FILE = join(dir, "config.json");
|
||||
process.env.SEAL_USE_TOKEN_STORE = join(dir, "tokens.json");
|
||||
process.env.SEAL_DISABLE_BG_UPDATE = "1";
|
||||
return dir;
|
||||
}
|
||||
|
||||
type Route = (url: string) => Response | undefined;
|
||||
|
||||
/** Routes the four upstream calls a probe makes; unhandled URLs fail loudly. */
|
||||
function mockSealApi(route: Route): void {
|
||||
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
||||
const url = input instanceof Request ? input.url : String(input);
|
||||
const response = route(url);
|
||||
if (!response) throw new Error(`probe test received an unexpected request: ${url}`);
|
||||
return response;
|
||||
}) as typeof fetch;
|
||||
}
|
||||
|
||||
function json(payload: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(payload), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" }
|
||||
});
|
||||
}
|
||||
|
||||
function okApi(): Route {
|
||||
return (url) => {
|
||||
if (url.includes("/api/v1/auth/me")) {
|
||||
return json({
|
||||
user: { id: "user-1", tenantId: "tenant-1", name: "Tester" },
|
||||
tenant: { id: "tenant-1", name: "Demo Tenant" }
|
||||
});
|
||||
}
|
||||
if (url.includes("/api/v1/rules")) {
|
||||
return json({
|
||||
rules: [{ id: "rule-1", description: "差旅报销规则", scope: "travel", strictness: "MUST_FOLLOW" }],
|
||||
hasPendingDeletes: false
|
||||
});
|
||||
}
|
||||
if (url.includes("/api/v1/knowledge/articles")) return json({ articles: [], total: 0 });
|
||||
if (url.includes("/api/v1/agent/ai-approval/config")) return json({ tone: "precise" });
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
|
||||
describe("seal-use probe stream contract", () => {
|
||||
test("writes exactly one JSON document to stdout and narrates on stderr", async () => {
|
||||
makeProbeEnv();
|
||||
mockSealApi(okApi());
|
||||
|
||||
const result = await runCli(["probe", "demo-corp"]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
// The whole contract: if the [probe] prose still reached stdout, this
|
||||
// parse would throw instead of yielding the payload.
|
||||
const payload = JSON.parse(result.stdout) as Record<string, any>;
|
||||
expect(result.stdout).not.toContain("[probe]");
|
||||
|
||||
expect(payload.corp).toEqual({ id: "demo-corp", name: "Demo Corp", sourceType: "seal" });
|
||||
expect(payload.enterprise).toMatchObject({
|
||||
provider: "seal",
|
||||
sealUrl: "https://demo-corp.sealai.cc",
|
||||
tenantName: "Demo Tenant"
|
||||
});
|
||||
expect(payload.summary).toEqual({
|
||||
rules: 1,
|
||||
hasPendingRuleDeletes: false,
|
||||
documents: 0,
|
||||
documentsTotal: 0,
|
||||
stylePreferences: "ok"
|
||||
});
|
||||
|
||||
expect(result.stderr).toContain("[probe] corp=demo-corp source=seal");
|
||||
expect(result.stderr).toContain("[probe] approval context summary");
|
||||
});
|
||||
|
||||
test("reports a failing style-preferences endpoint in the payload instead of throwing", async () => {
|
||||
makeProbeEnv();
|
||||
mockSealApi((url) => {
|
||||
const ok = okApi()(url);
|
||||
return url.includes("/api/v1/agent/ai-approval/config") ? json({ error: "upstream broke" }, 500) : ok;
|
||||
});
|
||||
|
||||
const result = await runCli(["probe", "demo-corp"]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
const payload = JSON.parse(result.stdout) as Record<string, any>;
|
||||
expect(payload.summary.stylePreferences).toContain("error:");
|
||||
expect(payload.summary.rules).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user