SPB Git

Repository Public

next-k-api.test.ts 5.1 KB 78 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/http/next-k-api.test.ts9 * Description      : HTTP-level checks of the route added by the10 *                    knowledge-screens workstream, against a RUNNING scratch11 *                    instance: GET /api/graph/constellation (auth, shape, cache12 *                    flag, refresh) and the `constellation` feature flag of GET13 *                    /api/meta. The session is created directly in the scratch14 *                    database — never against production.15 * Module           : tests16 * Repository       : https://git.spboucher.ai/note-spboucher-ai17 * Project Website  : https://note.spboucher.ai18 * Created          : 2026-11-1919 * Last Modified    : 2026-11-1920 *21 * Author           : Simon-Pierre Boucher22 * Position         : Professor of Finance23 * Institution      : Université du Québec en Outaouais (UQO)24 * Organization     : SP Boucher25 * Website          : https://spboucher.ai26 * Git              : https://git.spboucher.ai27 *28 * Preferred Contact: contact@spboucher.ai29 * Development      : dev@spboucher.ai30 * Support          : support@spboucher.ai31 * Security/Admin   : admin@spboucher.ai32 *33 * Copyright (c) 202634 * Simon-Pierre Boucher35 * All rights reserved where applicable. This file is governed by the36 * applicable project license, copyright notices and relevant law.37 * Existing third-party rights and notices remain unaffected.38 *39 * License          : None specified (no license file in this repository)40 *41 * Do not embed credentials, private keys, access tokens,42 * certificates or confidential personal information.43 *44 * =============================================================================45 * END OF SP BOUCHER SOURCE HEADER46 * =============================================================================47 */4849import "../helpers/testdb";50import { test, before, after } from "node:test";51import assert from "node:assert/strict";52import { eq } from "drizzle-orm";53import { db, schema, sql } from "../../src/db/index";54import { createSession, ownerRow } from "../../src/lib/auth/sessions";5556const BASE = (process.env.APP_URL ?? "").replace(/\/$/, ""); const UA = "SPNotes/2.0.0 (Macintosh; Mac OS X 27.0) next-k"; let cookie = "";57const get = async (path: string, anonymous = false) => { const r = await fetch(BASE + path, { headers: { "user-agent": UA, ...(anonymous ? {} : { cookie }) }, redirect: "manual" }); return { status: r.status, headers: r.headers, json: await r.json().catch(() => null) }; };58before(async () => {59  const host = new URL(BASE).hostname; assert.ok(host === "localhost" || host === "127.0.0.1", "APP_URL must be loopback");60  assert.ok(await fetch(`${BASE}/api/health`).then((r) => r.ok, () => false), `no server on ${BASE}`);61  const u = await ownerRow(); assert.ok(u); const s = await createSession({ userId: u!.id, credentialId: null, userAgent: UA, ip: "127.0.0.1" }); cookie = `__Host-note_session=${s.token}; note_session=${s.token}`;62});63after(async () => { await db.delete(schema.sessions).where(eq(schema.sessions.userAgent, UA)); await sql.end(); });6465test("GET /api/graph/constellation: needs a session; shape, honest counts, cache flag; meta announces the feature", async () => {66  const anon = await get("/api/graph/constellation", true); assert.equal(anon.status, 401); assert.equal(anon.json.code, "unauthenticated");67  const r = await get("/api/graph/constellation?refresh=1"); assert.equal(r.status, 200); assert.match(r.headers.get("cache-control") ?? "", /no-store/); const c = r.json;68  for (const k of ["model", "computedAt", "cached", "signature", "explained", "stats", "points", "clusters", "links", "projects", "tags"]) assert.ok(k in c, `key ${k}`);69  assert.equal(c.model, "text-embedding-3-small"); assert.equal(c.stats.shown, c.points.length); assert.ok(c.stats.embedded <= c.stats.pages); assert.equal(c.cached, false);70  if (c.stats.embedded === 0) { assert.deepEqual(c.points, []); assert.equal(c.computedAt, null); assert.equal(c.signature, null); }71  else {72    for (const p of c.points) { assert.match(p.id, /^[0-9a-f-]{36}$/); assert.ok(Math.abs(p.x) <= 1.05 && Math.abs(p.y) <= 1.05); assert.ok(p.cluster >= 0 && p.cluster < c.clusters.length); assert.ok(Array.isArray(p.tags)); }73    assert.equal(c.clusters.reduce((s: number, k: any) => s + k.size, 0), c.points.length); const ids = new Set(c.points.map((p: any) => p.id)); for (const l of c.links) assert.ok(ids.has(l.source) && ids.has(l.target));74    const again = await get("/api/graph/constellation"); assert.equal(again.json.cached, true); assert.equal(again.json.signature, c.signature); assert.deepEqual(again.json.points.map((p: any) => [p.id, p.x, p.y]), c.points.map((p: any) => [p.id, p.x, p.y]));75  }76  const meta = await get("/api/meta"); assert.ok(meta.json.features.includes("constellation")); assert.ok(meta.json.features.includes("graph-v2"), "existing flags are untouched");77});78
Files main
Menu

Theme