@sentropic/design-system-svelte 0.34.71 → 0.34.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,679 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { fireEvent, render, screen } from "@testing-library/svelte";
4
+ import { flushSync } from "svelte";
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+ import PanelStackFixture from "./PanelStackFixture.svelte";
7
+ import { PANEL_STACK_MAX_SECTIONS, PRIMARY_MIN_BLOCK_SIZE_PX } from "./PanelStack.svelte";
8
+ const sections = [
9
+ { id: "a", label: "Alpha" },
10
+ { id: "b", label: "Beta" },
11
+ { id: "c", label: "Gamma" }
12
+ ];
13
+ function bodies(container) {
14
+ return Array.from(container.querySelectorAll(".st-panelSection__body"));
15
+ }
16
+ function scrollOwnerBodies(container) {
17
+ return Array.from(container.querySelectorAll(".st-panelSection__body--scrollOwner"));
18
+ }
19
+ function collapsedBodies(container) {
20
+ return Array.from(container.querySelectorAll(".st-panelSection__body--collapsed"));
21
+ }
22
+ function trigger(container, id) {
23
+ return container.querySelector(`#st-panelSection-trigger-${id}`);
24
+ }
25
+ function body(container, id) {
26
+ return container.querySelector(`#st-panelSection-region-${id}`);
27
+ }
28
+ function sectionRootEl(container, id) {
29
+ return container.querySelector(`[data-panel-section-id="${id}"]`);
30
+ }
31
+ function sectionHeaderEl(container, id) {
32
+ return sectionRootEl(container, id)?.querySelector(".st-panelSection__header") ?? null;
33
+ }
34
+ function stackRoot(container) {
35
+ return container.querySelector(".st-panelStack");
36
+ }
37
+ class FakeResizeObserver {
38
+ callback;
39
+ static instances = [];
40
+ observed = new Set();
41
+ constructor(callback) {
42
+ this.callback = callback;
43
+ FakeResizeObserver.instances.push(this);
44
+ }
45
+ observe(target) {
46
+ this.observed.add(target);
47
+ }
48
+ unobserve(target) {
49
+ this.observed.delete(target);
50
+ }
51
+ disconnect() {
52
+ this.observed.clear();
53
+ }
54
+ deliver(target, height) {
55
+ if (this.observed.has(target))
56
+ this.callback([{ target, contentRect: { height } }]);
57
+ }
58
+ }
59
+ /** Simulate a real layout measurement landing for `target`, synchronously
60
+ * flushing whatever reactive consequence it has (e.g. the auto-collapse
61
+ * decision effect re-evaluating). */
62
+ function fireResize(target, height) {
63
+ flushSync(() => {
64
+ for (const instance of FakeResizeObserver.instances)
65
+ instance.deliver(target, height);
66
+ });
67
+ }
68
+ // ---------------------------------------------------------------------------
69
+ // The invariant this whole component pair exists to guarantee: AT ANY MOMENT,
70
+ // IN ANY STATE, EXACTLY ONE element inside the stack has `overflow: auto`, and
71
+ // the stack container itself never scrolls. We check this two ways: (1) at the
72
+ // CSS-source level — there is only one place in either file that can ever set
73
+ // `overflow: auto`, and the stack root is unconditionally `overflow: hidden` —
74
+ // and (2) at the DOM level, across every state reachable by clicking through
75
+ // both shapes.
76
+ // ---------------------------------------------------------------------------
77
+ describe("PanelStack / PanelSection — single scroll owner invariant", () => {
78
+ it("the stack root style is unconditionally overflow: hidden and never overflow: auto", () => {
79
+ const source = readFileSync(join(process.cwd(), "src/lib/PanelStack.svelte"), "utf8");
80
+ const style = /<style>([\s\S]*)<\/style>/.exec(source)?.[1] ?? "";
81
+ expect(style).toContain(".st-panelStack {");
82
+ expect(style).toMatch(/\.st-panelStack\s*{[^}]*overflow:\s*hidden/);
83
+ expect(style).not.toContain("overflow: auto");
84
+ });
85
+ it("exactly one CSS rule across the whole component pair ever sets overflow: auto, and it is the scroll-owner body", () => {
86
+ const stackSource = readFileSync(join(process.cwd(), "src/lib/PanelStack.svelte"), "utf8");
87
+ const sectionSource = readFileSync(join(process.cwd(), "src/lib/PanelSection.svelte"), "utf8");
88
+ // Only the compiled style blocks matter here — a doc-comment in the
89
+ // instance script is allowed to mention the property in prose.
90
+ const stackStyle = /<style>([\s\S]*)<\/style>/.exec(stackSource)?.[1] ?? "";
91
+ const sectionStyle = /<style>([\s\S]*)<\/style>/.exec(sectionSource)?.[1] ?? "";
92
+ const autoOccurrences = (stackStyle + sectionStyle).match(/overflow:\s*auto/g) ?? [];
93
+ expect(autoOccurrences.length).toBe(1);
94
+ expect(sectionStyle).toMatch(/\.st-panelSection__body--scrollOwner\s*{[^}]*overflow:\s*auto/);
95
+ });
96
+ it("sticky-item: EXACTLY one scroll-owner body exists across every reachable click sequence (never 0, never 2)", async () => {
97
+ const { container } = render(PanelStackFixture, {
98
+ props: { shape: "sticky-item", sections, defaultExpanded: "a" }
99
+ });
100
+ // Walk every header, in every order, clicking twice each (open then a
101
+ // no-op re-click on the same header) — after EVERY single click, assert
102
+ // the invariant.
103
+ for (const id of ["a", "b", "c", "b", "a", "c", "c"]) {
104
+ await fireEvent.click(trigger(container, id));
105
+ expect(scrollOwnerBodies(container).length).toBe(1);
106
+ expect(container.querySelector(".st-panelStack")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(false);
107
+ }
108
+ });
109
+ it("sticky-item: with no `expanded` and no `defaultExpanded`, the FIRST registered section is the scroll owner (never zero)", () => {
110
+ const { container } = render(PanelStackFixture, { props: { shape: "sticky-item", sections } });
111
+ expect(scrollOwnerBodies(container).length).toBe(1);
112
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
113
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("true");
114
+ });
115
+ it("split-primary: EXACTLY one scroll-owner body (the primary) across every reachable secondary-toggle sequence", async () => {
116
+ const { container } = render(PanelStackFixture, {
117
+ props: { shape: "split-primary", sections, primary: "a" }
118
+ });
119
+ for (const id of ["b", "c", "b", "b", "c", "a" /* no-op: primary has no trigger */]) {
120
+ const btn = trigger(container, id);
121
+ if (btn)
122
+ await fireEvent.click(btn);
123
+ const owners = scrollOwnerBodies(container);
124
+ expect(owners.length).toBe(1);
125
+ expect(owners[0]).toBe(body(container, "a"));
126
+ }
127
+ });
128
+ it("split-primary: with `primary` unset, the FIRST registered section owns the scroll (never zero)", () => {
129
+ const { container } = render(PanelStackFixture, { props: { shape: "split-primary", sections } });
130
+ expect(scrollOwnerBodies(container).length).toBe(1);
131
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
132
+ });
133
+ it("split-primary: with `primary` matching no section (typo), the FIRST registered section owns the scroll (never zero)", () => {
134
+ const { container } = render(PanelStackFixture, {
135
+ props: { shape: "split-primary", sections, primary: "does-not-exist" }
136
+ });
137
+ expect(scrollOwnerBodies(container).length).toBe(1);
138
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
139
+ });
140
+ it("dynamic sections (sticky-item): removing the current scroll owner leaves EXACTLY one owner — the next first section", async () => {
141
+ const { container, rerender } = render(PanelStackFixture, {
142
+ props: { shape: "sticky-item", sections, defaultExpanded: "b" }
143
+ });
144
+ expect(scrollOwnerBodies(container).length).toBe(1);
145
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "b"));
146
+ // "b" (the current owner) is removed from the stack entirely.
147
+ await rerender({ shape: "sticky-item", sections: [sections[0], sections[2]], defaultExpanded: "b" });
148
+ expect(scrollOwnerBodies(container).length).toBe(1);
149
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
150
+ });
151
+ it("dynamic sections (split-primary): removing the primary leaves EXACTLY one owner — the next first section", async () => {
152
+ const { container, rerender } = render(PanelStackFixture, {
153
+ props: { shape: "split-primary", sections, primary: "a" }
154
+ });
155
+ expect(scrollOwnerBodies(container).length).toBe(1);
156
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
157
+ // "a" (the primary) is removed from the stack entirely.
158
+ await rerender({ shape: "split-primary", sections: [sections[1], sections[2]], primary: "a" });
159
+ expect(scrollOwnerBodies(container).length).toBe(1);
160
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "b"));
161
+ });
162
+ it("every section body stays mounted (never removed) regardless of collapsed state", () => {
163
+ const { container } = render(PanelStackFixture, {
164
+ props: { shape: "sticky-item", sections, defaultExpanded: "a" }
165
+ });
166
+ expect(bodies(container).length).toBe(sections.length);
167
+ expect(collapsedBodies(container).length).toBe(sections.length - 1);
168
+ });
169
+ });
170
+ describe("PanelStack — sticky-item shape (default)", () => {
171
+ it("all headers stay visible, in document order, and are buttons", () => {
172
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
173
+ const buttons = Array.from(container.querySelectorAll(".st-panelSection__trigger"));
174
+ expect(buttons.map((b) => b.textContent?.trim())).toEqual(["Alpha", "Beta", "Gamma"]);
175
+ });
176
+ it("exactly one section expanded at a time (radio semantics) — opening b closes a", async () => {
177
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
178
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("true");
179
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
180
+ await fireEvent.click(trigger(container, "b"));
181
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("false");
182
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
183
+ expect(body(container, "a")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
184
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(true);
185
+ });
186
+ it("clicking the already-expanded header is a no-op (no collapse-to-zero gesture)", async () => {
187
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
188
+ await fireEvent.click(trigger(container, "a"));
189
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("true");
190
+ expect(scrollOwnerBodies(container).length).toBe(1);
191
+ });
192
+ it("uncontrolled: defaultExpanded seeds the initial state, then clicks own the state internally", async () => {
193
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "b" } });
194
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
195
+ await fireEvent.click(trigger(container, "c"));
196
+ expect(trigger(container, "c")?.getAttribute("aria-expanded")).toBe("true");
197
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
198
+ });
199
+ it("controlled: expanded prop is the source of truth — clicks fire onExpandedChange but do NOT move the DOM state on their own", async () => {
200
+ const onExpandedChange = vi.fn();
201
+ const { container, rerender } = render(PanelStackFixture, {
202
+ props: { sections, expanded: "a", onExpandedChange }
203
+ });
204
+ await fireEvent.click(trigger(container, "b"));
205
+ expect(onExpandedChange).toHaveBeenCalledWith("b");
206
+ // Prop hasn't been updated by the consumer yet — DOM must still show "a".
207
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("true");
208
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
209
+ // Consumer applies the callback's value -> DOM follows.
210
+ await rerender({ sections, expanded: "b", onExpandedChange });
211
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
212
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("false");
213
+ });
214
+ it("ARIA wiring: aria-controls points at an existing role=region, labelled back by the trigger", () => {
215
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
216
+ for (const id of ["a", "b", "c"]) {
217
+ const btn = trigger(container, id);
218
+ const controlsId = btn.getAttribute("aria-controls");
219
+ expect(controlsId).toBe(`st-panelSection-region-${id}`);
220
+ const region = container.querySelector(`#${controlsId}`);
221
+ expect(region).toBeTruthy();
222
+ expect(region?.getAttribute("role")).toBe("region");
223
+ expect(region?.getAttribute("aria-labelledby")).toBe(btn.id);
224
+ }
225
+ });
226
+ it("stack root exposes the accessible name via aria-label", () => {
227
+ render(PanelStackFixture, { props: { sections, defaultExpanded: "a", label: "Docked chat" } });
228
+ expect(screen.getByRole("group", { name: "Docked chat" })).toBeTruthy();
229
+ });
230
+ });
231
+ describe("PanelStack — split-primary shape", () => {
232
+ it("the primary section is always expanded and its header is a plain heading, NOT a button", () => {
233
+ const { container } = render(PanelStackFixture, {
234
+ props: { shape: "split-primary", sections, primary: "a" }
235
+ });
236
+ const primaryTrigger = container.querySelector("#st-panelSection-trigger-a");
237
+ expect(primaryTrigger.tagName).not.toBe("BUTTON");
238
+ expect(primaryTrigger.hasAttribute("aria-expanded")).toBe(false);
239
+ expect(body(container, "a")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(true);
240
+ // Non-primary sections DO get a real disclosure button.
241
+ const secondaryTrigger = trigger(container, "b");
242
+ expect(secondaryTrigger?.tagName).toBe("BUTTON");
243
+ expect(secondaryTrigger?.hasAttribute("aria-expanded")).toBe(true);
244
+ });
245
+ it("non-primary sections default closed (header only)", () => {
246
+ const { container } = render(PanelStackFixture, {
247
+ props: { shape: "split-primary", sections, primary: "a" }
248
+ });
249
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
250
+ expect(trigger(container, "c")?.getAttribute("aria-expanded")).toBe("false");
251
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
252
+ });
253
+ it("several non-primary sections can be expanded at once, and neither becomes the scroll owner", async () => {
254
+ const { container } = render(PanelStackFixture, {
255
+ props: { shape: "split-primary", sections, primary: "a" }
256
+ });
257
+ await fireEvent.click(trigger(container, "b"));
258
+ await fireEvent.click(trigger(container, "c"));
259
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
260
+ expect(trigger(container, "c")?.getAttribute("aria-expanded")).toBe("true");
261
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(false);
262
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(false);
263
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
264
+ // Only the primary ever owns the scroll.
265
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
266
+ });
267
+ it("a non-primary section can be closed again independently of the others", async () => {
268
+ const { container } = render(PanelStackFixture, {
269
+ props: { shape: "split-primary", sections, primary: "a" }
270
+ });
271
+ await fireEvent.click(trigger(container, "b"));
272
+ await fireEvent.click(trigger(container, "c"));
273
+ await fireEvent.click(trigger(container, "b"));
274
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
275
+ expect(trigger(container, "c")?.getAttribute("aria-expanded")).toBe("true");
276
+ });
277
+ it("ARIA wiring holds for the primary heading too: region is role=region, labelled by the heading's id", () => {
278
+ const { container } = render(PanelStackFixture, {
279
+ props: { shape: "split-primary", sections, primary: "a" }
280
+ });
281
+ const heading = container.querySelector("#st-panelSection-trigger-a");
282
+ const region = container.querySelector("#st-panelSection-region-a");
283
+ expect(region.getAttribute("role")).toBe("region");
284
+ expect(region.getAttribute("aria-labelledby")).toBe(heading.id);
285
+ });
286
+ });
287
+ describe("PanelStack / PanelSection — mount-once content", () => {
288
+ it("content survives a sticky-item collapse/expand round-trip: same DOM node instance, never remounted", async () => {
289
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
290
+ const before = container.querySelector('[data-testid="content-a"]');
291
+ expect(before).toBeTruthy();
292
+ await fireEvent.click(trigger(container, "b")); // collapse "a"
293
+ const whileCollapsed = container.querySelector('[data-testid="content-a"]');
294
+ expect(whileCollapsed).toBe(before);
295
+ await fireEvent.click(trigger(container, "a")); // re-expand "a"
296
+ const after = container.querySelector('[data-testid="content-a"]');
297
+ expect(after).toBe(before);
298
+ });
299
+ it("content survives a split-primary secondary collapse/expand round-trip: same DOM node instance", async () => {
300
+ const { container } = render(PanelStackFixture, {
301
+ props: { shape: "split-primary", sections, primary: "a" }
302
+ });
303
+ const before = container.querySelector('[data-testid="content-b"]');
304
+ expect(before).toBeTruthy();
305
+ await fireEvent.click(trigger(container, "b")); // expand
306
+ expect(container.querySelector('[data-testid="content-b"]')).toBe(before);
307
+ await fireEvent.click(trigger(container, "b")); // collapse again
308
+ expect(container.querySelector('[data-testid="content-b"]')).toBe(before);
309
+ });
310
+ });
311
+ describe("PanelStack / PanelSection — misc", () => {
312
+ it("renders header-trailing actions for a section that provides them", () => {
313
+ const withAction = [...sections.slice(0, 2), { ...sections[2], action: "Clear" }];
314
+ const { container } = render(PanelStackFixture, {
315
+ props: { sections: withAction, defaultExpanded: "a" }
316
+ });
317
+ expect(container.querySelector('[data-testid="action-c"]')?.textContent).toBe("Clear");
318
+ });
319
+ it("forwards a custom class on both the stack root and a section root, alongside the base class", () => {
320
+ const withClass = [{ ...sections[0], class: "consumer-a" }, sections[1], sections[2]];
321
+ const { container } = render(PanelStackFixture, {
322
+ props: { sections: withClass, defaultExpanded: "a", class: "consumer-stack" }
323
+ });
324
+ const root = container.querySelector(".st-panelStack");
325
+ expect(root?.classList.contains("consumer-stack")).toBe(true);
326
+ const sectionRoot = container.querySelector('[data-panel-section-id="a"]');
327
+ expect(sectionRoot?.classList.contains("st-panelSection")).toBe(true);
328
+ expect(sectionRoot?.classList.contains("consumer-a")).toBe(true);
329
+ });
330
+ });
331
+ // ---------------------------------------------------------------------------
332
+ // Ratified consumer-lane contract, three changes:
333
+ // 1. the stack is bounded to PANEL_STACK_MAX_SECTIONS (4) — a dev warning,
334
+ // never a throw or a silent truncation;
335
+ // 2. split-primary's primary fallback chain is now designated / first
336
+ // EXPANDED (DOM order) / first registered — not designated / first
337
+ // registered;
338
+ // 3. split-primary auto-collapses secondaries that would squeeze the
339
+ // primary below its floor, least-recently-expanded first, and never
340
+ // oscillates.
341
+ // ---------------------------------------------------------------------------
342
+ describe("PanelStack — bounded to PANEL_STACK_MAX_SECTIONS sections", () => {
343
+ it("PANEL_STACK_MAX_SECTIONS is 4", () => {
344
+ expect(PANEL_STACK_MAX_SECTIONS).toBe(4);
345
+ });
346
+ it("registering exactly the max (4) sections does not warn", () => {
347
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
348
+ const four = [...sections, { id: "d", label: "Delta" }];
349
+ const { container } = render(PanelStackFixture, { props: { sections: four, defaultExpanded: "a" } });
350
+ expect(bodies(container).length).toBe(4);
351
+ expect(warn).not.toHaveBeenCalled();
352
+ warn.mockRestore();
353
+ });
354
+ it("registering a 5th section warns once (dev visibility), but still renders ALL sections — never truncated", () => {
355
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
356
+ const five = [...sections, { id: "d", label: "Delta" }, { id: "e", label: "Epsilon" }];
357
+ const { container } = render(PanelStackFixture, { props: { sections: five, defaultExpanded: "a" } });
358
+ expect(bodies(container).length).toBe(5);
359
+ expect(warn).toHaveBeenCalledTimes(1);
360
+ expect(warn.mock.calls[0][0]).toContain("[PanelStack]");
361
+ expect(warn.mock.calls[0][0]).toContain("4");
362
+ // The bound is advisory, not enforced — the single-scroll-owner invariant
363
+ // must still hold with a 5th (unsupported-count) section present.
364
+ expect(scrollOwnerBodies(container).length).toBe(1);
365
+ warn.mockRestore();
366
+ });
367
+ });
368
+ describe("PanelStack — split-primary primary fallback chain", () => {
369
+ it("tier 1: a valid designated `primary` always wins, regardless of what's expanded", async () => {
370
+ const { container } = render(PanelStackFixture, {
371
+ props: { shape: "split-primary", sections, primary: "a" }
372
+ });
373
+ await fireEvent.click(trigger(container, "b"));
374
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
375
+ });
376
+ it("tier 2: primary unset — opening ONE secondary makes IT the primary (first EXPANDED)", async () => {
377
+ const { container } = render(PanelStackFixture, { props: { shape: "split-primary", sections } });
378
+ // Nothing expanded yet: last-resort tier (first registered) applies.
379
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
380
+ await fireEvent.click(trigger(container, "b"));
381
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "b")]);
382
+ // "a" was implicitly primary and is now demoted to an ordinary,
383
+ // closed-by-default secondary with a real disclosure button.
384
+ expect(container.querySelector("#st-panelSection-trigger-a")?.tagName).toBe("BUTTON");
385
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("false");
386
+ });
387
+ it("tier 2: primary unset — DOM order breaks ties, not click order", async () => {
388
+ const { container } = render(PanelStackFixture, { props: { shape: "split-primary", sections } });
389
+ // Click "c" first, then "b" — DOM order is a, b, c.
390
+ await fireEvent.click(trigger(container, "c"));
391
+ await fireEvent.click(trigger(container, "b"));
392
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "b")]);
393
+ });
394
+ it("tier 2 beats tier 3: a `primary` matching no section (typo) still prefers the first EXPANDED over the first registered", async () => {
395
+ const { container } = render(PanelStackFixture, {
396
+ props: { shape: "split-primary", sections, primary: "does-not-exist" }
397
+ });
398
+ await fireEvent.click(trigger(container, "c"));
399
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "c")]);
400
+ });
401
+ it("tier 3 (last resort): primary unset and nothing expanded — first registered section owns the scroll", () => {
402
+ const { container } = render(PanelStackFixture, { props: { shape: "split-primary", sections } });
403
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
404
+ });
405
+ });
406
+ describe("PanelStack — split-primary auto-collapse (secondaries yield to the primary's floor)", () => {
407
+ beforeEach(() => {
408
+ FakeResizeObserver.instances = [];
409
+ vi.stubGlobal("ResizeObserver", FakeResizeObserver);
410
+ });
411
+ afterEach(() => {
412
+ vi.unstubAllGlobals();
413
+ });
414
+ it("collapses the least-recently-expanded secondary when the primary would be squeezed below its floor, and stops as soon as it fits again (no avalanche)", async () => {
415
+ const { container } = render(PanelStackFixture, {
416
+ props: { shape: "split-primary", sections, primary: "a" }
417
+ });
418
+ await fireEvent.click(trigger(container, "b")); // opened first -> least recently expanded
419
+ await fireEvent.click(trigger(container, "c")); // opened second
420
+ const stackEl = stackRoot(container);
421
+ const headerA = sectionHeaderEl(container, "a");
422
+ const rootB = sectionRootEl(container, "b");
423
+ const rootC = sectionRootEl(container, "c");
424
+ fireResize(stackEl, 400);
425
+ fireResize(headerA, 40);
426
+ fireResize(rootB, 150);
427
+ // available = 400 - 40 - 150 = 210 >= 160: still fits, nothing collapses yet.
428
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
429
+ fireResize(rootC, 100);
430
+ // available = 400 - 40 - (150 + 100) = 110 < 160: over budget -> collapse "b" (LRU).
431
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
432
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
433
+ // "c" — opened more recently — is left alone. This is the no-avalanche
434
+ // assertion: the decision that collapsed "b" must NOT also collapse "c"
435
+ // in the same pass using "b"'s now-stale (pre-collapse) measurement.
436
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
437
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
438
+ // Simulate the real shrink that follows "b" actually collapsing.
439
+ fireResize(rootB, 40);
440
+ // available = 400 - 40 - (40 + 100) = 220 >= 160: fits now — "c" must
441
+ // remain untouched; the single earlier collapse was sufficient.
442
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
443
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
444
+ });
445
+ it("collapses MULTIPLE secondaries when one isn't enough, converges to a stable fixed point, and does not oscillate on repeated identical measurements", async () => {
446
+ const { container } = render(PanelStackFixture, {
447
+ props: { shape: "split-primary", sections, primary: "a" }
448
+ });
449
+ await fireEvent.click(trigger(container, "b"));
450
+ await fireEvent.click(trigger(container, "c"));
451
+ const stackEl = stackRoot(container);
452
+ const headerA = sectionHeaderEl(container, "a");
453
+ const rootB = sectionRootEl(container, "b");
454
+ const rootC = sectionRootEl(container, "c");
455
+ fireResize(stackEl, 250);
456
+ fireResize(headerA, 40);
457
+ fireResize(rootB, 150);
458
+ fireResize(rootC, 100);
459
+ // available = 250 - 40 - (150 + 100) = -40 < 160 -> collapse "b" (LRU).
460
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
461
+ fireResize(rootB, 40); // "b"'s real post-collapse (header-only) height.
462
+ // available = 250 - 40 - (40 + 100) = 70 < 160 -> still short -> collapse "c" too.
463
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
464
+ fireResize(rootC, 40); // "c"'s real post-collapse height.
465
+ // available = 250 - 40 - (40 + 40) = 130 < 160: still under the floor, but
466
+ // nothing left to collapse — the degenerate "pane too small even fully
467
+ // collapsed" case. The algorithm must stop, not throw, not loop.
468
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
469
+ // Fire the SAME measurements again, several times: must be a no-op —
470
+ // proof against oscillation (nothing flips back open, nothing new
471
+ // collapses, the single-scroll-owner invariant never wavers).
472
+ for (let i = 0; i < 5; i++) {
473
+ fireResize(stackEl, 250);
474
+ fireResize(headerA, 40);
475
+ fireResize(rootB, 40);
476
+ fireResize(rootC, 40);
477
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
478
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
479
+ expect(scrollOwnerBodies(container).length).toBe(1);
480
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
481
+ }
482
+ });
483
+ it("explicitly re-opening an auto-collapsed secondary clears its collapse immediately, and a later re-squeeze collapses the NEW least-recently-expanded one instead (the just-reopened section survives)", async () => {
484
+ const { container } = render(PanelStackFixture, {
485
+ props: { shape: "split-primary", sections, primary: "a" }
486
+ });
487
+ await fireEvent.click(trigger(container, "b")); // opened first
488
+ await fireEvent.click(trigger(container, "c")); // opened second
489
+ const stackEl = stackRoot(container);
490
+ const headerA = sectionHeaderEl(container, "a");
491
+ const rootB = sectionRootEl(container, "b");
492
+ const rootC = sectionRootEl(container, "c");
493
+ fireResize(stackEl, 400);
494
+ fireResize(headerA, 40);
495
+ fireResize(rootB, 150);
496
+ fireResize(rootC, 100);
497
+ // "b" (LRU) auto-collapses; single-owner invariant holds throughout.
498
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
499
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
500
+ // User explicitly re-opens "b" — clears the forced collapse right away,
501
+ // before any new measurement, and makes "b" the MOST recently expanded.
502
+ await fireEvent.click(trigger(container, "b"));
503
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
504
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
505
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
506
+ // A subsequent measurement reflecting "b" back at its natural size
507
+ // re-triggers the same squeeze — this time "c" (now the LRU) must yield,
508
+ // NOT "b" again.
509
+ fireResize(rootB, 150);
510
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
511
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
512
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
513
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
514
+ });
515
+ it("never collapses anything when the pane comfortably fits everyone (no false positives)", async () => {
516
+ const { container } = render(PanelStackFixture, {
517
+ props: { shape: "split-primary", sections, primary: "a" }
518
+ });
519
+ await fireEvent.click(trigger(container, "b"));
520
+ await fireEvent.click(trigger(container, "c"));
521
+ fireResize(stackRoot(container), 1000);
522
+ fireResize(sectionHeaderEl(container, "a"), 40);
523
+ fireResize(sectionRootEl(container, "b"), 150);
524
+ fireResize(sectionRootEl(container, "c"), 100);
525
+ // available = 1000 - 40 - (150 + 100) = 710 >= 160.
526
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
527
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
528
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
529
+ });
530
+ it("a custom, HIGHER primaryMinHeight raises the auto-collapse threshold: measurements that fit comfortably under the default floor now trigger a collapse", async () => {
531
+ const { container } = render(PanelStackFixture, {
532
+ props: { shape: "split-primary", sections, primary: "a", primaryMinHeight: 800 }
533
+ });
534
+ await fireEvent.click(trigger(container, "b")); // opened first -> least recently expanded
535
+ await fireEvent.click(trigger(container, "c"));
536
+ fireResize(stackRoot(container), 1000);
537
+ fireResize(sectionHeaderEl(container, "a"), 40);
538
+ fireResize(sectionRootEl(container, "b"), 150);
539
+ fireResize(sectionRootEl(container, "c"), 100);
540
+ // available = 1000 - 40 - (150 + 100) = 710. Against the DEFAULT floor
541
+ // (160) this comfortably fits (see the "no false positives" test above,
542
+ // same numbers) — but 710 < 800, this instance's custom floor, so "b"
543
+ // (LRU) must auto-collapse. Proves the JS threshold reads the live prop,
544
+ // not the hardcoded default.
545
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(true);
546
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
547
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
548
+ });
549
+ it("a custom, LOWER primaryMinHeight relaxes the auto-collapse threshold: measurements that would collapse a secondary under the default floor now fit", async () => {
550
+ const { container } = render(PanelStackFixture, {
551
+ props: { shape: "split-primary", sections, primary: "a", primaryMinHeight: 50 }
552
+ });
553
+ await fireEvent.click(trigger(container, "b"));
554
+ await fireEvent.click(trigger(container, "c"));
555
+ fireResize(stackRoot(container), 400);
556
+ fireResize(sectionHeaderEl(container, "a"), 40);
557
+ fireResize(sectionRootEl(container, "b"), 150);
558
+ fireResize(sectionRootEl(container, "c"), 100);
559
+ // available = 400 - 40 - (150 + 100) = 110. Against the DEFAULT floor
560
+ // (160) this collapses "b" (see the earlier "collapses the
561
+ // least-recently-expanded secondary…" test, identical numbers) — but
562
+ // 110 >= 50, this instance's custom lower floor, so nothing collapses.
563
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
564
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
565
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
566
+ });
567
+ it("floor constant used by the algorithm matches the documented value", () => {
568
+ // Sanity check that the scenarios above (which assume 160px) are actually
569
+ // exercising the real threshold and not a coincidence of the numbers
570
+ // chosen: reduce the deficit to exactly at the boundary.
571
+ expect(PRIMARY_MIN_BLOCK_SIZE_PX).toBe(160);
572
+ });
573
+ });
574
+ describe("PanelStack — auto-collapse is a no-throw no-op when ResizeObserver is unavailable", () => {
575
+ it("split-primary with several secondaries opened renders normally, without throwing, and keeps the single-scroll-owner invariant — jsdom has no ResizeObserver by default", async () => {
576
+ expect(typeof ResizeObserver).toBe("undefined");
577
+ const { container } = render(PanelStackFixture, {
578
+ props: { shape: "split-primary", sections, primary: "a" }
579
+ });
580
+ await fireEvent.click(trigger(container, "b"));
581
+ await fireEvent.click(trigger(container, "c"));
582
+ // No auto-collapse can possibly engage — both stay expanded as requested.
583
+ expect(body(container, "b")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
584
+ expect(body(container, "c")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
585
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
586
+ });
587
+ });
588
+ // ---------------------------------------------------------------------------
589
+ // Consumer-lane follow-up, two changes:
590
+ // 1. `primaryMinHeight` is the single source of truth for both the JS
591
+ // auto-collapse threshold (covered above) and the CSS floor — PanelStack
592
+ // mirrors it onto an inline custom property on its own root.
593
+ // 2. a controlled `expanded` pointing at a non-existent id must still
594
+ // resolve exactly one scroll owner, without PanelStack ever overriding
595
+ // the controlled value itself.
596
+ // ---------------------------------------------------------------------------
597
+ describe("PanelStack — primaryMinHeight drives the CSS custom property on the stack root", () => {
598
+ it("defaults to PRIMARY_MIN_BLOCK_SIZE_PX and emits it as the inline custom property", () => {
599
+ const { container } = render(PanelStackFixture, { props: { sections, defaultExpanded: "a" } });
600
+ expect(stackRoot(container).style.getPropertyValue("--st-component-panelStack-primaryMinBlockSize")).toBe(`${PRIMARY_MIN_BLOCK_SIZE_PX}px`);
601
+ });
602
+ it("a custom primaryMinHeight is reflected verbatim on the inline custom property, matching the prop", () => {
603
+ const { container } = render(PanelStackFixture, {
604
+ props: { sections, defaultExpanded: "a", primaryMinHeight: 240 }
605
+ });
606
+ expect(stackRoot(container).style.getPropertyValue("--st-component-panelStack-primaryMinBlockSize")).toBe("240px");
607
+ });
608
+ it("holds for split-primary too (the property is emitted regardless of shape)", () => {
609
+ const { container } = render(PanelStackFixture, {
610
+ props: { shape: "split-primary", sections, primary: "a", primaryMinHeight: 320 }
611
+ });
612
+ expect(stackRoot(container).style.getPropertyValue("--st-component-panelStack-primaryMinBlockSize")).toBe("320px");
613
+ });
614
+ });
615
+ describe("PanelStack — controlled expanded pointing at a non-existent id still resolves a scroll owner", () => {
616
+ it("leaves the controlled value untouched, but reports the fallback section as genuinely expanded (ARIA matches what renders), and warns once", () => {
617
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
618
+ const onExpandedChange = vi.fn();
619
+ const { container } = render(PanelStackFixture, {
620
+ props: { sections, expanded: "does-not-exist", onExpandedChange }
621
+ });
622
+ // The fallback section ("a", first registered) is what is ACTUALLY
623
+ // rendered and scrolling, so ARIA must say so — a screen reader must
624
+ // never announce "collapsed" for an on-screen, scrolling region (the
625
+ // same defect class as an aria-valuenow a splitter doesn't have).
626
+ expect(trigger(container, "a")?.getAttribute("aria-expanded")).toBe("true");
627
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("false");
628
+ expect(trigger(container, "c")?.getAttribute("aria-expanded")).toBe("false");
629
+ expect(body(container, "a")?.classList.contains("st-panelSection__body--scrollOwner")).toBe(true);
630
+ expect(body(container, "a")?.classList.contains("st-panelSection__body--collapsed")).toBe(false);
631
+ // The invariant this whole component pair exists to guarantee still
632
+ // holds: exactly one scroll owner, resolved internally — the first
633
+ // registered section, same fallback used everywhere else in this file.
634
+ expect(scrollOwnerBodies(container).length).toBe(1);
635
+ expect(scrollOwnerBodies(container)[0]).toBe(body(container, "a"));
636
+ // The controlled contract is still fully honoured: PanelStack never
637
+ // mutates `expanded` and never fires the change callback on its own —
638
+ // only a genuine user interaction would ever call onExpandedChange.
639
+ expect(onExpandedChange).not.toHaveBeenCalled();
640
+ expect(warn).toHaveBeenCalledTimes(1);
641
+ expect(warn.mock.calls[0][0]).toContain("[PanelStack]");
642
+ expect(warn.mock.calls[0][0]).toContain("does-not-exist");
643
+ warn.mockRestore();
644
+ });
645
+ it("a controlled expanded of exactly null is a DELIBERATE all-collapsed state: honoured as-is, zero scroll owners, no warning", () => {
646
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
647
+ const { container } = render(PanelStackFixture, {
648
+ props: { sections, expanded: null }
649
+ });
650
+ for (const id of ["a", "b", "c"]) {
651
+ expect(trigger(container, id)?.getAttribute("aria-expanded")).toBe("false");
652
+ }
653
+ // Nothing is hidden from the user in this state — every section is
654
+ // visibly, correctly, collapsed — so zero scroll owners is legitimate
655
+ // here, unlike the invalid-id case above, and nothing is healed.
656
+ expect(scrollOwnerBodies(container).length).toBe(0);
657
+ expect(warn).not.toHaveBeenCalled();
658
+ warn.mockRestore();
659
+ });
660
+ it("a valid controlled expanded is unaffected by the fallback (no warning, scroll owner matches the expanded section)", () => {
661
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
662
+ const { container } = render(PanelStackFixture, {
663
+ props: { sections, expanded: "b" }
664
+ });
665
+ expect(trigger(container, "b")?.getAttribute("aria-expanded")).toBe("true");
666
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "b")]);
667
+ expect(warn).not.toHaveBeenCalled();
668
+ warn.mockRestore();
669
+ });
670
+ it("split-primary ignores expanded entirely — an invalid value there never warns and never affects the primary's fallback", () => {
671
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => { });
672
+ const { container } = render(PanelStackFixture, {
673
+ props: { shape: "split-primary", sections, expanded: "does-not-exist" }
674
+ });
675
+ expect(scrollOwnerBodies(container)).toEqual([body(container, "a")]);
676
+ expect(warn).not.toHaveBeenCalled();
677
+ warn.mockRestore();
678
+ });
679
+ });