Files
SHiNE-server/shine-UI/tests/account-voting.test.mjs
T

54 lines
3.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { profileCardHtml } from '../js/components/profile-card.js';
import { accountRoleFromHeader, confirmationRights } from '../js/services/account-voting.js';
test('only explicit HEADER V3 type 1 is Primary; no legacy fallbacks', () => {
assert.equal(accountRoleFromHeader(1), 'primary');
assert.equal(accountRoleFromHeader(0), 'non_voting');
for (const x of [undefined, null, '1', 2, true]) assert.equal(accountRoleFromHeader(x), '');
});
test('confirmation permissions match all actor and target categories', () => {
const types = [null, 0, 1];
const statuses = ['', 'shining', 'unknown', 'not_interested'];
for (const a of types) for (const b of types) for (const as of statuses) for (const bs of statuses) {
const actor = { login: 'A@free', accountRole: accountRoleFromHeader(a), shineStatus: as };
const target = { login: 'B@free', accountRole: accountRoleFromHeader(b), shineStatus: bs };
assert.deepEqual(confirmationRights(actor, target), { primary: a === 1 && b === 1, shine: a === 1 && as === 'shining' && bs === 'shining' });
assert.deepEqual(confirmationRights(actor, { ...target, login: 'a@FREE' }), { primary: false, shine: false });
}
assert.deepEqual(confirmationRights(null, null), {primary:false,shine:false});
});
test('profile has no official toggle; both confirmation actions enforce shared rules', () => {
const src = name => readFileSync(new URL(`../js/pages/${name}`, import.meta.url), 'utf8');
const profile = src('profile-edit-view.js');
assert.match(profile, /class="badge profile-account-info is-no" data-status="account_role"/);
assert.doesNotMatch(profile, /saveProfileStatus\(login, 'account_role'/);
const relations = src('user-relation-manage-view.js');
assert.match(relations, /confirmationRights\(selfCard, targetCard\)/);
assert.match(relations, /!rights\.primary/);
assert.match(relations, /!rights\.shine/);
});
test('status cards show only declared statuses; unofficial shining accounts only show received confirmations', () => {
for (const isSelf of [true, false]) for (const official of [true, false]) for (const shining of [true, false]) {
const html = profileCardHtml({ isSelf, card: { login: 'test@free', accountRole: official ? 'primary' : 'non_voting', shineStatus: shining ? 'shining' : 'unknown', stats: { primaryReceivedCount: 9, primaryGivenCount: 9, shineReceivedCount: 9, shineGivenCount: 9 } } });
assert.equal(html.includes('data-profile-list="primary_received"'), official);
assert.equal(html.includes('data-profile-list="primary_given"'), official);
assert.equal(html.includes('data-profile-list="shine_received"'), shining);
assert.equal(html.includes('data-profile-list="shine_given"'), official && shining);
}
});
test('multi-profile mode is disabled at the direct route and session switching entry points', () => {
const read = path => readFileSync(new URL('../js/' + path, import.meta.url), 'utf8');
const state = read('state.js');
assert.match(state, /MULTI_PROFILE_ENABLED = false/);
assert.match(state, /switchToSavedProfile\(login\) \{\s*if \(!MULTI_PROFILE_ENABLED\) throw/);
assert.match(state, /prepareAddProfileLogin\(\) \{\s*if \(!MULTI_PROFILE_ENABLED\) throw/);
assert.match(state, /await terminateCurrentSession\(\{ closeServerSession: true, notifySessionReset: false \}\)/);
assert.doesNotMatch(read('pages/profile-view.js'), /data-self-profile-action="profiles"/);
assert.doesNotMatch(read('app.js'), /switchToSavedProfile\(/);
});