test: harden background task completion waits (#382)

This commit is contained in:
Cita
2026-04-02 09:49:50 +08:00
committed by GitHub
parent ace5ddd52b
commit bcd758edac
4 changed files with 46 additions and 18 deletions
@@ -0,0 +1,31 @@
import { type BackgroundTask } from '../services/backgroundTaskService.js';
type WaitForBackgroundTaskOptions = {
timeoutMs?: number;
pollMs?: number;
};
const DEFAULT_TIMEOUT_MS = 5_000;
const DEFAULT_POLL_MS = 10;
function isBackgroundTaskTerminal(task: BackgroundTask | null): boolean {
return task?.status === 'succeeded' || task?.status === 'failed';
}
export async function waitForBackgroundTaskToReachTerminalState(
getTask: (taskId: string) => BackgroundTask | null,
taskId: string,
options: WaitForBackgroundTaskOptions = {},
): Promise<BackgroundTask | null> {
const timeoutMs = Math.max(1, Math.trunc(options.timeoutMs ?? DEFAULT_TIMEOUT_MS));
const pollMs = Math.max(0, Math.trunc(options.pollMs ?? DEFAULT_POLL_MS));
const deadlineMs = Date.now() + timeoutMs;
let currentTask = getTask(taskId);
while (!isBackgroundTaskTerminal(currentTask) && Date.now() < deadlineMs) {
await new Promise((resolve) => setTimeout(resolve, pollMs));
currentTask = getTask(taskId);
}
return currentTask;
}