Files
client/scripts/smoke-file-settings-offscreen.mjs
T

204 lines
12 KiB
JavaScript

// Real hidden Electron File Transfer settings: switches, speed additions and concurrent TSInputNumber
// rows persist through main across a restart; out-of-range typing clamps in the UI and main rejects
// out-of-range IPC. The folder chooser answers only with a disposable-profile fixture folder; the second
// launch has no fixture, so the chooser and clipboard copy are both denied. No native dialog, OS clipboard,
// server, device or visible window is used.
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
import { launchApp } from './lib/electron-app.mjs';
const args = process.argv.slice(2);
const option = (name) => (args.includes(`--${name}`) ? args[args.indexOf(`--${name}`) + 1] : undefined);
const out = path.resolve(option('out') ?? 'smoke-out/file-settings-offscreen');
const profile = mkdtempSync(path.join(tmpdir(), 'teamspeach-file-settings-qa-'));
const fixtureFolder = path.join(profile, 'qa-downloads');
mkdirSync(out, { recursive: true });
mkdirSync(fixtureFolder);
const result = {
scope: 'Actual renderer and main files-settings store in a disposable hidden profile; folder choice only from a profile fixture, clipboard/native dialogs denied',
checks: [],
errors: [],
};
const ROOT = '#file-transfer-settings';
const row = (setting) => `${ROOT} [data-setting="${setting}"]`;
const spin = (setting) => `${row(setting)} input[role="spinbutton"]`;
const step = (setting, which) => `${row(setting)} .tsv-number-picker-button:${which === 'plus' ? 'last-child' : 'first-child'}`;
const toggle = (setting) => `${row(setting)} input[role="switch"]`;
const segment = (name) => `${row('downloadLocation')} [data-segment="${name}"]`;
const settings = (page) => page.evaluate(async () => (await window.teamspeach.filesSettings()).settings);
/** Polls main's stored settings from Node, so predicates never run as page code. */
async function until(page, predicate, timeout = 15000) {
const deadline = Date.now() + timeout;
for (;;) {
const current = await settings(page);
if (predicate(current)) return current;
if (Date.now() >= deadline) throw new Error(`Settings did not satisfy ${predicate}: ${JSON.stringify(current)}`);
await delay(50);
}
}
async function openTransfers(page) {
await page.click('#btn-settings');
await page.click('#settings-nav [data-panel="transfers"]');
await page.waitForSelector(spin('uploadSlots'), { state: 'visible' });
}
let app;
let page;
try {
app = await launchApp({ profile, exe: option('exe'), env: { TEAMSPEACH_TEST_FILES_DOWNLOAD_DIR: fixtureFolder } });
page = await app.firstWindow();
page.on('pageerror', (error) => result.errors.push(error.message));
await page.waitForSelector('#view-onboarding', { state: 'visible' });
await page.fill('#onboarding-nick', 'File Settings QA');
await page.click('#onboarding-submit');
await page.waitForSelector('#view-app', { state: 'visible' });
await openTransfers(page);
// Source composition and unchanged operational defaults.
const defaults = await settings(page);
assert.equal(defaults.alwaysAsk, true);
assert.equal(defaults.uploadSlots, 2);
assert.equal(defaults.downloadSlots, 2);
assert.equal(defaults.limitUpload, false);
assert.equal(defaults.limitDownload, false);
assert.deepEqual(await page.evaluate((root) => [...document.querySelectorAll(`${root} .ts-card > [data-setting]`)].map((node) => node.dataset.setting), ROOT),
['alwaysAsk', 'limitUpload', 'limitDownload', 'uploadSlots', 'downloadSlots']);
assert.equal(await page.locator(toggle('alwaysAsk')).isChecked(), true);
assert.equal(await page.locator(`${ROOT} input[type="number"]`).count(), 0);
assert.equal(await page.locator(spin('uploadSlots')).getAttribute('aria-valuenow'), '2');
assert.equal(await page.locator(spin('uploadSlots')).getAttribute('aria-valuemax'), '8');
assert.equal(await page.locator(`${ROOT} .form-error`).count(), 0);
await page.screenshot({ path: path.join(out, 'defaults.png') });
result.checks.push('Defaults stay always-ask and 2/2 concurrency; rows follow the source order with no leading note or number boxes; the folder row is absent while always-ask is on');
// Keyboard stepping keeps focus and bounds the other picker by the combined limit of 10.
await page.focus(spin('uploadSlots'));
for (let i = 0; i < 3; i++) await page.keyboard.press('ArrowUp');
await until(page, (s) => s.uploadSlots === 5);
assert.equal(await page.evaluate(() => document.activeElement?.id), 'file-transfer-uploadSlots');
assert.equal(await page.locator(spin('downloadSlots')).getAttribute('aria-valuemax'), '5');
for (let i = 0; i < 3; i++) await page.click(step('downloadSlots', 'plus'));
await until(page, (s) => s.downloadSlots === 5);
assert.equal(await page.locator(step('downloadSlots', 'plus')).isDisabled(), true);
assert.equal(await page.locator(step('uploadSlots', 'plus')).isDisabled(), true);
assert.equal(await page.locator(spin('uploadSlots')).getAttribute('aria-valuemax'), '5');
result.checks.push('ArrowUp and + persist 5 uploads / 5 downloads; focus stays in the spinbutton; + is disabled at the combined limit');
// Rejected bounds: invalid typing is flagged, clamps on Enter, letters are refused, Escape restores.
await page.fill(spin('uploadSlots'), '9');
assert.equal(await page.locator(spin('uploadSlots')).getAttribute('aria-invalid'), 'true');
await page.keyboard.press('Enter');
assert.equal(await page.locator(spin('uploadSlots')).inputValue(), '5');
assert.equal(await page.locator(spin('uploadSlots')).getAttribute('aria-invalid'), null);
assert.match(await page.locator(spin('uploadSlots')).getAttribute('class'), /tsv-clamped/);
await page.fill(spin('uploadSlots'), 'abc');
assert.match(await page.locator(spin('uploadSlots')).inputValue(), /^\d*$/);
await page.keyboard.press('Enter');
assert.equal(await page.locator(spin('uploadSlots')).inputValue(), '5');
await page.fill(spin('uploadSlots'), '3');
await until(page, (s) => s.uploadSlots === 3);
await page.keyboard.press('Escape');
await until(page, (s) => s.uploadSlots === 5);
assert.notEqual(await page.evaluate(() => document.activeElement?.id), 'file-transfer-uploadSlots');
assert.equal((await settings(page)).downloadSlots, 5);
result.checks.push('Typed 9 is marked invalid and clamps to 5 with the clamp notice; letters are refused; a typed 3 saves and Escape restores 5');
const rejected = await page.evaluate(async () => Promise.all([
{ uploadSlots: 6 }, { uploadSlots: 0 }, { downloadMiB: 1.3 }, { uploadMiB: 25601 }, { approvedDownloadDir: 'C:/' },
].map(async (partial) => (await window.teamspeach.filesSetSettings(partial)).ok)));
assert.deepEqual(rejected, [false, false, false, false, false]);
const afterRejected = await settings(page);
assert.equal(afterRejected.uploadSlots, 5);
assert.equal(afterRejected.downloadSlots, 5);
result.checks.push('Main rejects a combined total of 11, zero slots, non-half-step and >25600 MiB/s rates and renderer folder paths; stored values are unchanged');
// Speed limit additions: toggle, clamp, snap to the 0.5 step, arrow stepping.
await page.click(toggle('limitUpload'));
await until(page, (s) => s.limitUpload === true);
await page.waitForSelector(spin('uploadMiB'), { state: 'visible' });
assert.equal(await page.locator(spin('uploadMiB')).inputValue(), '10');
assert.equal(await page.locator(spin('uploadMiB')).getAttribute('aria-valuemax'), '25600');
await page.fill(spin('uploadMiB'), '30000');
assert.equal(await page.locator(spin('uploadMiB')).getAttribute('aria-invalid'), 'true');
await page.keyboard.press('Enter');
await until(page, (s) => s.uploadMiB === 25600);
await page.fill(spin('uploadMiB'), '2.3');
await page.keyboard.press('Enter');
await until(page, (s) => s.uploadMiB === 2.5);
await page.keyboard.press('ArrowDown');
await until(page, (s) => s.uploadMiB === 2);
await page.focus(toggle('limitDownload'));
await page.keyboard.press('Space');
await until(page, (s) => s.limitDownload === true);
await page.waitForSelector(spin('downloadMiB'), { state: 'visible' });
assert.equal(await page.evaluate(() => document.activeElement?.closest('[data-setting]')?.dataset.setting), 'limitDownload');
await page.keyboard.press('Space');
await until(page, (s) => s.limitDownload === false);
assert.equal(await page.locator(row('downloadMiB')).count(), 0);
result.checks.push('Upload speed addition clamps 30000 to 25600, snaps 2.3 to 2.5 and steps to 2; the download switch toggles by keyboard without losing focus');
// Download location: source disclosure, fixture folder choice, denied copy.
await page.click(toggle('alwaysAsk'));
await until(page, (s) => s.alwaysAsk === false);
await page.waitForSelector(row('downloadLocation'), { state: 'visible' });
assert.equal(await page.locator(segment('copy')).isDisabled(), true);
await page.click(segment('open'));
await until(page, (s) => s.downloadFolder === 'qa-downloads');
await page.waitForFunction((selector) => document.querySelector(selector)?.textContent === 'qa-downloads', `${row('downloadLocation')} .tsv-font-subtitle`);
assert.equal(await page.locator(segment('copy')).isDisabled(), false);
await page.click(segment('copy'));
await page.waitForSelector(`${ROOT} .form-error[role="alert"]`, { state: 'visible' });
assert.equal(await page.locator(`${ROOT} .form-error`).count(), 1);
assert.equal(await page.locator(row('downloadLocation')).evaluate((node) => node.parentElement.contains(document.querySelector('#file-transfer-settings .form-error'))), true);
result.copyDenied = await page.locator(`${ROOT} .form-error`).innerText();
await page.screenshot({ path: path.join(out, 'edited.png') });
result.checks.push('Turning always-ask off discloses Choose download location; the folder segment stores only the profile fixture; copy is denied in the General card');
await app.close();
app = null;
app = await launchApp({ profile, exe: option('exe') });
page = await app.firstWindow();
page.on('pageerror', (error) => result.errors.push(error.message));
await page.waitForSelector('#view-app', { state: 'visible' });
await openTransfers(page);
const restored = await settings(page);
for (const [key, value] of Object.entries({ alwaysAsk: false, uploadSlots: 5, downloadSlots: 5, limitUpload: true, uploadMiB: 2, limitDownload: false, downloadFolder: 'qa-downloads' })) {
assert.equal(restored[key], value, key);
}
assert.equal(await page.locator(toggle('alwaysAsk')).isChecked(), false);
assert.equal(await page.locator(toggle('limitUpload')).isChecked(), true);
assert.equal(await page.locator(spin('uploadSlots')).inputValue(), '5');
assert.equal(await page.locator(spin('downloadSlots')).inputValue(), '5');
assert.equal(await page.locator(spin('uploadMiB')).inputValue(), '2');
assert.equal(await page.locator(`${row('downloadLocation')} .tsv-font-subtitle`).innerText(), 'qa-downloads');
await page.click(segment('open'));
await page.waitForSelector(`${ROOT} .form-error[role="alert"]`, { state: 'visible' });
result.chooserDenied = await page.locator(`${ROOT} .form-error`).innerText();
assert.equal((await settings(page)).downloadFolder, 'qa-downloads');
await page.click(toggle('alwaysAsk'));
await until(page, (s) => s.alwaysAsk === true);
await page.waitForSelector(row('downloadLocation'), { state: 'detached' });
result.checks.push('Restart restores every value in the UI; without a fixture the folder chooser is denied and keeps the stored folder; always-ask is restored');
assert.deepEqual(result.errors, []);
result.ok = true;
} catch (error) {
result.ok = false;
result.failure = error.stack;
process.exitCode = 1;
result.state = await page?.evaluate(async () => ({
settings: (await window.teamspeach.filesSettings()).settings,
panel: document.querySelector('#file-transfer-settings')?.innerText,
})).catch(() => null);
await page?.screenshot({ path: path.join(out, 'failure.png') }).catch(() => {});
} finally {
if (app) await app.close().catch(() => {});
rmSync(profile, { recursive: true, force: true });
writeFileSync(path.join(out, 'result.json'), JSON.stringify(result, null, 2));
console.log(JSON.stringify(result, null, 2));
}