Add MindSpace canonical URL facade

This commit is contained in:
john
2026-07-02 21:04:49 +08:00
parent e4d6267c96
commit 66c530b1ed
4 changed files with 167 additions and 1 deletions
+70
View File
@@ -0,0 +1,70 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
canonicalizeMindSpacePublicUrl,
createMindSpacePublicPageUrl,
createMindSpacePublicUrl,
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('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',
);
});