SPB Git

Repository Public

next.ts 6.1 KB 85 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/helpers/next.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1913 * 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 */4243/** Shared set-up of the 3.0 suites: the scratch workspace, a registry of what a test created, and one clean-up that removes all of it. */44import "./testdb";45import assert from "node:assert/strict";46import { and, eq, gte, inArray, like } from "drizzle-orm";47import { db, schema, sql } from "../../src/db/index";48import { createPage } from "../../src/lib/pages/service";49import type { PMNode } from "../../src/lib/editor/text";5051/** Start of this test process (minus a margin for clock rounding): what the clean-up may remove from shared tables. */52export const STARTED = new Date(Date.now() - 2000);53export const made = { pages: [] as string[], tasks: [] as string[], concepts: [] as string[], references: [] as string[], attachments: [] as string[], tags: [] as string[] };54export async function workspace(): Promise<string> { const [w] = await db.select().from(schema.workspaces).limit(1); assert.ok(w, "workspace missing: run `pnpm run bootstrap` on the scratch database"); return w.id; }55export const doc = (...blocks: (string | PMNode)[]): PMNode => ({ type: "doc", content: blocks.map((b) => (typeof b === "string" ? { type: "paragraph", content: [{ type: "text", text: b }] } : b)) });56export const para = (text: string, id?: string): PMNode => ({ type: "paragraph", ...(id ? { attrs: { id } } : {}), content: [{ type: "text", text }] });57export async function mk(ws: string, title: string, content?: PMNode, extra: Partial<Parameters<typeof createPage>[1]> = {}) { const p = await createPage(ws, { title, content, ...extra }); made.pages.push(p.id); return p; }58export const status = async (p: Promise<unknown>) => { try { await p; return 200; } catch (e: any) { return e.status ?? 500; } };59export const code = async (p: Promise<unknown>) => { try { await p; return "ok"; } catch (e: any) { return e.code ?? `status-${e.status ?? 500}`; } };60export const eventsOf = async (pageId: string) => (await db.select().from(schema.knowledgeEvents).where(eq(schema.knowledgeEvents.pageId, pageId))).sort((a, b) => a.at.getTime() - b.at.getTime());61export const kinds = async (pageId: string) => (await eventsOf(pageId)).map((e) => e.kind);62export async function cleanup(ws: string, prefix: string) {63  const mine = await db.select({ id: schema.pages.id }).from(schema.pages).where(and(eq(schema.pages.workspaceId, ws), like(schema.pages.title, `${prefix}%`)));64  const ids = [...new Set([...made.pages, ...mine.map((m) => m.id)])];65  if (made.tasks.length) await db.delete(schema.tasks).where(inArray(schema.tasks.id, made.tasks));66  if (ids.length) {67    await db.delete(schema.tasks).where(inArray(schema.tasks.pageId, ids));68    await db.delete(schema.knowledgeEdges).where(inArray(schema.knowledgeEdges.fromId, ids)); await db.delete(schema.knowledgeEdges).where(inArray(schema.knowledgeEdges.toId, ids));69    await db.delete(schema.attachments).where(inArray(schema.attachments.pageId, ids));70    await db.delete(schema.pages).where(inArray(schema.pages.id, ids));71    await db.delete(schema.knowledgeEvents).where(inArray(schema.knowledgeEvents.pageId, ids));72    await db.delete(schema.syncTombstones).where(inArray(schema.syncTombstones.id, ids));73  }74  if (made.attachments.length) await db.delete(schema.attachments).where(inArray(schema.attachments.id, made.attachments));75  if (made.references.length) { await db.delete(schema.knowledgeEdges).where(inArray(schema.knowledgeEdges.toId, made.references)); await db.delete(schema.researchReferences).where(inArray(schema.researchReferences.id, made.references)); }76  if (made.concepts.length) await db.delete(schema.concepts).where(inArray(schema.concepts.id, made.concepts));77  await db.delete(schema.concepts).where(and(eq(schema.concepts.workspaceId, ws), like(schema.concepts.name, `${prefix}%`)));78  await db.delete(schema.knowledgeInsights).where(and(eq(schema.knowledgeInsights.workspaceId, ws), like(schema.knowledgeInsights.title, `${prefix}%`)));79  if (made.tags.length) await db.delete(schema.tags).where(and(eq(schema.tags.workspaceId, ws), inArray(schema.tags.name, made.tags)));80  await db.delete(schema.knowledgeEvents).where(and(eq(schema.knowledgeEvents.workspaceId, ws), like(schema.knowledgeEvents.summary, `${prefix}%`)));81  // only the tombstones written since this process started (other clients of the scratch instance keep theirs)82  await db.delete(schema.syncTombstones).where(and(inArray(schema.syncTombstones.kind, ["task", "edge", "concept", "reference", "canvas", "database", "database_property", "database_view", "database_row", "attachment"]), gte(schema.syncTombstones.deletedAt, STARTED)));83  await sql.end();84}85
Files main
Menu

Theme