SPB Git

Repository Public

helpers.ts 8.5 KB 103 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/e2e/helpers.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { execFileSync } from "node:child_process";44import fs from "node:fs";45import postgres from "postgres";46import type { BrowserContext, Page, CDPSession } from "@playwright/test";47import "../helpers/testdb";4849export const AUTH_DIR = "test-results/.auth"; export const STATE = `${AUTH_DIR}/state.json`; export const CRED = `${AUTH_DIR}/credential.json`;50export const sql = postgres(process.env.DATABASE_URL!, { max: Math.max(1, Math.min(2, Number(process.env.DB_POOL_MAX ?? 2) || 2)), onnotice: () => {} });51/** Run the passkey admin CLI exactly as the operator would on the node. */52export function cli(...args: string[]): string { return execFileSync("node", ["node_modules/tsx/dist/cli.mjs", "src/cli/auth.ts", ...args], { encoding: "utf8", env: process.env, stdio: ["ignore", "pipe", "pipe"] }); }53export function enrollmentToken(out: string): string { const m = out.match(/\/enroll#([A-Za-z0-9_-]{40,64})/); if (!m) throw new Error("no enrollment link in CLI output"); return m[1]; }54/** Chrome DevTools virtual authenticator: platform (internal), resident keys, user verification simulated as successful unless told otherwise. */55export async function virtualAuthenticator(page: Page, opts: { userVerified?: boolean } = {}): Promise<{ cdp: CDPSession; id: string }> {56  const cdp = await page.context().newCDPSession(page); await cdp.send("WebAuthn.enable");57  const { authenticatorId } = await cdp.send("WebAuthn.addVirtualAuthenticator", { options: { protocol: "ctap2", transport: "internal", hasResidentKey: true, hasUserVerification: true, isUserVerified: opts.userVerified ?? true, automaticPresenceSimulation: true } });58  return { cdp, id: authenticatorId };59}60export async function saveCredential(cdp: CDPSession, id: string) { const { credentials } = await cdp.send("WebAuthn.getCredentials", { authenticatorId: id }); fs.mkdirSync(AUTH_DIR, { recursive: true }); fs.writeFileSync(CRED, JSON.stringify(credentials[0]), { mode: 0o600 }); return credentials[0]; }61/** Give a fresh page the passkey enrolled by global setup (so it can sign in or step up). */62export async function withSavedPasskey(page: Page) { const a = await virtualAuthenticator(page); const credential = JSON.parse(fs.readFileSync(CRED, "utf8"));63  // A real authenticator never rewinds its signature counter (the server refuses that as a clone). Each import therefore starts above every earlier use.64  credential.signCount = Math.floor((Date.now() - 1_794_000_000_000) / 10);65  await a.cdp.send("WebAuthn.addCredential", { authenticatorId: a.id, credential }); return a; }66export async function saveState(ctx: BrowserContext) { fs.mkdirSync(AUTH_DIR, { recursive: true }); await ctx.storageState({ path: STATE }); }67/** No element may be wider than the viewport, and the document must not scroll sideways. */68export async function expectNoHorizontalOverflow(page: Page) {69  const r = await page.evaluate(() => { const w = document.documentElement.clientWidth; const bad: string[] = []; if (document.documentElement.scrollWidth > w + 1) bad.push(`document scrollWidth ${document.documentElement.scrollWidth} > ${w}`); const main = document.getElementById("main-scroll"); if (main && main.scrollWidth > main.clientWidth + 1) bad.push(`main scrollWidth ${main.scrollWidth} > ${main.clientWidth}`); return bad; });70  if (r.length) throw new Error(`Horizontal overflow: ${r.join("; ")}`);71}7273export const ORIGIN = process.env.E2E_BASE_URL ?? "http://localhost:8481";74/** Authenticated JSON call from the test process with the browser's cookies (adds the Origin header the CSRF check requires). */75export async function apiCall(page: Page, method: "POST" | "PATCH" | "PUT" | "DELETE" | "GET", url: string, data?: unknown, headers: Record<string, string> = {}) {76  const res = await page.request.fetch(url, { method, data: data as any, headers: { origin: ORIGIN, ...(data !== undefined ? { "content-type": "application/json" } : {}), ...headers } });77  let body: any = null; try { body = await res.json(); } catch { /* not json */ }78  return { status: res.status(), body, headers: res.headers() };79}80export const para = (text: string) => ({ type: "paragraph", content: [{ type: "text", text }] });81export const docOf = (...nodes: unknown[]) => ({ type: "doc", content: nodes });82/** Bottom-docked UI must not cover the element (e.g. the caret line or the primary action). */83export async function expectNotCovered(page: Page, selector: string) {84  const r = await page.evaluate((sel) => { const el = document.querySelector(sel); if (!el) return "missing"; const b = el.getBoundingClientRect(); const x = Math.min(window.innerWidth - 2, Math.max(2, b.left + b.width / 2)), y = b.top + b.height / 2; if (y < 0 || y > window.innerHeight) return `outside viewport (y=${Math.round(y)})`; const top = document.elementFromPoint(x, y); return top && (el === top || el.contains(top) || top.contains(el)) ? "ok" : `covered by <${top?.tagName.toLowerCase()} class="${(top as HTMLElement)?.className?.toString().slice(0, 60)}">`; }, selector);85  if (r !== "ok") throw new Error(`${selector}: ${r}`);86}87/** 1×1 PNG and a minimal one-page PDF for upload tests. */88export const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", "base64");89export const PDF = Buffer.from("%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R/Resources<</Font<</F1 5 0 R>>>>>>endobj\n4 0 obj<</Length 44>>stream\nBT /F1 12 Tf 20 100 Td (Hello note) Tj ET\nendstream endobj\n5 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj\nxref\n0 6\n0000000000 65535 f \n0000000009 00000 n \n0000000052 00000 n \n0000000101 00000 n \n0000000211 00000 n \n0000000300 00000 n \ntrailer<</Size 6/Root 1 0 R>>\nstartxref\n362\n%%EOF\n");9091/**92 * Remove the 20 000-row benchmark database seeded by scripts/db-seed-bench.ts, with its rows, its index entries and the tombstones93 * of this removal. Left in the workspace it slowed every other suite down (20 000 pages to index, a 6 MB global graph) and cut the94 * scans of the knowledge suites. Each database spec seeds it in beforeAll and removes it in afterAll.95 */96export async function removeBench(bench: { pageId: string; databaseId: string } | undefined) {97  if (!bench?.pageId) return;98  const ids = (await sql`select id from pages where id = ${bench.pageId} or database_id = ${bench.databaseId}`).map((r) => r.id as string);99  await sql`delete from chunks where page_id = any(${ids}::uuid[])`; await sql`delete from index_state where source_id = any(${ids}::uuid[])`; await sql`delete from knowledge_events where page_id = any(${ids}::uuid[])`;100  await sql`delete from pages where database_id = ${bench.databaseId}`; await sql`delete from pages where id = ${bench.pageId}`;101  await sql`delete from sync_tombstones where id = any(${ids}::uuid[]) or (kind like 'database%' and deleted_at > now() - interval '2 minutes')`;102}103
Files main
Menu

Theme