feat(fase-4): global search + RLS isolation audit (211 tests green)

4.0 Tests primero
  Vitest: search.test.ts (10 — S-01..S-04), rls-audit.test.ts (42 —
    3 intruders × 14 cross-collective ops proving zero leakage)
  pgTAP: 007_search_tsvector.sql (9 — columns, GIN indexes, function
    signature, generated-vector invariant)
  Playwright: search.test.ts (2 — SR-01 happy-path, SR-02 filter toggle)

4.1 Búsqueda global
  Migration 010_search_vectors.sql: STORED GENERATED tsvector columns
    on shopping_items/tasks/notes + GIN indexes + search_result_type
    enum + search_in_collective() SECURITY INVOKER function (RLS-aware,
    trash-excluded per RN-08) + GRANT EXECUTE to anon/authenticated.
    Uses `simple` config (no stemmer — bilingual en/es).
  Store apps/web/src/lib/stores/search.ts — RPC wrapper
  /search: debounced input (300ms, ≥2 chars), type filter chips with
    multi-toggle, results grouped per type with data-testid hooks for
    Playwright, deep-link per type (shopping_item → /lists/[id], task →
    /tasks/[id], note → /notes/[id])

4.4 Security
  rls-audit.test.ts replaces the curl-based manual audit with a
    parametrised matrix — zero cross-collective reads/writes under
    three different attacker roles

Realtime flake hardening
  Add 250ms settle after SUBSCRIBED in realtime-helpers.ts — the Phoenix
    socket reports SUBSCRIBED slightly before the backend finishes
    propagating the filter to the WAL subscription, so mutations fired
    immediately after subscribe could miss the filter under load.

4.Z Verification
  just test-all → 211 verdes, 2 skipped
    34 pgTAP (was 25, +9 search)
    140 Vitest integration (was 88, +10 search +42 rls-audit; 2 skipped)
    6 Vitest unit (unchanged)
    31 Playwright (was 29, +2 search)
  Deferred (prod-deploy scope):
    PWA install/push (needs real iOS/Android hardware)
    Kong rate-limit plugin config
    JWT_SECRET / ANON_KEY rotation
    Lighthouse PWA ≥ 90 manual validation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-13 12:47:38 +02:00
parent 673a320a66
commit 2806e06db2
13 changed files with 999 additions and 56 deletions

View File

@@ -0,0 +1,49 @@
/**
* SR-series (UI) — Fase 4.1
*
* Global search at /search. Debounced input (300ms) feeds
* search_in_collective() and groups results by type (Lists / Tasks / Notes).
*/
import { test, expect } from '@playwright/test';
import { USERS } from '../fixtures/users.js';
import { loginAs } from '../fixtures/login.js';
test.describe('Global search', () => {
test.beforeEach(async ({ page }) => {
await loginAs(page, USERS.borja);
});
test('SR-01: searching "milk" surfaces the seeded shopping item', async ({ page }) => {
await page.goto('/search');
const input = page.getByPlaceholder(/search|buscar/i);
await expect(input).toBeVisible({ timeout: 15_000 });
await input.fill('Milk');
// Wait beyond the 300ms debounce.
await page.waitForTimeout(600);
// The shopping item group exists and contains a row with "Milk".
const group = page.getByTestId('search-group-shopping_item');
await expect(group).toBeVisible({ timeout: 5_000 });
await expect(group.getByText(/milk/i).first()).toBeVisible({ timeout: 5_000 });
});
test('SR-02: deactivating the Lists filter hides shopping items from results', async ({ page }) => {
await page.goto('/search');
const input = page.getByPlaceholder(/search|buscar/i);
await expect(input).toBeVisible({ timeout: 15_000 });
await input.fill('Milk');
await page.waitForTimeout(600);
// Seeded "Milk" lives in shopping_items — it must be visible before the filter toggle
await expect(page.getByTestId('search-group-shopping_item')).toBeVisible({ timeout: 5_000 });
// Deactivate the "Lists" chip (multi-toggle filter). The shopping_item group disappears.
await page.getByRole('button', { name: /^lists$|^listas$/i }).click();
await page.waitForTimeout(400);
await expect(page.getByTestId('search-group-shopping_item')).toHaveCount(0, {
timeout: 3_000
});
});
});