Skip to content

Build your personal knowledge base with Trilium Notes

test: fix typecheck

Elian Doran296df0733d3d
diff --git a/apps/server/src/routes/api/etapi_tokens.spec.ts b/apps/server/src/routes/api/etapi_tokens.spec.ts
index 059776fed4..98bd0d1d8e 100644
--- a/apps/server/src/routes/api/etapi_tokens.spec.ts
+++ b/apps/server/src/routes/api/etapi_tokens.spec.ts
@@ -15,7 +15,7 @@ describe("ETAPI tokens API", () => {
const etapiTokenId = cls.init(() =>
etapiTokensRoute.getTokens().find(t => t.name === "spec-token")!.etapiTokenId
- );
+ )!;
expect(etapiTokenId).toBeTruthy();
cls.init(() => etapiTokensRoute.patchToken(req({ name: "renamed" }, { etapiTokenId })));
diff --git a/apps/server/src/routes/api/llm_chat.spec.ts b/apps/server/src/routes/api/llm_chat.spec.ts
index 87609a0101..a62333be91 100644
--- a/apps/server/src/routes/api/llm_chat.spec.ts
+++ b/apps/server/src/routes/api/llm_chat.spec.ts
@@ -23,7 +23,7 @@ vi.mock("../../services/llm/stream.js", () => ({
async *streamToChunks () { for (const c of state.chunks) yield c; }
}));
-const generateChatTitle = vi.fn(async () => {});
+const generateChatTitle = vi.fn(async (..._args: unknown[]) => {});
vi.mock("../../services/llm/chat_title.js", () => ({ generateChatTitle: (...args: unknown[]) => generateChatTitle(...args) }));
import llmChatRoute from "./llm_chat.js";
diff --git a/apps/server/src/routes/api/llm_special_notes.spec.ts b/apps/server/src/routes/api/llm_special_notes.spec.ts
index bd5f321d5e..796f41a505 100644
--- a/apps/server/src/routes/api/llm_special_notes.spec.ts
+++ b/apps/server/src/routes/api/llm_special_notes.spec.ts
@@ -7,7 +7,7 @@ import llmSpecialNotesRoute from "./llm_special_notes.js";
describe("LLM special notes API", () => {
it("creates an LLM chat note", () => {
const chat = cls.init(() => llmSpecialNotesRoute.createLlmChat());
- expect(chat.id ?? chat.noteId).toBeTruthy();
+ expect(chat.noteId).toBeTruthy();
});
it("gets or creates, then returns it as the most recent chat", () => {
@@ -33,7 +33,7 @@ describe("LLM special notes API", () => {
it("saves a chat by note id", () => {
const created = cls.init(() => llmSpecialNotesRoute.createLlmChat());
- const noteId = (created.id ?? created.noteId) as string;
+ const noteId = created.noteId;
const req = { body: { llmChatNoteId: noteId } } as unknown as Request<{ llmChatNoteId: string }>;
const saved = cls.init(() => llmSpecialNotesRoute.saveLlmChat(req));
expect(saved).toBeTruthy();
diff --git a/packages/trilium-core/src/services/events.spec.ts b/packages/trilium-core/src/services/events.spec.ts
new file mode 100644
index 0000000000..f377837d85
--- /dev/null
+++ b/packages/trilium-core/src/services/events.spec.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it } from "vitest";
+
+import events from "./events.js";
+
+/**
+ * The events service is a pure in-memory pub/sub over a module-level listener
+ * map. Each test uses a unique event name so listeners don't leak across tests.
+ */
+describe("events service", () => {
+ it("subscribes to a single (non-array) event and delivers the payload", () => {
+ const received: unknown[] = [];
+ events.subscribe("TEST_SINGLE", (data) => received.push(data));
+
+ events.emit("TEST_SINGLE", { a: 1 });
+
+ expect(received).toEqual([ { a: 1 } ]);
+ });
+
+ it("subscribes to an array of events", () => {
+ const calls: string[] = [];
+ events.subscribe([ "TEST_A", "TEST_B" ], () => calls.push("hit"));
+
+ events.emit("TEST_A");
+ events.emit("TEST_B");
+
+ expect(calls).toHaveLength(2);
+ });
+
+ it("emitting an event with no listeners is a no-op", () => {
+ expect(() => events.emit("TEST_NO_LISTENERS")).not.toThrow();
+ });
+
+ it("runs the becca-loader listener before regular listeners", () => {
+ const order: string[] = [];
+ events.subscribe("TEST_ORDER", () => order.push("regular"));
+ events.subscribeBeccaLoader("TEST_ORDER", () => order.push("becca"));
+
+ events.emit("TEST_ORDER");
+
+ expect(order).toEqual([ "becca", "regular" ]);
+ });
+
+ it("subscribeBeccaLoader accepts an array of events", () => {
+ const calls: string[] = [];
+ events.subscribeBeccaLoader([ "TEST_BL_A", "TEST_BL_B" ], () => calls.push("hit"));
+
+ events.emit("TEST_BL_A");
+ events.emit("TEST_BL_B");
+
+ expect(calls).toHaveLength(2);
+ });
+
+ it("isolates a throwing listener so the others still run", () => {
+ const calls: string[] = [];
+ events.subscribe("TEST_THROW", () => {
+ throw new Error("boom");
+ });
+ events.subscribe("TEST_THROW", () => calls.push("second"));
+
+ expect(() => events.emit("TEST_THROW")).not.toThrow();
+ expect(calls).toEqual([ "second" ]);
+ });
+});