fix: a network switch in Settings leaves no balance from the previous network stored (closes #653)
check / check (push) Waiting to run
e2e / e2e-chrome (push) Waiting to run
e2e / e2e-firefox (push) Waiting to run

The switch is now made on the stored record that its save has just read. The switched fields are then copied into the page's state. A save writes only the fields that the page changed since its last save. A switch made on the page's state alone mishandled what another page stored since then:

- A balance from the previous network stayed under the new network.
- A token tracked on the previous network moved to the new network.
- An endpoint URL saved for the previous network was replaced by the page's older one.

Tests store each of these before the switch and check the stored record after it.

Model: opus-5-5
This commit is contained in:
2026-10-27 07:22:50 +00:00
parent fc6cffcaba
commit 1effa7c6eb
6 changed files with 120 additions and 10 deletions
+50
View File
@@ -6,6 +6,9 @@
// approved a site's request, which changes only the stored record. Driven
// through the real popup over a stored profile on Ethereum Mainnet. The same
// refresh in the background is in tests/backgroundStateIsolation.test.js.
//
// The last test covers a refresh by the background that is saved just before
// the switch in Settings (https://git.eeqj.de/sneak/AutistMask/issues/653).
const {
bootPopup,
@@ -159,3 +162,50 @@ test("a refresh that returns after the background switched the network writes no
expectNothingStored(page);
});
// The background's refresh saves Ethereum Mainnet's balances while the switch
// in Settings waits for the lock. This page last saved a balance of "0" and no
// tokens, the same values the switch sets.
test("a background refresh saved while a switch in Settings waits for the lock leaves none of its balances stored", async () => {
const profile = unversionedValidProfile();
profile.wallets[0].addresses[0].balance = "0";
const page = await bootPopup(profile);
expect(page.pageErrors).toEqual([]);
const background = require("../src/background/state");
await page.click("btn-settings");
// The background takes the lock first and holds it until the test calls
// `letGo()`. It then saves balances as backgroundRefresh() in
// src/background/index.js does.
let letGo;
const refreshSave = background.updateState(async (fresh) => {
await new Promise((resolve) => {
letGo = resolve;
});
fresh.wallets[0].addresses[0].balance = "7.0";
fresh.wallets[0].addresses[0].tokenBalances = [
{
address: TOKEN_ADDRESS,
name: "Test Token",
symbol: "TST",
decimals: 18,
balance: "5.0",
holders: null,
},
];
fresh.lastBalanceRefresh = Date.now();
});
await page.settle();
const select = page.node("settings-network");
select.value = "sepolia";
const switching = Promise.all(select.listeners.change.map((fn) => fn()));
await page.settle();
letGo();
await refreshSave;
await switching;
await page.settle();
expectNothingStored(page);
});
+22
View File
@@ -47,6 +47,7 @@ function loadModuleWith(persisted) {
return {
mod: require("../src/shared/state"),
chainSwitch: require("../src/shared/chainSwitch"),
storage,
written: () => storage.read("autistmask"),
};
}
@@ -105,6 +106,27 @@ describe("a custom endpoint survives a chain switch", () => {
expect(mod.state.rpcUrl).toBe(CUSTOM_RPC);
});
// Another page saves the user's own node for Ethereum Mainnet after this
// page's last save. This page's switch keeps that node for Ethereum
// Mainnet.
test("an endpoint another page saved for the network being left is remembered", async () => {
const { mod, chainSwitch, storage, written } = loadModuleWith({
wallets: walletFixture(),
networkId: "mainnet",
rpcUrl: MAINNET.defaultRpcUrl,
blockscoutUrl: MAINNET.defaultBlockscoutUrl,
networkEndpoints: {},
});
await mod.loadState();
storage.write("autistmask", { ...written(), rpcUrl: CUSTOM_RPC });
await chainSwitch.onChainSwitch("sepolia");
expect(written().networkEndpoints.mainnet.rpcUrl).toBe(CUSTOM_RPC);
await chainSwitch.onChainSwitch("mainnet");
expect(mod.state.rpcUrl).toBe(CUSTOM_RPC);
});
test("the remembered endpoints survive an extension restart", async () => {
const first = loadModuleWith({
wallets: walletFixture(),
+19
View File
@@ -177,6 +177,25 @@ test("a token added by contract address on Sepolia is tracked on Sepolia only",
expect(onSepolia).not.toContain("DAI");
});
// Another popup page tracks DAI on Ethereum Mainnet after this page's last
// save. This page's switch in Settings keeps DAI, tracked on Ethereum Mainnet.
test("a token another page tracked stays on its network through a switch in Settings", async () => {
const boot = await bootPopup(unversionedValidProfile());
expect(boot.pageErrors).toEqual([]);
boot.storage.write("autistmask", {
...boot.storage.read("autistmask"),
trackedTokens: [MAINNET_TOKEN],
});
await boot.click("btn-settings");
await chooseNetwork(boot, "sepolia");
const stored = boot.storage.read("autistmask");
expect(stored.networkId).toBe("sepolia");
expect(stored.trackedTokens).toEqual([]);
expect(stored.networkTrackedTokens.mainnet).toEqual([MAINNET_TOKEN]);
});
describe("Back after a switch from the Settings gear", () => {
test("SettingsAddToken no longer offers the common tokens", async () => {
const boot = await bootOnAddToken("mainnet");