import assert from 'node:assert/strict'; import path from 'node:path'; import test from 'node:test'; import { buildMindSpacePublicRoutePath, buildMindSpaceWorkspaceRef, buildMindSpacePublicUrlForUser, createMindSpaceLocalRuntime, resolveMindSpaceAgentWorkspaceCapability, resolveMindSpacePublishRoot, resolveMindSpaceRuntimeConfig, resolveMindSpaceServerRuntimeOptions, resolveMindSpaceStorageRoot, resolveMindSpaceUserPublishDir, resolvePortalH5Root, resolveWorkspaceMindSpacePublicUrl, isPublicWorkspaceHtmlRelativePath, } from './mindspace-runtime-config.mjs'; test('resolvePortalH5Root decouples persistent workspace data from the code directory', () => { assert.equal( resolvePortalH5Root('/private/tmp/memind-worktree', { MEMIND_PORTAL_H5_ROOT: '/Users/john/Project/Memind', }), path.resolve('/Users/john/Project/Memind'), ); assert.equal( resolvePortalH5Root('/private/tmp/memind-worktree', {}), path.resolve('/private/tmp/memind-worktree'), ); }); test('resolveMindSpaceStorageRoot uses env override when present', () => { const root = resolveMindSpaceStorageRoot('/tmp/h5', { MINDSPACE_STORAGE_ROOT: '/srv/mindspace-data' }); assert.equal(root, path.resolve('/srv/mindspace-data')); }); test('resolveMindSpaceRuntimeConfig returns storage root and public base url', () => { const config = resolveMindSpaceRuntimeConfig('/tmp/h5', { MINDSPACE_STORAGE_ROOT: '/srv/mindspace-data', H5_PUBLIC_BASE_URL: 'https://example.com/', }); assert.deepEqual(config, { storageRoot: path.resolve('/srv/mindspace-data'), publicBaseUrl: 'https://example.com', }); }); test('resolveMindSpaceServerRuntimeOptions centralizes runtime roots, limits, and worker settings', () => { const config = resolveMindSpaceServerRuntimeOptions('/tmp/h5', { MINDSPACE_STORAGE_ROOT: '/srv/mindspace-data', H5_PUBLIC_BASE_URL: 'https://example.com/', MINDSPACE_SERVER_ADAPTER: 'remote', MINDSPACE_MAX_FILE_BYTES: '8192', MINDSPACE_FREE_AI_DAILY_LIMIT: '12', MINDSPACE_FREE_PUBLIC_PAGE_LIMIT: '8', MINDSPACE_FREE_MONTHLY_VIEW_LIMIT: '2048', MINDSPACE_EXPERIENCE_ENABLED: 'false', MINDSPACE_AGENT_WORKER_ENABLED: 'true', MINDSPACE_AGENT_WORKER_CONCURRENCY: '0', MINDSPACE_AGENT_WORKER_POLL_MS: '150', MINDSPACE_AGENT_WORKER_STALE_MS: '5000', MINDSPACE_AGENT_SSE_POLL_MS: '120', MINDSPACE_REMOTE_BASE_URL: 'https://mindspace.example.com/', MINDSPACE_REMOTE_AUTH_TOKEN: 'secret-token', MINDSPACE_REMOTE_OPERATION_BASE_PATH: '/internal/mindspace-adapter', MINDSPACE_REMOTE_TIMEOUT_MS: '500', }); assert.deepEqual(config, { storageRoot: path.resolve('/srv/mindspace-data'), publicBaseUrl: 'https://example.com', adapterKind: 'remote', publishRoot: path.resolve('/tmp/h5/MindSpace'), maxFileBytes: 8192, aiDailyLimit: 12, publicPageLimit: 8, monthlyViewLimit: 2048, experienceEnabled: false, agentWorker: { enabled: true, concurrency: 1, pollMs: 200, staleMs: 10_000, ssePollMs: 500, }, remote: { baseUrl: 'https://mindspace.example.com/', authToken: 'secret-token', operationBasePath: '/internal/mindspace-adapter', timeoutMs: 1000, }, }); }); test('runtime config resolves publish roots through a single helper', () => { assert.equal(resolveMindSpacePublishRoot('/tmp/h5'), path.resolve('/tmp/h5/MindSpace')); assert.equal( resolveMindSpaceUserPublishDir('/tmp/h5', { id: 'user-1' }), path.resolve('/tmp/h5/MindSpace/user-1'), ); }); test('buildMindSpacePublicUrlForUser uses runtime public base url config', () => { assert.equal( buildMindSpacePublicUrlForUser({ h5Root: '/tmp/h5', env: { H5_PUBLIC_BASE_URL: 'https://example.com/' }, user: { id: 'user-1' }, relativePath: 'public/report.html', }), 'https://example.com/MindSpace/user-1/public/report.html', ); }); test('buildMindSpacePublicRoutePath normalizes route assembly', () => { assert.equal(buildMindSpacePublicRoutePath('user-1'), '/MindSpace/user-1/'); assert.equal( buildMindSpacePublicRoutePath('user-1', ['public', 'report 1.html']), '/MindSpace/user-1/public/report%201.html', ); }); test('agent workspace capability keeps legacy sandbox root and adds future workspace ref', () => { const capability = resolveMindSpaceAgentWorkspaceCapability({ h5Root: '/tmp/h5', env: { GOOSED_SANDBOX_PUBLISH_ROOT: '/srv/goosed-mindspace' }, user: { id: 'user-1' }, }); assert.equal(capability.workspaceRoot, path.resolve('/tmp/h5/MindSpace/user-1')); assert.equal(capability.sandboxRoot, path.resolve('/srv/goosed-mindspace/user-1')); assert.equal(capability.workspaceRef, buildMindSpaceWorkspaceRef({ id: 'user-1' })); }); test('createMindSpaceLocalRuntime builds a local storage-backed facade', async () => { const runtime = createMindSpaceLocalRuntime({ h5Root: '/tmp/h5', env: { MINDSPACE_STORAGE_ROOT: '/srv/mindspace-data', H5_PUBLIC_BASE_URL: 'https://example.com', }, }); assert.equal(runtime.storageRoot, path.resolve('/srv/mindspace-data')); assert.equal(runtime.publicBaseUrl, 'https://example.com'); assert.equal(runtime.storageAdapter.kind, 'local-fs'); assert.equal( runtime.serviceFacade.createPublicPageUrl({ ownerKey: 'user-1', filename: 'report.html' }), 'https://example.com/MindSpace/user-1/public/report.html', ); }); test('resolveWorkspaceMindSpacePublicUrl builds MindSpace public html urls only', () => { const url = resolveWorkspaceMindSpacePublicUrl({ h5Root: '/tmp/h5', env: { H5_PUBLIC_BASE_URL: 'https://example.com' }, userId: '32035858-9a20-425b-89da-c118ef0779aa', relativePath: 'public/demo.html', }); assert.equal( url, 'https://example.com/MindSpace/32035858-9a20-425b-89da-c118ef0779aa/public/demo.html', ); assert.equal( resolveWorkspaceMindSpacePublicUrl({ h5Root: '/tmp/h5', userId: 'user-1', relativePath: 'oa/report.csv', }), null, ); assert.equal(isPublicWorkspaceHtmlRelativePath('public/index.html'), true); });