// Run with Node: exercises the exact WebView script without executing school HTML scripts. const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const source = fs.readFileSync(path.join(__dirname, '../../lib/features/school/school_page_probe.dart'), 'utf8'); const script = source.match(/schoolPageProbeScript = r'''([\s\S]*?)''';/)[1]; const home = 'https://yjsjy.uestc.edu.cn/pyxx/home/index'; const timetable = 'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1'; function term(id) { const result = { selectedIndex: 1, options: [false, false].map((selected, index) => ({ selected, value: String(73 - index), setAttribute() { this.selected = true; }, removeAttribute() { this.selected = false; }, })), cloneNode() { return term(id); }, get outerHTML() { return ``; }, }; return result; } function doc(url, { password = false, chrome = false, table = false, exams = false, frames = [] } = {}) { return { location: new URL(url), querySelector(selector) { if (selector === 'input[type="password"]') return password ? {} : null; if (selector === '#navbar' || selector === '#main-container') return chrome ? {} : null; if (selector === '#tbl') return table ? {outerHTML: 'synthetic timetable
'} : null; if (selector === '#drpxq') return table ? term('drpxq') : null; if (selector === '#sample-table-1') return exams ? {outerHTML: 'synthetic exams
'} : null; if (selector === '#xqbh') return exams ? term('xqbh') : null; return null; }, querySelectorAll(selector) { return selector === 'iframe,frame' ? frames : []; }, }; } function probe(document, logout = () => {}) { return JSON.parse(vm.runInNewContext(script, { URL, document, location: document.location, window: {logout}, })); } assert.equal(probe(doc(home)).authenticated, true, 'real logout function on known graduate home'); assert.equal(probe(doc(timetable, {chrome: true})).authenticated, true, 'graduate shell plus logout'); assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx?targetUrl=-1', {chrome: true})).authenticated, true, 'graduate root shell'); assert.equal(probe(doc(home), undefined).authenticated, true); // default function assert.equal(probe(doc(home), null).authenticated, false, 'home URL alone is insufficient'); assert.equal(probe(doc('https://yjsjy.uestc.edu.cn/pyxx/jzsso/login')).authenticated, false, 'callback alone is insufficient'); const top = probe(doc(timetable, {table: true})); assert.equal(top.pages.length, 1); assert.deepEqual(top.terms, [73, 72], 'bounded school term choices contain only dropdown IDs'); assert.match(top.pages[0].html, /value="1" selected="selected"/); assert.doesNotMatch(top.pages[0].html, /value="0" selected/); const inaccessible = {}; Object.defineProperty(inaccessible, 'contentDocument', {get() { throw new Error('cross-origin'); }}); const embedded = probe(doc(home, {frames: [ inaccessible, {contentDocument: doc('https://captcha.example.test/pyxx/timetable', {table: true})}, {contentDocument: doc('http://yjsjy.uestc.edu.cn/pyxx/timetable', {table: true})}, {contentDocument: doc(timetable, {password: true, table: true})}, {contentDocument: doc(timetable, {frames: [{contentDocument: doc(timetable, {table: true})}]})}, ]})); assert.equal(embedded.pages.length, 1, 'only nested same-origin graduate timetable is captured'); assert.equal(embedded.pages[0].url, timetable); assert.equal(probe(doc(home, {password: true, table: true})).pages.length, 0); assert.equal(probe(doc('https://idas.uestc.edu.cn/authserver/login', {table: true})).pages.length, 0); for (const url of [ 'https://yjsjy.uestc.edu.cn.evil.test/pyxx/home/index', 'https://yjsjy.uestc.edu.cn:444/pyxx/home/index', 'https://user@yjsjy.uestc.edu.cn/pyxx/home/index', 'file:///pyxx/home/index', ]) assert.equal(probe(doc(url, {table: true})).pages.length, 0); assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx/home/index', {table: true})).pages.length, 1); assert.equal(probe(doc(home, {exams: true})).pages[0].kind, 'exams'); console.log('School WebView probe: authentication, table selection, frames, password exclusion, origins passed.');