d35330d530
- 提取存储解析、通知已读、表格偏好和企业筛选参数逻辑 - 修复分页尺寸切换的旧页码、筛选丢失及 Semi 重复回调问题 - 补充 51 项回归测试,统一检查入口并修复格式配置和 JSON 语法 - 记录 179 项测试、生产构建和隔离浏览器交互验收结果 - 删除过期迁移与性能报告并同步开发文档 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
150 lines
5.0 KiB
JavaScript
150 lines
5.0 KiB
JavaScript
/*
|
|
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}');
|
|
});
|