fix: switching the network in Settings tells open pages (closes #500)
Once Settings has saved the new network it asks the background to send chainChanged with the new chain id to every open tab, through the same function an approved site switch request uses. Only the extension's own pages can ask for that. Choosing the network already active changes nothing and sends nothing. Model: opus-5-5
This commit was merged in pull request #503.
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
//
|
||||
// The endpoint half of #308 lives in tests/networkEndpoints.test.js, which
|
||||
// covers the popup's chain switch; this file covers the background's, which
|
||||
// goes through storage rather than the shared state singleton.
|
||||
// goes through storage rather than the shared state singleton. The last block
|
||||
// covers what the background tells open tabs when the user switches the
|
||||
// network in Settings.
|
||||
|
||||
const { networkById } = require("../src/shared/networks");
|
||||
const { makeStorageStub } = require("./support/storageStub");
|
||||
@@ -225,6 +227,14 @@ function loadBackground() {
|
||||
answerPrompt,
|
||||
closePrompt,
|
||||
opened,
|
||||
// A message to the background from `sender`, and its answer.
|
||||
send: (msg, sender) => {
|
||||
let reply = null;
|
||||
messageListener(msg, sender, (r) => {
|
||||
reply = r;
|
||||
});
|
||||
return reply;
|
||||
},
|
||||
walletState: () => storage.read("autistmask"),
|
||||
chainChangedEvents: () =>
|
||||
toTabs.filter((m) => m.eventName === "chainChanged"),
|
||||
@@ -390,3 +400,109 @@ describe("only the user switches the network", () => {
|
||||
expect(bg.walletState().rpcUrl).toBe(CUSTOM_RPC);
|
||||
});
|
||||
});
|
||||
|
||||
// Switching the network in Settings tells open pages, as an approved site
|
||||
// request does (https://git.eeqj.de/sneak/AutistMask/issues/500). These drive
|
||||
// the real Settings view over the background's storage, with what it sends
|
||||
// delivered to the background from the extension's own page.
|
||||
describe("switching the network in Settings tells every open tab", () => {
|
||||
const POPUP = { url: EXT_URL + "src/popup/index.html" };
|
||||
|
||||
// A stand-in for one DOM node: enough of an element for init() to set
|
||||
// properties on it and hang listeners off it.
|
||||
function fakeElement() {
|
||||
return {
|
||||
value: "",
|
||||
checked: false,
|
||||
textContent: "",
|
||||
style: {},
|
||||
dataset: {},
|
||||
classList: { add() {}, remove() {} },
|
||||
listeners: {},
|
||||
addEventListener(event, handler) {
|
||||
this.listeners[event] = handler;
|
||||
},
|
||||
querySelectorAll: () => [],
|
||||
};
|
||||
}
|
||||
|
||||
// Settings, opened the way the popup opens it. Returns its network
|
||||
// selector.
|
||||
async function openSettings(bg) {
|
||||
const elements = {};
|
||||
const element = (id) => (elements[id] ||= fakeElement());
|
||||
jest.doMock("../src/popup/views/helpers", () => ({
|
||||
$: element,
|
||||
showView: () => {},
|
||||
updateDebugBanner: () => {},
|
||||
showFlash: () => {},
|
||||
escapeHtml: (s) => s,
|
||||
flashCopyFeedback: () => {},
|
||||
goBack: () => {},
|
||||
pushCurrentView: () => {},
|
||||
onViewLeave: () => {},
|
||||
VIEWS: [],
|
||||
}));
|
||||
global.chrome.runtime.sendMessage = (msg) => {
|
||||
bg.send(msg, POPUP);
|
||||
};
|
||||
await require("../src/shared/state").loadState();
|
||||
require("../src/popup/views/settings").init({
|
||||
pageClosed: new AbortController().signal,
|
||||
});
|
||||
const select = element("settings-network");
|
||||
select.value = "mainnet";
|
||||
return select;
|
||||
}
|
||||
|
||||
// The user picks `networkId` in the selector.
|
||||
async function choose(select, networkId) {
|
||||
select.value = networkId;
|
||||
await select.listeners.change();
|
||||
await settle();
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
jest.dontMock("../src/popup/views/helpers");
|
||||
});
|
||||
|
||||
test("switching to the other network sends chainChanged once, with its chain id", async () => {
|
||||
const bg = loadBackground();
|
||||
const select = await openSettings(bg);
|
||||
|
||||
await choose(select, "sepolia");
|
||||
|
||||
expect(bg.walletState().networkId).toBe("sepolia");
|
||||
expect(bg.chainChangedEvents()).toEqual([
|
||||
{
|
||||
type: "AUTISTMASK_EVENT",
|
||||
eventName: "chainChanged",
|
||||
data: SEPOLIA.chainId,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("choosing the network already active changes nothing and sends nothing", async () => {
|
||||
const bg = loadBackground();
|
||||
const select = await openSettings(bg);
|
||||
const before = bg.walletState();
|
||||
|
||||
await choose(select, "mainnet");
|
||||
|
||||
expect(bg.walletState()).toEqual(before);
|
||||
expect(bg.chainChangedEvents()).toEqual([]);
|
||||
});
|
||||
|
||||
test("a page cannot make the background send chainChanged", async () => {
|
||||
const bg = loadBackground();
|
||||
|
||||
const reply = bg.send(
|
||||
{ type: "AUTISTMASK_NETWORK_CHANGED", chainId: SEPOLIA.chainId },
|
||||
{ url: CONNECTED_ORIGIN + "/" },
|
||||
);
|
||||
await settle();
|
||||
|
||||
expect(reply).toEqual({ error: "Unauthorized sender" });
|
||||
expect(bg.chainChangedEvents()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user