import assert from "node:assert/strict"; import { test } from "node:test"; import { ApiClient, ApiClientError, type FetchResponse } from "../src/index.js"; function response(body: unknown, status = 200): FetchResponse { return { status, ok: status >= 200 && status < 300, json: async () => body }; } test("客户端发送认证头、查询参数和 JSON 请求体", async () => { const calls: Array<{ url: string; init?: { headers?: Record; body?: string } }> = []; const client = new ApiClient({ baseUrl: "http://api.test/", token: "token-1", fetcher: async (url, init) => { calls.push({ url, init }); return response({ count: 2 }); }, }); await client.listAdminBookings({ status: "received", unreadOnly: true, tagId: "tag/1" }); await client.createAdminTag("重点"); assert.equal( calls[0]?.url, "http://api.test/admin/bookings?status=received&unreadOnly=true&tagId=tag%2F1", ); assert.equal(calls[0]?.init?.headers?.authorization, "Bearer token-1"); assert.equal(calls[1]?.init?.body, JSON.stringify({ name: "重点" })); }); test("客户端将统一错误响应转换为 ApiClientError", async () => { const client = new ApiClient({ baseUrl: "http://api.test", fetcher: async () => response( { data: null, error: { code: "HTTP_401", message: "无权限" }, requestId: "req-1" }, 401, ), }); await assert.rejects( () => client.getAdminUnreadCount(), (error: unknown) => { assert.ok(error instanceof ApiClientError); assert.equal(error.status, 401); assert.equal(error.code, "HTTP_401"); assert.equal(error.requestId, "req-1"); return true; }, ); }); test("发布检查与发布操作使用当前品牌的管理接口", async () => { const calls: Array<{ url: string; method?: string; body?: string }> = []; const client = new ApiClient({ baseUrl: "http://api.test", token: "brand-token", fetcher: async (url, init) => { calls.push({ url, method: init?.method, body: init?.body }); return response({ ready: true, checks: [] }); }, }); await client.getAdminReleaseReadiness(); await client.publishAdminRelease("首次发布"); assert.deepEqual(calls, [ { url: "http://api.test/admin/releases/readiness", method: "GET", body: undefined }, { url: "http://api.test/admin/releases/publish", method: "POST", body: JSON.stringify({ note: "首次发布" }), }, ]); }); test("小程序上传配置会提交请求地址", async () => { const calls: Array<{ url: string; method?: string; body?: string }> = []; const client = new ApiClient({ baseUrl: "http://api.test", token: "brand-token", fetcher: async (url, init) => { calls.push({ url, method: init?.method, body: init?.body }); return response({ appId: "wx123", apiBaseUrl: "https://wechat.example.com/api" }); }, }); await client.updateAdminMiniAppSettings({ appId: "wx123", apiBaseUrl: "https://wechat.example.com/api", }); assert.deepEqual(calls, [ { url: "http://api.test/admin/miniapp-releases/settings", method: "PATCH", body: JSON.stringify({ appId: "wx123", apiBaseUrl: "https://wechat.example.com/api", }), }, ]); }); test("模板目录客户端发送分页并支持版本发布和审计", async () => { const calls: Array<{ url: string; method?: string }> = []; const client = new ApiClient({ baseUrl: "http://api.test", token: "template-admin-token", fetcher: async (url, init) => { calls.push({ url, method: init?.method }); return response( url.includes("/audit") ? [] : url.endsWith("/publish") ? { changed: true, version: { id: "version-1", status: "published" } } : { items: [], page: 2, pageSize: 10, total: 0 }, ); }, }); await client.listAdminTemplatePackages({ page: 2, pageSize: 10 }); await client.publishAdminTemplatePackage("version/1"); await client.listAdminTemplatePackageAudit("version/1"); await client.previewAdminTemplatePackage("version/1", "personal & design"); await client.previewAdminTemplatePackage("version/1", "personal & design", "1".repeat(64)); assert.deepEqual(calls, [ { url: "http://api.test/admin/templates?page=2&pageSize=10", method: "GET" }, { url: "http://api.test/admin/templates/version%2F1/publish", method: "POST" }, { url: "http://api.test/admin/templates/version%2F1/audit", method: "GET" }, { url: "http://api.test/admin/templates/version%2F1/preview?industry=personal%20%26%20design", method: "GET", }, { url: "http://api.test/admin/templates/version%2F1/preview?industry=personal%20%26%20design&session=1111111111111111111111111111111111111111111111111111111111111111", method: "GET", }, ]); }); test("模板升级和回滚提交预览版本、checksum 与逐项保留确认", async () => { const calls: Array<{ url: string; method?: string; body?: string }> = []; const client = new ApiClient({ baseUrl: "http://api.test", token: "brand-token", fetcher: async (url, init) => { calls.push({ url, method: init?.method, body: init?.body }); return response({ installation: { templateVersion: "2.0.0" } }); }, }); await client.upgradeAdminTemplate({ templatePackageVersionId: "target-version", expectedContentChecksum: "target-checksum", expectedCurrentVersion: "1.0.0", expectedCurrentPackageVersionId: "source-version", confirm: true, preservedBindings: [{ key: "founder", binding: "brand.config.founder", action: "preserve" }], }); await client.rollbackAdminTemplate({ expectedCurrentVersion: "2.0.0", confirm: true }); assert.deepEqual(calls, [ { url: "http://api.test/admin/workspace/template/upgrade", method: "POST", body: JSON.stringify({ templatePackageVersionId: "target-version", expectedContentChecksum: "target-checksum", expectedCurrentVersion: "1.0.0", expectedCurrentPackageVersionId: "source-version", confirm: true, preservedBindings: [ { key: "founder", binding: "brand.config.founder", action: "preserve" }, ], }), }, { url: "http://api.test/admin/workspace/template/rollback", method: "POST", body: JSON.stringify({ expectedCurrentVersion: "2.0.0", confirm: true }), }, ]); }); test("模板运营失败指标客户端发送查询窗口", async () => { const calls: Array<{ url: string; method?: string }> = []; const client = new ApiClient({ baseUrl: "http://api.test", token: "template-admin-token", fetcher: async (url, init) => { calls.push({ url, method: init?.method }); return response({ since: "2026-10-01", until: "2026-10-17", days: 14, backfilled: false, failures: [], }); }, }); await client.getAdminTemplateOperationFailures({ days: 14 }); assert.deepEqual(calls, [ { url: "http://api.test/admin/template-operations/failures?days=14", method: "GET" }, ]); });