Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(claude): update test skill
Elian Doran2093015254fb
diff --git a/.claude/skills/writing-unit-tests/SKILL.md b/.claude/skills/writing-unit-tests/SKILL.mdindex 53b118873f..9231982fca 100644--- a/.claude/skills/writing-unit-tests/SKILL.md+++ b/.claude/skills/writing-unit-tests/SKILL.md@@ -21,7 +21,8 @@ Also follow `CLAUDE.md`: write **concise** tests (group related assertions in on| A jQuery widget / type widget | Extract logic → test fn; or instantiate + assert on `$widget` | [client-logic-and-services.md](client-logic-and-services.md) || A client service (`apps/client/src/services/`) | `easy-froca` + override `server.*`; or pure logic | [client-logic-and-services.md](client-logic-and-services.md) || A server service (`apps/server/src` or `packages/trilium-core/src`) | Real in-memory DB (`sql_init` + `cls.init`) or mocked becca | [server-and-core.md](server-and-core.md) |-| An internal REST API route | `supertest` agent + `/login` + `/bootstrap` CSRF | [server-and-core.md](server-and-core.md) |+| A shared **core** API route (`packages/trilium-core/src/routes/api/*`) | `CoreApiTester` — in-process, cross-runtime, real services (incl. zip export/import/multipart), minimal mocks | [server-and-core.md](server-and-core.md) Pattern 0 |+| An internal REST API route's Express transport (CSRF/auth/wiring) | `supertest` agent + `/login` + `/bootstrap` CSRF | [server-and-core.md](server-and-core.md) Pattern 1 || An ETAPI endpoint | `supertest` + basic-auth via `spec/etapi/utils.ts` | [server-and-core.md](server-and-core.md) || Pure logic (parsers, formatters, math, data maps) | Plain Vitest, no harness | any reference |@@ -52,6 +53,7 @@ coverage: {- If a config sets Vite `root: "src"` (e.g. `apps/standalone`), coverage `include` globs resolve **relative to `src`**, so use `["**/*.{ts,tsx}"]`, not `["src/**/…"]`.- **Files outside the project `root` need `coverage.allowExternal: true`.** v8 defaults it to `false`, which **silently drops** every out-of-root file — so an `include` glob alone (e.g. `../../packages/trilium-core/src/**`) is ignored and contributes nothing. `trilium-core` has no runner of its own; its coverage is measured *through* `apps/server` and `apps/standalone`, and both **must** set `allowExternal: true` **plus** a core glob in `coverage.include` whose `../` depth matches that suite's `root`: `../../packages/trilium-core/src/**` for server (root `apps/server`), `../../../packages/trilium-core/src/**` for standalone (root `apps/standalone/src`). Without `allowExternal` core never reaches the lcov or Codecov. The lcov writes these as `../…/packages/…` paths; `codecov.yml`'s `fixes:` entries strip the `../` so they map onto the repo tree.- For provably-unreachable defensive branches, mark them with `/* v8 ignore next */` / `/* v8 ignore start */…/* v8 ignore stop */` and a one-line reason — don't delete the guard or write a fake test.+- **Checking one file's coverage:** the v8 **text** reporter crashes (`PARSE_ERROR` while remapping unrelated uncovered core files) on single-spec `--coverage` runs. Use json instead and read the file's `lines.pct`: `pnpm --filter server exec vitest run <spec> --coverage --coverage.reporter=json-summary --coverage.reportsDirectory=/tmp/cov && node -e 'const c=require("/tmp/cov/coverage-summary.json");for(const[k,v]of Object.entries(c))if(k.endsWith("<file>.ts"))console.log(v.lines.pct)'`. The full-suite text report (run over a directory) is fine.## Universal gotchasdiff --git a/.claude/skills/writing-unit-tests/server-and-core.md b/.claude/skills/writing-unit-tests/server-and-core.mdindex 938ae09bb1..86692efd73 100644--- a/.claude/skills/writing-unit-tests/server-and-core.md+++ b/.claude/skills/writing-unit-tests/server-and-core.md@@ -12,11 +12,48 @@ The same `packages/trilium-core/src/**` spec executes under the **server** suiteThere are **two distinct HTTP code paths** — testing one does not cover the other:- **ETAPI** (`apps/server/src/etapi/*`): basic-auth token, helpers in `spec/etapi/utils.ts`.-- **Internal API** (`apps/server/src/routes/api/*` + `packages/trilium-core/src/routes/api/*`): session cookie + CSRF. **This is the large uncovered surface.**+- **Internal API**: the `apps/server/src/routes/api/*` Express wrappers, plus the **shared core handlers** in `packages/trilium-core/src/routes/api/*` (registered by `buildSharedApiRoutes`). The shared core handlers are driven cross-runtime by `CoreApiTester` (Pattern 0); the thin Express transport on top is covered by supertest (Pattern 1).-## Pattern 1 — internal REST API via supertest agent+## Pattern 0 — the cross-runtime core route driver (`CoreApiTester`) — PREFER THIS for core routes-Reuse the agent + CSRF flow from `apps/server/src/routes/api/options.spec.ts`. Start with read/transform handlers (`tree`, `recent_changes`, `note_map`, `similar_notes`, `stats`) — they need no setup beyond the seeded demo content. One spec file per handler keeps fork isolation clean.+For specs under `packages/trilium-core/src/routes/api/*.spec.ts`, use the in-process, transport-agnostic driver `packages/trilium-core/src/test/api_tester.ts`. It registers the exact handlers from `routes.buildSharedApiRoutes` and runs them **without Express/HTTP**, so the same spec runs under **both** the node (better-sqlite3) and standalone (sql.js WASM) suites. It exercises the real lifecycle: param/query parsing, cls + SQL transaction wrapping, `convertEntitiesToPojo`, the `[status, body]`/`undefined→204` conventions, `HttpError`→status mapping, and JSON round-tripping. It deliberately skips platform middleware (auth/CSRF/rate-limit/multipart) — those are server concerns (Pattern 1).++```ts+import { beforeAll, describe, expect, it } from "vitest";+import { createTextNote } from "../../test/api_fixtures";+import { CoreApiTester } from "../../test/api_tester";++let api: CoreApiTester;+describe("X API (core)", () => {+ beforeAll(() => { api = CoreApiTester.build(); });+ it("...", async () => {+ const res = await api.get("/api/...", { query: { q: "1" } }); // also post/put/patch/delete+ expect(res.status).toBe(200); // → { status, headers, body }+ });+});+```+- `api.<verb>(path, { body, query, headers, file })`. `createTextNote(api, {...})` → `{ noteId, branchId }`. Assert real state via `getSql()` / `becca`.+- Mutations are auto-wrapped in cls + a SQL transaction — **no `cls.init` needed** (unlike Pattern 3 direct service calls).+- Header-reading handlers (e.g. sync) work: pass `headers` and the handler's `req.get(name)` reads them case-insensitively.++### It runs REAL services end to end — including streaming + multipart. Don't mock them.+Both test setups (`apps/server/spec/setup.ts`, `apps/standalone/src/test_setup.ts`) inject the **real platform providers** (zip = archiver/fflate, image = sharp/magic-bytes, backup = fs/OPFS), and **both vitest suites run on Node** (the standalone setup itself imports `node:fs`/`node:module`) — so `Buffer`, `node:stream`, `node:fs` are available in either runtime. The tester's mock `res` is a real Node `Writable` that also implements the Express surface (`set`/`setHeader`/`removeHeader`/`status`/`send`/`sendStatus`/`write`/`end`), so the **server** export path (`archiver.pipe(res)`, needs a real writable) and the **browser** path (`BrowserZipArchive.finalize()` → `res.send(bytes)`) both run. Match the ETAPI **zero-mock** convention: drive real inputs and assert real output.+- **Multipart**: pass `file: { originalname, mimetype, buffer: Buffer.from(...) }` — the real import/image handlers run.+- **Binary/streamed response bodies** come back as a `Buffer` (real zips start with `PK`); text as a string; JSON as an object.+- **Need a real zip cross-runtime?** Export→import round-trip: `const zip = await api.get(\`/api/branches/${"<branchId>"}/export/subtree/html/1.0/t\`)` gives a real zip `Buffer` in `zip.body`; feed it back to `.../notes-import` as the `file.buffer`.++### When a core route still needs a *targeted* mock (the genuine exceptions)+Only where the platform op can't run against the ephemeral test env — keep it minimal and documented, and prefer `vi.spyOn(importedServiceObject, "method")` over `vi.mock`:+- **backup**: better-sqlite3 `.backup()` rejects `SQLITE_NOTADB` against the in-memory fixture (node) and there's no OPFS in happy-dom (standalone) → spy `backupNow`/`getBackupContent`; keep `getExistingBackups` + 400/404 real.+- **sync** `testSync`/`syncNow`/`forceFullSync`: real network → spy `syncService.login`/`sync`. `getChanged`/`update`/`checkSync` run real.+- **setup** `createInitialDatabase`/`createDatabaseForSync`: they wipe/replace the DB → spy them.+- A genuinely runtime-specific path (e.g. backup download needs the file on disk, node-only): gate with `it.skipIf(typeof window !== "undefined")` + a one-line reason; the line still counts via the node suite.++## Pattern 1 — Express transport / middleware via supertest agent++Use this only for what exists *because of* Express and can't be reached by Pattern 0: CSRF enforcement, auth, multipart wiring, and that core routes are wired into the app end to end. See `apps/server/src/routes/api/core_routes_http.spec.ts` (boots the real app via `bootLoggedInApp()` in `spec/support/internal_api.ts`).++Reuse the agent + CSRF flow from `apps/server/src/routes/api/options.spec.ts` (or `bootLoggedInApp()`). Keep these specs thin — per-handler behaviour belongs in Pattern 0; here you assert only the transport (e.g. a mutating request without `x-csrf-token` → 403, a GET served end to end). One spec file per `beforeAll` boot keeps fork isolation clean.```tsimport type { Application } from "express";@@ -104,7 +141,7 @@ Target the `mutates:false` `execute()` of LLM tools (`llm/tools/*`). Don't try t## Best ROI targets1. `packages/trilium-core/src/services/notes.ts` (real-DB) — largest cold file; also pulls in blob hashing, entity_changes, saveLinks.-2. `packages/trilium-core/src/routes/api/*` internal handlers (supertest) — untouched by ETAPI specs. Read handlers first, then `branches`/`cloning`/`bulk_action`/`attributes`.+2. ~~`packages/trilium-core/src/routes/api/*` internal handlers~~ — **done**: all at 100% lines via `CoreApiTester` (Pattern 0). If extending, follow that pattern and keep it mock-free.3. `apps/server/src/services/anonymization.ts`, `totp.ts` (pure).4. `packages/trilium-core/src/services/date_notes.ts`, `export/markdown.ts`, `import/enex.ts`, `special_notes.ts`.5. `apps/server/src/services/llm/tools/*` + `request.ts`/`open_id.ts` (mocked).