|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { after, afterEach, before, test } from "node:test"; |
| 3 | + |
| 4 | +import { JSDOM } from "jsdom"; |
| 5 | + |
| 6 | +const dom = new JSDOM("<!doctype html><html><body></body></html>", { |
| 7 | + url: "http://localhost", |
| 8 | +}); |
| 9 | + |
| 10 | +before(() => { |
| 11 | + Object.assign(globalThis, { |
| 12 | + CustomEvent: dom.window.CustomEvent, |
| 13 | + document: dom.window.document, |
| 14 | + Element: dom.window.Element, |
| 15 | + Event: dom.window.Event, |
| 16 | + getComputedStyle: dom.window.getComputedStyle.bind(dom.window), |
| 17 | + HTMLElement: dom.window.HTMLElement, |
| 18 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 19 | + Node: dom.window.Node, |
| 20 | + NodeFilter: dom.window.NodeFilter, |
| 21 | + ResizeObserver: class { |
| 22 | + disconnect() {} |
| 23 | + observe() {} |
| 24 | + unobserve() {} |
| 25 | + }, |
| 26 | + window: dom.window, |
| 27 | + }); |
| 28 | + // Copy remaining DOM-level globals that Radix Popover focus machinery |
| 29 | + // references without a `window.` prefix (NodeFilter, HTMLInputElement, |
| 30 | + // …). Bulk-copy follows the HarnessCatalogDialog.acpForcedGate test pattern |
| 31 | + // so new Radix internals cannot reintroduce per-global whack-a-mole. |
| 32 | + for (const key of Object.getOwnPropertyNames(dom.window)) { |
| 33 | + if ( |
| 34 | + !(key in globalThis) && |
| 35 | + (key.startsWith("HTML") || |
| 36 | + key.startsWith("SVG") || |
| 37 | + key.startsWith("CSS") || |
| 38 | + [ |
| 39 | + "Node", |
| 40 | + "NodeFilter", |
| 41 | + "NodeList", |
| 42 | + "NamedNodeMap", |
| 43 | + "Event", |
| 44 | + "CustomEvent", |
| 45 | + "MouseEvent", |
| 46 | + "KeyboardEvent", |
| 47 | + "FocusEvent", |
| 48 | + "InputEvent", |
| 49 | + "PointerEvent", |
| 50 | + "EventTarget", |
| 51 | + "Text", |
| 52 | + "DocumentFragment", |
| 53 | + "Range", |
| 54 | + "Selection", |
| 55 | + ].includes(key)) |
| 56 | + ) { |
| 57 | + const val = dom.window[key]; |
| 58 | + if (val !== undefined) globalThis[key] = val; |
| 59 | + } |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +afterEach(async () => { |
| 64 | + const { cleanup } = await import("@testing-library/react"); |
| 65 | + cleanup(); |
| 66 | +}); |
| 67 | + |
| 68 | +after(() => dom.window.close()); |
| 69 | + |
| 70 | +const HEX = "ea9b4d7a7a78a3e3729e5568b14d764d4962be0e1f20f749bcf8d9dbbf9a9328"; |
| 71 | +const NPUB = "npub1a2d567n60z37xu57245tzntkf4yk90swrus0wjdulrvah0u6jv5qusyp60"; |
| 72 | +const COMPACT_NPUB = "npub1a2d…yp60"; |
| 73 | + |
| 74 | +test("compact PubKey renders the truncated npub, never the hex", async () => { |
| 75 | + const React = await import("react"); |
| 76 | + const { render } = await import("@testing-library/react"); |
| 77 | + const { PubKey } = await import("./PubKey.tsx"); |
| 78 | + |
| 79 | + const view = render(React.createElement(PubKey, { pubkey: HEX })); |
| 80 | + |
| 81 | + const trigger = view.getByRole("button", { name: "Show full public key" }); |
| 82 | + assert.equal(trigger.textContent, COMPACT_NPUB); |
| 83 | + assert.equal(view.queryByText(HEX), null); |
| 84 | +}); |
| 85 | + |
| 86 | +test("non-interactive compact PubKey renders the truncated npub as text", async () => { |
| 87 | + const React = await import("react"); |
| 88 | + const { render } = await import("@testing-library/react"); |
| 89 | + const { PubKey } = await import("./PubKey.tsx"); |
| 90 | + |
| 91 | + const view = render( |
| 92 | + React.createElement(PubKey, { interactive: false, pubkey: HEX }), |
| 93 | + ); |
| 94 | + |
| 95 | + assert.equal(view.getByText(COMPACT_NPUB).tagName, "SPAN"); |
| 96 | + assert.equal(view.queryByRole("button"), null); |
| 97 | + assert.equal(view.queryByText(HEX), null); |
| 98 | +}); |
| 99 | + |
| 100 | +test("full PubKey renders the complete npub with an npub-only copy popover", async () => { |
| 101 | + const React = await import("react"); |
| 102 | + const { fireEvent, render, within } = await import("@testing-library/react"); |
| 103 | + const { PubKey } = await import("./PubKey.tsx"); |
| 104 | + |
| 105 | + const view = render( |
| 106 | + React.createElement(PubKey, { pubkey: HEX, variant: "full" }), |
| 107 | + ); |
| 108 | + |
| 109 | + assert.equal(within(view.container).getByText(NPUB).textContent, NPUB); |
| 110 | + assert.equal(view.queryByText(HEX), null); |
| 111 | + |
| 112 | + fireEvent.click( |
| 113 | + within(view.container).getByRole("button", { name: "Copy public key" }), |
| 114 | + ); |
| 115 | + // The copy popover offers the full canonical npub only — no hex row. |
| 116 | + assert.equal(view.getAllByText(NPUB).length, 2); |
| 117 | + assert.equal( |
| 118 | + view.getByRole("button", { name: "Copy npub" }).tagName, |
| 119 | + "BUTTON", |
| 120 | + ); |
| 121 | + assert.equal(view.queryByRole("button", { name: "Copy hex" }), null); |
| 122 | + assert.equal(view.queryByText("hex"), null); |
| 123 | +}); |
| 124 | + |
| 125 | +test("invalid keys render Unavailable with no copy affordance", async () => { |
| 126 | + const React = await import("react"); |
| 127 | + const { render, within } = await import("@testing-library/react"); |
| 128 | + const { PubKey } = await import("./PubKey.tsx"); |
| 129 | + |
| 130 | + const compact = render(React.createElement(PubKey, { pubkey: "zz" })); |
| 131 | + assert.equal( |
| 132 | + within(compact.container).getByText("Unavailable").tagName, |
| 133 | + "SPAN", |
| 134 | + ); |
| 135 | + assert.equal( |
| 136 | + within(compact.container).queryByRole("button", { |
| 137 | + name: "Show full public key", |
| 138 | + }), |
| 139 | + null, |
| 140 | + ); |
| 141 | + |
| 142 | + const full = render( |
| 143 | + React.createElement(PubKey, { pubkey: "zz", variant: "full" }), |
| 144 | + ); |
| 145 | + assert.equal(within(full.container).getByText("Unavailable").tagName, "SPAN"); |
| 146 | + // No copy action for an identity that cannot be encoded. |
| 147 | + assert.equal( |
| 148 | + within(full.container).queryByRole("button", { name: "Copy public key" }), |
| 149 | + null, |
| 150 | + ); |
| 151 | +}); |
| 152 | + |
| 153 | +test("degenerate-length hex keys render Unavailable, never a fake npub", async () => { |
| 154 | + const React = await import("react"); |
| 155 | + const { render, within } = await import("@testing-library/react"); |
| 156 | + const { PubKey } = await import("./PubKey.tsx"); |
| 157 | + |
| 158 | + // `npubEncode` happily encodes an 8-char hex into a valid-checksum npub — |
| 159 | + // that is not an identity key, so no surface may show or copy it. |
| 160 | + const short = render(React.createElement(PubKey, { pubkey: "deadbeef" })); |
| 161 | + assert.equal( |
| 162 | + within(short.container).getByText("Unavailable").tagName, |
| 163 | + "SPAN", |
| 164 | + ); |
| 165 | + assert.equal( |
| 166 | + within(short.container).queryByRole("button", { |
| 167 | + name: "Show full public key", |
| 168 | + }), |
| 169 | + null, |
| 170 | + ); |
| 171 | + |
| 172 | + const shortFull = render( |
| 173 | + React.createElement(PubKey, { pubkey: "deadbeef", variant: "full" }), |
| 174 | + ); |
| 175 | + assert.equal( |
| 176 | + within(shortFull.container).getByText("Unavailable").tagName, |
| 177 | + "SPAN", |
| 178 | + ); |
| 179 | + assert.equal( |
| 180 | + within(shortFull.container).queryByRole("button", { |
| 181 | + name: "Copy public key", |
| 182 | + }), |
| 183 | + null, |
| 184 | + ); |
| 185 | + assert.equal(shortFull.container.textContent?.includes("npub1"), false); |
| 186 | +}); |
0 commit comments