Spaces:
Paused
Paused
File size: 3,851 Bytes
488aedf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | /**
* Tests for unified session context policy.
*/
import { describe, expect, it } from "bun:test";
import type { ProxyConfig } from "../config/types.js";
import { createClientSessionResolver } from "./client-session.js";
import { resolveSessionContext, sessionIdForHeader, shouldForwardSessionId } from "./session-context.js";
const BASE_CONFIG: ProxyConfig = {
server: { port: 8080, host: "0.0.0.0" },
auth: {},
provider: "zai",
plan: "start-plan",
providers: {
zai: { anthropicBase: "https://api.z.ai/api/anthropic", openaiBase: "https://api.z.ai/api/coding/paas/v4" },
bigmodel: { anthropicBase: "https://open.bigmodel.cn/api/anthropic", openaiBase: "https://open.bigmodel.cn/api/coding/paas/v4" },
},
defaultModel: "glm-4.6",
models: ["glm-4.6"],
identity: {
appVersion: "3.3.3",
sourceTitle: "zcode",
refererOrigin: "https://zcode.z.ai",
},
clientIdentity: { mode: "observe", ttlSeconds: 900, maxSessions: 1024 },
responses: { enabled: true, storeMaxEntries: 1000, storeTtlMs: 86400000 },
endpointRouting: { enabled: false, origin: "https://zcode.z.ai" },
clientSigning: { enabled: false }, highspeed: { enabled: false, origin: "https://zcode.z.ai", models: [] },
mcp: { enabled: true, webSearch: true, webReader: false, zread: false, gateway: { enabled: true, upstreamOrigin: "https://zcode.chatglm.site" } },
async: { enabled: false, origin: "https://zcode.z.ai", pollIntervalMs: 5000, keepAliveIntervalMs: 3000, maxWaitMs: 0, maxRetries: 3, settleTimeoutMs: 8000, controlTimeoutMs: 15000, defaultModel: "" },
claim: { enabled: false, auto: true, origin: "https://zcode.z.ai", pollIntervalMs: 300000, cooldownMs: 600000, planId: "" },
logging: { level: "info" },
};
describe("session context", () => {
it("uses the lineage resolver for session inference (both plan callers share it)", () => {
const resolver = createClientSessionResolver();
const body = JSON.stringify({ model: "glm-4.6", messages: [{ role: "user", content: "Hi" }] });
const req = new Request("http://localhost:8080/v1/chat/completions", {
method: "POST",
headers: { "content-type": "application/json" },
body,
});
const session = resolveSessionContext({
clientReq: req,
body,
upstreamFormat: "openai",
model: "glm-4.6",
config: BASE_CONFIG,
resolver,
});
expect(session?.source).toBe("lineage");
expect(session?.action).toBe("observe");
expect(session?.upstreamSessionId).toBeTruthy();
});
it("does not resolve any session when clientIdentity mode is off", () => {
const body = JSON.stringify({ model: "glm-4.6", messages: [{ role: "user", content: "Hi" }] });
const session = resolveSessionContext({
clientReq: new Request("http://localhost:8080/v1/chat/completions", { method: "POST", body }),
body,
upstreamFormat: "openai",
model: "glm-4.6",
config: { ...BASE_CONFIG, clientIdentity: { ...BASE_CONFIG.clientIdentity, mode: "off" } },
});
expect(session).toBeUndefined();
});
it("forwards session IDs only for explicit or enforce contexts", () => {
expect(shouldForwardSessionId({
source: "lineage",
action: "observe",
upstreamSessionId: "11111111-1111-4111-8111-111111111111",
})).toBe(false);
expect(sessionIdForHeader({
source: "lineage",
action: "observe",
upstreamSessionId: "11111111-1111-4111-8111-111111111111",
})).toBeUndefined();
expect(sessionIdForHeader({
source: "lineage",
action: "enforce",
upstreamSessionId: "11111111-1111-4111-8111-111111111111",
})).toBe("11111111-1111-4111-8111-111111111111");
expect(sessionIdForHeader({
source: "explicit",
action: "observe",
upstreamSessionId: "sess_thread_1",
})).toBe("sess_thread_1");
});
});
|