Files
InkNote/frontend/src/search.test.ts
T
marcandClaude Opus 5 92d525b3ab TipTap editor, version history, search, attachments — and a cleanup
The app is meant to run in daily private use now, so this pass was mostly
about the ways it could quietly lose data.

Reliability and security:
- the server no longer invents a JWT_SECRET or ENCRYPTION_KEY when one is
  missing; it refuses to start. The old fallback silently made every stored
  note unreadable after a restart.
- notes are encrypted with AES-256-GCM (v2:); existing CBC bodies still read.
- login is rate limited and compares against a dummy hash when the user does
  not exist, so timing does not reveal who is registered.
- a server response issued before a local save is never adopted. This had
  already eaten drawing strokes and note links; sync.hasPendingWrites() now
  guards every path that takes a body from the server.

Editor and features:
- TipTap replaces the contenteditable editor: tables, task lists, and links
  to other notes and notebooks, with a backlinks bar on the target.
- content format v2 ({v:2,doc}); v1 {html,tasks} bodies migrate on open.
- version history: the state before each write is snapshotted, coalesced
  into 5-minute windows, 50 versions or 90 days per note.
- full-text search runs in the client, because the server cannot read
  encrypted bodies and a server index would defeat the encryption.
- image attachments are stored by SHA-256 and deduplicated; the client
  fetches them authenticated and caches the blob, so they work offline.
- drawings are vector strokes in a fixed 1200x1600 page space and now look
  the same on every screen (0.0-0.2% deviation across four widths).
- login is by username; existing email accounts migrate automatically.

Operations:
- data lives on host bind mounts, not Docker volumes, so Proxmox Backup
  covers it. Back up .env with it — without ENCRYPTION_KEY the dump is
  unreadable.

Structure:
- NoteApp.jsx is gone. App.jsx now holds only where the user is; note data,
  search, layout, history and drag each live behind a hook, and the four
  panes are stateless.
- the whole data layer is TypeScript, with 81 unit tests concentrated on
  the places that have actually broken.
- fixed along the way: the browser back button had a dead press, because
  restoring a history entry pushed a duplicate one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 17:00:14 +02:00

109 lines
3.7 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import * as search from "./search";
import { serializeNoteContent } from "./editor/content";
const body = (text: string) =>
serializeNoteContent({
type: "doc",
content: [{ type: "paragraph", content: [{ type: "text", text }] }],
});
const seed = () => {
search.removeEntry("a");
search.removeEntry("b");
search.removeEntry("c");
search.setEntry("a", "Haushalt", body("Der Kühlschrank braucht eine neue Dichtung."));
search.setEntry("b", "Finanzen", body("Steuererklärung abgeben, Belege liegen im Ordner."));
search.setEntry("c", "Dichtung", body("Nur der Titel enthält das Wort."));
};
beforeEach(seed);
describe("search", () => {
it("returns null for an empty query, meaning 'no search'", () => {
expect(search.search("")).toBeNull();
expect(search.search(" ")).toBeNull();
});
it("finds a word that appears only in the body", () => {
const hits = search.search("Belege");
expect(hits?.map((h) => h.id)).toEqual(["b"]);
});
it("ranks a title match above a body match", () => {
const hits = search.search("Dichtung");
expect(hits?.map((h) => h.id)).toEqual(["c", "a"]);
});
it("requires every term to match", () => {
expect(search.search("Belege Ordner")?.map((h) => h.id)).toEqual(["b"]);
expect(search.search("Belege Kühlschrank")).toEqual([]);
});
it("ignores case", () => {
expect(search.search("BELEGE")?.map((h) => h.id)).toEqual(["b"]);
});
it("ignores diacritics, so an ASCII keyboard still finds umlauts", () => {
expect(search.search("kuhlschrank")?.map((h) => h.id)).toEqual(["a"]);
expect(search.search("Steuererklarung")?.map((h) => h.id)).toEqual(["b"]);
});
it("returns a snippet around the match", () => {
const hit = search.search("Dichtung")?.find((h) => h.id === "a");
expect(hit?.snippet).toMatch(/Dichtung/);
});
it("respects the result limit", () => {
for (let i = 0; i < 30; i++) search.setEntry(`x${i}`, `Note ${i}`, body("common word"));
expect(search.search("common", { limit: 5 })?.length).toBe(5);
for (let i = 0; i < 30; i++) search.removeEntry(`x${i}`);
});
it("stops matching a note once it is removed from the index", () => {
expect(search.search("Belege")?.length).toBe(1);
search.removeEntry("b");
expect(search.search("Belege")).toEqual([]);
});
it("reflects an updated body immediately", () => {
search.setEntry("a", "Haushalt", body("Völlig anderer Inhalt jetzt."));
expect(search.search("Kühlschrank")).toEqual([]);
expect(search.search("anderer")?.map((h) => h.id)).toEqual(["a"]);
});
});
describe("contentToText", () => {
it("reads a v2 document", () => {
expect(search.contentToText(body("hello world"))).toBe("hello world");
});
it("reads a v1 body, HTML and task list alike", () => {
const v1 = JSON.stringify({
html: "<p>legacy <b>text</b></p>",
tasks: [{ id: "t", text: "buy milk", done: false }],
});
const text = search.contentToText(v1);
expect(text).toContain("legacy");
expect(text).toContain("buy milk");
});
it("does not execute or leak markup from a v1 body", () => {
const text = search.contentToText(JSON.stringify({ html: "<p>safe</p><script>bad()</script>" }));
expect(text).toContain("safe");
expect(text).not.toContain("<script>");
});
it("survives content that is not valid JSON", () => {
expect(() => search.contentToText("<p>bare html</p>")).not.toThrow();
expect(search.contentToText("<p>bare html</p>")).toContain("bare html");
});
it("returns an empty string for empty input", () => {
expect(search.contentToText(null)).toBe("");
expect(search.contentToText(undefined)).toBe("");
expect(search.contentToText("")).toBe("");
});
});