test: 补齐 Admin 会话边界回归

This commit is contained in:
que01 committed 2026-10-19 11:35:41 +08:00
1 parent 8e29932bc6
commit 5d88cb2040
5 files changed
+897 -718

No files matched your search

+56
View File
@@ -45,6 +45,62 @@ test("客户端登出会调用当前会话接口", async () => {
]);
});
test("Admin 会话客户端读取当前会话且登录不返回 token", async () => {
const calls: Array<{ url: string; method?: string; body?: string }> = [];
const client = new ApiClient({
baseUrl: "http://api.test",
fetcher: async (url, init) => {
calls.push({ url, method: init?.method, body: init?.body });
return url.endsWith("/session/current")
? response({
authenticated: true,
displayName: "测试管理员",
mustChangePassword: false,
role: "owner",
platformTemplateRole: "none",
})
: response({
displayName: "测试管理员",
mustChangePassword: false,
role: "owner",
platformTemplateRole: "none",
});
},
});
const current = await client.getAdminSession();
const login = await client.loginAdmin("admin", "password-123");
assert.equal(current.authenticated, true);
assert.equal("token" in login, false);
assert.deepEqual(calls, [
{ url: "http://api.test/admin/session/current", method: "GET", body: undefined },
{
url: "http://api.test/admin/session/login",
method: "POST",
body: JSON.stringify({ username: "admin", password: "password-123" }),
},
]);
});
test("品牌切换客户端不再接收 token", async () => {
const calls: Array<{ url: string; method?: string; body?: string }> = [];
const client = new ApiClient({
baseUrl: "http://api.test",
fetcher: async (url, init) => {
calls.push({ url, method: init?.method, body: init?.body });
return response({ role: "owner", brand: { id: "brand-1", name: "品牌一" } });
},
});
const switched = await client.switchAdminBrand("brand-1");
assert.equal("token" in switched, false);
assert.deepEqual(calls, [
{
url: "http://api.test/admin/brands/brand-1/switch",
method: "POST",
body: undefined,
},
]);
});
test("客户端将统一错误响应转换为 ApiClientError", async () => {
const client = new ApiClient({
baseUrl: "http://api.test",