main
John Lauer Preserve existing Codex nonce panel identity before activation 4b85a6a
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs'), os = require('node:os'), path = require('node:path');
const testBindings = fs.mkdtempSync(path.join(os.tmpdir(), 'adom-panel-bindings-'));
process.env.ADOM_CODEX_BINDINGS_DIR = testBindings;
const {prepare, prepareForProvider, watch} = require('../runtime/thread-names.cjs');
const a = '12345678-1234-1234-1234-123456789abc';
const b = '87654321-1234-1234-1234-123456789abc';
const html = '<html><head><script nonce="ok" src="app.js"></script></head></html>';
assert.equal(prepare(prepare(html)), prepare(html));
assert.ok(prepare(html).indexOf('adom-thread-identity') < prepare(html).indexOf('src="app.js"'));
assert.equal(prepare('<html></html>'), '<html></html>');
const modern = '<meta http-equiv="Content-Security-Policy" content="default-src \'none\'; script-src \'self\' https://*.vscode-cdn.net; style-src \'self\';"><script type="module" src="app.js"></script>';
const prepared = prepare(modern);
assert.ok(prepared.includes('adom-thread-identity'));
assert.ok(/script-src 'nonce-[^']+' 'self' https:\/\/\*\.vscode-cdn.net;/.test(prepared));
assert.ok(prepared.includes("style-src 'self';"));
assert.ok(!prepared.includes('unsafe-inline'));
assert.equal(prepare(prepared), prepared);
function panel(route, globalState) {
  let handler, dispose;
  const calls = [], registrations = new Map();
  let title = 'Codex';
  const p = {get title() {return title;}, set title(value) {title = value;}, visible: true, onDidChangeViewState() {return {dispose() {}};}};
  const connection = {
    registerProvider(key, callbacks) {registrations.set(key, callbacks); return {dispose() {registrations.delete(key);}};},
    sendRequest(key, id, method, params) {calls.push({key, id, method, params});},
  };
  watch({onDidReceiveMessage(fn) {handler = fn; return {dispose() {}};}}, p,
    {codexMcpConnection: connection, editorPanels: new Map([[p, {initialRoute: route}]]), globalState},
    fn => {dispose = fn; return {dispose() {}};});
  return {p, calls, registrations, message: m => handler(m), dispose: () => dispose()};
}
const restored = panel('/local/' + a), fresh = panel('/extension/panel/new');
assert.equal(fresh.calls.length, 0);
fresh.message({type: 'adom-thread-identity', threadId: b});
assert.equal(fresh.calls[0].params.threadId, b);
const first = restored.calls[0], callbacks = restored.registrations.get(first.key);
callbacks.onNotification({method: 'thread/name/updated', params: {threadId: b, threadName: 'Wrong thread'}});
assert.equal(restored.p.title, 'Codex');
callbacks.onNotification({method: 'thread/name/updated', params: {threadId: a, threadName: 'User name'}});
callbacks.onResult({id: first.id, result: {thread: {id: a, name: 'Stale generated name'}}});
assert.equal(restored.p.title, 'User name');
restored.p.title = 'Late raw first prompt';
assert.equal(restored.p.title, 'User name');
assert.equal(fresh.p.title, 'Codex');
restored.dispose(); fresh.dispose();
assert.equal(restored.registrations.size, 0);
console.log('Passed: restored/new panel identity, rename events, stale reads, isolation, disposal and CSP bootstrap.');
(async () => {
  const saved = new Map();
  const state = {get: key => saved.get(key), update(key, value) {saved.set(key, value);}};
  const route = '/extension/panel/new?nonce=unique-tab';
  const initial = panel(route, state);
  initial.message({type: 'adom-thread-identity', threadId: a}); initial.dispose();
  const reopened = panel(route, state);
  await new Promise(resolve => setImmediate(resolve));
  assert.equal(reopened.calls[0].params.threadId, a);
  const restoredHTML = await prepareForProvider('<meta name="initial-route" content="'+route+'">'+html, {globalState: state});
  assert.ok(restoredHTML.includes('content="/local/'+a+'"'));
  const freshHTML = await prepareForProvider('<meta name="initial-route" content="/extension/panel/new?nonce=other">'+html, {globalState: state});
  assert.ok(freshHTML.includes('content="/extension/panel/new?nonce=other"'));
  require('../runtime/panel-bindings.cjs').save('/extension/panel/new?adom=legacy-123', b);
  const migratedHTML = await prepareForProvider('<meta name="initial-route" content="/extension/panel/new?adom%3Dlegacy-123">'+html, {});
  assert.ok(migratedHTML.includes('content="/local/'+b+'"'));
  const generic = panel('/extension/panel/new', state);
  await new Promise(resolve => setImmediate(resolve));
  assert.equal(generic.calls.length, 0);
  const other = panel('/extension/panel/new?nonce=different-tab', state);
  await new Promise(resolve => setImmediate(resolve));
  assert.equal(other.calls.length, 0);
  reopened.dispose(); generic.dispose(); other.dispose();
  console.log('Passed: hidden nonce-tab restore retains identity; reusable/new resources never inherit another thread.');
})().catch(error => {console.error(error); process.exitCode = 1;}).finally(() => fs.rmSync(testBindings, {recursive:true, force:true}));