Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src-tauri/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ pub struct Settings {
/// finishes downloading (an update too), its setup opens straight away. Off: it waits for Set Up.
#[serde(default = "yes")]
pub auto_install: bool,
/// Settings → Update All Games Automatically: every installed game takes its new release as
/// soon as the launcher finds it, like each game's own Update automatically. Off by default,
/// so nobody's games start updating by themselves after the launcher updates.
#[serde(default)]
pub auto_update_all: bool,
}

fn yes() -> bool {
Expand Down Expand Up @@ -547,6 +552,7 @@ impl Default for Settings {
linux_graphics: String::new(),
steam_sync: false,
auto_install: true,
auto_update_all: false,
}
}
}
Expand Down Expand Up @@ -931,6 +937,21 @@ mod tests {
assert!(!restored.auto_install);
}

#[test]
fn update_all_games_automatically_is_off_by_default_and_for_settings_saved_before_it() {
assert!(!super::Settings::default().auto_update_all);
let settings: super::Settings = serde_json::from_str(r#"{"library_dir":"/games"}"#).unwrap();
assert!(!settings.auto_update_all);
}

#[test]
fn update_all_games_automatically_turned_on_stays_on() {
let settings: super::Settings = serde_json::from_str(r#"{"library_dir":"/games","auto_update_all":true}"#).unwrap();
let saved = serde_json::to_string(&settings).unwrap();
let restored: super::Settings = serde_json::from_str(&saved).unwrap();
assert!(restored.auto_update_all);
}

#[test]
fn run_in_background_is_off_by_default() {
assert!(!super::Settings::default().close_to_tray);
Expand Down
4 changes: 3 additions & 1 deletion tests/auto-install.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const app = fs.readFileSync(path.join(__dirname, '../ui/app.js'), 'utf8');
const tv = fs.readFileSync(path.join(__dirname, '../ui/tv.js'), 'utf8');
const fn = (src, name, indent = '') => src.match(new RegExp(`^${indent}(?:async )?function ${name}\\([^]*?^${indent}}`, 'm'))[0];
const autoInstallSrc = app.match(/^const autoInstall = .*$/m)[0];
// The Updates page also draws Update All Games Automatically.
const autoUpdateAllSrc = app.match(/^const autoUpdateAll = .*$/m)[0];
// The part of the download-finished handler that decides what opens next.
const doneHandler = app.slice(app.indexOf("T.event.listen('install'"), app.indexOf("T.event.listen('game'"));

Expand All @@ -28,7 +30,7 @@ function settingsContext(settings, setTab = 'updates') {
});
c.window = c;
c.clockRow = () => ''; c.monitorPicked = () => ''; c.monitorName = () => '';
vm.runInContext(autoInstallSrc, c);
vm.runInContext(autoInstallSrc + '\n' + autoUpdateAllSrc, c);
return c;
}

Expand Down
167 changes: 167 additions & 0 deletions tests/auto-update-all.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');

const app = fs.readFileSync(path.join(__dirname, '../ui/app.js'), 'utf8');
const tv = fs.readFileSync(path.join(__dirname, '../ui/tv.js'), 'utf8');
const fn = (src, name, indent = '') => src.match(new RegExp(`^${indent}(?:async )?function ${name}\\([^]*?^${indent}}`, 'm'))[0];
const helpersSrc = [/^const autoInstall = .*$/m, /^const autoUpdateAll = .*$/m, /^const autoUpSkipped = .*$/m]
.map((re) => app.match(re)[0]).join('\n') + '\n' + fn(app, 'autoUpdates') + '\n' + fn(app, 'toggleAutoUpdate');

function settingsContext(settings) {
const c = vm.createContext({
S: {
os: 'windows', settings, libs: [], library: {}, owned: {},
ids: { usage: 'usage-id', stats: 'stats-id' }, setTab: 'updates', view: 'settings',
},
t: (s) => s, N_: (s) => s, tc: (s) => s, esc: String,
tf: (s, values) => s.replace(/\{(\w+)\}/g, (_, k) => values[k]),
LANGS: [['en', 'English']], systemLang: () => 'en',
SET_SECTIONS: [['updates', 'Updates']], SET_ICONS: {}, DL_LIMITS: [0],
okMark: () => '', portableRow: () => '', geRow: () => '', networkGroup: () => '',
backdrop: () => '', bar: () => '', region: (s) => s, pageHead: (s) => s,
homePinned: () => false, ownedGames: () => [], bgMode: () => '', langName: () => 'English',
svg: () => '', hints: () => '', HEART: '', setClock: () => {},
});
c.window = c;
c.clockRow = () => ''; c.monitorPicked = () => ''; c.monitorName = () => '';
vm.runInContext(helpersSrc, c);
return c;
}

// Arrays made inside the sandbox aren't this file's arrays, so they're compared as plain data.
const plain = (x) => JSON.parse(JSON.stringify(x));

// A library of the given games, and a record of what a game's own switch saves.
function switchContext(settings, library) {
const c = settingsContext(settings);
c.S.library = library;
c.calls = [];
c.invoke = async (command, args) => { c.calls.push({ command, autoUpdate: args.autoUpdate }); return { ...library[args.id], auto_update: args.autoUpdate }; };
c.saveUi = async (patch) => { c.calls.push({ ui: patch }); c.S.settings.ui = { ...(c.S.settings.ui || {}), ...patch }; };
c.toast = () => {}; c.render = () => {};
c.fail = (error) => { throw error; };
return c;
}

test('Update All Games Automatically is off for settings saved before the switch existed', () => {
const c = settingsContext({});
vm.runInContext(fn(app, 'pageSettings'), c);
assert.match(c.pageSettings(), /aria-checked="false" aria-label="Update All Games Automatically" data-act="toggle" data-k="auto_update_all"/);
});

test('Update All Games Automatically shows as on once the player turns it on', () => {
const c = settingsContext({ auto_update_all: true });
vm.runInContext(fn(app, 'pageSettings'), c);
assert.match(c.pageSettings(), /aria-checked="true" aria-label="Update All Games Automatically" data-act="toggle" data-k="auto_update_all"/);
});

test('Big Picture shows the same Update All Games Automatically switch', () => {
const c = settingsContext({});
vm.runInContext(fn(tv, 'pageSettings', ' '), c);
assert.match(c.pageSettings(), /data-toggle="auto_update_all" data-sw="0"><span class="k">Update All Games Automatically/);
c.S.settings.auto_update_all = true;
assert.match(c.pageSettings(), /data-toggle="auto_update_all" data-sw="1"><span class="k">Update All Games Automatically/);
});

test('a game updates by itself with only its own switch on', () => {
const c = switchContext({}, { a: { auto_update: true }, b: { auto_update: false } });
assert.equal(c.autoUpdates('a'), true);
assert.equal(c.autoUpdates('b'), false);
});

test('every game updates by itself with the Settings switch on', () => {
const c = switchContext({ auto_update_all: true }, { b: { auto_update: false } });
assert.equal(c.autoUpdates('b'), true);
});

test('a game turned off in its Manage tab waits, even with the Settings switch on', () => {
const c = switchContext({ auto_update_all: true, ui: { auto_update_skip: ['b'] } }, { b: { auto_update: false } });
assert.equal(c.autoUpdates('b'), false);
});

// The Updates card of a game's Manage tab, for a game with neither its own switch nor Pause on.
function manageUpdates(settings) {
const c = switchContext(settings, { g: { repo: 'dev/hydro', tag: 'v1', auto_update: false } });
Object.assign(c, { isLab: () => false, isSF: () => false, fmtB: String, heldIds: () => new Set() });
vm.runInContext(fn(app, 'manageUpdateRows'), c);
return c.manageUpdateRows('g', c.S.library.g).join('');
}

test('with the Settings switch on, the Manage tab shows the game\'s switch on and says why', () => {
const html = manageUpdates({ auto_update_all: true });
assert.match(html, /Settings → Updates turns this on for every game\. Off here: this game waits for you\./);
assert.match(html, /aria-checked="true" aria-label="Update automatically" data-act="auto-up"/);
});

test('with the Settings switch off, the Manage tab shows the game\'s own setting', () => {
const html = manageUpdates({});
assert.match(html, /Installs new releases as soon as the launcher finds them\./);
assert.match(html, /aria-checked="false" aria-label="Update automatically" data-act="auto-up"/);
});

test('a local build never updates by itself, even with the Settings switch on', () => {
const c = switchContext({ auto_update_all: true }, { l: { local: true, auto_update: true } });
assert.equal(c.autoUpdates('l'), false);
});

test('with the Settings switch on, a game\'s own switch goes on the skip list and off it again', async () => {
const c = switchContext({ auto_update_all: true }, { b: { name: 'B', auto_update: false } });
await c.toggleAutoUpdate('b');
assert.equal(c.autoUpdates('b'), false);
await c.toggleAutoUpdate('b');
assert.equal(c.autoUpdates('b'), true);
assert.deepEqual(plain(c.calls), [{ ui: { auto_update_skip: ['b'] } }, { ui: { auto_update_skip: [] } }]);
});

test('with the Settings switch on, turning off a game that had its own switch on clears that too', async () => {
const c = switchContext({ auto_update_all: true }, { a: { name: 'A', auto_update: true } });
await c.toggleAutoUpdate('a');
assert.equal(c.autoUpdates('a'), false);
assert.deepEqual(plain(c.calls), [{ command: 'set_game_options', autoUpdate: false }, { ui: { auto_update_skip: ['a'] } }]);
});

test('with the Settings switch off, a game\'s own switch saves only the game', async () => {
const c = switchContext({}, { b: { name: 'B', auto_update: false } });
await c.toggleAutoUpdate('b');
assert.equal(c.autoUpdates('b'), true);
assert.deepEqual(plain(c.calls), [{ command: 'set_game_options', autoUpdate: true }]);
});

test('Big Picture\'s game options carry the same switch', () => {
assert.match(tv, /window\.autoUpdates\(id\); return \{ label: on \? t\('Turn off automatic updates'\) : t\('Update automatically'\)/);
assert.match(tv, /run: async \(\) => \{ await window\.toggleAutoUpdate\(id\); draw\(true\); \}/);
});

test('the update check starts the updates of games that update by themselves', () => {
assert.match(fn(app, 'checkUpdates'), /\.filter\(\(id\) => autoUpdates\(id\) && !S\.running\.has\(id\)/);
});

test('turning the Settings switch on starts the updates already waiting', async () => {
const c = settingsContext({});
const saved = [];
let checks = 0;
c.invoke = async (command, args) => saved.push(args.settings.auto_update_all);
c.render = () => {};
c.fail = (error) => { throw error; };
c.checkUpdates = () => { checks += 1; };
vm.runInContext('let settingsSaving = Promise.resolve();' + fn(app, 'applyEasySkip') + fn(app, 'saveSettings') + fn(app, 'act'), c);
const toggle = { dataset: { act: 'toggle', k: 'auto_update_all' } };
await c.act(toggle);
await c.act(toggle);
assert.deepEqual(saved, [true, false]);
assert.equal(checks, 1);
});

test('Big Picture\'s Turn off automatic updates reads in Title Case in English', () => {
const src = fs.readFileSync(path.join(__dirname, '../ui/titlecase.js'), 'utf8');
const c = vm.createContext({
document: { body: null, documentElement: { lang: 'en' }, addEventListener: () => {} },
MutationObserver: class { observe() {} },
});
c.window = c;
vm.runInContext(src, c);
assert.equal(c.titleCase('Turn off automatic updates'), 'Turn Off Automatic Updates');
});
7 changes: 4 additions & 3 deletions tests/manage-tab.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const fileRows = (inst, signedIn) => {
const run = new Function('S', 't', 'esc', `${fn(app, 'manageFileRows')}; return manageFileRows;`);
return run({ account: { signedIn } }, same, same)('g', inst).join('');
};
// The rows of the Updates card, with the game's updates paused or not.
// The rows of the Updates card, with the game's updates paused or not (and Settings → Update All
// Games Automatically off).
const updateRows = (inst, held) => {
const run = new Function('S', 't', 'esc', 'isLab', 'isSF', 'fmtB', 'heldIds', `${fn(app, 'manageUpdateRows')}; return manageUpdateRows;`);
const run = new Function('S', 't', 'esc', 'isLab', 'isSF', 'fmtB', 'heldIds', 'autoUpdateAll', 'autoUpdates', `${fn(app, 'manageUpdateRows')}; return manageUpdateRows;`);
const no = () => false;
return run({}, same, same, no, no, String, () => new Set(held ? ['g'] : []))('g', inst).join('');
return run({}, same, same, no, no, String, () => new Set(held ? ['g'] : []), no, () => !!inst.auto_update)('g', inst).join('');
};
const GAME = { path: 'F:\\Library\\hydro', repo: 'dev/hydro', tag: 'v1.3.0' };

Expand Down
Loading
Loading