Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/unit/next-int-import.test.ts9 * Description : 3.0 integration — a note exported to Markdown and imported10 * again keeps every block whose documented Markdown form11 * identifies its node (docs/next/EDITOR-NODES.md). The file12 * importer used to read `[[<page uuid>#<block>]]` as a wiki13 * link by title and `` as an14 * image, so block references and drawings were lost. No15 * database.16 * Module : tests17 * Repository : https://git.spboucher.ai/note-spboucher-ai18 * Project Website : https://note.spboucher.ai19 * Created : 2026-11-1920 * Last Modified : 2026-11-1921 *22 * Author : Simon-Pierre Boucher23 * Position : Professor of Finance24 * Institution : Université du Québec en Outaouais (UQO)25 * Organization : SP Boucher26 * Website : https://spboucher.ai27 * Git : https://git.spboucher.ai28 *29 * Preferred Contact: contact@spboucher.ai30 * Development : dev@spboucher.ai31 * Support : support@spboucher.ai32 * Security/Admin : admin@spboucher.ai33 *34 * Copyright (c) 202635 * Simon-Pierre Boucher36 * All rights reserved where applicable. This file is governed by the37 * applicable project license, copyright notices and relevant law.38 * Existing third-party rights and notices remain unaffected.39 *40 * License : None specified (no license file in this repository)41 *42 * Do not embed credentials, private keys, access tokens,43 * certificates or confidential personal information.44 *45 * =============================================================================46 * END OF SP BOUCHER SOURCE HEADER47 * =============================================================================48 */4950import { test } from "node:test";51import assert from "node:assert/strict";52import fs from "node:fs";53import { toMarkdown } from "../../src/lib/editor/text";54import { mdToDoc, previewText, readMarkdown } from "../../src/lib/import/markdown";55import { normaliseFilter } from "../../src/lib/databases/filter-input";56import { pgIso, pgNum } from "../../src/lib/pg-values";57import { errorCode } from "../../src/lib/errors";5859const fixture = JSON.parse(fs.readFileSync("tests/fixtures/next-nodes/all-nodes.json", "utf8"));60const ctx = { titles: new Map<string, string>([["known page", "99999999-9999-4999-8999-999999999999"]]) } as any;6162test("export → file import keeps the nodes their Markdown form identifies", async () => {63 const md = toMarkdown(fixture); const doc = await mdToDoc(ctx, md); const types = (doc.content ?? []).map((n: any) => n.type);64 assert.deepEqual(types, ["mermaid", "drawing", "paragraph", "table", "queryView", "paragraph", "heading", "pdf", "blockRef", "runCode", "audio", "canvasLink"]);65 const by = (t: string) => (doc.content ?? []).find((n: any) => n.type === t)!.attrs as any;66 assert.deepEqual([by("blockRef").pageId, by("blockRef").blockId], ["aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", "par0000009"]);67 assert.deepEqual([by("drawing").preview, by("drawing").ink], ["11111111-1111-4111-8111-111111111111", null], "a drawing comes back with its preview only");68 assert.equal(by("canvasLink").pageId, "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb"); assert.equal(by("pdf").attachmentId, "33333333-3333-4333-8333-333333333333"); assert.equal(by("audio").transcript.length, 3);69 assert.match(by("mermaid").code, /graph TD/); assert.equal(by("runCode").output.text, "6");70});71test("wiki links by title and ordinary images are still imported as before", async () => {72 const doc = await mdToDoc(ctx, "See [[Known page]] and [[Unknown page|alias]].\n\n\n\n\n");73 const flat = JSON.stringify(doc); assert.match(flat, /"type":"mention","attrs":\{"id":"99999999-9999-4999-8999-999999999999"/); assert.match(flat, /alias/); assert.equal((flat.match(/"type":"image"/g) ?? []).length, 2); assert.doesNotMatch(flat, /"type":"drawing"/);74 assert.equal(previewText("Before.\n\n[[aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa#par0000009]]\n\nAfter [[Title]].").replace(/\s+/g, " ").trim(), "Before. After Title.", "the preview text is the text the imported page will hold (a block reference has none)");75});76test("a first paragraph that looks like `Key: value` is text when the file has front matter; a Notion export still gives properties", () => {77 const own = readMarkdown("---\ntitle: My note\nid: 99999999-9999-4999-8999-999999999999\n---\n# My note\n\nNote: this first paragraph must survive.\n\nSecond paragraph.\n", "my-note.md");78 assert.equal(own.title, "My note"); assert.deepEqual(own.nh.props, {}); assert.match(own.body, /^Note: this first paragraph must survive\./); assert.doesNotMatch(own.body, /^# My note/);79 const notion = readMarkdown("# Roadmap\n\nStatus: Doing\nOwner: SPB\n\nBody text.\n", "Roadmap abc123.md");80 assert.equal(notion.title, "Roadmap"); assert.deepEqual(notion.nh.props, { Status: "Doing", Owner: "SPB" }); assert.equal(notion.body.trim(), "Body text.");81});82test("database filter input: a bare rule is wrapped, groups pass, anything else is refused (never dropped)", () => {83 const rule = { property: "p", op: "eq", value: 1 };84 assert.deepEqual(normaliseFilter(rule), { and: [rule] }); assert.deepEqual(normaliseFilter({ or: [rule, { and: [rule] }] }), { or: [rule, { and: [rule] }] });85 assert.deepEqual(normaliseFilter({}), {}); assert.equal(normaliseFilter(null), null); assert.equal(normaliseFilter(undefined), undefined);86 for (const bad of [{ foo: 1 }, "x", 3, [rule], { and: [rule], property: "p" }, { property: "p" }, { and: "x" }]) assert.throws(() => normaliseFilter(bad), (e: any) => e.status === 400 && e.code === "bad_filter", JSON.stringify(bad));87});88test("JSON-safe PostgreSQL values and default error codes", () => {89 assert.equal(pgIso("2026-11-19 13:45:47.060354-05"), "2026-11-19T18:45:47.060Z"); assert.equal(pgIso("2026-11-19 13:45:47+00"), "2026-11-19T13:45:47.000Z"); assert.equal(pgIso("2026-11-19 13:45:47.5+0530"), "2026-11-19T08:15:47.500Z");90 assert.equal(pgIso("2026-11-19"), "2026-11-19T00:00:00.000Z"); assert.equal(pgIso(null), null); assert.equal(pgIso(new Date(0)), "1970-01-01T00:00:00.000Z"); assert.equal(pgIso("2026-11-19T18:45:47.060Z"), "2026-11-19T18:45:47.060Z");91 assert.deepEqual([pgNum("20002"), pgNum(null), pgNum(7), pgNum("x")], [20002, 0, 7, 0]);92 assert.deepEqual([errorCode(400), errorCode(404), errorCode(409), errorCode(404, "not_canvas"), errorCode(500), errorCode(418)], ["bad_request", "not_found", "invalid_state", "not_canvas", "internal", "error"]);93});94