|
| 1 | +// #2188: the host's read-only sentence in the sidebar. It is drawn in the wide sidebar under the |
| 2 | +// top buttons; the narrow rail drew nothing, so a window that is open elsewhere looked |
| 3 | +// editable. The rail is too narrow for the sentence, so it is clipped to one line and the |
| 4 | +// whole text stays readable through `title` and `aria-label`. |
| 5 | +// |
| 6 | +// The wide branch is the shipped behaviour and its output must not change: same class, |
| 7 | +// same role, no extra attribute. The one deliberate change on this diff's side is a host |
| 8 | +// sentence of only whitespace, which now draws nothing in either shape (see the last test). |
| 9 | +import assert from 'node:assert/strict' |
| 10 | +import { test } from 'node:test' |
| 11 | +import { fileURLToPath } from 'node:url' |
| 12 | +import { runInNewContext } from 'node:vm' |
| 13 | +import { build } from 'esbuild' |
| 14 | + |
| 15 | +const { outputFiles } = await build({ |
| 16 | + stdin: { |
| 17 | + contents: "export { TeamSidebar, SIDEBAR_CSS } from './src/client/sidebar.js'", |
| 18 | + resolveDir: fileURLToPath(new URL('..', import.meta.url)), |
| 19 | + }, |
| 20 | + bundle: true, format: 'cjs', platform: 'browser', write: false, logLevel: 'silent', |
| 21 | + external: ['react', 'react-dom', '@deepseek-ai/dsh-client-ui-primitives'], |
| 22 | +}) |
| 23 | + |
| 24 | +const h = (type, props, ...children) => ({ type, props: { ...props, children: children.flat(Infinity) } }) |
| 25 | +class Component { |
| 26 | + constructor(props) { this.props = props } |
| 27 | + setState() {} |
| 28 | +} |
| 29 | +// Renders the sidebar's own output only: a child component stays an element, so a hook a row |
| 30 | +// needs never runs. The host sentence is drawn by TeamSidebar itself. |
| 31 | +const react = { |
| 32 | + createElement: h, |
| 33 | + Fragment: Symbol('Fragment'), |
| 34 | + Component, |
| 35 | + PureComponent: Component, |
| 36 | + createRef: () => ({ current: null }), |
| 37 | + useState: initial => [typeof initial === 'function' ? initial() : initial, () => {}], |
| 38 | + useEffect() {}, |
| 39 | + useLayoutEffect() {}, |
| 40 | + useRef: value => ({ current: value }), |
| 41 | + useCallback: fn => fn, |
| 42 | + useMemo: fn => fn(), |
| 43 | +} |
| 44 | +const slots = { entries: () => [], subscribe: () => () => {}, version: () => 0 } |
| 45 | + |
| 46 | +const load = () => { |
| 47 | + const module = { exports: {} } |
| 48 | + runInNewContext(outputFiles[0].text, { |
| 49 | + module, |
| 50 | + exports: module.exports, |
| 51 | + console, |
| 52 | + Date, |
| 53 | + JSON, |
| 54 | + Promise, |
| 55 | + Symbol, |
| 56 | + __flybot__: { slots, call: () => {} }, |
| 57 | + require: (name) => { |
| 58 | + if (name === 'react') return react |
| 59 | + if (name === 'react-dom') return { createPortal: node => node } |
| 60 | + if (name === '@deepseek-ai/dsh-client-ui-primitives') return {} |
| 61 | + throw new Error(`Unexpected platform module ${name}`) |
| 62 | + }, |
| 63 | + }) |
| 64 | + return module.exports |
| 65 | +} |
| 66 | + |
| 67 | +const { TeamSidebar, SIDEBAR_CSS } = load() |
| 68 | + |
| 69 | +const HOST_TEXT = 'The Bot team is open in another dsh.' |
| 70 | + |
| 71 | +function renderSidebar({ wide, readOnly = HOST_TEXT, readOnlyKey = true }) { |
| 72 | + const roster = { ready: true, bots: [], rooms: [], agents: [], byId: {}, roomsById: {}, mainBotId: undefined, ...readOnlyKey ? { readOnly } : {} } |
| 73 | + const actions = { |
| 74 | + openInbox() {}, openBoard() {}, openPalette() {}, openNewChat() {}, |
| 75 | + landOnMainBot() {}, clearUnread() {}, isAgentSession: () => false, |
| 76 | + } |
| 77 | + return TeamSidebar({ |
| 78 | + wide, |
| 79 | + useRoster: select => select(roster), |
| 80 | + useUi: select => select({}), |
| 81 | + useSessions: select => select({ ids: [], byId: {} }), |
| 82 | + useRegistry: () => {}, |
| 83 | + useUpdate: select => select({}), |
| 84 | + useWork: select => select({}), |
| 85 | + useSessionStatus: select => select({}), |
| 86 | + useSessionRetainInfo: select => select({}), |
| 87 | + useActivity: select => select({}), |
| 88 | + actions, |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +// The element tree down to the child components, which stay opaque. |
| 93 | +const walk = (node, out = []) => { |
| 94 | + if (Array.isArray(node)) { |
| 95 | + for (const child of node) walk(child, out) |
| 96 | + return out |
| 97 | + } |
| 98 | + if (!node || typeof node !== 'object') return out |
| 99 | + out.push(node) |
| 100 | + walk(node.props?.children, out) |
| 101 | + return out |
| 102 | +} |
| 103 | +const all = tree => walk(tree) |
| 104 | +const byClass = (tree, name) => all(tree).filter(node => { |
| 105 | + const value = node.props?.className |
| 106 | + return typeof value === 'string' && value.split(' ').includes(name) |
| 107 | +}) |
| 108 | +const textOf = node => (node.props?.children ?? []) |
| 109 | + .filter(child => typeof child === 'string' || typeof child === 'number') |
| 110 | + .join('') |
| 111 | + |
| 112 | +test('a read-only host keeps the wide sidebar sentence exactly as it was', () => { |
| 113 | + const tree = renderSidebar({ wide: true }) |
| 114 | + assert.equal(byClass(tree, 'bt-rail').length, 0) |
| 115 | + const hints = byClass(tree, 'bt-read-only') |
| 116 | + assert.equal(hints.length, 1) |
| 117 | + assert.equal(hints[0].type, 'div') |
| 118 | + assert.equal(hints[0].props.className, 'bt-hint bt-read-only') |
| 119 | + assert.equal(hints[0].props.role, 'status') |
| 120 | + assert.equal(textOf(hints[0]), HOST_TEXT) |
| 121 | + // The wide branch gained no tooltip: the sentence is drawn in full there. |
| 122 | + assert.equal(hints[0].props.title, undefined) |
| 123 | + assert.equal(hints[0].props['aria-label'], undefined) |
| 124 | +}) |
| 125 | + |
| 126 | +test('a read-only host says so in the sidebar rail, with the whole sentence as its tooltip', () => { |
| 127 | + const tree = renderSidebar({ wide: false }) |
| 128 | + assert.equal(byClass(tree, 'bt-rail').length, 1) |
| 129 | + const hints = byClass(tree, 'bt-read-only') |
| 130 | + assert.equal(hints.length, 1) |
| 131 | + assert.equal(hints[0].type, 'div') |
| 132 | + assert.equal(hints[0].props.role, 'status') |
| 133 | + // The clipped line still carries the sentence itself, and the full text for a hover, |
| 134 | + // a screen reader and a reader of the accessibility tree. |
| 135 | + assert.equal(textOf(hints[0]), HOST_TEXT) |
| 136 | + assert.equal(hints[0].props.title, HOST_TEXT) |
| 137 | + assert.equal(hints[0].props['aria-label'], HOST_TEXT) |
| 138 | +}) |
| 139 | + |
| 140 | +test('the rail clips the sentence to one line and the wide rule is untouched', () => { |
| 141 | + assert.match(SIDEBAR_CSS, /\.bt-side \.bt-read-only\{margin:4px 8px 6px;padding:8px 10px;border-radius:10px;background:var\(--bt-hover\);font-size:12px;line-height:1\.45;text-align:left\}/) |
| 142 | + const rail = SIDEBAR_CSS.match(/\.bt-rail \.bt-read-only\{([^}]*)\}/) |
| 143 | + assert.ok(rail, 'the rail rule is missing') |
| 144 | + assert.match(rail[1], /white-space:nowrap/) |
| 145 | + assert.match(rail[1], /overflow:hidden/) |
| 146 | + assert.match(rail[1], /text-overflow:ellipsis/) |
| 147 | + assert.match(rail[1], /text-align:center/) |
| 148 | + // Both rules match inside the rail (it carries `bt-side` too), so the rail rule must win. |
| 149 | + assert.ok(SIDEBAR_CSS.indexOf('.bt-rail .bt-read-only') > SIDEBAR_CSS.indexOf('.bt-side .bt-read-only')) |
| 150 | +}) |
| 151 | + |
| 152 | +test('a writable window, or a host sentence of only whitespace, draws no read-only sentence in either shape', () => { |
| 153 | + for (const wide of [true, false]) { |
| 154 | + const shape = wide ? 'wide' : 'rail' |
| 155 | + for (const readOnly of [null, '', ' ', '\n\t ']) { |
| 156 | + const tree = renderSidebar({ wide, readOnly }) |
| 157 | + assert.equal(byClass(tree, 'bt-read-only').length, 0, `${shape}, ${JSON.stringify(readOnly)}`) |
| 158 | + } |
| 159 | + // The host said nothing at all: the field is absent, not empty. |
| 160 | + assert.equal(byClass(renderSidebar({ wide, readOnlyKey: false }), 'bt-read-only').length, 0, `${shape}, no field`) |
| 161 | + } |
| 162 | +}) |
0 commit comments