super-productivity/electron/backup.test.cjs
Johannes Millan 7291f2fdc8
fix(backup): show the Windows Store backup path only when it exists (#9218)
* docs(sync): clarify multi-device token usage

* fix(backup): show Windows Store backup path only if it exists

Settings showed the MSIX-virtualized path unconditionally under
process.windowsStore, without ever checking it is there. On installs
where the package is not virtualized the backups sit in the real
AppData\Roaming, so the settings page advertised a folder that does not
exist and rendered a dead file:// link - for the backup feature, whose
whole point is being findable after a disaster (#9209).

AppData redirection is not a constant: it applies only to virtualized
packages, and since Windows 10 1903 the OS resolves it per file. That is
why #995 and #9209 report the exact opposite symptom and both are right.
Probe for the directory instead of assuming, and fall back to the path we
actually write to, which is correct in either case.

Closes #9209

* fix(backup): pin the Windows Store backup path with a regression test

Follow-up to the previous commit, addressing multi-agent review.

Move the display-path decision next to the constants it reads, as
getBackupDirForDisplay(). BACKUP_DIR_WINSTORE stops being exported, so
the rule "verify before displaying" is enforced by the only accessor
instead of being an honour-system note in a doc comment - which is the
shape the 2025 fix got wrong. backup.ts already imports existsSync, so
this costs no new import and matches getRelaunchExecPath in
ipc-handlers/app-control.ts.

Correct the doc comment: it claimed BACKUP_DIR_WINSTORE is never read,
while BACKUP_LOAD_DATA accepts it as an allow-listed read root fifty
lines below. Left as-is otherwise; narrowing a GHSA-x937-wf3j-88q3 guard
does not belong in a display-path fix.

Add electron/backup.test.cjs pinning both directions of the loop this
bug has already run once: Store + LocalCache present must show it (#995),
Store + LocalCache absent must fall back (#9209). Verified by sabotage -
restoring the unconditional path fails the #9209 case, deleting the probe
fails the #995 case, and each kills only its own test.

Document the two accepted shortcuts with their ceiling and upgrade path:
the hardcoded package family name (unverifiable from source, degrades
gracefully), and the stale LocalCache dir left by a virtualized to
full-trust flip.

* docs(backup): record how the hardcoded package family name was verified

Computed it from the shipped v18.15.1 AppxManifest: PublisherId is the
first 8 bytes of SHA-256 over the UTF-16LE Publisher string in base32
(digits + a-z minus i/l/o/u). CN=AC30A249-AFE7-4B23-AE54-A95B4FDF8928
yields ch45amy23cdv6, matching the constant exactly - so it has not
drifted, and the previous comment's claim that it cannot be verified was
too pessimistic: any release artifact re-checks it without Windows.

* docs: trim backup path comments and document the electron test harness

Cut the duplicated MSIX explanation across the two doc blocks (-7 lines)
while keeping the parts that stop this bug round-tripping a third time:
the per-file 1903 semantics, the #995/#9209 link, and both shortcuts'
ceilings.

Add npm run test:electron to AGENTS.md. It was absent, and the harness
is easy to miss because it uses electron/*.test.cjs while the rest of
the repo uses .spec.ts - which tsconfig.electron.json actively excludes,
so a spec dropped there silently never runs. Searching for the repo's
dominant convention turns up nothing and invites the conclusion that
main-process code is untestable; it is not.
2026-07-21 12:54:06 +02:00

110 lines
3.6 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const Module = require('node:module');
require('ts-node/register/transpile-only');
const originalModuleLoad = Module._load;
const backupModulePath = path.resolve(__dirname, 'backup.ts');
// Windows-shaped so the `.replace('Roaming', ...)` derivation actually fires on
// Linux/macOS CI too — otherwise BACKUP_DIR_WINSTORE would equal BACKUP_DIR and
// every assertion below would pass vacuously.
const USER_DATA = 'C:\\Users\\testuser\\AppData\\Roaming\\superProductivity';
// path.join, like the module under test — the separator is the host's, not '\'.
const BACKUP_DIR = path.join(USER_DATA, 'backups');
const BACKUP_DIR_WINSTORE = BACKUP_DIR.replace(
'Roaming',
'Local\\Packages\\53707johannesjo.SuperProductivity_ch45amy23cdv6\\LocalCache\\Roaming',
);
let existingPaths;
const resetModule = () => {
delete require.cache[backupModulePath];
};
const installMocks = () => {
Module._load = function patchedLoad(request, parent, isMain) {
if (request === 'electron') {
return {
app: { getPath: () => USER_DATA },
ipcMain: { on: () => {}, handle: () => {} },
};
}
if (request === 'electron-log/main') {
return { log: () => {}, error: () => {} };
}
// Scoped to backup.ts so ts-node / node:test keep the real fs.
if (
request === 'fs' &&
parent &&
typeof parent.filename === 'string' &&
parent.filename.endsWith('backup.ts')
) {
const realFs = originalModuleLoad.call(this, request, parent, isMain);
return { ...realFs, existsSync: (p) => existingPaths.has(p) };
}
return originalModuleLoad.call(this, request, parent, isMain);
};
};
const loadBackupModule = () => {
resetModule();
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(backupModulePath);
};
test.beforeEach(() => {
existingPaths = new Set();
installMocks();
});
test.afterEach(() => {
Module._load = originalModuleLoad;
delete process.windowsStore;
resetModule();
});
// Sanity check: without this the Windows-only branches below could never be
// reached on a Linux CI runner and the suite would be green for the wrong reason.
test('derives a distinct Windows Store path from the userData path', () => {
assert.notEqual(BACKUP_DIR_WINSTORE, BACKUP_DIR);
assert.match(BACKUP_DIR_WINSTORE, /LocalCache\\Roaming/);
});
test('non-Store builds always get the real backup dir', () => {
const { getBackupDirForDisplay } = loadBackupModule();
// Even if a LocalCache dir is left over from a previous Store install.
existingPaths.add(BACKUP_DIR_WINSTORE);
assert.equal(getBackupDirForDisplay(), BACKUP_DIR);
});
// Regression test for #995: a virtualized Store package redirects its writes
// into LocalCache, so showing the plain Roaming path sends the user to a folder
// their backups are not in.
test('Store builds get the LocalCache path when it exists', () => {
process.windowsStore = true;
const { getBackupDirForDisplay } = loadBackupModule();
existingPaths.add(BACKUP_DIR_WINSTORE);
assert.equal(getBackupDirForDisplay(), BACKUP_DIR_WINSTORE);
});
// Regression test for #9209: a non-virtualized Store package writes to the real
// AppData\Roaming, so the LocalCache path does not exist and must not be shown.
test('Store builds fall back to the real backup dir when LocalCache is absent', () => {
process.windowsStore = true;
const { getBackupDirForDisplay } = loadBackupModule();
existingPaths.add(BACKUP_DIR);
assert.equal(getBackupDirForDisplay(), BACKUP_DIR);
});