import assert from 'node:assert/strict'; import test from 'node:test'; import { createMindSpaceWorkspacePublicationDeliveryService, } from './mindspace-workspace-publication-delivery-service.mjs'; function fileStat() { return { isFile: () => true, }; } function createSetup(overrides = {}) { const calls = []; const pool = { async query(sql, params) { calls.push(['query', sql, params]); return [[{ id: 'user-1' }]]; }, }; const service = createMindSpaceWorkspacePublicationDeliveryService({ pool, h5Root: '/project', storageRoot: '/storage', async resolvePublicRequestFn(options) { calls.push([ 'resolve-workspace', options.requestPath, ]); return { action: 'serve', ownerKey: 'user-1', filePath: '/project/MindSpace/user-1/public/page.html', }; }, resolveUserPublishDirFn( _h5Root, user, ) { return `/project/MindSpace/${user.id}`; }, async getDeliveryContractFn(options) { calls.push(['contract', options]); return { status: 'ready' }; }, async materializePrivateAssetsFn( options, ) { calls.push(['materialize', options]); return { html: '
localized
', changed: true, }; }, async readFileFn(targetPath, encoding) { calls.push([ 'read', targetPath, encoding, ]); if (encoding === 'utf8') { return 'localized
', }); assert.equal( Object.hasOwn(result, 'filePath'), false, ); const materialize = setup.calls.find( ([kind]) => kind === 'materialize', ); assert.equal( materialize[1].htmlRelativePath, 'public/page.html', ); assert.equal( materialize[1].writeBack, true, ); }); test('enforces the page delivery contract inside MindSpace', async () => { const setup = createSetup({ async getDeliveryContractFn() { return { status: 'validating' }; }, }); const result = await setup.service .resolveWorkspaceRequest({ requestPath: '/user-1/public/page.html', }); assert.deepEqual(result, { action: 'not_ready', ownerId: 'user-1', relativePath: 'public/page.html', }); assert.equal( setup.calls.some( ([kind]) => kind === 'read', ), false, ); }); test('materializes and returns thumbnail binaries through the logical contract', async () => { const setup = createSetup({ async resolvePublicRequestFn() { return { action: 'serve', ownerKey: 'user-1', filePath: '/project/MindSpace/user-1/public/page.thumbnail.png', }; }, async statFn(targetPath) { if ( targetPath.endsWith( '.thumbnail.png', ) ) { return fileStat(); } return null; }, }); const result = await setup.service .resolveWorkspaceRequest({ requestPath: '/user-1/public/page.thumbnail.png', }); assert.equal(result.action, 'deliver'); assert.equal(result.kind, 'binary'); assert.equal( Buffer.from( result.bodyBase64, 'base64', ).toString(), 'asset-body', ); assert.equal( result.cacheControl, 'public, max-age=300', ); }); test('reads /u owner public assets without returning storage paths', async () => { const setup = createSetup(); const result = await setup.service .readOwnerPublicAsset({ ownerSlug: 'Alice', requestPath: '/assets/demo.txt', }); assert.equal(result.action, 'deliver'); assert.equal( result.relativePath, 'public/assets/demo.txt', ); assert.equal( Buffer.from( result.bodyBase64, 'base64', ).toString(), 'asset-body', ); assert.equal( Object.hasOwn(result, 'filePath'), false, ); const query = setup.calls.find( ([kind]) => kind === 'query', ); assert.deepEqual(query[2], [ 'alice', 'alice', ]); }); test('rejects invalid owner-public paths and renders long images in MindSpace', async () => { const setup = createSetup(); const rejected = await setup.service .readOwnerPublicAsset({ ownerSlug: 'alice', requestPath: '/%2e%2e/secret.txt', }); assert.equal( rejected.action, 'not_found', ); const rendered = await setup.service.renderLongImage({ pageUrl: 'https://portal.example/MindSpace/user-1/public/page.html', }); assert.equal( Buffer.from( rendered.bodyBase64, 'base64', ).toString(), 'png-body', ); assert.equal( rendered.servedName, 'mindspace-public-page.long.png', ); }); test('scopes recent workspace discovery inside MindSpace', async () => { const setup = createSetup(); const result = await setup.service .listRecentlyModifiedPublicHtml({ userId: 'user-1', sinceMs: 1234, }); assert.deepEqual(result, { relativePaths: ['public/recent.html'], }); assert.deepEqual( setup.calls.find( ([kind]) => kind === 'recent-html', ), [ 'recent-html', '/project/MindSpace/user-1', { sinceMs: 1234 }, ], ); }); test('validates run deliverables without exposing workspace paths to Portal', async () => { const setup = createSetup(); const result = await setup.service .validateRunDeliverables({ userId: 'user-1', deliverables: { pages: [ { pageId: 'page-1', workspaceRelativePath: 'public/page.html', }, ], }, }); assert.deepEqual( result.errors.map(({ code }) => code), [ 'forbidden_local_storage', 'page_data_policy_action_missing', 'browser_storage_forbidden', ], ); assert.deepEqual( setup.calls.find( ([kind]) => kind === 'browser-storage-scan', )[1], { publishDir: '/project/MindSpace/user-1', relativePaths: ['public/page.html'], }, ); });