dorfteich/apps/api/src/search/search.provider.test.ts
Claude Opus 4.8 91dfccf226
All checks were successful
CD / Build and push images (push) Successful in 3m5s
CI / Lint, typecheck, test (push) Successful in 2m19s
CI / Auth e2e pack (push) Successful in 2m51s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m13s
CD / Promote to Int (push) Successful in 12s
Add SearchProvider interface with PostgreSQL FTS (#49)
Full-text search behind a swappable interface (ADR 0010).

- prisma: `page_content_cache.search_vector tsvector` (Unsupported column);
  migration adds it plus a GIN index (raw SQL — the index is a production
  perf optimization; correctness holds without it, so schema-pushed test DBs
  work unchanged).
- shared: `normalizeForSearch` (NFKD + strip diacritics + lowercase) folds
  both the indexed text and the query, so 'Baume' finds 'Bäume' without the
  Postgres `unaccent` extension; search query schema + result view + highlight
  sentinels.
- api search module:
  - abstract `SearchProvider` (DI token: indexPage / removePage / search /
    reindexAll) so an external engine can replace the binding — a fake proves
    the seam in a test.
  - `PostgresSearchProvider`: weighted vector (title A, labels B, body C),
    `websearch_to_tsquery`, `ts_headline` snippets, results filtered to the
    ponds the user may read; `GET /search?q=&pondId=&labels=`.
  - `search:reindex` CLI (rebuilds from the content cache, idempotent).
  - reindex hooks: page create/rename (title) and label assign/unassign/
    rename/delete (labels are weight-B).
- collab: the persistence hook maintains `search_vector` in the same
  transaction as the content cache (same weighting, normalized).
- tests: shared normalize/schema; api db (title ranks above body, highlight,
  diacritic-insensitive match, permission filter, idempotent reindex) and the
  fake-provider DI test; collab persistence already covers the write path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PGdhRiwU1WRL4XxJfZYipY
2026-07-09 13:25:05 +02:00

53 lines
1.7 KiB
TypeScript

import { SearchResultView } from '@dorfteich/shared';
import { Test } from '@nestjs/testing';
import { User } from '@prisma/client';
import { describe, expect, it, vi } from 'vitest';
import { AuthedRequest } from '../auth/auth.guard';
import { SearchController } from './search.controller';
import { SearchProvider } from './search.provider';
/**
* Proves the `SearchProvider` seam (ADR 0010 / issue #49): the controller
* depends on the abstract token, so a fake implementation can replace the
* PostgreSQL binding entirely in a test — which is exactly how an external
* engine would be swapped in.
*/
describe('SearchProvider DI seam (issue #49)', () => {
it('lets a fake provider replace the real binding', async () => {
const hit: SearchResultView = {
pageId: 'p1',
title: 'Hit',
slug: 'hit',
pondId: 'pond1',
pondSlug: 'pond',
pondName: 'Pond',
labelIds: [],
snippet: 'a snippet',
};
const fake: SearchProvider = {
indexPage: vi.fn(),
removePage: vi.fn(),
reindexAll: vi.fn(),
search: vi.fn().mockResolvedValue([hit]),
};
const moduleRef = await Test.createTestingModule({
controllers: [SearchController],
providers: [{ provide: SearchProvider, useValue: fake }],
}).compile();
const controller = moduleRef.get(SearchController);
const user = { id: 'u1', isSiteAdmin: false } as User;
const result = await controller.query('hello', 'pond1', 'a,b', {
user,
} as AuthedRequest);
expect(result).toEqual([hit]);
expect(fake.search).toHaveBeenCalledWith(
{ q: 'hello', pondId: 'pond1', labels: ['a', 'b'] },
user,
);
});
});