Files
TPSchedule/mobile/test/school/full_page_recovery_js_test.cjs

26 lines
1.8 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const path = require('node:path');
const root = path.join(__dirname, '../../lib/features/school');
const diagnostic = fs.readFileSync(path.join(root, 'school_browser_diagnostics.dart'), 'utf8').match(/schoolPageDiagnosticsScript = r'''([\s\S]*?)''';/)[1];
const wrapper = fs.readFileSync(path.join(root, 'school_full_page_recovery.dart'), 'utf8').match(/'''(\(\(\) => \{[\s\S]*?)'''/)[1];
const url = 'https://eams.uestc.edu.cn/eams/home.action';
function context(password = false) {
const document = {scripts: Array(2), styleSheets: [], querySelector: () => password ? {} : null, querySelectorAll: () => []};
for (const key of ['cookie', 'body', 'forms']) Object.defineProperty(document, key, {get() {throw Error('private_data_read');}});
return vm.createContext({document, location: {href: url}, window: {}, performance: {getEntriesByType: () => []}});
}
function run(ctx, nonce) {
return JSON.parse(vm.runInContext(wrapper.replace('$diagnostic', diagnostic.replace('__EXPECTED_URL__', JSON.stringify(url))).replace('$nonce', nonce), ctx));
}
const ctx = context();
assert.equal(run(ctx, 'first-document').documentToken, 'first-document');
assert.equal(run(ctx, 'unused-token').documentToken, 'first-document');
assert.equal(Object.keys(ctx.window).includes('__tpscheduleDocumentToken'), false);
assert.equal(run(context(), 'new-document').documentToken, 'new-document');
assert.equal(run(context(true), 'ignored'), null);
const moved = context(); moved.location.href = 'https://idas.uestc.edu.cn/authserver/login';
assert.equal(run(moved, 'ignored'), null);
console.log('Recovery snapshot: actual wrapper, stable document nonce, new-document change, password/redirect guards and no private reads passed.');