|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import React from "react"; |
| 4 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 5 | +import { truncatePubkey } from "../lib/pubkey.ts"; |
| 6 | +import { createMarkdownComponents } from "./markdown.tsx"; |
| 7 | +import { renderCachedMarkdown } from "./markdown/nodeCache.ts"; |
| 8 | +import { MarkdownRuntimeContext } from "./markdown/runtimeContext.ts"; |
| 9 | + |
| 10 | +const KEY = `150b20bd${"a".repeat(52)}15dc`; |
| 11 | + |
| 12 | +for (const agent of [false, true]) { |
| 13 | + test(`rendered ${agent ? "agent" : "human"} abbreviates a bound key without changing its metadata`, () => { |
| 14 | + const label = `Scout (${KEY}) 2`; |
| 15 | + const name = label.toLowerCase(); |
| 16 | + const html = renderToStaticMarkup( |
| 17 | + React.createElement( |
| 18 | + MarkdownRuntimeContext.Provider, |
| 19 | + { |
| 20 | + value: { |
| 21 | + channels: [], |
| 22 | + mentionPubkeysByName: { [name]: KEY }, |
| 23 | + agentMentionPubkeysByName: agent ? { [name]: KEY } : {}, |
| 24 | + }, |
| 25 | + }, |
| 26 | + renderCachedMarkdown({ |
| 27 | + content: `Ask @${label}`, |
| 28 | + mentionNames: [label], |
| 29 | + components: createMarkdownComponents(false, false), |
| 30 | + variant: `compact-mention-${agent}`, |
| 31 | + }), |
| 32 | + ), |
| 33 | + ); |
| 34 | + assert.equal( |
| 35 | + html.replace(/<[^>]+>/g, ""), |
| 36 | + `Ask Scout (${truncatePubkey(KEY)}) 2`, |
| 37 | + ); |
| 38 | + assert.ok(html.includes(`data-mention-label="${label}"`)); |
| 39 | + assert.ok(html.includes(`data-mention-pubkey="${KEY}"`)); |
| 40 | + assert.ok(html.includes(`title="${label}"`)); |
| 41 | + assert.ok(html.includes(`aria-label="${label}"`)); |
| 42 | + assert.match( |
| 43 | + html, |
| 44 | + new RegExp( |
| 45 | + `inline-chip-leading-fragment[^>]*inline-chip-icon-${agent ? "agent" : "human"}`, |
| 46 | + ), |
| 47 | + ); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +test("an unresolved qualified mention stays literal rather than claiming an abbreviated identity", () => { |
| 52 | + const label = `Scout (${KEY})`; |
| 53 | + const html = renderToStaticMarkup( |
| 54 | + React.createElement( |
| 55 | + MarkdownRuntimeContext.Provider, |
| 56 | + { |
| 57 | + value: { channels: [], mentionPubkeysByName: {} }, |
| 58 | + }, |
| 59 | + renderCachedMarkdown({ |
| 60 | + content: `Ask @${label}`, |
| 61 | + mentionNames: [label], |
| 62 | + components: createMarkdownComponents(false, false), |
| 63 | + variant: "unresolved-compact-mention", |
| 64 | + }), |
| 65 | + ), |
| 66 | + ); |
| 67 | + assert.ok(html.includes(`@${label}`)); |
| 68 | + assert.doesNotMatch(html, /data-mention=/); |
| 69 | +}); |
0 commit comments