mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:28:38 +00:00
refactor(agents): dedupe plugin hooks and test helpers
This commit is contained in:
@@ -9,8 +9,22 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("refreshQwenPortalCredentials", () => {
|
||||
const expiredCredentials = () => ({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
expires: Date.now() - 1000,
|
||||
});
|
||||
|
||||
const runRefresh = async () => await refreshQwenPortalCredentials(expiredCredentials());
|
||||
|
||||
const stubFetchResponse = (response: unknown) => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue(response);
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
return fetchSpy;
|
||||
};
|
||||
|
||||
it("refreshes tokens with a new access token", async () => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue({
|
||||
const fetchSpy = stubFetchResponse({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
@@ -19,13 +33,8 @@ describe("refreshQwenPortalCredentials", () => {
|
||||
expires_in: 3600,
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
const result = await refreshQwenPortalCredentials({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
expires: Date.now() - 1000,
|
||||
});
|
||||
const result = await runRefresh();
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
"https://chat.qwen.ai/api/v1/oauth2/token",
|
||||
@@ -39,7 +48,7 @@ describe("refreshQwenPortalCredentials", () => {
|
||||
});
|
||||
|
||||
it("keeps refresh token when refresh response omits it", async () => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue({
|
||||
stubFetchResponse({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
@@ -47,19 +56,14 @@ describe("refreshQwenPortalCredentials", () => {
|
||||
expires_in: 1800,
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
const result = await refreshQwenPortalCredentials({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
expires: Date.now() - 1000,
|
||||
});
|
||||
const result = await runRefresh();
|
||||
|
||||
expect(result.refresh).toBe("old-refresh");
|
||||
});
|
||||
|
||||
it("keeps refresh token when response sends an empty refresh token", async () => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue({
|
||||
stubFetchResponse({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
@@ -68,19 +72,14 @@ describe("refreshQwenPortalCredentials", () => {
|
||||
expires_in: 1800,
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
const result = await refreshQwenPortalCredentials({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
expires: Date.now() - 1000,
|
||||
});
|
||||
const result = await runRefresh();
|
||||
|
||||
expect(result.refresh).toBe("old-refresh");
|
||||
});
|
||||
|
||||
it("errors when refresh response has invalid expires_in", async () => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue({
|
||||
stubFetchResponse({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
@@ -89,31 +88,53 @@ describe("refreshQwenPortalCredentials", () => {
|
||||
expires_in: 0,
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
await expect(
|
||||
refreshQwenPortalCredentials({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
expires: Date.now() - 1000,
|
||||
}),
|
||||
).rejects.toThrow("Qwen OAuth refresh response missing or invalid expires_in");
|
||||
await expect(runRefresh()).rejects.toThrow(
|
||||
"Qwen OAuth refresh response missing or invalid expires_in",
|
||||
);
|
||||
});
|
||||
|
||||
it("errors when refresh token is invalid", async () => {
|
||||
const fetchSpy = vi.fn().mockResolvedValue({
|
||||
stubFetchResponse({
|
||||
ok: false,
|
||||
status: 400,
|
||||
text: async () => "invalid_grant",
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
|
||||
await expect(runRefresh()).rejects.toThrow("Qwen OAuth refresh token expired or invalid");
|
||||
});
|
||||
|
||||
it("errors when refresh token is missing before any request", async () => {
|
||||
await expect(
|
||||
refreshQwenPortalCredentials({
|
||||
access: "old-access",
|
||||
refresh: "old-refresh",
|
||||
refresh: " ",
|
||||
expires: Date.now() - 1000,
|
||||
}),
|
||||
).rejects.toThrow("Qwen OAuth refresh token expired or invalid");
|
||||
).rejects.toThrow("Qwen OAuth refresh token missing");
|
||||
});
|
||||
|
||||
it("errors when refresh response omits access token", async () => {
|
||||
stubFetchResponse({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
refresh_token: "new-refresh",
|
||||
expires_in: 1800,
|
||||
}),
|
||||
});
|
||||
|
||||
await expect(runRefresh()).rejects.toThrow("Qwen OAuth refresh response missing access token");
|
||||
});
|
||||
|
||||
it("errors with server payload text for non-400 status", async () => {
|
||||
stubFetchResponse({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: "Server Error",
|
||||
text: async () => "gateway down",
|
||||
});
|
||||
|
||||
await expect(runRefresh()).rejects.toThrow("Qwen OAuth refresh failed: gateway down");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user