SHA256
30 lines
1.7 KiB
JavaScript
30 lines
1.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { publicLikeCount, hasLikeStatistics } from '../js/services/public-like-count.js';
|
|
|
|
test('public likes only count declared Primary accounts', () => {
|
|
const post = { likesCount: 20, primaryLikesCount: 5, shiningLikesCount: 2 };
|
|
assert.equal(publicLikeCount(post), 5);
|
|
assert.equal(hasLikeStatistics(post), true);
|
|
assert.equal(publicLikeCount({ likesCount: 20, primaryLikesCount: 0 }), 0);
|
|
assert.equal(hasLikeStatistics({ likesCount: 20, primaryLikesCount: 0 }), true);
|
|
assert.equal(publicLikeCount({ likesCount: 20 }), 0);
|
|
assert.equal(publicLikeCount({ primaryLikesCount: -1 }), 0);
|
|
assert.equal(publicLikeCount({ primaryLikesCount: 'invalid' }), 0);
|
|
assert.equal(hasLikeStatistics({ likesCount: 0, primaryLikesCount: 0 }), false);
|
|
});
|
|
|
|
test('feed and both thread layouts use the public count and keep stats accessible', () => {
|
|
const src = name => readFileSync(new URL(`../js/pages/${name}`, import.meta.url), 'utf8');
|
|
const feed = src('channel-view.js');
|
|
const thread = src('channel-thread-view.js');
|
|
assert.match(feed, /likesStatsButton\.textContent = String\(publicLikeCount\(post\)\)/);
|
|
assert.equal((feed.match(/hasLikeStatistics\(post\) \? \[likesStatsButton\]/g) || []).length, 2);
|
|
assert.equal((thread.match(/const likes = publicLikeCount\(node\);/g) || []).length, 2);
|
|
assert.match(thread, /commentLikeStats\.textContent = String\(likes\)/);
|
|
assert.equal((thread.match(/hasLikeStatistics\(node\)/g) || []).length, 3);
|
|
assert.match(feed, /counts: post/);
|
|
assert.match(thread, /showLikeStatistics\(\{ counts: node \}\)/);
|
|
});
|