|
| 1 | +/** |
| 2 | + * Widget-boundary coverage for the shared <PubKey> identity gate. |
| 3 | + * |
| 4 | + * Codec vectors and the exact compact/neutral strings live in |
| 5 | + * ../lib/pubkey.test.mjs. This suite pins what static rendering shows: the |
| 6 | + * rendered text per variant, and that an unencodable identity — including |
| 7 | + * degenerate-length hex and short-payload npubs whose npubEncode outputs |
| 8 | + * carry valid checksums — renders the neutral label with no copy affordance, |
| 9 | + * never a fake npub. The clipboard write behind the copy affordance and the |
| 10 | + * popover the widget opens are real-bridge interactions owned by the E2E |
| 11 | + * regressions: the full variant's copy is pinned by the new-DM recipient |
| 12 | + * verification flow (tests/e2e/pubkey-display-screenshots.spec.ts) and the |
| 13 | + * compact variant's by the agent-access owner hint |
| 14 | + * (tests/e2e/agent-access-warning.spec.ts); both drive CopyRow through the |
| 15 | + * mock bridge into the actual browser clipboard. |
| 16 | + */ |
| 17 | +import assert from "node:assert/strict"; |
| 18 | +import { after, afterEach, before, test } from "node:test"; |
| 19 | + |
| 20 | +import { JSDOM } from "jsdom"; |
| 21 | + |
| 22 | +const dom = new JSDOM("<!doctype html><html><body></body></html>", { |
| 23 | + url: "http://localhost", |
| 24 | +}); |
| 25 | + |
| 26 | +before(() => { |
| 27 | + Object.assign(globalThis, { |
| 28 | + document: dom.window.document, |
| 29 | + getComputedStyle: dom.window.getComputedStyle.bind(dom.window), |
| 30 | + HTMLElement: dom.window.HTMLElement, |
| 31 | + IS_REACT_ACT_ENVIRONMENT: true, |
| 32 | + Node: dom.window.Node, |
| 33 | + ResizeObserver: class { |
| 34 | + disconnect() {} |
| 35 | + observe() {} |
| 36 | + unobserve() {} |
| 37 | + }, |
| 38 | + window: dom.window, |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +afterEach(async () => { |
| 43 | + const { cleanup } = await import("@testing-library/react"); |
| 44 | + cleanup(); |
| 45 | +}); |
| 46 | + |
| 47 | +after(() => dom.window.close()); |
| 48 | + |
| 49 | +const HEX = "ea9b4d7a7a78a3e3729e5568b14d764d4962be0e1f20f749bcf8d9dbbf9a9328"; |
| 50 | +const NPUB = "npub1a2d567n60z37xu57245tzntkf4yk90swrus0wjdulrvah0u6jv5qusyp60"; |
| 51 | +const COMPACT_NPUB = "npub1a2d…yp60"; |
| 52 | + |
| 53 | +async function renderPubKey(props) { |
| 54 | + const React = await import("react"); |
| 55 | + const { render, within } = await import("@testing-library/react"); |
| 56 | + const { PubKey } = await import("./PubKey.tsx"); |
| 57 | + const view = render(React.createElement(PubKey, props)); |
| 58 | + // render()'s bound queries search the whole body; scope to this render so |
| 59 | + // earlier mounts (cleaned up only per test) stay invisible. |
| 60 | + return { ...within(view.container), container: view.container }; |
| 61 | +} |
| 62 | + |
| 63 | +test("compact PubKey renders the truncated npub, never the hex", async () => { |
| 64 | + const trigger = await renderPubKey({ pubkey: HEX }); |
| 65 | + assert.equal( |
| 66 | + trigger.getByRole("button", { name: "Show full public key" }).textContent, |
| 67 | + COMPACT_NPUB, |
| 68 | + ); |
| 69 | + assert.equal(trigger.queryByText(HEX), null); |
| 70 | + |
| 71 | + // A parent row that owns the interaction gets the same text, not a button. |
| 72 | + const text = await renderPubKey({ interactive: false, pubkey: HEX }); |
| 73 | + assert.equal(text.getByText(COMPACT_NPUB).tagName, "SPAN"); |
| 74 | + assert.equal(text.queryByRole("button"), null); |
| 75 | + assert.equal(text.queryByText(HEX), null); |
| 76 | + |
| 77 | + // An all-uppercase Bech32 npub is a valid identity (parsePubkeyInput |
| 78 | + // accepts it); the gate must render its canonical compact form, not the |
| 79 | + // neutral label. |
| 80 | + const upper = await renderPubKey({ pubkey: NPUB.toUpperCase() }); |
| 81 | + assert.equal( |
| 82 | + upper.getByRole("button", { name: "Show full public key" }).textContent, |
| 83 | + COMPACT_NPUB, |
| 84 | + ); |
| 85 | + assert.equal(upper.queryByText("Unavailable"), null); |
| 86 | +}); |
| 87 | + |
| 88 | +test("full PubKey renders the complete npub with a copy affordance", async () => { |
| 89 | + const view = await renderPubKey({ pubkey: HEX, variant: "full" }); |
| 90 | + assert.equal(view.getByText(NPUB).textContent, NPUB); |
| 91 | + assert.equal( |
| 92 | + view.getByRole("button", { name: "Copy public key" }).tagName, |
| 93 | + "BUTTON", |
| 94 | + ); |
| 95 | + assert.equal(view.queryByText(HEX), null); |
| 96 | +}); |
| 97 | + |
| 98 | +test("unencodable keys render Unavailable with no copy affordance", async () => { |
| 99 | + // "zz" cannot decode; "deadbeef" is a degenerate-length hex that npubEncode |
| 100 | + // would happily turn into a checksum-valid fake npub; npub1m6kmamcvty5gd |
| 101 | + // and npub106246s decode fine but are checksum-valid short-payload npubs |
| 102 | + // (8-char and empty identity payloads). All four would masquerade as |
| 103 | + // displayable identities — the gate refuses every one. |
| 104 | + for (const pubkey of [ |
| 105 | + "zz", |
| 106 | + "deadbeef", |
| 107 | + "npub1m6kmamcvty5gd", |
| 108 | + "npub106246s", |
| 109 | + ]) { |
| 110 | + for (const variant of [undefined, "full"]) { |
| 111 | + const view = await renderPubKey({ pubkey, variant }); |
| 112 | + const label = `${pubkey} ${variant ?? "compact"}`; |
| 113 | + assert.equal(view.getByText("Unavailable").tagName, "SPAN", label); |
| 114 | + assert.equal(view.queryByRole("button"), null, label); |
| 115 | + assert.equal(view.container.textContent?.includes("npub1"), false, label); |
| 116 | + } |
| 117 | + } |
| 118 | +}); |
0 commit comments