mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 21:14:31 +00:00
refactor(agent): dedupe harness and command workflows
This commit is contained in:
@@ -26,31 +26,48 @@ const urlToString = (url: Request | URL | string): string => {
|
||||
return "url" in url ? url.url : String(url);
|
||||
};
|
||||
|
||||
function createOAuthFetchFn(params: {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
username: string;
|
||||
passthrough?: boolean;
|
||||
}): typeof fetch {
|
||||
return async (input, init) => {
|
||||
const url = urlToString(input);
|
||||
if (url === CHUTES_TOKEN_ENDPOINT) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
access_token: params.accessToken,
|
||||
refresh_token: params.refreshToken,
|
||||
expires_in: 3600,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
if (url === CHUTES_USERINFO_ENDPOINT) {
|
||||
return new Response(JSON.stringify({ username: params.username }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (params.passthrough) {
|
||||
return fetch(input, init);
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
};
|
||||
}
|
||||
|
||||
describe("loginChutes", () => {
|
||||
it("captures local redirect and exchanges code for tokens", async () => {
|
||||
const port = await getFreePort();
|
||||
const redirectUri = `http://127.0.0.1:${port}/oauth-callback`;
|
||||
|
||||
const fetchFn: typeof fetch = async (input, init) => {
|
||||
const url = urlToString(input);
|
||||
if (url === CHUTES_TOKEN_ENDPOINT) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
access_token: "at_local",
|
||||
refresh_token: "rt_local",
|
||||
expires_in: 3600,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
if (url === CHUTES_USERINFO_ENDPOINT) {
|
||||
return new Response(JSON.stringify({ username: "local-user" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
return fetch(input, init);
|
||||
};
|
||||
const fetchFn = createOAuthFetchFn({
|
||||
accessToken: "at_local",
|
||||
refreshToken: "rt_local",
|
||||
username: "local-user",
|
||||
passthrough: true,
|
||||
});
|
||||
|
||||
const onPrompt = vi.fn(async () => {
|
||||
throw new Error("onPrompt should not be called for local callback");
|
||||
@@ -74,26 +91,11 @@ describe("loginChutes", () => {
|
||||
});
|
||||
|
||||
it("supports manual flow with pasted redirect URL", async () => {
|
||||
const fetchFn: typeof fetch = async (input) => {
|
||||
const url = urlToString(input);
|
||||
if (url === CHUTES_TOKEN_ENDPOINT) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
access_token: "at_manual",
|
||||
refresh_token: "rt_manual",
|
||||
expires_in: 3600,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
if (url === CHUTES_USERINFO_ENDPOINT) {
|
||||
return new Response(JSON.stringify({ username: "manual-user" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
};
|
||||
const fetchFn = createOAuthFetchFn({
|
||||
accessToken: "at_manual",
|
||||
refreshToken: "rt_manual",
|
||||
username: "manual-user",
|
||||
});
|
||||
|
||||
let capturedState: string | null = null;
|
||||
const creds = await loginChutes({
|
||||
@@ -121,26 +123,11 @@ describe("loginChutes", () => {
|
||||
});
|
||||
|
||||
it("does not reuse code_verifier as state", async () => {
|
||||
const fetchFn: typeof fetch = async (input) => {
|
||||
const url = urlToString(input);
|
||||
if (url === CHUTES_TOKEN_ENDPOINT) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
access_token: "at_manual",
|
||||
refresh_token: "rt_manual",
|
||||
expires_in: 3600,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
if (url === CHUTES_USERINFO_ENDPOINT) {
|
||||
return new Response(JSON.stringify({ username: "manual-user" }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
};
|
||||
const fetchFn = createOAuthFetchFn({
|
||||
accessToken: "at_manual",
|
||||
refreshToken: "rt_manual",
|
||||
username: "manual-user",
|
||||
});
|
||||
|
||||
const createPkce = () => ({
|
||||
verifier: "verifier_123",
|
||||
|
||||
Reference in New Issue
Block a user