| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- /* 官方插件命令进程的代理端点(技术方案 5.8):CLI 不持有凭证,端点按 session token 认出会话、
- * 只放行 Sites 接口、向 host 现取身份头后转发给 zcode-server。这里用真实的 UDS 和一个本地
- * 假 zcode-server 验证转发细节,凭证解析由 host 侧测试覆盖。 */
- import { stat } from "node:fs/promises";
- import { createServer, request, type IncomingHttpHeaders, type Server } from "node:http";
- import { dirname } from "node:path";
- import { afterEach, assert, describe, expect, it, vi } from "vitest";
- import {
- ZCODE_OFFICIAL_ENDPOINT_ENV_KEY,
- ZCODE_OFFICIAL_ENDPOINT_TOKEN_ENV_KEY,
- } from "@zcode/shared";
- import type { ExecutionPort, ExecutionRequest } from "@zcode/contracts";
- import {
- supportsBackgroundTaskWaiter,
- supportsBashBackgroundLifecycle,
- type BackgroundTaskWaiterExecutionPort,
- type BashBackgroundLifecycleExecutionPort,
- } from "@zcode/core";
- import {
- createOfficialCommandEndpoint,
- withOfficialCommandEnv,
- type OfficialCommandEndpoint,
- } from "../../src/official-command-endpoint/index.js";
- import { createOfficialCommandAuthHeadersPort } from "../../src/zcode-protocol/official-mcp-auth-port.js";
- const IDENTITY = { Authorization: "Bearer zcode-jwt", "X-Bigmodel-Authorization": "Bearer maas" };
- const WORKSPACE_A = { workspacePath: "/w/a" };
- const WORKSPACE_B = { workspacePath: "/w/b", workspaceIdentity: "remote:b" };
- type Seen = { method: string; url: string; headers: IncomingHttpHeaders; body: Buffer };
- async function startUpstream(
- reply: (seen: Seen) => { status: number; body: string } = () => ({
- status: 200,
- body: JSON.stringify({ code: 0, msg: "success", data: { ok: true }, logid: "l" }),
- }),
- ): Promise<{ origin: string; seen: Seen[]; server: Server }> {
- const seen: Seen[] = [];
- const server = createServer((req, res) => {
- const chunks: Buffer[] = [];
- req.on("data", (chunk: Buffer) => chunks.push(chunk));
- req.on("end", () => {
- const entry = {
- body: Buffer.concat(chunks),
- headers: req.headers,
- method: req.method ?? "",
- url: req.url ?? "",
- };
- seen.push(entry);
- const { status, body } = reply(entry);
- res.writeHead(status, { "content-type": "application/json", "set-cookie": "a=b" }).end(body);
- });
- });
- await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
- const address = server.address() as { port: number };
- return { origin: `http://127.0.0.1:${address.port}`, seen, server };
- }
- function call(
- env: Record<string, string>,
- path: string,
- init: {
- method?: string;
- headers?: Record<string, string>;
- body?: Buffer | string;
- token?: string;
- } = {},
- ): Promise<{ status: number; headers: IncomingHttpHeaders; body: string }> {
- return new Promise((resolve, reject) => {
- const body = init.body;
- const req = request(
- {
- socketPath: env[ZCODE_OFFICIAL_ENDPOINT_ENV_KEY],
- path,
- method: init.method ?? "GET",
- headers: {
- Authorization: `Bearer ${init.token ?? env[ZCODE_OFFICIAL_ENDPOINT_TOKEN_ENV_KEY]}`,
- ...(body !== undefined ? { "Content-Length": String(Buffer.byteLength(body)) } : {}),
- ...init.headers,
- },
- },
- (res) => {
- const chunks: Buffer[] = [];
- res.on("data", (chunk: Buffer) => chunks.push(chunk));
- res.on("end", () =>
- resolve({
- body: Buffer.concat(chunks).toString(),
- headers: res.headers,
- status: res.statusCode ?? 0,
- }),
- );
- },
- );
- req.on("error", reject);
- req.end(body);
- });
- }
- describe("official command endpoint", () => {
- const cleanups: Array<() => Promise<void>> = [];
- afterEach(async () => {
- while (cleanups.length > 0) await cleanups.pop()!();
- });
- async function setup(
- options: {
- reply?: Parameters<typeof startUpstream>[0];
- resolveHeaders?: ReturnType<typeof vi.fn>;
- origin?: string;
- } = {},
- ) {
- const upstream = await startUpstream(options.reply);
- cleanups.push(() => new Promise((resolve) => upstream.server.close(() => resolve())));
- const resolveHeaders =
- options.resolveHeaders ?? vi.fn(async () => ({ ok: true as const, headers: IDENTITY }));
- const endpoint: OfficialCommandEndpoint = createOfficialCommandEndpoint({
- appVersion: "3.16.0",
- fetch: globalThis.fetch,
- resolveHeaders,
- resolveZCodeApiOrigin: () => options.origin ?? upstream.origin,
- });
- cleanups.push(() => endpoint.close());
- return { endpoint, resolveHeaders, upstream };
- }
- it("gives each session its own token on one private socket", async () => {
- const { endpoint } = await setup();
- const a = await endpoint.envForSession("session-a", WORKSPACE_A);
- const again = await endpoint.envForSession("session-a", WORKSPACE_A);
- const b = await endpoint.envForSession("session-b", WORKSPACE_B);
- expect(again).toEqual(a);
- expect(b[ZCODE_OFFICIAL_ENDPOINT_ENV_KEY]).toBe(a[ZCODE_OFFICIAL_ENDPOINT_ENV_KEY]);
- expect(b[ZCODE_OFFICIAL_ENDPOINT_TOKEN_ENV_KEY]).not.toBe(
- a[ZCODE_OFFICIAL_ENDPOINT_TOKEN_ENV_KEY],
- );
- if (process.platform !== "win32") {
- const dir = await stat(dirname(a[ZCODE_OFFICIAL_ENDPOINT_ENV_KEY]!));
- expect(dir.mode & 0o077).toBe(0);
- }
- });
- it("asks for identity with the workspace of the session that owns the token", async () => {
- const { endpoint, resolveHeaders } = await setup();
- const a = await endpoint.envForSession("session-a", WORKSPACE_A);
- const b = await endpoint.envForSession("session-b", WORKSPACE_B);
- expect((await call(b, "/api/v1/sites/usage")).status).toBe(200);
- expect((await call(a, "/api/v1/sites/usage")).status).toBe(200);
- expect(resolveHeaders.mock.calls.map(([input]) => input.workspace)).toEqual([
- WORKSPACE_B,
- WORKSPACE_A,
- ]);
- });
- it("rejects unknown tokens and paths outside the allowed prefix without asking for identity", async () => {
- const { endpoint, resolveHeaders, upstream } = await setup();
- const env = await endpoint.envForSession("session-a", WORKSPACE_A);
- const badToken = await call(env, "/api/v1/sites", { token: "forged" });
- expect(badToken.status).toBe(403);
- expect(badToken.headers["x-zcode-official-endpoint-error"]).toBe("invalid");
- for (const path of ["/api/v1/users/me", "/api/v1/sitesx", "/api/v1/sites/../mcp/usage", "/"]) {
- const res = await call(env, path);
- expect(res.status, path).toBe(403);
- expect(JSON.parse(res.body).code).toBe(2007);
- }
- expect(resolveHeaders).not.toHaveBeenCalled();
- expect(upstream.seen).toHaveLength(0);
- });
- it("replaces client credentials with the official identity and streams the body", async () => {
- const { endpoint, resolveHeaders, upstream } = await setup();
- const env = await endpoint.envForSession("session-a", WORKSPACE_A);
- const pkg = Buffer.alloc(3 * 1024 * 1024, 7);
- const res = await call(env, "/api/v1/sites/1/deployments/2/package?x=1", {
- body: pkg,
- headers: {
- "Content-Type": "application/gzip",
- Cookie: "c=d",
- "X-Bigmodel-Authorization": "Bearer forged",
- "X-ZCode-App-Version": "0.0.1",
- "X-Zcode-Plan": "max",
- },
- method: "PUT",
- });
- expect(res.status).toBe(200);
- expect(JSON.parse(res.body)).toMatchObject({ code: 0, data: { ok: true } });
- expect(res.headers["set-cookie"]).toBeUndefined();
- expect(resolveHeaders).toHaveBeenCalledWith(
- expect.objectContaining({ pluginId: "zcode-site", targetOrigin: upstream.origin }),
- );
- const [seen] = upstream.seen;
- expect(seen!.method).toBe("PUT");
- expect(seen!.url).toBe("/api/v1/sites/1/deployments/2/package?x=1");
- expect(seen!.headers.authorization).toBe("Bearer zcode-jwt");
- expect(seen!.headers["x-bigmodel-authorization"]).toBe("Bearer maas");
- expect(seen!.headers["x-zcode-app-version"]).toBe("3.16.0");
- expect(seen!.headers["content-type"]).toBe("application/gzip");
- expect(seen!.headers["content-length"]).toBe(String(pkg.length));
- expect(seen!.headers["transfer-encoding"]).toBeUndefined();
- expect(seen!.headers.cookie).toBeUndefined();
- expect(seen!.headers["x-zcode-plan"]).toBeUndefined();
- expect(seen!.body.equals(pkg)).toBe(true);
- });
- it("answers 401 without identity and 2007 for other identity failures", async () => {
- const resolveHeaders = vi
- .fn()
- .mockResolvedValueOnce({ ok: false, reason: "official_auth_unavailable" })
- .mockResolvedValueOnce({ ok: false, reason: "official_mcp_origin_untrusted" })
- .mockRejectedValueOnce(new Error("host gone"));
- const { endpoint, upstream } = await setup({ resolveHeaders });
- const env = await endpoint.envForSession("session-a", WORKSPACE_A);
- const signedOut = await call(env, "/api/v1/sites/usage");
- expect(signedOut.status).toBe(401);
- expect(signedOut.body).toBe("");
- const untrusted = await call(env, "/api/v1/sites/usage");
- expect(untrusted.status).toBe(502);
- expect(JSON.parse(untrusted.body).code).toBe(2007);
- expect((await call(env, "/api/v1/sites/usage")).status).toBe(401);
- expect(upstream.seen).toHaveLength(0);
- });
- it("marks an unreachable zcode-server as a network error", async () => {
- const { endpoint } = await setup({ origin: "http://127.0.0.1:1" });
- const env = await endpoint.envForSession("session-a", WORKSPACE_A);
- const res = await call(env, "/api/v1/sites/usage");
- expect(res.status).toBe(502);
- expect(res.headers["x-zcode-official-endpoint-error"]).toBe("network");
- });
- it("reports every deployment request to the observer of the token's session, whatever the result", async () => {
- const { endpoint } = await setup({
- reply: (seen) =>
- seen.url === "/api/v1/sites/busy/deployments"
- ? { status: 409, body: JSON.stringify({ code: 3610, msg: "busy", data: null }) }
- : { status: 200, body: JSON.stringify({ code: 0, msg: "success", data: {} }) },
- });
- const observerA = { onSiteDeploymentRequested: vi.fn() };
- const observerB = { onSiteDeploymentRequested: vi.fn() };
- const a = await endpoint.envForSession("session-a", WORKSPACE_A, observerA);
- await endpoint.envForSession("session-b", WORKSPACE_B, observerB);
- await call(a, "/api/v1/sites/s-1/deployments", { body: "{}", method: "POST" });
- const busy = await call(a, "/api/v1/sites/busy/deployments", { body: "{}", method: "POST" });
- expect(busy.status).toBe(409);
- expect(observerA.onSiteDeploymentRequested).toHaveBeenCalledTimes(2);
- expect(observerB.onSiteDeploymentRequested).not.toHaveBeenCalled();
- });
- it("does not report other Sites calls", async () => {
- const { endpoint, upstream } = await setup();
- const observer = { onSiteDeploymentRequested: vi.fn() };
- const env = await endpoint.envForSession("session-a", WORKSPACE_A, observer);
- await call(env, "/api/v1/sites/s-1/deployments");
- await call(env, "/api/v1/sites/s-1/deployments/d-1/package", { body: "x", method: "PUT" });
- await call(env, "/api/v1/sites", { body: "{}", method: "POST" });
- expect(upstream.seen).toHaveLength(3);
- expect(observer.onSiteDeploymentRequested).not.toHaveBeenCalled();
- });
- it("stops serving and removes the socket on close", async () => {
- const { endpoint } = await setup();
- const env = await endpoint.envForSession("session-a", WORKSPACE_A);
- await endpoint.close();
- await expect(call(env, "/api/v1/sites/usage")).rejects.toThrow();
- expect(await endpoint.envForSession("session-a", WORKSPACE_A)).toEqual({});
- if (process.platform !== "win32") {
- await expect(stat(dirname(env[ZCODE_OFFICIAL_ENDPOINT_ENV_KEY]!))).rejects.toThrow();
- }
- });
- });
- describe("withOfficialCommandEnv", () => {
- function fakePort() {
- const run = vi.fn(async (_request: ExecutionRequest) => ({}) as never);
- const start = vi.fn(async (_request: ExecutionRequest) => ({}) as never);
- const getBackgroundTask = vi.fn(async () => undefined);
- return { getBackgroundTask, run, start } satisfies ExecutionPort;
- }
- const request = (env?: ExecutionRequest["env"]): ExecutionRequest =>
- ({ command: { mode: "shell", command: "true" }, ...(env ? { env } : {}) }) as ExecutionRequest;
- it("adds the session endpoint env to foreground and background runs", async () => {
- const port = fakePort();
- const wrapped = withOfficialCommandEnv(port, async () => ({ ZCODE_OFFICIAL_ENDPOINT: "/s" }));
- await wrapped.run(request({ set: { A: "1", ZCODE_OFFICIAL_ENDPOINT: "/forged" } }));
- await wrapped.start!(request());
- await wrapped.run(request({ base: "empty" }));
- expect(port.run.mock.calls[0]![0].env).toEqual({
- set: { A: "1", ZCODE_OFFICIAL_ENDPOINT: "/s" },
- });
- expect(port.start.mock.calls[0]![0].env).toEqual({ set: { ZCODE_OFFICIAL_ENDPOINT: "/s" } });
- expect(port.run.mock.calls[1]![0].env).toEqual({ base: "empty" });
- expect(wrapped.getBackgroundTask).toBeDefined();
- expect(wrapped.cancelBackgroundTask).toBeUndefined();
- expect(supportsBashBackgroundLifecycle(wrapped)).toBe(false);
- expect(supportsBackgroundTaskWaiter(wrapped)).toBe(false);
- });
- it("preserves adb resolution with the original receiver and no endpoint lookup", async () => {
- const resolveEnv = vi.fn(async () => ({}));
- const result = { status: "resolved" as const, path: "/opt/sdk/platform-tools/adb" };
- const port = {
- ...fakePort(),
- resolveAdbExecutable: vi.fn(async function (this: ExecutionPort, _input: unknown) {
- expect(this).toBe(port);
- return result;
- }),
- };
- const wrapped = withOfficialCommandEnv(port, resolveEnv);
- const input = { file: "adb", cwd: "/project", workspaceRoot: "/project" };
- expect(await wrapped.resolveAdbExecutable!(input)).toBe(result);
- expect(port.resolveAdbExecutable).toHaveBeenCalledWith(input);
- expect(resolveEnv).not.toHaveBeenCalled();
- expect(withOfficialCommandEnv(fakePort(), resolveEnv).resolveAdbExecutable).toBeUndefined();
- });
- const lifecycleModes = ["explicit", "auto_on_timeout"] as const;
- const backgroundOutcome = {
- kind: "backgrounded" as const,
- task: { taskId: "task-1", status: "running" as const, startedAt: new Date(0) },
- };
- it.each(lifecycleModes)(
- "preserves the %s Bash lifecycle and injects the session env",
- async (mode) => {
- const runBash = vi
- .fn<BashBackgroundLifecycleExecutionPort["runBashWithBackgroundLifecycle"]>()
- .mockResolvedValue(backgroundOutcome);
- const port = Object.freeze({ ...fakePort(), runBashWithBackgroundLifecycle: runBash });
- const endpointEnv = {
- ZCODE_OFFICIAL_ENDPOINT: "/session",
- ZCODE_OFFICIAL_ENDPOINT_TOKEN: "token",
- };
- const resolveEnv = vi.fn(async () => endpointEnv);
- const wrapped = withOfficialCommandEnv(port, resolveEnv);
- const originalRequest = request({ set: { A: "1", ZCODE_OFFICIAL_ENDPOINT: "/forged" } });
- const lifecycle = { mode };
- const options = { signal: new AbortController().signal, onEvent: vi.fn() };
- assert(
- supportsBashBackgroundLifecycle(wrapped),
- "environment wrapper lost the Bash lifecycle",
- );
- const result = await wrapped.runBashWithBackgroundLifecycle(
- originalRequest,
- lifecycle,
- options,
- );
- expect(result).toBe(backgroundOutcome);
- expect(runBash).toHaveBeenCalledTimes(1);
- expect(runBash.mock.contexts[0]).toBe(port);
- const [forwardedRequest, forwardedLifecycle, forwardedOptions] = runBash.mock.calls[0]!;
- expect(forwardedRequest).toEqual({
- ...originalRequest,
- env: { set: { A: "1", ...endpointEnv } },
- });
- expect(forwardedLifecycle).toBe(lifecycle);
- expect(forwardedOptions).toBe(options);
- expect(originalRequest.env?.set?.ZCODE_OFFICIAL_ENDPOINT).toBe("/forged");
- expect(resolveEnv).toHaveBeenCalledTimes(1);
- expect(port.run).not.toHaveBeenCalled();
- expect(port.start).not.toHaveBeenCalled();
- },
- );
- it.each(lifecycleModes)(
- "preserves a clean environment for the %s Bash lifecycle",
- async (mode) => {
- const runBash = vi
- .fn<BashBackgroundLifecycleExecutionPort["runBashWithBackgroundLifecycle"]>()
- .mockResolvedValue(backgroundOutcome);
- const resolveEnv = vi.fn(async () => ({ ZCODE_OFFICIAL_ENDPOINT: "/session" }));
- const port = { ...fakePort(), runBashWithBackgroundLifecycle: runBash };
- const wrapped = withOfficialCommandEnv(port, resolveEnv);
- const originalRequest = request({ base: "empty" });
- assert(supportsBashBackgroundLifecycle(wrapped));
- await wrapped.runBashWithBackgroundLifecycle(originalRequest, { mode });
- expect(runBash.mock.calls[0]![0]).toBe(originalRequest);
- expect(resolveEnv).not.toHaveBeenCalled();
- },
- );
- it("preserves lifecycle errors when the endpoint env is unavailable", async () => {
- const failure = new Error("execution failed");
- const runBash = vi
- .fn<BashBackgroundLifecycleExecutionPort["runBashWithBackgroundLifecycle"]>()
- .mockRejectedValue(failure);
- const resolveEnv = vi.fn().mockRejectedValue(new Error("listen failed"));
- const warn = vi.fn();
- const port = { ...fakePort(), runBashWithBackgroundLifecycle: runBash };
- const wrapped = withOfficialCommandEnv(port, resolveEnv, { warn });
- const originalRequest = request();
- assert(supportsBashBackgroundLifecycle(wrapped));
- await expect(
- wrapped.runBashWithBackgroundLifecycle(originalRequest, { mode: "explicit" }),
- ).rejects.toBe(failure);
- expect(runBash.mock.calls[0]![0]).toBe(originalRequest);
- expect(warn).toHaveBeenCalledTimes(1);
- });
- it("preserves the background task waiter so tracking does not fall back to polling", async () => {
- const snapshot = {
- taskId: "task-1",
- status: "completed" as const,
- startedAt: new Date(0),
- };
- const waitForBackgroundTask = vi
- .fn<BackgroundTaskWaiterExecutionPort["waitForBackgroundTask"]>()
- .mockResolvedValue(snapshot);
- const port = Object.freeze({ ...fakePort(), waitForBackgroundTask });
- const wrapped = withOfficialCommandEnv(port, async () => ({
- ZCODE_OFFICIAL_ENDPOINT: "/session",
- }));
- const options = { signal: new AbortController().signal };
- assert(supportsBackgroundTaskWaiter(wrapped), "environment wrapper lost the background waiter");
- const result = await wrapped.waitForBackgroundTask("task-1", options);
- expect(result).toBe(snapshot);
- expect(waitForBackgroundTask).toHaveBeenCalledTimes(1);
- expect(waitForBackgroundTask.mock.contexts[0]).toBe(port);
- expect(waitForBackgroundTask.mock.calls[0]).toEqual(["task-1", options]);
- });
- it("still runs the command when the endpoint is unavailable", async () => {
- const port = fakePort();
- const warn = vi.fn();
- const wrapped = withOfficialCommandEnv(
- port,
- async () => {
- throw new Error("listen failed");
- },
- { warn },
- );
- await wrapped.run(request());
- expect(port.run.mock.calls[0]![0].env).toBeUndefined();
- expect(warn).toHaveBeenCalled();
- });
- // 回归:包装层曾只转发 run/start,丢掉 Bash 后台生命周期能力,Desktop 注入端点 env 后
- // run_in_background 的 Bash 全部报 "ExecutionPort does not support the Bash background lifecycle"。
- it("keeps the Bash background lifecycle and adds the endpoint env to it", async () => {
- const runBashWithBackgroundLifecycle = vi.fn(
- async (_request: ExecutionRequest, _lifecycle: { mode: string }) => ({}) as never,
- );
- const port = { ...fakePort(), runBashWithBackgroundLifecycle };
- const wrapped = withOfficialCommandEnv(port, async () => ({ ZCODE_OFFICIAL_ENDPOINT: "/s" }));
- const lifecycle = (wrapped as Partial<typeof port>).runBashWithBackgroundLifecycle;
- expect(lifecycle).toBeTypeOf("function");
- await lifecycle!(request(), { mode: "explicit" });
- expect(runBashWithBackgroundLifecycle.mock.calls[0]![0].env).toEqual({
- set: { ZCODE_OFFICIAL_ENDPOINT: "/s" },
- });
- expect(runBashWithBackgroundLifecycle.mock.calls[0]![1]).toEqual({ mode: "explicit" });
- const plain = withOfficialCommandEnv(fakePort(), async () => ({}));
- expect("runBashWithBackgroundLifecycle" in plain).toBe(false);
- });
- });
- describe("createOfficialCommandAuthHeadersPort", () => {
- const ask = (workspace: { workspacePath: string; workspaceIdentity?: string } = WORKSPACE_A) => ({
- pluginId: "zcode-site",
- targetOrigin: "https://zcode.test",
- workspace,
- });
- it("asks for plugin-command identity with the session workspace and without an mcpKey", async () => {
- const requestClient = vi.fn(async () => ({ ok: true as const, headers: IDENTITY }));
- const port = createOfficialCommandAuthHeadersPort({
- resolveContext: () => ({ requestClient }) as never,
- });
- await expect(port.resolveHeaders(ask())).resolves.toEqual({ ok: true, headers: IDENTITY });
- await port.resolveHeaders(ask(WORKSPACE_B));
- const params = (requestClient.mock.calls as unknown as [string, Record<string, unknown>][]).map(
- ([, value]) => value,
- );
- expect(params[0]).toMatchObject({
- consumer: "plugin-command",
- pluginId: "zcode-site",
- targetOrigin: "https://zcode.test",
- workspace: { workspaceKey: "/w/a", workspacePath: "/w/a" },
- });
- expect(params[0]).not.toHaveProperty("mcpKey");
- expect(params[1]).toMatchObject({
- workspace: { workspaceIdentity: "remote:b", workspaceKey: "remote:b", workspacePath: "/w/b" },
- });
- });
- it("is unavailable before the protocol server is ready or when the host fails", async () => {
- const before = createOfficialCommandAuthHeadersPort({ resolveContext: () => undefined });
- await expect(before.resolveHeaders(ask())).resolves.toEqual({
- ok: false,
- reason: "official_auth_unavailable",
- });
- const warn = vi.fn();
- const failing = createOfficialCommandAuthHeadersPort({
- logger: { warn },
- resolveContext: () =>
- ({
- requestClient: vi
- .fn()
- .mockRejectedValue(new Error("Request workspace is not owned by this CLI")),
- }) as never,
- });
- await expect(failing.resolveHeaders(ask())).resolves.toEqual({
- ok: false,
- reason: "official_auth_unavailable",
- });
- expect(warn).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- error: "Request workspace is not owned by this CLI",
- event: "zcode_protocol.official_command_endpoint.auth_request_failed",
- workspaceKey: "/w/a",
- }),
- );
- });
- });
|