|
| 1 | +import React from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { Provider } from "react-redux"; |
| 4 | +import { |
| 5 | + Account, |
| 6 | + Asset, |
| 7 | + BASE_FEE, |
| 8 | + Networks, |
| 9 | + Operation, |
| 10 | + StrKey, |
| 11 | + TransactionBuilder, |
| 12 | + xdr, |
| 13 | +} from "stellar-sdk"; |
| 14 | + |
| 15 | +import { makeDummyStore } from "popup/__testHelpers__"; |
| 16 | +import { Operations } from "../index"; |
| 17 | + |
| 18 | +// setOptions never triggers the asset scanner, but mock it so the component's |
| 19 | +// effect can never reach the network in the test environment. |
| 20 | +jest.mock("popup/helpers/blockaid", () => ({ |
| 21 | + scanAsset: jest.fn().mockResolvedValue(undefined), |
| 22 | +})); |
| 23 | + |
| 24 | +// Build a setOptions transaction with the bundled SDK, serialize it to XDR and |
| 25 | +// decode it back — the exact path Freighter uses to obtain the operation object |
| 26 | +// it renders on the signing-approval screen. A present-but-zero Uint32 field |
| 27 | +// (e.g. masterWeight: 0) decodes to the JS number 0. |
| 28 | +// Valid ed25519 strkeys minted from fixed bytes — avoids curve math (and the |
| 29 | +// crypto RNG, which is unavailable in this test environment). |
| 30 | +const SOURCE_KEY = StrKey.encodeEd25519PublicKey(Buffer.alloc(32, 1)); |
| 31 | +const ADDED_SIGNER = StrKey.encodeEd25519PublicKey(Buffer.alloc(32, 7)); |
| 32 | + |
| 33 | +type SetOptionsOptions = Parameters<typeof Operation.setOptions>[0]; |
| 34 | + |
| 35 | +const decodeOperation = (operation: xdr.Operation) => { |
| 36 | + const account = new Account(SOURCE_KEY, "0"); |
| 37 | + const tx = new TransactionBuilder(account, { |
| 38 | + fee: BASE_FEE, |
| 39 | + networkPassphrase: Networks.TESTNET, |
| 40 | + }) |
| 41 | + .addOperation(operation) |
| 42 | + .setTimeout(0) |
| 43 | + .build(); |
| 44 | + |
| 45 | + return TransactionBuilder.fromXDR(tx.toXDR(), Networks.TESTNET) |
| 46 | + .operations as Operation[]; |
| 47 | +}; |
| 48 | + |
| 49 | +const decodeSetOptions = (options: SetOptionsOptions) => |
| 50 | + decodeOperation(Operation.setOptions(options)); |
| 51 | + |
| 52 | +const renderOps = (operations: Operation[]) => |
| 53 | + render( |
| 54 | + <Provider store={makeDummyStore({})}> |
| 55 | + <Operations |
| 56 | + flaggedKeys={{}} |
| 57 | + isMemoRequired={false} |
| 58 | + operations={operations} |
| 59 | + /> |
| 60 | + </Provider>, |
| 61 | + ); |
| 62 | + |
| 63 | +// Read the value rendered next to a given operation-detail label. |
| 64 | +const rowValue = (key: string) => { |
| 65 | + const keyEl = screen |
| 66 | + .getAllByTestId("OperationKeyVal__key") |
| 67 | + .find((el) => el.textContent === key); |
| 68 | + return keyEl?.parentElement |
| 69 | + ?.querySelector('[data-testid="OperationKeyVal__value"]') |
| 70 | + ?.textContent?.trim(); |
| 71 | +}; |
| 72 | + |
| 73 | +const MASTER_KEY_WARNING = /disables your account's master key/i; |
| 74 | + |
| 75 | +describe("Operations — setOptions field visibility", () => { |
| 76 | + it("decoder yields numeric 0 (falsy) for masterWeight/thresholds", () => { |
| 77 | + const [op] = decodeSetOptions({ |
| 78 | + masterWeight: 0, |
| 79 | + lowThreshold: 0, |
| 80 | + medThreshold: 0, |
| 81 | + highThreshold: 0, |
| 82 | + }) as any[]; |
| 83 | + |
| 84 | + expect(op.masterWeight).toBe(0); |
| 85 | + expect(op.lowThreshold).toBe(0); |
| 86 | + expect(op.medThreshold).toBe(0); |
| 87 | + expect(op.highThreshold).toBe(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it("renders masterWeight 0, zeroed thresholds, and a signer change, with a master-key warning", () => { |
| 91 | + renderOps( |
| 92 | + decodeSetOptions({ |
| 93 | + masterWeight: 0, |
| 94 | + lowThreshold: 0, |
| 95 | + medThreshold: 0, |
| 96 | + highThreshold: 0, |
| 97 | + signer: { ed25519PublicKey: ADDED_SIGNER, weight: 1 }, |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
| 101 | + expect(screen.getByText("Set Options")).toBeInTheDocument(); |
| 102 | + expect(screen.getByText("Signer")).toBeInTheDocument(); |
| 103 | + expect(rowValue("Master Weight")).toBe("0"); |
| 104 | + expect(rowValue("High Threshold")).toBe("0"); |
| 105 | + expect(rowValue("Medium Threshold")).toBe("0"); |
| 106 | + expect(rowValue("Low Threshold")).toBe("0"); |
| 107 | + expect(screen.getByText(MASTER_KEY_WARNING)).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("renders masterWeight 0 on its own with the warning, never an empty operation", () => { |
| 111 | + renderOps(decodeSetOptions({ masterWeight: 0 })); |
| 112 | + |
| 113 | + expect(rowValue("Master Weight")).toBe("0"); |
| 114 | + expect(screen.getByText(MASTER_KEY_WARNING)).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("non-zero masterWeight/threshold render and do not warn", () => { |
| 118 | + renderOps(decodeSetOptions({ masterWeight: 2, highThreshold: 3 })); |
| 119 | + |
| 120 | + expect(rowValue("Master Weight")).toBe("2"); |
| 121 | + expect(rowValue("High Threshold")).toBe("3"); |
| 122 | + expect(screen.queryByText(MASTER_KEY_WARNING)).not.toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("HOME DOMAIN: clearing the home domain is surfaced, not hidden", () => { |
| 126 | + renderOps(decodeSetOptions({ homeDomain: "" })); |
| 127 | + |
| 128 | + expect(rowValue("Home Domain")).toBe("(clearing home domain)"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("FLAGS: a single-bit setFlags decodes to its label", () => { |
| 132 | + renderOps(decodeSetOptions({ setFlags: 1 })); |
| 133 | + |
| 134 | + expect(rowValue("Set Flags")).toBe("Authorization Required"); |
| 135 | + }); |
| 136 | + |
| 137 | + it("FLAGS: a combined setFlags bitmask decodes every set bit, not a blank value", () => { |
| 138 | + // REVOCABLE (2) | CLAWBACK (8) = 10. The SDK types setFlags as a single |
| 139 | + // AuthFlag, but the wire format is a bitmask — cast to exercise that. |
| 140 | + renderOps(decodeSetOptions({ setFlags: 10 as any })); |
| 141 | + |
| 142 | + expect(rowValue("Set Flags")).toBe( |
| 143 | + "Authorization Revocable, Authorization Clawback Enabled", |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it("FLAGS: a combined clearFlags bitmask decodes every set bit", () => { |
| 148 | + // REQUIRED (1) | REVOCABLE (2) = 3 |
| 149 | + renderOps(decodeSetOptions({ clearFlags: 3 as any })); |
| 150 | + |
| 151 | + expect(rowValue("Clear Flags")).toBe( |
| 152 | + "Authorization Required, Authorization Revocable", |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe("Operations — manageData value visibility", () => { |
| 158 | + it("renders a set value", () => { |
| 159 | + renderOps( |
| 160 | + decodeOperation(Operation.manageData({ name: "k", value: "hi" })), |
| 161 | + ); |
| 162 | + |
| 163 | + expect(rowValue("Value")).toBe("hi"); |
| 164 | + }); |
| 165 | + |
| 166 | + it("renders the Value row for an empty value rather than hiding it", () => { |
| 167 | + renderOps(decodeOperation(Operation.manageData({ name: "k", value: "" }))); |
| 168 | + |
| 169 | + expect(screen.getByText("Value")).toBeInTheDocument(); |
| 170 | + expect(rowValue("Value")).toBe(""); |
| 171 | + }); |
| 172 | + |
| 173 | + it("surfaces a deletion when value is absent ", () => { |
| 174 | + renderOps( |
| 175 | + decodeOperation(Operation.manageData({ name: "k", value: null })), |
| 176 | + ); |
| 177 | + |
| 178 | + expect(rowValue("Value")).toBe("(deleting entry)"); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe("Operations — setTrustLineFlags visibility", () => { |
| 183 | + const TRUSTOR = StrKey.encodeEd25519PublicKey(Buffer.alloc(32, 3)); |
| 184 | + const ASSET = new Asset( |
| 185 | + "USDC", |
| 186 | + StrKey.encodeEd25519PublicKey(Buffer.alloc(32, 9)), |
| 187 | + ); |
| 188 | + |
| 189 | + const decodeSetTrustLineFlags = (flags: { |
| 190 | + authorized?: boolean; |
| 191 | + authorizedToMaintainLiabilities?: boolean; |
| 192 | + clawbackEnabled?: boolean; |
| 193 | + }) => |
| 194 | + decodeOperation( |
| 195 | + Operation.setTrustLineFlags({ trustor: TRUSTOR, asset: ASSET, flags }), |
| 196 | + ); |
| 197 | + |
| 198 | + it("renders a flag being enabled", () => { |
| 199 | + renderOps(decodeSetTrustLineFlags({ authorized: true })); |
| 200 | + |
| 201 | + expect(rowValue("Authorized")).toBe("Enabled"); |
| 202 | + }); |
| 203 | + |
| 204 | + it("renders a flag being cleared (set to false), not hidden", () => { |
| 205 | + renderOps(decodeSetTrustLineFlags({ authorized: false })); |
| 206 | + |
| 207 | + expect(rowValue("Authorized")).toBe("Disabled"); |
| 208 | + }); |
| 209 | + |
| 210 | + it("does not render a flag that is left unchanged", () => { |
| 211 | + renderOps(decodeSetTrustLineFlags({ clawbackEnabled: false })); |
| 212 | + |
| 213 | + expect( |
| 214 | + screen |
| 215 | + .getAllByTestId("OperationKeyVal__key") |
| 216 | + .some((el) => el.textContent === "Authorized"), |
| 217 | + ).toBe(false); |
| 218 | + expect(rowValue("Clawback Enabled")).toBe("Disabled"); |
| 219 | + }); |
| 220 | +}); |
0 commit comments