hub-document-tree-self-hosted-route.test.mjs file-level

at sha256:8 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:9 feat(calendar): hosted bridge/gateway route parity and timeline noteRec… · aaronrene · Jun 19, 2026
1 /**
2 * Self-hosted Hub DocumentTree route contract tests.
3 *
4 * The local Hub route exposes nested heading-only DocumentTree metadata without
5 * adding search, persistence, provider, body, snippet, resource URI, summary,
6 * sidecar, LLM, or write-back surfaces.
7 */
8 import { describe, it } from 'node:test';
9 import assert from 'node:assert/strict';
10 import fs from 'fs';
11 import path from 'path';
12 import { fileURLToPath } from 'url';
13
14 const __dirname = path.dirname(fileURLToPath(import.meta.url));
15 const repoRoot = path.dirname(__dirname);
16
17 function readRepoFile(relativePath) {
18 return fs.readFileSync(path.join(repoRoot, relativePath), 'utf8');
19 }
20
21 function routeSource() {
22 const src = readRepoFile('hub/server.mjs');
23 const start = src.indexOf("app.get('/api/v1/document-tree'");
24 const end = src.indexOf("// GET /api/v1/metadata-facets", start);
25 assert.notEqual(start, -1, 'self-hosted document-tree route must exist');
26 assert.notEqual(end, -1, 'route must stay before metadata-facets route');
27 return src.slice(start, end);
28 }
29
30 describe('self-hosted Hub DocumentTree route', () => {
31 it('unit: registers the local Hub route behind auth, rate limit, and vault access', () => {
32 const src = readRepoFile('hub/server.mjs');
33
34 assert.match(src, /app\.use\('\/api\/v1\/document-tree', jwtAuth, apiLimiter, requireVaultAccess\)/);
35 assert.match(src, /import \{ buildDocumentTree \} from '\.\.\/lib\/document-tree\.mjs'/);
36 assert.match(src, /app\.get\('\/api\/v1\/document-tree'/);
37 });
38
39 it('integration: reads one vault-relative path through readNote and buildDocumentTree only', () => {
40 const route = routeSource();
41
42 assert.match(route, /const requestedPath = typeof req\.query\.path === 'string'/);
43 assert.match(route, /resolveVaultRelativePath\(req\.vaultPath, requestedPath\)/);
44 assert.match(route, /buildDocumentTree\(readNote\(req\.vaultPath, requestedPath\)\)/);
45 assert.doesNotMatch(route, /runSearch|runKeywordSearch|embedWithUsage|completeChat/);
46 });
47
48 it('end-to-end: OpenAPI documents the same body-free endpoint', () => {
49 const api = readRepoFile('docs/openapi.yaml');
50
51 assert.match(api, /\/document-tree:/);
52 assert.match(api, /knowtation\.document_tree\/v0/);
53 assert.match(api, /#\/components\/schemas\/DocumentTree/);
54 });
55
56 it('stress: route checks remain bounded to Hub server, parser, and OpenAPI sources', () => {
57 const started = Date.now();
58 const sources = [
59 readRepoFile('hub/server.mjs'),
60 readRepoFile('lib/document-tree.mjs'),
61 readRepoFile('docs/openapi.yaml'),
62 ];
63
64 assert.equal(sources.length, 3);
65 assert.ok(Date.now() - started < 300);
66 });
67
68 it('data-integrity: route does not write, persist, index, vectorize, cache, sidecar, or summarize DocumentTree', () => {
69 const route = routeSource();
70
71 assert.doesNotMatch(route, /writeNote|deleteNote|localStorage|sessionStorage|index|vector|summary|memory|sidecar/i);
72 });
73
74 it('performance: route stays one-note and provider-free', () => {
75 const route = routeSource();
76
77 assert.match(route, /buildDocumentTree\(readNote\(req\.vaultPath, requestedPath\)\)/);
78 assert.doesNotMatch(route, /runListNotes|\/api\/v1\/notes\?|PageIndex|OCR|LLM|provider/i);
79 });
80
81 it('security: route sanitizes invalid, missing, forbidden, and upstream errors', () => {
82 const route = routeSource();
83
84 assert.match(route, /code: 'INVALID_PATH'/);
85 assert.match(route, /code: 'FORBIDDEN'/);
86 assert.match(route, /code: 'NOT_FOUND'/);
87 assert.match(route, /code: 'UPSTREAM_ERROR'/);
88 assert.doesNotMatch(route, /res\.json\(\{[\s\S]*body|snippet|frontmatter|raw_canister_payload|provider_payload|mcp_resource_uri/);
89 });
90 });