mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-07 22:09:57 +00:00
test: share startup account lifecycle helpers
This commit is contained in:
72
extensions/test-utils/start-account-lifecycle.ts
Normal file
72
extensions/test-utils/start-account-lifecycle.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import type { ChannelAccountSnapshot, ChannelGatewayContext } from "openclaw/plugin-sdk/test-utils";
|
||||
import { expect, vi } from "vitest";
|
||||
import { createStartAccountContext } from "./start-account-context.js";
|
||||
|
||||
export function startAccountAndTrackLifecycle<TAccount extends { accountId: string }>(params: {
|
||||
startAccount: (ctx: ChannelGatewayContext<TAccount>) => Promise<unknown>;
|
||||
account: TAccount;
|
||||
}) {
|
||||
const patches: ChannelAccountSnapshot[] = [];
|
||||
const abort = new AbortController();
|
||||
const task = params.startAccount(
|
||||
createStartAccountContext({
|
||||
account: params.account,
|
||||
abortSignal: abort.signal,
|
||||
statusPatchSink: (next) => patches.push({ ...next }),
|
||||
}),
|
||||
);
|
||||
let settled = false;
|
||||
void task.then(() => {
|
||||
settled = true;
|
||||
});
|
||||
return {
|
||||
abort,
|
||||
patches,
|
||||
task,
|
||||
isSettled: () => settled,
|
||||
};
|
||||
}
|
||||
|
||||
export async function abortStartedAccount(params: {
|
||||
abort: AbortController;
|
||||
task: Promise<unknown>;
|
||||
}) {
|
||||
params.abort.abort();
|
||||
await params.task;
|
||||
}
|
||||
|
||||
export async function expectPendingUntilAbort(params: {
|
||||
waitForStarted: () => Promise<void>;
|
||||
isSettled: () => boolean;
|
||||
abort: AbortController;
|
||||
task: Promise<unknown>;
|
||||
assertBeforeAbort?: () => void;
|
||||
assertAfterAbort?: () => void;
|
||||
}) {
|
||||
await params.waitForStarted();
|
||||
expect(params.isSettled()).toBe(false);
|
||||
params.assertBeforeAbort?.();
|
||||
await abortStartedAccount({ abort: params.abort, task: params.task });
|
||||
params.assertAfterAbort?.();
|
||||
}
|
||||
|
||||
export async function expectStopPendingUntilAbort(params: {
|
||||
waitForStarted: () => Promise<void>;
|
||||
isSettled: () => boolean;
|
||||
abort: AbortController;
|
||||
task: Promise<unknown>;
|
||||
stop: ReturnType<typeof vi.fn>;
|
||||
}) {
|
||||
await expectPendingUntilAbort({
|
||||
waitForStarted: params.waitForStarted,
|
||||
isSettled: params.isSettled,
|
||||
abort: params.abort,
|
||||
task: params.task,
|
||||
assertBeforeAbort: () => {
|
||||
expect(params.stop).not.toHaveBeenCalled();
|
||||
},
|
||||
assertAfterAbort: () => {
|
||||
expect(params.stop).toHaveBeenCalledOnce();
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user