const assert = require("node:assert/strict");
const { test } = require("node:test");
const { loadApplication } = require("./support/application.cjs");
function setup() {
const document = { activeElement: null, listeners: new Map() };
const window = { innerWidth: 400, innerHeight: 600, listeners: new Map(), setTimeout, clearTimeout };
for (const target of [document, window]) {
target.addEventListener = (key, fn) => target.listeners.set(key, fn);
target.removeEventListener = key => target.listeners.delete(key);
}
const walk = node => [node, ...node.children.flatMap(walk)];
function element(tag) {
return {
tag, children: [], attributes: {}, listeners: {}, style: {}, textContent: "",
append(...nodes) { nodes.forEach(node => { node.parent = this; this.children.push(node); }); },
remove() { this.parent.children = this.parent.children.filter(node => node !== this); this.parent = null; },
setAttribute(key, value) { this.attributes[key] = value; },
addEventListener(key, fn) { this.listeners[key] = fn; },
contains(target) { return walk(this).includes(target); },
get isConnected() { return this === modal || !!this.parent?.isConnected; },
getBoundingClientRect() { return { left: 350, bottom: 550, height: 250 }; },
querySelector() { return walk(this).find(node => node.tag === "a" || node.tag === "button"); },
focus() { document.activeElement = this; this.listeners.focus?.(); }
};
}
const modal = element("main");
Object.assign(document, {
createElement: element, createElementNS: (_, tag) => element(tag),
getElementById: id => id === "opportunities-extension-modal" ? modal : null
});
const copied = [];
const app = loadApplication("content", { document, window, setTimeout, clearTimeout,
navigator: { clipboard: { writeText: async value => copied.push(value) } } });
// Keep the copy test independent of the toast's own DOM/timer implementation.
modal.querySelector = () => ({ classList: { toggle() {} } });
window.setTimeout = () => 0;
app.fusionCrmServiceRequests.requestCrmServiceRequest = async () => [
{ SrId: "NOT-THE-URL-ID", SrNumber: "SR /?&1", Title: "",
ReportedHours_c: 0, OpportunityName_Id_c: { OptyNumber: "OP1" } },
{ SrNumber: "SR2", OpportunityName_Id_c: { OptyNumber: "OP1" } }
];
app.opportunitiesQueryState.periodRequestVersion = 1;
const panel = () => modal.children.find(node => node.className === "opportunities-extension-sr-popover");
return { app, document, window, modal, walk, copied, panel };
}
test("SR popover uses encoded SrNumber, safe details and existing copy control for every record", async () => {
const { app, modal, walk, copied, panel } = setup();
await app.opportunityCrmServiceRequests.load("synthetic", 1);
const badge = app.opportunityCrmServiceRequests.createBadge("OP1");
modal.append(badge);
badge.listeners.pointerenter();
const nodes = walk(panel());
const links = nodes.filter(node => node.tag === "a");
assert.equal(links.length, 2);
for (const link of links) {
assert.equal(link.parent.children[0].textContent, "SR Number:");
assert.equal(link.parent.children[2].className, "opportunities-extension-copy-button");
}
const details = nodes.filter(node => node.className === "opportunities-extension-sr-details");
assert.equal(details.length, 2);
details.forEach(node => assert.doesNotMatch(node.textContent, /SR Number:|SR \/\?&1|SR2/));
assert.equal(links[0].href, "https://eeho.fa.us2.oraclecloud.com/fscmUI/redwood/service/ec/container/sr/edit?srNumber=SR%20%2F%3F%261");
assert.equal(links[0].rel, "noopener noreferrer");
assert.equal(links[0].target, "_blank");
assert.ok(nodes.some(node => node.textContent.includes("")));
assert.equal(nodes.some(node => node.tag === "script"), false);
const copies = nodes.filter(node => node.className === "opportunities-extension-copy-button");
assert.equal(copies.length, 2);
assert.equal(copies[0].title, "Copy SR number");
copies[0].listeners.click({ preventDefault() {}, stopPropagation() {} });
await Promise.resolve();
assert.deepEqual(copied, ["SR /?&1"]);
assert.equal(panel().style.width, "376px");
app.opportunityCrmServiceRequests.reset();
assert.equal(panel(), undefined);
});
test("SR popover pins on click, closes on Escape/outside/scroll/reset and removes global listeners", async () => {
const { app, modal, document, window, panel } = setup();
await app.opportunityCrmServiceRequests.load("synthetic", 1);
const badge = app.opportunityCrmServiceRequests.createBadge("OP1");
modal.append(badge);
badge.listeners.click({ stopPropagation() {} });
assert.equal(badge.attributes["aria-expanded"], "true");
assert.ok(panel().contains(document.activeElement));
document.listeners.get("keydown")({ key: "Escape", preventDefault() {}, stopPropagation() {} });
assert.equal(panel(), undefined);
assert.equal(document.activeElement, badge);
assert.equal(document.listeners.size, 0);
assert.equal(window.listeners.size, 0);
badge.listeners.pointerenter();
document.listeners.get("pointerdown")({ target: modal });
assert.equal(panel(), undefined);
badge.listeners.pointerenter();
document.listeners.get("scroll")({ target: panel() });
assert.ok(panel(), "scroll inside the tooltip keeps it open");
document.listeners.get("scroll")({ target: modal });
assert.equal(panel(), undefined);
badge.listeners.pointerenter();
app.opportunityCrmServiceRequests.reset();
assert.equal(panel(), undefined);
assert.equal(document.listeners.size, 0);
});