General source code refactoring

This commit is contained in:
2026-09-17 12:07:01 -03:00
parent 4421b28efe
commit ac817a69d4
92 changed files with 18211 additions and 19394 deletions

View File

@@ -1,7 +1,5 @@
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const { loadApplication } = require("./support/application.cjs");
class Element {
constructor(tag) {
@@ -28,24 +26,6 @@ function setup(mockSalesToken = true) {
static now() { return now; }
}
let visibleAlerts = [];
const source = fs.readFileSync(path.join(__dirname, "../src/content.js"), "utf8");
const marker = " if (!isAllowedPage()) {";
assert.ok(source.includes(marker));
const instrumented = source.replace(marker, `
accessToken = "test-bearer-secret";
getXsrfToken = async () => ({ token: "test-xsrf-cookie", cookieName: "XSRF-TOKEN-DYNAMIC", source: "test" });
refreshXsrfCookie = async () => ({ refreshed: false });
${mockSalesToken ? 'requestTokenRelaySalesCommon = async () => "test-sales-common-secret";' : ''}
renderDebugPanel = () => {};
renderOpportunitiesTable = () => {};
globalThis.helpers = {
requestGenerateAiSummary, createProductsCountButton, createProductsDetailRow,
createAiSummaryAlert, aiSummariesByOptyNumber, expandedProductOptyNumbers,
opptyProductsByNumber, requestLog, formatHeaderBlock, requestTokenRelay,
getAccessToken: () => accessToken, requestTokenRelaySalesCommon
};
return;
${marker}`);
const context = {
document: {
createElement: (tag) => new Element(tag),
@@ -54,8 +34,21 @@ ${marker}`);
fetch: (url, options) => new Promise((resolve, reject) => calls.push({ url, options, resolve, reject })),
Intl, Date: TestDate, Map, Set
};
vm.runInNewContext(instrumented, context);
return { ...context.helpers, calls, advance: (ms) => { now += ms; }, show: (alerts) => { visibleAlerts = alerts; } };
const app = loadApplication("content", context);
app.authSessionState.accessToken = "test-bearer-secret";
app.authXsrfContent.getXsrfToken = async () => ({ token: "test-xsrf-cookie", cookieName: "XSRF-TOKEN-DYNAMIC", source: "test" });
app.platformContentMessages.refreshXsrfCookie = async () => ({ refreshed: false });
if (mockSalesToken) app.authSalesCommon.requestTokenRelaySalesCommon = async () => "test-sales-common-secret";
app.debugContentView.renderDebugPanel = () => {};
app.opportunitiesTable.renderOpportunitiesTable = () => {};
return {
...app.aiSummaryContent, ...app.productsDetails, ...app.aiSummaryState,
...app.productsContentState, ...app.debugContentLog, ...app.debugState,
requestTokenRelay: app.authTokenRelay.requestTokenRelay,
requestTokenRelaySalesCommon: app.authSalesCommon.requestTokenRelaySalesCommon,
getAccessToken: () => app.authSessionState.accessToken,
calls, advance: (ms) => { now += ms; }, show: (alerts) => { visibleAlerts = alerts; }
};
}
function json(data, status = 200) {