Files
client/test/information-panels.test.js
T

168 lines
12 KiB
JavaScript

'use strict';
// Settings > About data service: CHANGELOG.md, core/renderer/Chromium notices and the production npm tree.
// Synthetic fixtures in a temporary directory plus the repository's own files; no Electron, network or profile.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const {
parseChangelog, parseCoreNotices, parseVendorNotices, parseChromiumCredits, collectProductionPackages,
npmRepository, informationPaths, createInformationService, registerInformationIpc,
} = require('../app/main/information-panels.js');
const ROOT = path.resolve(__dirname, '..');
const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'tp-info-'));
const write = (file, text) => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, text); };
test('changelog headings, paragraphs and continued bullet items become official entries', () => {
const entries = parseChangelog([
'# Changelog', '', '## Unreleased', '', '- First change with `code`', ' continued here.', '- Second', '',
'## 0.1.4 — 2026-10-24', '', 'Intro paragraph', 'on two lines.', '', '- Fix', '',
'## 0.1.0 — 2026-10-24 (pre-release, superseded by 0.1.1)', '', '- Initial',
].join('\n'));
assert.deepEqual(entries, [
{ version: 'Unreleased', date: null, qualifier: null, unreleased: true, notes: [], changes: ['First change with `code` continued here.', 'Second'] },
{ version: '0.1.4', date: '2026-10-24', qualifier: null, unreleased: false, notes: ['Intro paragraph on two lines.'], changes: ['Fix'] },
{ version: '0.1.0', date: '2026-10-24', qualifier: '(pre-release, superseded by 0.1.1)', unreleased: false, notes: [], changes: ['Initial'] },
]);
});
test('the repository CHANGELOG.md parses into dated releases', () => {
const entries = parseChangelog(fs.readFileSync(path.join(ROOT, 'CHANGELOG.md'), 'utf8'));
assert.ok(entries.length >= 2);
for (const entry of entries) assert.ok(entry.changes.length > 0 || entry.notes.length > 0, entry.version);
assert.ok(entries.some((e) => e.date && /^\d+\.\d+\.\d+$/.test(e.version)));
});
test('core notices: table rows, shared license sections and the bundled libopus section', () => {
const md = [
'# Third-party notices for teamspeach-core', '', 'Generated by a tool.', '',
'| Crate | Version | License | Source |', '|---|---|---|---|',
'| aead | 0.5.2 | MIT OR Apache-2.0 | registry+https://github.com/rust-lang/crates.io-index |',
'| tsproto | 0.2.0 | MIT | git+https://github.com/ReSpeak/tsclientlib?rev=ee#ee |',
'| tsclientlib | 0.2.0 | MIT | vendored (core/vendor) |', '',
'## libopus (bundled in audiopus_sys, statically linked)', '', '```', 'Opus text', '```', '',
'## License texts', '', '### aead 0.5.2 (LICENSE-APACHE); tsproto 0.2.0 (LICENSE-APACHE)', '', '```', 'Apache', '```', '',
'### aead 0.5.2 (LICENSE-MIT)', '', '```', 'MIT text', '```',
].join('\n');
const { intro, entries } = parseCoreNotices(md);
assert.equal(intro, 'Generated by a tool.');
assert.deepEqual(entries.map((e) => [e.name, e.repository]), [
['libopus (bundled in audiopus_sys, statically linked)', null],
['aead', 'https://crates.io/crates/aead/0.5.2'],
['tsproto', 'https://github.com/ReSpeak/tsclientlib'],
['tsclientlib', null],
]);
assert.deepEqual(entries[1].texts, [{ label: 'LICENSE-APACHE', text: 'Apache' }, { label: 'LICENSE-MIT', text: 'MIT text' }]);
assert.equal(entries[0].texts[0].text, 'Opus text');
assert.equal(entries[3].texts.length, 0);
});
test('vendor notices split per package, keep the scope statement and unwrap parenthesised expressions', () => {
const { intro, entries } = parseVendorNotices([
'Chat renderer dependency notices', 'Includes build-only deps.', '',
'===== @scope/pkg@1.2.3 (MIT) =====', '', '--- LICENSE ---', 'MIT body', '',
'===== dompurify@3.4.17 ((MPL-2.0 OR Apache-2.0)) =====', '--- LICENSE ---', 'Dual',
].join('\n'));
assert.equal(intro, 'Includes build-only deps.');
assert.deepEqual(entries, [
{ name: '@scope/pkg', version: '1.2.3', license: 'MIT', text: '--- LICENSE ---\nMIT body' },
{ name: 'dompurify', version: '3.4.17', license: 'MPL-2.0 OR Apache-2.0', text: '--- LICENSE ---\nDual' },
]);
});
test('Chromium credits: one product per block, entities decoded, only http(s) homepages linked', () => {
const html = '<div class="product">\n<span class="title">A &amp; B</span>\n<span class="homepage"><a href="https://a.example/">homepage</a></span>\n'
+ '<div class="license">\n<pre>&quot;x&quot; &lt;y&gt; &#169;</pre>\n</div>\n</div>\n'
+ '<div class="product">\n<span class="title">C</span>\n<span class="homepage"><a href="javascript:x">homepage</a></span>\n<div class="license"><pre>c</pre></div></div>';
assert.deepEqual(parseChromiumCredits(html), [
{ name: 'A & B', repository: 'https://a.example/', text: '"x" <y> ©' },
{ name: 'C', repository: null, text: 'c' },
]);
});
test('npm repository fields normalise to https URLs only', () => {
assert.equal(npmRepository('nodeca/js-yaml'), 'https://github.com/nodeca/js-yaml');
assert.equal(npmRepository({ url: 'git+https://github.com/npm/node-semver.git' }), 'https://github.com/npm/node-semver');
assert.equal(npmRepository({ url: 'git@github.com:jprichardson/node-jsonfile.git' }), 'https://github.com/jprichardson/node-jsonfile');
assert.equal(npmRepository({ url: 'git://github.com/debug-js/debug.git' }), 'https://github.com/debug-js/debug');
assert.equal(npmRepository({ url: 'git+ssh://git@github.com/isaacs/sax-js.git' }), 'https://github.com/isaacs/sax-js');
assert.equal(npmRepository({ url: 'file:../x' }), null);
assert.equal(npmRepository(undefined), null);
});
test('production npm tree follows dependencies only, resolves nested copies and lists license files', () => {
const dir = tmp();
write(path.join(dir, 'package.json'), JSON.stringify({ dependencies: { a: '1' }, devDependencies: { dev: '1' } }));
write(path.join(dir, 'node_modules/a/package.json'), JSON.stringify({ name: 'a', version: '1.0.0', license: 'MIT', dependencies: { b: '2' } }));
write(path.join(dir, 'node_modules/a/LICENSE'), 'A');
write(path.join(dir, 'node_modules/a/node_modules/b/package.json'), JSON.stringify({ name: 'b', version: '2.0.0', license: 'ISC' }));
write(path.join(dir, 'node_modules/b/package.json'), JSON.stringify({ name: 'b', version: '1.0.0', license: 'ISC' }));
write(path.join(dir, 'node_modules/dev/package.json'), JSON.stringify({ name: 'dev', version: '1.0.0' }));
const { packages, missing } = collectProductionPackages(dir);
assert.deepEqual(packages.map((p) => [p.name, p.version, p.files]), [['a', '1.0.0', ['LICENSE']], ['b', '2.0.0', []]]);
assert.deepEqual(missing, []);
});
test('service serves a fixture installation and only ids from its own index', async () => {
const dir = tmp();
const exe = path.join(dir, 'electron', 'electron.exe');
write(path.join(dir, 'CHANGELOG.md'), '# Changelog\n\n## 1.0.0 — 2026-01-02\n\n- Hello\n');
write(path.join(dir, 'LICENSES/NOTICE.md'), 'Our notice');
write(path.join(dir, 'core-bin/win-x64/THIRD_PARTY_NOTICES.md'), '| Crate | Version | License | Source |\n|---|---|---|---|\n| x | 1.0.0 | MIT | registry+https://github.com/rust-lang/crates.io-index |\n\n## License texts\n\n### x 1.0.0 (LICENSE)\n\n```\nX\n```\n');
write(path.join(dir, 'package.json'), JSON.stringify({ dependencies: {} }));
write(path.join(dir, 'app/renderer/vendor/THIRD_PARTY_NOTICES.txt'), 'Title\n\n===== v@1.0.0 (MIT) =====\nV\n');
write(path.join(path.dirname(exe), 'LICENSE'), 'Electron MIT');
const service = createInformationService({ isPackaged: false, appRoot: dir, appPath: dir, resourcesPath: path.join(dir, 'electron', 'resources'), execPath: exe, rendererDir: path.join(dir, 'app/renderer'), platform: 'win32', arch: 'x64', electronVersion: '44.9.3' });
assert.deepEqual((await service.getChangelog()).entries[0].changes, ['Hello']);
const list = await service.getLicenses();
assert.equal(list.ok, true);
const byGroup = Object.fromEntries(list.groups.map((g) => [g.id, g]));
assert.deepEqual(list.groups.map((g) => g.id), ['teamspeach', 'electron', 'chromium', 'npm', 'core', 'renderer']);
assert.equal(byGroup.chromium.error.code, 'missing');
assert.equal(byGroup.electron.entries[0].version, '44.9.3');
const crate = byGroup.core.entries[0];
assert.equal((await service.getLicenseText(crate.id)).text, '--- LICENSE ---\nX');
assert.equal((await service.getLicenseText(byGroup.teamspeach.entries[0].id)).text, 'Our notice');
assert.equal((await service.getLicenseText(byGroup.renderer.entries[0].id)).text, 'V');
assert.equal((await service.getLicenseText('core:99')).error.code, 'not_found');
assert.equal((await service.getLicenseText('../../etc/passwd')).error.code, 'bad_request');
assert.equal((await service.getLicenseText({ id: 1 })).error.code, 'bad_request');
});
test('missing CHANGELOG.md is reported, not replaced', async () => {
const dir = tmp();
write(path.join(dir, 'package.json'), '{}');
const service = createInformationService({ isPackaged: true, appRoot: dir, appPath: dir, resourcesPath: path.join(dir, 'resources'), execPath: path.join(dir, 'app.exe'), rendererDir: path.join(dir, 'renderer'), platform: 'linux', arch: 'x64' });
assert.deepEqual(await service.getChangelog(), { ok: false, error: { code: 'missing', file: 'CHANGELOG.md' } });
});
test('packaged paths are fixed below resources and the executable directory', () => {
const p = informationPaths({ isPackaged: true, appRoot: '/x', appPath: '/opt/tp/resources/app.asar', resourcesPath: '/opt/tp/resources', execPath: '/opt/tp/teamspeach', rendererDir: '/opt/tp/resources/app.asar/app/renderer', platform: 'linux', arch: 'x64' });
assert.deepEqual(p.changelog, [path.join('/opt/tp/resources', 'CHANGELOG.md')]);
assert.equal(p.licensesDir, path.join('/opt/tp/resources', 'licenses'));
assert.deepEqual(p.electronLicenses, [path.join('/opt/tp', 'LICENSE.electron.txt'), path.join('/opt/tp/resources', 'LICENSE.electron.txt')]);
assert.deepEqual(p.chromiumCredits, [path.join('/opt/tp', 'LICENSES.chromium.html')]);
});
test('IPC handlers refuse untrusted senders', async () => {
const handlers = new Map();
const service = { getChangelog: async () => ({ ok: true }), getLicenses: async () => ({ ok: true }), getLicenseText: async (id) => ({ ok: true, id }) };
registerInformationIpc({ ipcMain: { handle: (name, fn) => handlers.set(name, fn) }, trustedSender: (event) => event.trusted, service });
assert.deepEqual([...handlers.keys()], ['about:changelog', 'about:licenses', 'about:license-text']);
assert.equal((await handlers.get('about:licenses')({ trusted: false })).error.code, 'forbidden');
assert.deepEqual(await handlers.get('about:license-text')({ trusted: true }, 'npm:0'), { ok: true, id: 'npm:0' });
});
test('repository sources parse: core notices, renderer vendor notices and the Electron credits page', { skip: !fs.existsSync(path.join(ROOT, 'node_modules/electron/dist/LICENSES.chromium.html')) && 'electron dist not installed' }, () => {
const core = parseCoreNotices(fs.readFileSync(path.join(ROOT, 'core/THIRD_PARTY_NOTICES.md'), 'utf8'));
assert.ok(core.entries.length > 100);
assert.ok(core.entries.filter((e) => !e.name.startsWith('libopus')).every((e) => e.texts.length > 0), 'every crate has a license text');
const vendor = parseVendorNotices(fs.readFileSync(path.join(ROOT, 'app/renderer/vendor/THIRD_PARTY_NOTICES.txt'), 'utf8'));
assert.ok(vendor.entries.some((e) => e.name === 'katex'));
const chromium = parseChromiumCredits(fs.readFileSync(path.join(ROOT, 'node_modules/electron/dist/LICENSES.chromium.html'), 'utf8'));
assert.ok(chromium.length > 100);
assert.ok(chromium.every((p) => p.name && p.text));
});