SPB Git

Repository Public

client-logic.test.ts 4.4 KB 72 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/client-logic.test.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 */4243/** Pure client-side logic that must not depend on a browser. */44import { test } from "node:test";45import assert from "node:assert/strict";46import { sameJson } from "../../src/lib/same-json";47import { safeNext, deviceLabel, sha256, newToken } from "../../src/lib/auth/tokens";4849test("sameJson ignores key order (jsonb round trip) but not content", () => {50  assert.ok(sameJson({ type: "text", text: "a" }, { text: "a", type: "text" }));51  assert.ok(sameJson({ a: [1, { b: 2, c: 3 }], d: undefined }, { a: [1, { c: 3, b: 2 }] }));52  assert.ok(!sameJson({ a: [1, 2] }, { a: [2, 1] })); assert.ok(!sameJson({ a: 1 }, { a: 1, b: 2 })); assert.ok(!sameJson({ a: null }, { a: {} })); assert.ok(!sameJson([], {}));53});54test("post-login destinations are same-site relative paths only", () => {55  for (const bad of ["https://evil.example", "//evil.example", "/\\evil", "javascript:alert(1)", "/login", "/enroll#x", "/api/export", "", null, undefined]) assert.equal(safeNext(bad as any), "/", String(bad));56  assert.equal(safeNext("/p/abc?x=1#block-2"), "/p/abc?x=1#block-2");57});58test("device labels are readable and tokens are 256-bit", () => {59  assert.equal(deviceLabel("Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 Version/18.5 Mobile/15E148 Safari/604.1"), "Safari · iPhone");60  assert.equal(deviceLabel("Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/140.0 Mobile Safari/537.36"), "Chrome · Android"); assert.equal(deviceLabel(null), "Unknown device");61  const t = newToken(); assert.equal(Buffer.from(t, "base64url").length, 32); assert.notEqual(t, newToken()); assert.equal(sha256(t).length, 64);62});6364import { ensureBlockIds } from "../../src/lib/editor/text";65test("block ids are added once, kept when present, and duplicates are repaired", () => {66  const doc = { type: "doc", content: [{ type: "paragraph", attrs: { id: "keepme0001" }, content: [{ type: "text", text: "a" }] }, { type: "paragraph", content: [{ type: "text", text: "b" }] }, { type: "paragraph", attrs: { id: "keepme0001" } }, { type: "bulletList", content: [{ type: "listItem", content: [{ type: "paragraph" }] }] }] };67  const r = ensureBlockIds(doc as any); assert.equal(r.changed, true); const c = (r.doc as any).content;68  assert.equal(c[0].attrs.id, "keepme0001"); assert.match(c[1].attrs.id, /^[\w-]{10}$/); assert.notEqual(c[2].attrs.id, "keepme0001"); assert.ok(c[3].attrs.id); assert.ok(c[3].content[0].content[0].attrs.id); assert.match(c[3].content[0].attrs?.id, /^[\w-]{10}$/, "3.0: list items get an id too (citations into lists, tasks anchored to a taskItem)");69  assert.equal(doc.content[1].attrs, undefined, "the input is not mutated");70  const again = ensureBlockIds(r.doc); assert.equal(again.changed, false); assert.equal(again.doc, r.doc);71});72
Files main
Menu

Theme