fix(ql3): isolate native console asynchronous sessions

This commit is contained in:
whyour
2026-09-04 03:56:18 +08:00
parent 9708a37154
commit 03afd7e899
9 changed files with 538 additions and 126 deletions
@@ -1,114 +1,6 @@
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { test } = require('node:test');
// A minimal DOM; all navigation, requests and validation execute the shipped JS.
class Element {
constructor(tag = 'div') {
this.tag = tag;
this.children = [];
this.dataset = {};
this.listeners = {};
this.isConnected = true;
}
set textContent(value) {
this.text = value;
this.replaceChildren();
}
get textContent() {
return (this.text || '') + this.children.map((x) => x.textContent).join('');
}
append(...children) {
for (const child of children) {
if (child.tag === 'fragment') this.append(...child.children);
else {
child.parent = this;
this.children.push(child);
}
}
}
detach() {
this.isConnected = false;
this.children.forEach((child) => child.detach());
}
replaceChildren(...children) {
this.children.forEach((child) => child.detach());
this.children = [];
this.append(...children);
}
setAttribute(name, value) {
this[name] = value;
}
removeAttribute(name) {
delete this[name];
}
addEventListener(name, handler) {
this.listeners[name] = handler;
}
querySelector() {
return new Element();
}
querySelectorAll(selector) {
return this.children.flatMap((child) => [
...(selector === child.tag || selector === `.${child.className}`
? [child]
: []),
...child.querySelectorAll(selector),
]);
}
focus() {}
}
function fixture(view, response) {
const nodes = new Map();
const calls = [];
const context = vm.createContext({
TextDecoder,
Uint8Array,
URLSearchParams,
Intl,
console,
window: { atob },
document: {
getElementById(id) {
if (!nodes.has(id)) nodes.set(id, new Element());
return nodes.get(id);
},
querySelector: () => new Element(),
createElement: (tag) => new Element(tag),
createDocumentFragment: () => new Element('fragment'),
addEventListener() {},
},
fetch: async (url, options) => {
calls.push({ url, options });
const body = await response(url, calls.length);
return {
ok: !body.httpStatus,
status: body.httpStatus || 200,
json: async () => body,
headers: { get: () => null },
};
},
});
const source = fs.readFileSync(
path.join(__dirname, '../assets/console/console.js'),
'utf8',
);
assert.ok(source.endsWith('})();\n'));
vm.runInContext(
source.slice(0, -6) +
'globalThis.client = { state, nodes, refresh, selectTask, selectTrigger, disconnect };})();',
context,
);
Object.assign(context.client.state, {
token: 'memory-only-test-token',
project: 'default',
view,
});
return { ...context.client, calls };
}
const { fixture } = require('./support/consoleClient.cjs');
const cursor = (view, row) =>
view === 'runs'