refactor(shared): 提取公共状态逻辑并修复用户列表分页
统一存储解析、通知已读状态、表格偏好和企业日志筛选参数。修复切换每页条数时沿用旧页码、丢失筛选条件和重复触发分页回调的问题。 同步调整项目检查脚本、格式配置和开发文档。
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { resolveColumnVisibility } from './columnVisibility.js';
|
||||
|
||||
const defaults = Object.freeze({ time: true, model: true, channel: false });
|
||||
|
||||
test('missing or malformed saved JSON falls back to a fresh copy of defaults', () => {
|
||||
for (const saved of [
|
||||
undefined,
|
||||
null,
|
||||
'',
|
||||
'{',
|
||||
'not-json',
|
||||
'{"time":false,}',
|
||||
]) {
|
||||
const resolved = resolveColumnVisibility(defaults, saved);
|
||||
assert.deepEqual(resolved, defaults);
|
||||
assert.notStrictEqual(resolved, defaults);
|
||||
}
|
||||
});
|
||||
|
||||
test('null, arrays, and primitive JSON values are not preference maps', () => {
|
||||
for (const saved of [
|
||||
'null',
|
||||
'[]',
|
||||
'[false]',
|
||||
'"text"',
|
||||
'0',
|
||||
'true',
|
||||
'false',
|
||||
]) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, saved), defaults);
|
||||
}
|
||||
});
|
||||
|
||||
test('non-string saved data is ignored without coercion or mutation', () => {
|
||||
const saved = Object.freeze({ time: false });
|
||||
for (const value of [saved, [false], 1, true]) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, value), defaults);
|
||||
}
|
||||
assert.deepEqual(saved, { time: false });
|
||||
});
|
||||
|
||||
test('saved false and true values override defaults while new columns keep defaults', () => {
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(defaults, '{"time":false,"channel":true}'),
|
||||
{ time: false, model: true, channel: true },
|
||||
);
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, '{}'), defaults);
|
||||
});
|
||||
|
||||
test('invalid individual preferences keep defaults without discarding valid booleans', () => {
|
||||
for (const value of [null, 0, 1, '', 'false', [], {}]) {
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(
|
||||
defaults,
|
||||
JSON.stringify({ time: value, channel: value, model: false }),
|
||||
),
|
||||
{ time: true, model: false, channel: false },
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('unknown and prototype-related saved keys do not enter the result', () => {
|
||||
const resolved = resolveColumnVisibility(
|
||||
defaults,
|
||||
'{"time":false,"removedColumn":true,"__proto__":{"polluted":true},"constructor":false,"toString":false}',
|
||||
);
|
||||
assert.deepEqual(resolved, { time: false, model: true, channel: false });
|
||||
assert.equal(Object.getPrototypeOf(resolved), Object.prototype);
|
||||
assert.equal(Object.hasOwn(resolved, '__proto__'), false);
|
||||
});
|
||||
|
||||
for (const [table, restrictedKeys] of [
|
||||
['usage logs', ['channel', 'username', 'retry']],
|
||||
['task logs', ['channel']],
|
||||
]) {
|
||||
for (const isAdmin of [true, false]) {
|
||||
test(`${table}: ${isAdmin ? 'admin' : 'non-admin'} preferences respect role restrictions`, () => {
|
||||
const roleDefaults = {
|
||||
model: true,
|
||||
...Object.fromEntries(restrictedKeys.map((key) => [key, isAdmin])),
|
||||
};
|
||||
const hiddenKeys = isAdmin ? [] : restrictedKeys;
|
||||
for (const checked of [true, false]) {
|
||||
const preferences = {
|
||||
model: false,
|
||||
...Object.fromEntries(restrictedKeys.map((key) => [key, checked])),
|
||||
};
|
||||
const resolved = resolveColumnVisibility(
|
||||
roleDefaults,
|
||||
JSON.stringify(preferences),
|
||||
hiddenKeys,
|
||||
);
|
||||
assert.equal(resolved.model, false);
|
||||
for (const key of restrictedKeys) {
|
||||
assert.equal(resolved[key], isAdmin && checked);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(roleDefaults, null, hiddenKeys),
|
||||
roleDefaults,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test('forced-hidden keys apply even when saved preferences are missing or invalid', () => {
|
||||
for (const saved of [null, '{', 'null', '[]', '{}', '{"time":true}']) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, saved, ['time']), {
|
||||
time: false,
|
||||
model: true,
|
||||
channel: false,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('resolution never mutates defaults or hidden keys and returns independent results', () => {
|
||||
const hiddenKeys = Object.freeze(['channel']);
|
||||
const saved = '{"time":false,"channel":true}';
|
||||
const first = resolveColumnVisibility(defaults, saved, hiddenKeys);
|
||||
const second = resolveColumnVisibility(defaults, saved, hiddenKeys);
|
||||
|
||||
assert.notStrictEqual(first, second);
|
||||
first.model = false;
|
||||
assert.deepEqual(second, { time: false, model: true, channel: false });
|
||||
assert.deepEqual(defaults, { time: true, model: true, channel: false });
|
||||
assert.deepEqual(hiddenKeys, ['channel']);
|
||||
assert.equal(saved, '{"time":false,"channel":true}');
|
||||
});
|
||||
Reference in New Issue
Block a user