diff --git a/src/discord/monitor/native-command.model-picker.test.ts b/src/discord/monitor/native-command.model-picker.test.ts index c913379a8b6..59598b39f01 100644 --- a/src/discord/monitor/native-command.model-picker.test.ts +++ b/src/discord/monitor/native-command.model-picker.test.ts @@ -44,6 +44,21 @@ function createModelsProviderData(entries: Record): ModelsProv return createBaseModelsProviderData(entries, { defaultProviderOrder: "sorted" }); } +async function waitForCondition( + predicate: () => boolean, + opts?: { attempts?: number; delayMs?: number }, +): Promise { + const attempts = opts?.attempts ?? 50; + const delayMs = opts?.delayMs ?? 0; + for (let index = 0; index < attempts; index += 1) { + if (predicate()) { + return; + } + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } + throw new Error("condition not met"); +} + function createModelPickerContext(): ModelPickerContext { const cfg = { channels: { @@ -317,7 +332,7 @@ describe("Discord model picker interactions", () => { await button.run(submitInteraction as unknown as PickerButtonInteraction, submitData); expect(withTimeoutSpy).toHaveBeenCalledTimes(1); - expect(dispatchSpy).toHaveBeenCalledTimes(1); + await waitForCondition(() => dispatchSpy.mock.calls.length === 1); expect(submitInteraction.followUp).toHaveBeenCalledTimes(1); const followUpPayload = submitInteraction.followUp.mock.calls[0]?.[0] as { components?: Array<{ components?: Array<{ content?: string }> }>;