Skip to content

Commit dc80d34

Browse files
committed
test(web): slim the composer measurement tests to what they alone catch
The 240-480px width sweep only differed from the narrow measurement tests on mutants those tests already killed, and its composer-side half recomputed the host width instead of exercising ChatComposer's read, so it proved less than it claimed. The hysteresis boundary it leaned on now sits with the pure decision in composerFooterLayout.test.ts, next to the pairing tests. Each remaining assertion was checked against a mutant of the thing it guards.
1 parent c05976b commit dc80d34

3 files changed

Lines changed: 69 additions & 105 deletions

File tree

Lines changed: 35 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { resolveContextStripLabelsCompact } from "../BranchToolbar.logic";
43
import { 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
*/
1912
class 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. */
4741
function 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`. */
7267
const STRIP_PADDING_INLINE = 12;
73-
const STRIP_GAP = 8;
68+
const STRIP_GAP = 4;
7469
const ICON_WIDTH = 16;
7570
const 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

123108
function 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

190156
afterEach(() => vi.unstubAllGlobals());
191157

192158
describe("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
});

‎apps/web/src/components/composerFooterLayout.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,34 @@ describe("context strip labels and resting composer controls", () => {
318318
resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
319319
).toEqual({ hiddenCount: 2, visible: true });
320320
});
321+
322+
it("only expands labels once the freed room clears the hysteresis margin", () => {
323+
const needed = chromeWidth + labelWidth + naturalWidth;
324+
// Room to spare is not enough while compact: expansion needs the 16px
325+
// margin, or a width that jitters around the boundary flaps the labels.
326+
expect(
327+
resolveContextStripLabelsCompact({
328+
compact: true,
329+
neededWidth: needed,
330+
availableWidth: needed + 15,
331+
}),
332+
).toBe(true);
333+
expect(
334+
resolveContextStripLabelsCompact({
335+
compact: true,
336+
neededWidth: needed,
337+
availableWidth: needed + 16,
338+
}),
339+
).toBe(false);
340+
// Without that margin pending, a strip that already shows labels keeps them.
341+
expect(
342+
resolveContextStripLabelsCompact({
343+
compact: false,
344+
neededWidth: needed,
345+
availableWidth: needed,
346+
}),
347+
).toBe(false);
348+
});
321349
});
322350

323351
describe("resolveRestingComposerControlsLayout hysteresis", () => {

‎apps/web/src/lib/contentInlineWidth.test.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,22 @@ afterEach(() => vi.unstubAllGlobals());
1111

1212
describe("contentInlineWidth", () => {
1313
it("drops the inline padding clientWidth counts as room", () => {
14+
// Asymmetric sides: a read of only one side, or of the physical props,
15+
// answers for less than the full padding.
16+
expect(box(724, { paddingInlineStart: "12px", paddingInlineEnd: "0px" })).toBe(712);
1417
expect(box(724, { paddingInlineStart: "4px", paddingInlineEnd: "8px" })).toBe(712);
1518
});
1619

17-
it("keeps the whole width when the box has no inline padding", () => {
18-
expect(box(724, { paddingInlineStart: "0px", paddingInlineEnd: "0px" })).toBe(724);
19-
});
20-
21-
it("reads the logical sides so padding-only-one-side still counts", () => {
22-
expect(box(100, { paddingInlineStart: "12px", paddingInlineEnd: "0px" })).toBe(88);
23-
expect(box(100, { paddingInlineStart: "0px", paddingInlineEnd: "12px" })).toBe(88);
24-
});
25-
2620
it("treats an unresolvable padding as none instead of answering NaN", () => {
2721
// Computed padding is px in browsers, but a missing or keyword value must
2822
// not poison a fit decision into "nothing fits".
2923
expect(box(100, { paddingInlineStart: "auto", paddingInlineEnd: "8px" })).toBe(92);
3024
expect(box(100, {})).toBe(100);
3125
});
3226

33-
it("can go negative when padding exceeds the border-box content", () => {
34-
// Callers compare against a needed width, so a squeezed box must read as
35-
// having no usable room rather than a clipped zero.
27+
it("goes negative rather than clipping a box whose padding exceeds it", () => {
28+
// Callers compare against a needed width, so a squeezed box has to read as
29+
// short of room.
3630
expect(box(10, { paddingInlineStart: "8px", paddingInlineEnd: "8px" })).toBe(-6);
3731
});
3832
});

0 commit comments

Comments
 (0)