11import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
22
3- import { resolveContextStripLabelsCompact } from "../BranchToolbar.logic" ;
43import { measureContextStrip } from "./contextStripMeasurement" ;
5- import { measureRestingComposerControls } from "./restingComposerControlsMeasurement" ;
6- import {
7- resolveRestingComposerControlsLayout ,
8- resolveRestingComposerControlsNaturalWidth ,
9- } from "../composerFooterLayout" ;
10- import { contentInlineWidth } from "../../lib/contentInlineWidth" ;
114
125/**
13- * A strip row whose widths are derived the way flex derives them, so the
14- * composer's host width is a consequence of the strip's own layout rather than a
15- * number in this file. That coupling is the point: the strip decides whether its
16- * labels fit, the composer decides how many resting controls fit in the host the
17- * strip hands it, and the two have to agree at every width .
6+ * A strip row with the composer's resting controls docked beside its labels.
7+ *
8+ * Widths are the laid-out ones a real strip produces, so the reservation the
9+ * measurement makes for the hosted controls is read from a host that exists
10+ * rather than asserted against a number .
1811 */
1912class FakeElement {
2013 children : FakeElement [ ] = [ ] ;
@@ -43,7 +36,8 @@ class FakeElement {
4336 }
4437}
4538
46- /** The composer's resting controls: a model picker and one labeled block. */
39+ /** Picker 52 + block 140 + one gap. The overflow trigger only counts once
40+ * something actually moves into it, so nothing hidden here adds to it. */
4741function buildControls ( ) : FakeElement {
4842 const picker = new FakeElement ( ) ;
4943 picker . rectWidth = 52 ;
@@ -69,23 +63,18 @@ function buildControls(): FakeElement {
6963 return controls ;
7064}
7165
66+ /** The strip's `ps-1 pe-2` and, after BranchToolbar's override, `gap-1`. */
7267const STRIP_PADDING_INLINE = 12 ;
73- const STRIP_GAP = 8 ;
68+ const STRIP_GAP = 4 ;
7469const ICON_WIDTH = 16 ;
7570const LABEL_TEXT_WIDTH = 100 ;
76-
77- interface Strip {
78- element : FakeElement ;
79- host : FakeElement ;
80- controls : FakeElement ;
81- }
71+ const CONTROLS_NATURAL_WIDTH = 196 ;
8272
8373/**
84- * Lay the row out at a given strip width. `compact` collapses the label the way
85- * the compact styles do: the icon stays, the text box shrinks to nothing while
86- * its text keeps its natural width for the next measurement.
74+ * `compact` collapses the label the way the compact styles do: the icon stays,
75+ * the text box shrinks to nothing while its text keeps its natural width.
8776 */
88- function buildStrip ( stripWidth : number , compact : boolean ) : Strip {
77+ function buildStrip ( compact : boolean ) : FakeElement {
8978 const labelShown = compact ? 0 : LABEL_TEXT_WIDTH ;
9079
9180 const labelText = new FakeElement ( ) ;
@@ -94,30 +83,26 @@ function buildStrip(stripWidth: number, compact: boolean): Strip {
9483 label . rectWidth = labelShown ;
9584 label . textNodes = [ labelText ] ;
9685
97- // The control the label lives inside, laid out at its visible width.
9886 const trigger = new FakeElement ( ) ;
9987 trigger . offsetWidth = ICON_WIDTH + labelShown ;
10088 trigger . found . set ( "[data-composer-label]" , [ label ] ) ;
10189
10290 const contextGroup = new FakeElement ( ) ;
10391 contextGroup . children = [ trigger ] ;
10492
105- const controls = buildControls ( ) ;
10693 const host = new FakeElement ( ) ;
10794 host . hostSelector = '[data-chat-resting-composer-controls-host="true"]' ;
108- host . found . set ( '[data-chat-composer-resting-controls="true"]' , [ controls ] ) ;
109- // Flex gives the host whatever the other groups leave over.
110- host . clientWidth = stripWidth - STRIP_PADDING_INLINE - STRIP_GAP - trigger . offsetWidth ;
95+ host . found . set ( '[data-chat-composer-resting-controls="true"]' , [ buildControls ( ) ] ) ;
11196
11297 const strip = new FakeElement ( ) ;
11398 strip . style . paddingInlineStart = "4px" ;
11499 strip . style . paddingInlineEnd = "8px" ;
115100 strip . style . columnGap = `${ String ( STRIP_GAP ) } px` ;
116- strip . clientWidth = stripWidth ;
101+ strip . clientWidth = 724 ;
117102 strip . children = [ contextGroup , host ] ;
118103 strip . found . set ( "[data-composer-label]" , [ label ] ) ;
119104
120- return { element : strip , host , controls } ;
105+ return strip ;
121106}
122107
123108function stubDom ( root : FakeElement ) {
@@ -162,82 +147,39 @@ function stubDom(root: FakeElement) {
162147 vi . stubGlobal ( "getComputedStyle" , ( element : object ) => styles . get ( element ) ?? { } ) ;
163148}
164149
165- /** What the strip and the composer each conclude about the same row. */
166- function probe ( stripWidth : number , compact : boolean ) {
167- const strip = buildStrip ( stripWidth , compact ) ;
168- stubDom ( strip . element ) ;
169-
170- const measured = measureContextStrip ( strip . element as unknown as HTMLElement ) ! ;
171- const measurement = measureRestingComposerControls ( strip . controls as unknown as HTMLElement ) ! ;
172- const hostWidth = contentInlineWidth ( strip . host as unknown as HTMLElement ) ;
173- const layout = resolveRestingComposerControlsLayout ( { ...measurement , hostWidth } ) ;
174-
175- return {
176- availableWidth : measured . availableWidth ,
177- neededWidth : measured . neededWidth ,
178- hostWidth,
179- controlsNaturalWidth : resolveRestingComposerControlsNaturalWidth ( measurement ) ,
180- stripCompact : resolveContextStripLabelsCompact ( {
181- compact,
182- neededWidth : measured . neededWidth ,
183- availableWidth : measured . availableWidth ,
184- } ) ,
185- controlsCompacted :
186- Boolean ( layout . hiddenCount ) || Boolean ( layout . iconOnlyCount ) || ! layout . visible ,
187- } ;
150+ function measure ( compact : boolean ) {
151+ const strip = buildStrip ( compact ) ;
152+ stubDom ( strip ) ;
153+ return measureContextStrip ( strip as unknown as HTMLElement ) ! ;
188154}
189155
190156afterEach ( ( ) => vi . unstubAllGlobals ( ) ) ;
191157
192158describe ( "measureContextStrip" , ( ) => {
193159 it ( "does not count the strip's own padding as room for its children" , ( ) => {
194- expect ( probe ( 724 , true ) . availableWidth ) . toBe ( 724 - STRIP_PADDING_INLINE ) ;
160+ // Counting it opens a band exactly the padding wide where the strip keeps
161+ // its labels while the docked controls have already collapsed to icons.
162+ expect ( measure ( true ) . availableWidth ) . toBe ( 724 - STRIP_PADDING_INLINE ) ;
195163 } ) ;
196164
197- it ( "reserves the natural width of the controls hosted beside the labels " , ( ) => {
198- const result = probe ( 724 , true ) ;
199- expect ( result . controlsNaturalWidth ) . toBe ( 196 ) ;
200- expect ( result . neededWidth ) . toBe (
201- ICON_WIDTH + LABEL_TEXT_WIDTH + result . controlsNaturalWidth + STRIP_GAP ,
165+ it ( "reserves the natural width of the controls, not what they currently show " , ( ) => {
166+ // Reserving only the visible controls lets the labels expand into room the
167+ // composer just gave up, which shrinks the host and hides them again.
168+ expect ( measure ( true ) . neededWidth ) . toBe (
169+ ICON_WIDTH + LABEL_TEXT_WIDTH + CONTROLS_NATURAL_WIDTH + STRIP_GAP ,
202170 ) ;
203171 } ) ;
204172
205173 it ( "asks for the same width whether the labels show or only their icons do" , ( ) => {
206- // Otherwise the strip reads its own compact state as freed room and expands
207- // again on the next pass.
208- expect ( probe ( 724 , true ) . neededWidth ) . toBe ( probe ( 724 , false ) . neededWidth ) ;
174+ // Otherwise the strip reads its own compact state as freed room and
175+ // expands again on the next pass.
176+ expect ( measure ( true ) . neededWidth ) . toBe ( measure ( false ) . neededWidth ) ;
209177 } ) ;
210178
211179 it ( "keeps the caller's answer while the row has no layout" , ( ) => {
212- const strip = buildStrip ( 0 , false ) ;
213- stubDom ( strip . element ) ;
214- expect ( measureContextStrip ( strip . element as unknown as HTMLElement ) ) . toBeNull ( ) ;
215- } ) ;
216-
217- it ( "holds the labels compact until the hysteresis margin clears" , ( ) => {
218- const needed = probe ( 724 , true ) . neededWidth ;
219- const decide = ( extra : number , compact : boolean ) =>
220- probe ( needed + STRIP_PADDING_INLINE + extra , compact ) . stripCompact ;
221- expect ( decide ( 0 , false ) ) . toBe ( false ) ;
222- expect ( decide ( 0 , true ) ) . toBe ( true ) ;
223- expect ( decide ( 16 , true ) ) . toBe ( false ) ;
224- } ) ;
225- } ) ;
226-
227- describe ( "context strip and resting composer controls" , ( ) => {
228- it ( "never collapses the labels and the hosted controls at the same time" , ( ) => {
229- // The band this guards is exactly the strip's inline padding: counting that
230- // padding as free space lets the strip keep its labels while the host beside
231- // them is already too narrow, so the controls drop to icons. Sweeping every
232- // width closes the whole class instead of the one reported width, and holds
233- // for both label states because the strip re-decides from its own state.
234- const broken : number [ ] = [ ] ;
235- for ( let width = 240 ; width <= 480 ; width += 1 ) {
236- for ( const compact of [ false , true ] ) {
237- const result = probe ( width , compact ) ;
238- if ( ! result . stripCompact && result . controlsCompacted ) broken . push ( width ) ;
239- }
240- }
241- expect ( broken ) . toEqual ( [ ] ) ;
180+ const strip = buildStrip ( false ) ;
181+ strip . clientWidth = 0 ;
182+ stubDom ( strip ) ;
183+ expect ( measureContextStrip ( strip as unknown as HTMLElement ) ) . toBeNull ( ) ;
242184 } ) ;
243185} ) ;
0 commit comments