Spaces:
Paused
Paused
File size: 8,733 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | /**
* Tests for coding plan key resolver.
*/
import { describe, it, expect } from "bun:test";
import { KeyResolver } from "./resolver.js";
function bizResponse(data: unknown): Response {
return new Response(JSON.stringify({ code: 0, data }), {
status: 200,
headers: { "content-type": "application/json" },
});
}
function mockFetch(responses: Record<string, (body?: string) => Response>): typeof fetch {
return (async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const url = typeof input === "string" ? input : input.toString();
const body = init?.body as string | undefined;
for (const [pattern, handler] of Object.entries(responses)) {
if (url.includes(pattern)) return handler(body);
}
return new Response("not found", { status: 404 });
}) as typeof fetch;
}
describe("KeyResolver", () => {
it("resolveZaiBizToken THROWS on shape drift (access_token missing) instead of returning undefined (CL-06)", async () => {
const fetchImpl = mockFetch({
"/auth/z/login": () => new Response(JSON.stringify({ renamed_token: "biz_token_123" }), {
status: 200, headers: { "content-type": "application/json" },
}),
});
const resolver = new KeyResolver(fetchImpl);
await expect(resolver.resolveZaiBizToken("access_abc")).rejects.toThrow(/unexpected shape/);
});
it("resolveZaiBizToken exchanges access token for biz token", async () => {
const fetchImpl = mockFetch({
"/auth/z/login": () => new Response(JSON.stringify({
access_token: "biz_token_123",
}), { status: 200, headers: { "content-type": "application/json" } }),
});
const resolver = new KeyResolver(fetchImpl);
const bizToken = await resolver.resolveZaiBizToken("access_abc");
expect(bizToken).toBe("biz_token_123");
});
it("resolveCustomerInfo picks default org using bundle field names", async () => {
const fetchImpl = mockFetch({
"getCustomerInfo": () => bizResponse({
organizations: [
{ organizationId: "org1", organizationName: "Some Org", projects: [] },
{ organizationId: "org2", organizationName: "默认机构", projects: [
{ projectId: "proj1", projectName: "默认项目" },
{ projectId: "proj2", projectName: "Other" },
]},
],
}),
});
const resolver = new KeyResolver(fetchImpl);
const { orgId, projectId } = await resolver.resolveCustomerInfo("https://api.z.ai", "Bearer tok");
expect(orgId).toBe("org2");
expect(projectId).toBe("proj1");
});
it("resolveCustomerInfo falls back to first org when no default", async () => {
const fetchImpl = mockFetch({
"getCustomerInfo": () => bizResponse({
organizations: [
{ organizationId: "orgA", organizationName: "Org A", projects: [{ projectId: "projA", projectName: "Proj A" }] },
],
}),
});
const resolver = new KeyResolver(fetchImpl);
const { orgId, projectId } = await resolver.resolveCustomerInfo("https://api.z.ai", "Bearer tok");
expect(orgId).toBe("orgA");
expect(projectId).toBe("projA");
});
it("resolveCustomerInfo throws when no orgs", async () => {
const fetchImpl = mockFetch({
"getCustomerInfo": () => bizResponse({ organizations: [] }),
});
const resolver = new KeyResolver(fetchImpl);
expect(resolver.resolveCustomerInfo("https://api.z.ai", "Bearer tok")).rejects.toThrow(/No organizations/);
});
it("findOrCreateApiKey finds existing key named zcode-api-key", async () => {
const fetchImpl = mockFetch({
"api_keys": () => bizResponse([
{ name: "other-key", apiKey: "xxx" },
{ name: "zcode-api-key", apiKey: "existingApiKey" },
]),
});
const resolver = new KeyResolver(fetchImpl);
const result = await resolver.findOrCreateApiKey("https://api.z.ai", "Bearer tok", "org1", "proj1");
expect(result.apiKey).toBe("existingApiKey");
});
it("findOrCreateApiKey creates new key when not found", async () => {
let createdKey = false;
const fetchImpl = mockFetch({
"api_keys": (body) => {
if (body) {
createdKey = true;
return bizResponse({ apiKey: "newApiKey123" });
}
return bizResponse([]);
},
});
const resolver = new KeyResolver(fetchImpl);
const result = await resolver.findOrCreateApiKey("https://api.z.ai", "Bearer tok", "org1", "proj1");
expect(createdKey).toBe(true);
expect(result.apiKey).toBe("newApiKey123");
});
it("findOrCreateApiKey THROWS when the create response loses apiKey (shape drift, CL-06)", async () => {
const fetchImpl = mockFetch({
"api_keys": (body) => {
if (body) return bizResponse({ key: "renamed-field" }); // drift: apiKey → key
return bizResponse([]);
},
});
const resolver = new KeyResolver(fetchImpl);
await expect(resolver.findOrCreateApiKey("https://api.z.ai", "Bearer tok", "org1", "proj1"))
.rejects.toThrow(/unexpected shape/);
});
it("findOrCreateApiKey falls through to create when the listed key entry is malformed (CL-06)", async () => {
let created = false;
const fetchImpl = mockFetch({
"api_keys": (body) => {
if (body) {
created = true;
return bizResponse({ apiKey: "freshlyCreated" });
}
return bizResponse([{ name: "zcode-api-key", apiKey: "" }]);
},
});
const resolver = new KeyResolver(fetchImpl);
const result = await resolver.findOrCreateApiKey("https://api.z.ai", "Bearer tok", "org1", "proj1");
expect(created).toBe(true);
expect(result.apiKey).toBe("freshlyCreated");
});
it("getSecretKey retrieves secret via apiKey value", async () => {
const fetchImpl = mockFetch({
"copy/": () => bizResponse({ secretKey: "theSecretKey" }),
});
const resolver = new KeyResolver(fetchImpl);
const secret = await resolver.getSecretKey("https://api.z.ai", "Bearer tok", "org1", "proj1", "myApiKey123");
expect(secret).toBe("theSecretKey");
});
it("resolveCodingPlanCredential returns Z.AI credential with secret", async () => {
const fetchImpl = mockFetch({
"/auth/z/login": () => new Response(JSON.stringify({ access_token: "bizTok" }), {
status: 200, headers: { "content-type": "application/json" },
}),
"getCustomerInfo": () => bizResponse({
organizations: [{ organizationId: "o1", organizationName: "默认机构", projects: [{ projectId: "p1", projectName: "默认项目" }] }],
}),
"api_keys/copy": () => bizResponse({ secretKey: "mySecret" }),
"api_keys": (body) => {
if (body) return bizResponse({ apiKey: "myApiKey" });
return bizResponse([]);
},
});
const resolver = new KeyResolver(fetchImpl);
const cred = await resolver.resolveCodingPlanCredential("accessTok", "zai");
expect(cred.apiKey).toBe("myApiKey");
expect(cred.secret).toBe("mySecret");
expect(cred.provider).toBe("zai");
});
it("resolveCodingPlanCredential zai REJECTS on missing secretKey (3.12.3 requireSecretKey)", async () => {
const fetchImpl = mockFetch({
"/auth/z/login": () => new Response(JSON.stringify({ access_token: "bizTok" }), {
status: 200, headers: { "content-type": "application/json" },
}),
"getCustomerInfo": () => bizResponse({
organizations: [{ organizationId: "o1", organizationName: "默认机构", projects: [{ projectId: "p1", projectName: "默认项目" }] }],
}),
"api_keys/copy": () => bizResponse({}),
"api_keys": (body) => {
if (body) return bizResponse({ apiKey: "myApiKey" });
return bizResponse([]);
},
});
const resolver = new KeyResolver(fetchImpl);
await expect(resolver.resolveCodingPlanCredential("accessTok", "zai"))
.rejects.toThrow(/missing secretKey/);
});
it("resolveCodingPlanCredential bigmodel tolerates a missing secretKey (bundle keeps requireSecretKey unset)", async () => {
const fetchImpl = mockFetch({
"getCustomerInfo": () => bizResponse({
organizations: [{ organizationId: "o1", organizationName: "默认机构", projects: [{ projectId: "p1", projectName: "默认项目" }] }],
}),
"api_keys/copy": () => bizResponse({}),
"api_keys": (body) => {
if (body) return bizResponse({ apiKey: "bmKey" });
return bizResponse([]);
},
});
const resolver = new KeyResolver(fetchImpl);
const cred = await resolver.resolveCodingPlanCredential("accessTok", "bigmodel");
expect(cred.apiKey).toBe("bmKey");
expect(cred.secret).toBeUndefined();
expect(cred.provider).toBe("bigmodel");
});
});
|