Skip to content

Build your personal knowledge base with Trilium Notes

chore(llm): try to fix flaky test

Elian Doran8ac0425ca555
diff --git a/apps/server/src/routes/api/llm.spec.ts b/apps/server/src/routes/api/llm.spec.ts
index 81bd9e039f..e14012893a 100644
--- a/apps/server/src/routes/api/llm.spec.ts
+++ b/apps/server/src/routes/api/llm.spec.ts
@@ -3,7 +3,7 @@ import { beforeAll, describe, expect, it, vi, beforeEach, afterEach } from "vite
import supertest from "supertest";
import config from "../../services/config.js";
import { refreshAuth } from "../../services/auth.js";
-import type { WebSocket } from 'ws';
+import { sleepFor } from "@triliumnext/commons";
// Mock the CSRF protection middleware to allow tests to pass
vi.mock("../csrf_protection.js", () => ({
@@ -499,6 +499,7 @@ describe("LLM API Tests", () => {
const ws = (await import("../../services/ws.js")).default;
// Verify thinking message was sent
+ await sleepFor(1_000);
expect(ws.sendMessageToAllClients).toHaveBeenCalledWith({
type: 'llm-stream',
chatNoteId: testChatId,
diff --git a/packages/commons/src/lib/test-utils.ts b/packages/commons/src/lib/test-utils.ts
index edfe90ef00..86ebfb0d60 100644
--- a/packages/commons/src/lib/test-utils.ts
+++ b/packages/commons/src/lib/test-utils.ts
@@ -54,3 +54,11 @@ export function trimIndentation(strings: TemplateStringsArray, ...values: any[])
}
return output.join("\n");
}
+
+export function flushPromises() {
+ return new Promise(setImmediate);
+}
+
+export function sleepFor(duration: number) {
+ return new Promise(resolve => setTimeout(resolve, duration));
+}