mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:31:23 +00:00
test: dedupe lifecycle oauth and prompt-limit fixtures
This commit is contained in:
@@ -14,6 +14,27 @@ const urlToString = (url: Request | URL | string): string => {
|
||||
return "url" in url ? url.url : String(url);
|
||||
};
|
||||
|
||||
function createStoredCredential(
|
||||
now: number,
|
||||
): Parameters<typeof refreshChutesTokens>[0]["credential"] {
|
||||
return {
|
||||
access: "at_old",
|
||||
refresh: "rt_old",
|
||||
expires: now - 10_000,
|
||||
email: "fred",
|
||||
clientId: "cid_test",
|
||||
} as unknown as Parameters<typeof refreshChutesTokens>[0]["credential"];
|
||||
}
|
||||
|
||||
function expectRefreshedCredential(
|
||||
refreshed: Awaited<ReturnType<typeof refreshChutesTokens>>,
|
||||
now: number,
|
||||
) {
|
||||
expect(refreshed.access).toBe("at_new");
|
||||
expect(refreshed.refresh).toBe("rt_old");
|
||||
expect(refreshed.expires).toBe(now + 1800 * 1000 - 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
describe("chutes-oauth", () => {
|
||||
it("exchanges code for tokens and stores username as email", async () => {
|
||||
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
@@ -87,20 +108,12 @@ describe("chutes-oauth", () => {
|
||||
|
||||
const now = 2_000_000;
|
||||
const refreshed = await refreshChutesTokens({
|
||||
credential: {
|
||||
access: "at_old",
|
||||
refresh: "rt_old",
|
||||
expires: now - 10_000,
|
||||
email: "fred",
|
||||
clientId: "cid_test",
|
||||
} as unknown as Parameters<typeof refreshChutesTokens>[0]["credential"],
|
||||
credential: createStoredCredential(now),
|
||||
fetchFn,
|
||||
now,
|
||||
});
|
||||
|
||||
expect(refreshed.access).toBe("at_new");
|
||||
expect(refreshed.refresh).toBe("rt_old");
|
||||
expect(refreshed.expires).toBe(now + 1800 * 1000 - 5 * 60 * 1000);
|
||||
expectRefreshedCredential(refreshed, now);
|
||||
});
|
||||
|
||||
it("refreshes tokens and ignores empty refresh_token values", async () => {
|
||||
@@ -122,19 +135,11 @@ describe("chutes-oauth", () => {
|
||||
|
||||
const now = 3_000_000;
|
||||
const refreshed = await refreshChutesTokens({
|
||||
credential: {
|
||||
access: "at_old",
|
||||
refresh: "rt_old",
|
||||
expires: now - 10_000,
|
||||
email: "fred",
|
||||
clientId: "cid_test",
|
||||
} as unknown as Parameters<typeof refreshChutesTokens>[0]["credential"],
|
||||
credential: createStoredCredential(now),
|
||||
fetchFn,
|
||||
now,
|
||||
});
|
||||
|
||||
expect(refreshed.access).toBe("at_new");
|
||||
expect(refreshed.refresh).toBe("rt_old");
|
||||
expect(refreshed.expires).toBe(now + 1800 * 1000 - 5 * 60 * 1000);
|
||||
expectRefreshedCredential(refreshed, now);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -32,6 +32,20 @@ async function getSessionsSpawnTool(opts: CreateOpenClawToolsOpts) {
|
||||
type GatewayRequest = { method?: string; params?: unknown };
|
||||
type AgentWaitCall = { runId?: string; timeoutMs?: number };
|
||||
|
||||
function buildDiscordCleanupHooks(onDelete: (key: string | undefined) => void) {
|
||||
return {
|
||||
onAgentSubagentSpawn: (params: unknown) => {
|
||||
const rec = params as { channel?: string; timeout?: number } | undefined;
|
||||
expect(rec?.channel).toBe("discord");
|
||||
expect(rec?.timeout).toBe(1);
|
||||
},
|
||||
onSessionsDelete: (params: unknown) => {
|
||||
const rec = params as { key?: string } | undefined;
|
||||
onDelete(rec?.key);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function setupSessionsSpawnGatewayMock(opts: {
|
||||
includeSessionsList?: boolean;
|
||||
includeChatHistory?: boolean;
|
||||
@@ -216,15 +230,9 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
||||
callGatewayMock.mockReset();
|
||||
let deletedKey: string | undefined;
|
||||
const ctx = setupSessionsSpawnGatewayMock({
|
||||
onAgentSubagentSpawn: (params) => {
|
||||
const rec = params as { channel?: string; timeout?: number } | undefined;
|
||||
expect(rec?.channel).toBe("discord");
|
||||
expect(rec?.timeout).toBe(1);
|
||||
},
|
||||
onSessionsDelete: (params) => {
|
||||
const rec = params as { key?: string } | undefined;
|
||||
deletedKey = rec?.key;
|
||||
},
|
||||
...buildDiscordCleanupHooks((key) => {
|
||||
deletedKey = key;
|
||||
}),
|
||||
});
|
||||
|
||||
const tool = await getSessionsSpawnTool({
|
||||
@@ -309,15 +317,9 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
||||
let deletedKey: string | undefined;
|
||||
const ctx = setupSessionsSpawnGatewayMock({
|
||||
includeChatHistory: true,
|
||||
onAgentSubagentSpawn: (params) => {
|
||||
const rec = params as { channel?: string; timeout?: number } | undefined;
|
||||
expect(rec?.channel).toBe("discord");
|
||||
expect(rec?.timeout).toBe(1);
|
||||
},
|
||||
onSessionsDelete: (params) => {
|
||||
const rec = params as { key?: string } | undefined;
|
||||
deletedKey = rec?.key;
|
||||
},
|
||||
...buildDiscordCleanupHooks((key) => {
|
||||
deletedKey = key;
|
||||
}),
|
||||
agentWaitResult: { status: "ok", startedAt: 3000, endedAt: 4000 },
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user