import assert from 'node:assert/strict'; import test from 'node:test'; import { canonicalizeMindSpacePublicUrl, createMindSpacePublicPageUrl, createMindSpacePublicUrl, createMindSpaceUserRootUrl, normalizeMindSpacePublicRelativePath, parseMindSpacePublicUrl, } from './mindspace-canonical-url.mjs'; test('createMindSpacePublicUrl encodes owner and relative path segments', () => { assert.equal( createMindSpacePublicUrl({ publicBaseUrl: 'https://m.tkmind.cn/', ownerKey: 'user 1', relativePath: 'public/report 1.html', }), 'https://m.tkmind.cn/MindSpace/user%201/public/report%201.html', ); }); test('createMindSpaceUserRootUrl preserves trailing slash root URL', () => { assert.equal( createMindSpaceUserRootUrl({ publicBaseUrl: 'https://m.tkmind.cn/', ownerKey: 'user 1', }), 'https://m.tkmind.cn/MindSpace/user%201/', ); }); test('createMindSpacePublicPageUrl always places html pages under public zone', () => { assert.equal( createMindSpacePublicPageUrl({ publicBaseUrl: 'https://m.tkmind.cn', ownerKey: 'user-1', filename: 'report.html', }), 'https://m.tkmind.cn/MindSpace/user-1/public/report.html', ); assert.equal( createMindSpacePublicPageUrl({ publicBaseUrl: 'https://m.tkmind.cn', ownerKey: 'user-1', filename: 'public/report.html', }), 'https://m.tkmind.cn/MindSpace/user-1/public/report.html', ); }); test('normalizeMindSpacePublicRelativePath rejects absolute URLs and traversal', () => { assert.equal(normalizeMindSpacePublicRelativePath('/public/a.html'), 'public/a.html'); for (const value of ['https://example.com/a.html', '//example.com/a.html', '../a.html', 'public/../a.html', 'a\\b']) { assert.throws( () => normalizeMindSpacePublicRelativePath(value), (error) => error.code === 'invalid_mindspace_public_relative_path', ); } }); test('parseMindSpacePublicUrl extracts owner and relative path', () => { assert.deepEqual( parseMindSpacePublicUrl('https://m.tkmind.cn/MindSpace/user-1/public/report.html?x=1'), { ownerKey: 'user-1', relativePath: 'public/report.html', isPublicZone: true, filename: 'report.html', }, ); }); test('canonicalizeMindSpacePublicUrl rewrites only the host base', () => { assert.equal( canonicalizeMindSpacePublicUrl('https://old.example/MindSpace/user-1/public/report.html', { publicBaseUrl: 'https://m.tkmind.cn', }), 'https://m.tkmind.cn/MindSpace/user-1/public/report.html', ); });