2026-06-15 15:04:43 -07:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import test from 'node:test';
|
|
|
|
|
import { mapPlazaError, plazaInternals } from './plaza-posts.mjs';
|
|
|
|
|
|
|
|
|
|
test('normalizeTags trims, dedupes by order, and caps at five', () => {
|
|
|
|
|
assert.deepEqual(plazaInternals.normalizeTags([' a ', 'b', '', 'c', 'd', 'e', 'f']), [
|
|
|
|
|
'a',
|
|
|
|
|
'b',
|
|
|
|
|
'c',
|
|
|
|
|
'd',
|
|
|
|
|
'e',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('clampLimit enforces defaults and maximum', () => {
|
|
|
|
|
assert.equal(plazaInternals.clampLimit(undefined), 20);
|
|
|
|
|
assert.equal(plazaInternals.clampLimit(999), 50);
|
|
|
|
|
assert.equal(plazaInternals.clampLimit(0), 20);
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-19 23:06:43 +08:00
|
|
|
test('normalizeSort accepts recommend, hot, or new', () => {
|
|
|
|
|
assert.equal(plazaInternals.normalizeSort(undefined), 'recommend');
|
2026-06-15 15:04:43 -07:00
|
|
|
assert.equal(plazaInternals.normalizeSort('new'), 'new');
|
|
|
|
|
assert.equal(plazaInternals.normalizeSort('hot'), 'hot');
|
2026-06-19 23:06:43 +08:00
|
|
|
assert.equal(plazaInternals.normalizeSort('recommended'), 'recommend');
|
2026-06-15 15:04:43 -07:00
|
|
|
});
|
|
|
|
|
|
2026-06-26 15:19:03 +08:00
|
|
|
test('defaultPlazaCoverUrl derives a thumbnail png from publication url', () => {
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.defaultPlazaCoverUrl('/u/john/pages/demo'),
|
|
|
|
|
'/u/john/pages/demo.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.defaultPlazaCoverUrl('/u/john/pages/demo?embed=plaza'),
|
|
|
|
|
'/u/john/pages/demo.thumbnail.png?embed=plaza',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.defaultPlazaCoverUrl('/u/john/pages/demo/'),
|
|
|
|
|
'/u/john/pages/demo/index.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('resolvePlazaCoverUrl prefers explicit cover urls and falls back to publication thumbnail', () => {
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaCoverUrl(' https://cdn.example.com/cover.png ', '/u/john/pages/demo'),
|
|
|
|
|
'https://cdn.example.com/cover.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaCoverUrl('/u/john/pages/demo.thumbnail.png', 'https://example.com/u/john/pages/demo'),
|
|
|
|
|
'https://example.com/u/john/pages/demo.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaCoverUrl('', '/u/john/pages/demo'),
|
|
|
|
|
'/u/john/pages/demo.thumbnail.png',
|
|
|
|
|
);
|
2026-08-29 16:00:00 +08:00
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.defaultPlazaCoverUrl('/MindSpace/user/public/page.html'),
|
|
|
|
|
'/MindSpace/user/public/page.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaCoverUrl(
|
|
|
|
|
'https://m.tkmind.cn/MindSpace/u/public/demo.html.thumbnail.png',
|
|
|
|
|
'/u/john/pages/demo',
|
|
|
|
|
),
|
|
|
|
|
'https://m.tkmind.cn/MindSpace/u/public/demo.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('resolvePlazaPublicationCoverUrl prefers portal publication thumbnail over MindSpace cover', () => {
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaPublicationCoverUrl({
|
|
|
|
|
inputCoverUrl: '',
|
|
|
|
|
publicUrl: 'https://m.tkmind.cn/MindSpace/wx_ul610et8/public/demo.html',
|
|
|
|
|
publicationUrlSlug: 'plaza-batch-001',
|
|
|
|
|
userSlug: 'wx_ul610et8',
|
|
|
|
|
}),
|
|
|
|
|
'/u/wx_ul610et8/pages/plaza-batch-001.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaPublicationCoverUrl({
|
|
|
|
|
inputCoverUrl: 'https://m.tkmind.cn/MindSpace/wx_ul610et8/public/demo.html.thumbnail.png',
|
|
|
|
|
publicUrl: 'https://m.tkmind.cn/MindSpace/wx_ul610et8/public/demo.html',
|
|
|
|
|
publicationUrlSlug: 'plaza-batch-001',
|
|
|
|
|
userSlug: 'wx_ul610et8',
|
|
|
|
|
}),
|
|
|
|
|
'/u/wx_ul610et8/pages/plaza-batch-001.thumbnail.png',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
plazaInternals.resolvePlazaPublicationCoverUrl({
|
|
|
|
|
inputCoverUrl: 'https://cdn.example.com/cover.png',
|
|
|
|
|
publicUrl: '/u/john/pages/demo',
|
|
|
|
|
publicationUrlSlug: 'demo',
|
|
|
|
|
userSlug: 'john',
|
|
|
|
|
}),
|
|
|
|
|
'https://cdn.example.com/cover.png',
|
|
|
|
|
);
|
2026-06-26 15:19:03 +08:00
|
|
|
});
|
|
|
|
|
|
2026-06-15 15:04:43 -07:00
|
|
|
test('mapPlazaError maps documented API codes', () => {
|
|
|
|
|
assert.equal(mapPlazaError({ code: 'ALREADY_PUBLISHED' }), 409);
|
|
|
|
|
assert.equal(mapPlazaError({ code: 'PUBLICATION_NOT_ONLINE' }), 422);
|
|
|
|
|
assert.equal(mapPlazaError({ code: 'POST_PERMISSION_DENIED' }), 403);
|
|
|
|
|
assert.equal(mapPlazaError({ code: 'unknown' }), 500);
|
|
|
|
|
});
|