Measure session object identity and listener counts across engine switches

window.__gevSession names each session object by a stable instance id and counts applications, attachments and detaches. scripts/qa-app-once.mjs (report-only with --report for now) runs three round trips and a parked-renderer rollback on a keyless preview with paid map, model and voice endpoints blocked.
This commit is contained in:
German Khokhlov committed 2026-11-03 17:08:22 +04:00
1 parent 5564156c09
commit b2cc00833d
5 files changed
+716

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
// Identity and attachment counters for the page session, read by the switch
// acceptance scripts (scripts/qa-app-once.mjs, qa-voice-engine-switch.mjs):
// an engine switch keeps every session object and only re-attaches the parts
// bound to an engine, so each id below stays the same across switches.
const ids = new WeakMap();
let nextId = 0;
/**
* A small number naming one object for this page: the same object always
* reads the same id, a different object a different one. Null for values
* that are not objects.
* @param {unknown} value
* @returns {number|null}
*/
export function instanceId(value) {
if (
value === null ||
(typeof value !== 'object' && typeof value !== 'function')
)
return null;
let id = ids.get(value);
if (id === undefined) {
id = ++nextId;
ids.set(value, id);
}
return id;
}
/**
* The page's diagnostic handle (`window.__gevSession`).
* `read()` returns the session objects by name; `ids` maps each name to its
* {@link instanceId}. `count(name)` bumps a counter such as `attachments`.
* @param {() => Record<string, unknown>} read
*/
export function createSessionDiagnostics(read) {
const counters = {
applications: 0,
attachments: 0,
detaches: 0,
rollbacks: 0,
};
return Object.freeze({
get ids() {
return Object.fromEntries(
Object.entries(read() ?? {}).map(([name, value]) => [
name,
instanceId(value),
]),
);
},
get counters() {
return { ...counters };
},
get attachments() {
return counters.attachments;
},
get detaches() {
return counters.detaches;
},
count(name) {
counters[name] = (counters[name] ?? 0) + 1;
return counters[name];
},
});
}
+43
View File
@@ -0,0 +1,43 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createSessionDiagnostics, instanceId } from './sessionDiagnostics.js';
test('an object keeps its id and another object gets another one', () => {
const a = {};
const b = {};
const fn = () => {};
assert.equal(instanceId(a), instanceId(a));
assert.notEqual(instanceId(a), instanceId(b));
assert.equal(typeof instanceId(fn), 'number');
assert.equal(instanceId(null), null);
assert.equal(instanceId(undefined), null);
assert.equal(instanceId(3), null);
});
test('the diagnostics read ids at the time they are asked for', () => {
let shell = {};
const voice = {};
const diagnostics = createSessionDiagnostics(() => ({ shell, voice }));
const first = diagnostics.ids;
assert.equal(first.shell, instanceId(shell));
assert.deepEqual(diagnostics.ids, first);
shell = {};
assert.notEqual(diagnostics.ids.shell, first.shell);
assert.equal(diagnostics.ids.voice, first.voice);
});
test('counters start at zero and count each event', () => {
const diagnostics = createSessionDiagnostics(() => ({}));
assert.equal(diagnostics.attachments, 0);
diagnostics.count('attachments');
diagnostics.count('attachments');
diagnostics.count('detaches');
assert.equal(diagnostics.attachments, 2);
assert.equal(diagnostics.detaches, 1);
assert.deepEqual(diagnostics.counters, {
applications: 0,
attachments: 2,
detaches: 1,
rollbacks: 0,
});
});