refactor(agent): dedupe harness and command workflows

This commit is contained in:
Peter Steinberger
2026-02-16 14:52:09 +00:00
parent 04892ee230
commit f717a13039
204 changed files with 7366 additions and 11540 deletions

View File

@@ -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",