@celestia-island/hikari 0.55.520 → 0.55.521

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.
@@ -1,7 +1,7 @@
1
1
  import { afterEach, describe, expect, it, vi } from "vitest";
2
2
  import { createApp, h, nextTick, ref } from "vue";
3
3
 
4
- import HkDirectionalSwap, { resolveSwapDirection } from "./HkDirectionalSwap";
4
+ import HkDirectionalSwap, { readUnstretchedHeight, resolveSwapDirection } from "./HkDirectionalSwap";
5
5
 
6
6
  /**
7
7
  * HkDirectionalSwap contract:
@@ -63,6 +63,46 @@ describe("resolveSwapDirection", () => {
63
63
  });
64
64
  });
65
65
 
66
+ // MUT:swap-height-unstretch-priority — the un-stretch is a read aid,
67
+ // not a style: `readUnstretchedHeight` must hand the item back EXACTLY
68
+ // as it arrived — value AND priority. `getPropertyValue` returns the
69
+ // value alone, so a restore through `setProperty(prop, value)` silently
70
+ // drops an `!important` (R2′ probe: prior `center !important` came back
71
+ // as `center` with priority ""). Pinned on the helper directly because
72
+ // the swap creates the item in the same tick its enter hook reads it —
73
+ // no test can hand the component a prior inline style.
74
+ describe("readUnstretchedHeight", () => {
75
+ /** A grid item whose height depends on its own `align-self`, the way
76
+ * a real cell's does: the row's 100 while stretched, its own 40 under
77
+ * `start`. Returns the element. */
78
+ function gridItem(): HTMLElement {
79
+ const el = document.createElement("div");
80
+ document.body.appendChild(el);
81
+ Object.defineProperty(el, "offsetHeight", {
82
+ configurable: true,
83
+ get: () => (el.style.alignSelf === "start" ? 40 : 100),
84
+ });
85
+ return el;
86
+ }
87
+
88
+ it("reads the un-stretched height and leaves no inline align-self behind", () => {
89
+ const el = gridItem();
90
+ expect(readUnstretchedHeight(el)).toBe(40);
91
+ expect(el.style.getPropertyValue("align-self")).toBe("");
92
+ expect(el.style.getPropertyPriority("align-self")).toBe("");
93
+ expect(el.style.length).toBe(0);
94
+ });
95
+
96
+ it("restores a prior inline align-self with its priority intact", () => {
97
+ const el = gridItem();
98
+ el.style.setProperty("align-self", "center", "important");
99
+ expect(el.style.getPropertyPriority("align-self")).toBe("important"); // precondition
100
+ expect(readUnstretchedHeight(el)).toBe(40);
101
+ expect(el.style.getPropertyValue("align-self")).toBe("center");
102
+ expect(el.style.getPropertyPriority("align-self"), "priority must survive the restore").toBe("important");
103
+ });
104
+ });
105
+
66
106
  describe("HkDirectionalSwap", () => {
67
107
  it("renders the first generation with the direction and axis as data attributes", async () => {
68
108
  const c = mount({ swapKey: "preview", direction: "forward", axis: "horizontal" });
@@ -241,6 +281,105 @@ describe("HkDirectionalSwap", () => {
241
281
  const descGet = Object.getOwnPropertyDescriptor(CSSStyleDeclaration.prototype, "height")!.get!;
242
282
  const descSet = Object.getOwnPropertyDescriptor(CSSStyleDeclaration.prototype, "height")!.set!;
243
283
 
284
+ /** Model the grid cell's REAL geometry in a DOM with no layout engine
285
+ * (the stub above returns constants, which is exactly how the first
286
+ * cut of the tween shipped green and never ran in a browser):
287
+ * - the host renders at its ROW's height — the tallest generation
288
+ * present in ITS OWN cell (`natural` by `data-swap-key`; the
289
+ * leaving copy counts until Vue removes it), scoped through the
290
+ * element's own cell so two instances in one document never see
291
+ * each other's items;
292
+ * comment-gate-ok: the model is pinned by the `stubGrid models the
293
+ * cell` self-test below (MUT:swap-height-stubgrid-model), which sits
294
+ * after the helper because the pin needs the helper to exist first;
295
+ * - an item renders at that row height too, because grid items
296
+ * STRETCH (`align-self: normal`) — EXCEPT while its inline
297
+ * `align-self` is `start`, when it renders at its own content
298
+ * height. That is the only read that yields the incoming
299
+ * generation's own height, and the component must take it.
300
+ * - while the host is ARMED (`data-height-anim`), a browser renders
301
+ * the tween's animated value, not the row: `rendered` supplies it
302
+ * (null/unset = no armed model, the row stands in).
303
+ * Host reads are counted into `hostReads` as above. Returns restore. */
304
+ function stubGrid(natural: Record<string, number>, rendered?: () => number | null): () => void {
305
+ const proto = HTMLElement.prototype;
306
+ const shadow = Object.getOwnPropertyDescriptor(proto, "offsetHeight");
307
+ hostReads.count = 0;
308
+ const row = (el: HTMLElement): number => {
309
+ const cell = el.classList.contains("hk-dswap-item") ? el.parentElement : el;
310
+ let tallest = 0;
311
+ for (const it of cell?.querySelectorAll<HTMLElement>(":scope > .hk-dswap-item") ?? []) {
312
+ tallest = Math.max(tallest, natural[it.dataset.swapKey ?? ""] ?? 0);
313
+ }
314
+ return tallest;
315
+ };
316
+ Object.defineProperty(proto, "offsetHeight", {
317
+ configurable: true,
318
+ get(this: HTMLElement) {
319
+ if (this.classList.contains("hk-dswap-item")) {
320
+ return this.style.alignSelf === "start" ? (natural[this.dataset.swapKey ?? ""] ?? 0) : row(this);
321
+ }
322
+ hostReads.count += 1;
323
+ if (this.hasAttribute("data-height-anim") && rendered) {
324
+ const animated = rendered();
325
+ if (animated !== null) return animated;
326
+ }
327
+ return row(this);
328
+ },
329
+ });
330
+ return () => {
331
+ if (shadow) Object.defineProperty(proto, "offsetHeight", shadow);
332
+ else delete (proto as unknown as Record<string, unknown>).offsetHeight;
333
+ };
334
+ }
335
+
336
+ /** Make ONE side of the exchange look like it transitions for `ms` so
337
+ * Vue waits for it. happy-dom does not resolve the sheet's `var()`-
338
+ * based `transition` shorthand (both sides compute an empty duration),
339
+ * so Vue's `whenTransitionEnds` resolves BOTH at once and the leave —
340
+ * scheduled first — settles first; the whole exchange lands in one
341
+ * frame. Faking a duration on one side separates the two settles by
342
+ * ~`ms`, and either order can be produced: `leave` parks the leaving
343
+ * copy (enter settles first), `enter` parks the entering one (leave
344
+ * settles first).
345
+ * comment-gate-ok: both orders are pinned by the two settle pins
346
+ * below (MUT:swap-height-release-waits-for-leave / -enter), whose
347
+ * 1000ms bound sits under the watchdog on purpose.
348
+ * The fake is a PLAIN object carrying just the five
349
+ * fields `getTransitionInfo` reads — happy-dom's declaration accessors
350
+ * are brand-checked ("Receiver must be an instance of
351
+ * CSSStyleDeclaration"), so a prototype-chained wrapper throws the
352
+ * moment Vue reads a field the wrapper does not shadow, and Vue's
353
+ * frame callback dies with it (that side then never settles, and the
354
+ * 1500ms watchdog is what "releases" — a green pin with the wrong
355
+ * cause). The fake lists both properties the sheet transitions
356
+ * (`opacity, transform`) so Vue parses the same property list it
357
+ * would live. The duration is written in SECONDS: Vue's `toMs` parses
358
+ * `transitionDuration` by dropping the last character and reading the
359
+ * rest as a number of seconds (`"0.3s"` → 300), so a `"600ms"` reads
360
+ * as `NaN` and the side resolves at once — which is also why
361
+ * happy-dom's own millisecond-form durations never make Vue wait.
362
+ * Returns restore.
363
+ * comment-gate-ok: the NaN read was probed (toMs("300ms") → NaN,
364
+ * toMs("") → 0); it is what makes the un-faked side settle in the
365
+ * frame, and the settle pins are the load-bearing check on the
366
+ * brand-check hazard described above (MUT:swap-height-release-waits-for-enter). */
367
+ function slowSide(side: "enter" | "leave", ms: number): () => void {
368
+ const original = window.getComputedStyle;
369
+ const marker = side === "leave" ? "hk-dswap-leave-active" : "hk-dswap-enter-active";
370
+ vi.spyOn(window, "getComputedStyle").mockImplementation(((el: Element) => {
371
+ if (!el.classList?.contains(marker)) return original(el);
372
+ return {
373
+ transitionDuration: `${ms / 1000}s`,
374
+ transitionDelay: "0s",
375
+ transitionProperty: "opacity, transform",
376
+ animationDuration: "0s",
377
+ animationDelay: "0s",
378
+ } as unknown as CSSStyleDeclaration;
379
+ }) as typeof window.getComputedStyle);
380
+ return () => vi.restoreAllMocks();
381
+ }
382
+
244
383
  /** Drive microtasks/frames until `cond` holds; assert on failure so a
245
384
  * stuck wait names itself instead of timing out silently. */
246
385
  async function until(cond: () => boolean, label: string): Promise<void> {
@@ -250,6 +389,49 @@ describe("HkDirectionalSwap", () => {
250
389
  expect(cond(), label).toBe(true);
251
390
  }
252
391
 
392
+ // MUT:swap-height-stubgrid-model — the height pins are only as good
393
+ // as the geometry `stubGrid` models; R3 showed a helper that stopped
394
+ // gating `rendered` on the armed attribute went unnoticed (the
395
+ // follow-up pin's whole discriminating power is that a RELEASED host
396
+ // reads the row again). Pinned on the helper itself: one cell, two
397
+ // generations of different heights, read every way the component does.
398
+ it("stubGrid models the cell: row = tallest present, items stretch, un-stretch reads own, armed reads rendered", async () => {
399
+ const restore = stubGrid({ preview: 240, source: 100 }, () => 170);
400
+ try {
401
+ const key = ref("preview");
402
+ const container = document.createElement("div");
403
+ document.body.appendChild(container);
404
+ const app = createApp({ render: () => h(HkDirectionalSwap, { swapKey: key.value }, () => [h("p", key.value)]) });
405
+ app.mount(container);
406
+ mounts.push({ app, container });
407
+ await settle();
408
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
409
+ const item = (k: string) => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === k)!;
410
+ // One generation: the host IS that generation's height.
411
+ expect(host.offsetHeight).toBe(240);
412
+ expect(item("preview").offsetHeight).toBe(240);
413
+ // Two generations: the row is the taller one, and the shorter
414
+ // incoming copy reads stretched to it.
415
+ key.value = "source";
416
+ await nextTick();
417
+ await nextTick();
418
+ expect(host.offsetHeight).toBe(240);
419
+ expect(item("source").offsetHeight).toBe(240);
420
+ // …until un-stretched, when it reads its own content height.
421
+ item("source").style.alignSelf = "start";
422
+ expect(item("source").offsetHeight).toBe(100);
423
+ item("source").style.removeProperty("align-self");
424
+ // Armed (and only armed): the host reads the animated value.
425
+ expect(host.offsetHeight).toBe(240);
426
+ host.setAttribute("data-height-anim", "");
427
+ expect(host.offsetHeight).toBe(170);
428
+ host.removeAttribute("data-height-anim");
429
+ expect(host.offsetHeight).toBe(240);
430
+ } finally {
431
+ restore();
432
+ }
433
+ });
434
+
253
435
  // MUT:swap-height-default-off — without the prop the height must stay
254
436
  // untouched (no data-height-anim, no inline height) at rest and
255
437
  // mid-exchange, AND the release hooks must not clobber a height the
@@ -302,8 +484,20 @@ describe("HkDirectionalSwap", () => {
302
484
  // value behind and turns the mid assertion red; a regression that
303
485
  // skips the freeze turns the release semantics red (nothing armed →
304
486
  // nothing to release → the host keeps whatever it had).
487
+ //
488
+ // The geometry is the REAL one (stubGrid): the SHRINKING direction,
489
+ // where the incoming copy is the shorter generation. Read stretched
490
+ // (the first cut), the target was the row — the outgoing copy's 240 —
491
+ // and the tween wrote 240px → 240px: never ran, and the whole change
492
+ // landed at the release as a snap (measured live on the docs shell's
493
+ // card). The write sequence is the pin: freeze at the taller
494
+ // generation, target at the incoming copy's OWN height.
495
+ // comment-gate-ok: the live figure is the probe quoted in the PR body;
496
+ // the behaviour is pinned by this test's own write-sequence assertion
497
+ // (MUT:swap-height-anim-arms, above — the stretched read writes the
498
+ // same value twice and the assertion goes red).
305
499
  it("arms the tween, lands the incoming height, and releases on settle", async () => {
306
- const restore = stubLayout(() => 100, () => 240);
500
+ const restore = stubGrid({ preview: 240, source: 100 });
307
501
  try {
308
502
  const key = ref("preview");
309
503
  const container = document.createElement("div");
@@ -325,12 +519,22 @@ describe("HkDirectionalSwap", () => {
325
519
  await nextTick();
326
520
  await nextTick();
327
521
  // Mid-exchange: the tween armed and BOTH writes landed, in order:
328
- // freeze at the current height (100px), then the target (240px).
329
- // An implementation that arms but never writes (R1 M10: both
330
- // writes deleted, every read-based pin green) writes nothing and
331
- // turns this red; freeze-only writes one entry (R1 M2).
522
+ // freeze at the pre-insertion height (240 — the row while the
523
+ // cell held only the outgoing copy), then the target (100 — the
524
+ // incoming copy's own height, read un-stretched). The stretched
525
+ // read writes [240px, 240px] and turns this red; an implementation
526
+ // that arms but never writes (R1 M10) writes nothing; freeze-only
527
+ // writes one entry (R1 M2).
332
528
  expect(host.hasAttribute("data-height-anim")).toBe(true);
333
- expect(writes).toEqual(["100px", "240px"]);
529
+ expect(writes).toEqual(["240px", "100px"]);
530
+ // MUT:swap-height-unstretch-restored — the un-stretch is a read
531
+ // aid, not a style: the item is put back exactly as it was (no
532
+ // inline `align-self` when it arrived with none; a leftover would
533
+ // take the item out of the cell's stretch for the rest of the
534
+ // exchange and show the band).
535
+ const incoming = [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "source")!;
536
+ expect(incoming.style.alignSelf).toBe("");
537
+ expect(incoming.style.getPropertyValue("align-self")).toBe("");
334
538
  // MUT:swap-height-flush — the forced layout BETWEEN the two writes
335
539
  // is what establishes the freeze as the transition's before-value.
336
540
  // The two writes are synchronous neighbours, so nothing else can
@@ -352,6 +556,271 @@ describe("HkDirectionalSwap", () => {
352
556
  }
353
557
  });
354
558
 
559
+ // MUT:swap-height-from-pre-insertion — the START height is the host
560
+ // as it stood BEFORE the incoming copy inserted. In the GROWING
561
+ // direction the insertion itself lifts the row to the taller copy the
562
+ // same frame (measured live: 189 at rest, 200 the instant the taller
563
+ // copy lands), so an enter-hook read already reports the NEW height
564
+ // and the tween writes [240px, 240px] — the growth then happens at
565
+ // the START, as a snap, which is the very thing the prop exists to
566
+ // soften. The before-leave hook is the last read before the row
567
+ // moves; the write sequence pins it: freeze at 100 (the outgoing,
568
+ // shorter copy), target at 240.
569
+ it("freezes at the pre-insertion height when the incoming copy is taller", async () => {
570
+ const restore = stubGrid({ preview: 100, source: 240 });
571
+ try {
572
+ const key = ref("preview");
573
+ const container = document.createElement("div");
574
+ document.body.appendChild(container);
575
+ const app = createApp({
576
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
577
+ });
578
+ app.mount(container);
579
+ mounts.push({ app, container });
580
+ await settle();
581
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
582
+ const { writes, restore: unspy } = spyHeightWrites(host);
583
+ key.value = "source";
584
+ await nextTick();
585
+ await nextTick();
586
+ expect(host.hasAttribute("data-height-anim")).toBe(true);
587
+ expect(writes).toEqual(["100px", "240px"]);
588
+ unspy();
589
+ } finally {
590
+ restore();
591
+ }
592
+ });
593
+
594
+ // MUT:swap-height-release-waits-for-leave — the release needs BOTH
595
+ // settles: the enter at its end AND no leaving copy in the cell. With
596
+ // a leaving copy still present, `auto` resolves to the taller row it
597
+ // still holds — releasing there jumps the host UP to the outgoing
598
+ // height, then DOWN again when the copy goes (the docs shell's card
599
+ // showed exactly that band). This Chromium settles the leave first,
600
+ // so the order alone never bit here; `slowSide("leave", …)` forces the other
601
+ // order (enter settled, leave still fading) and the pin requires the
602
+ // tween to stay armed, at its target, until the copy is actually gone.
603
+ it("holds the tween armed until the leaving copy is gone when the enter settles first", async () => {
604
+ const restoreSlow = slowSide("leave", 600);
605
+ const restore = stubGrid({ preview: 240, source: 100 });
606
+ try {
607
+ const key = ref("preview");
608
+ const container = document.createElement("div");
609
+ document.body.appendChild(container);
610
+ const app = createApp({
611
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
612
+ });
613
+ app.mount(container);
614
+ mounts.push({ app, container });
615
+ await settle();
616
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
617
+ const { writes, restore: unspy } = spyHeightWrites(host);
618
+ key.value = "source";
619
+ await nextTick();
620
+ await nextTick();
621
+ const entering = () => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "source");
622
+ const leaving = () => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "preview");
623
+ // The enter settles in the frame (no transition detected on it) —
624
+ // Vue strips its active class — while the leaving copy is parked
625
+ // on its faked 600ms.
626
+ await vi.waitFor(() => expect(entering()?.classList.contains("hk-dswap-enter-active")).toBe(false), { timeout: 1000 });
627
+ expect(leaving(), "the leaving copy must still be in the cell for this pin to mean anything").toBeTruthy();
628
+ // Enter settled, leave pending: STILL armed at the target.
629
+ expect(host.hasAttribute("data-height-anim"), "released too early (enter-only settle)").toBe(true);
630
+ expect(host.style.height).toBe("100px");
631
+ expect(writes).toEqual(["240px", "100px"]);
632
+ // Then the leave resolves and the release follows. The bound sits
633
+ // UNDER the watchdog's 1500ms on purpose: a settle path that never
634
+ // fires would still be swept (and released) by the watchdog, and
635
+ // this pin would go green with the wrong cause.
636
+ // comment-gate-ok: the bound is enforced by this pin's own waitFor
637
+ // (MUT:swap-height-release-waits-for-leave, above).
638
+ await vi.waitFor(() => expect(host.hasAttribute("data-height-anim")).toBe(false), { timeout: 1000 });
639
+ expect(host.style.height).toBe("");
640
+ expect(writes).toEqual(["240px", "100px", ""]);
641
+ unspy();
642
+ } finally {
643
+ restore();
644
+ restoreSlow();
645
+ }
646
+ });
647
+
648
+ // MUT:swap-height-release-waits-for-enter — the other half of the
649
+ // release condition: with the LEAVE settled and the enter still
650
+ // running, releasing would drop the host from its MID-TWEEN height to
651
+ // `auto` in one frame — a snap to the target instead of the last
652
+ // stretch of the tween. (`slowSide("leave", …)` above cannot see
653
+ // this: there the enter settles first, so a release keyed on `leaving`
654
+ // alone lands at the right moment by accident — M7 survived it.)
655
+ it("holds the tween armed until the enter settles when the leave settles first", async () => {
656
+ const restoreSlow = slowSide("enter", 600);
657
+ const restore = stubGrid({ preview: 240, source: 100 });
658
+ try {
659
+ const key = ref("preview");
660
+ const container = document.createElement("div");
661
+ document.body.appendChild(container);
662
+ const app = createApp({
663
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
664
+ });
665
+ app.mount(container);
666
+ mounts.push({ app, container });
667
+ await settle();
668
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
669
+ const { writes, restore: unspy } = spyHeightWrites(host);
670
+ key.value = "source";
671
+ await nextTick();
672
+ await nextTick();
673
+ const leaving = () => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "preview");
674
+ const entering = () => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "source");
675
+ // The leave settles in the frame — the copy is gone — while the
676
+ // entering copy is still parked on its faked 600ms.
677
+ await vi.waitFor(() => expect(leaving()).toBeUndefined(), { timeout: 1000 });
678
+ expect(entering()?.classList.contains("hk-dswap-enter-active"), "the enter must still be running for this pin to mean anything").toBe(true);
679
+ // Leave settled, enter pending: STILL armed at the target.
680
+ expect(host.hasAttribute("data-height-anim"), "released too early (leave-only settle)").toBe(true);
681
+ expect(host.style.height).toBe("100px");
682
+ expect(writes).toEqual(["240px", "100px"]);
683
+ // Then the enter resolves and the release follows (bounded under
684
+ // the watchdog's 1500ms for the same reason as above).
685
+ await vi.waitFor(() => expect(host.hasAttribute("data-height-anim")).toBe(false), { timeout: 1000 });
686
+ expect(host.style.height).toBe("");
687
+ expect(writes).toEqual(["240px", "100px", ""]);
688
+ // MUT:swap-height-entersettle-reset — a SECOND exchange in the same
689
+ // order. The first left `enterSettled` true; `onEnter` must reset
690
+ // it, or this exchange's after-leave (which settles first here)
691
+ // reads a stale true and releases while the enter is still parked —
692
+ // a snap to `auto` mid-tween (R3: deleting the reset in `onEnter`
693
+ // left the one-swap pins green).
694
+ key.value = "preview";
695
+ await nextTick();
696
+ await nextTick();
697
+ // Roles swap with the key: the leaving copy is the source pane now.
698
+ const itemEl = (k: string) => [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === k);
699
+ await vi.waitFor(() => expect(itemEl("source")).toBeUndefined(), { timeout: 1000 });
700
+ expect(itemEl("preview")?.classList.contains("hk-dswap-enter-active"), "the second enter must still be running for this pin to mean anything").toBe(true);
701
+ expect(host.hasAttribute("data-height-anim"), "released on a stale enterSettled from the previous exchange").toBe(true);
702
+ expect(host.style.height).toBe("240px");
703
+ expect(writes).toEqual(["240px", "100px", "", "100px", "240px"]);
704
+ await vi.waitFor(() => expect(host.hasAttribute("data-height-anim")).toBe(false), { timeout: 1000 });
705
+ expect(host.style.height).toBe("");
706
+ expect(writes).toEqual(["240px", "100px", "", "100px", "240px", ""]);
707
+ unspy();
708
+ } finally {
709
+ restore();
710
+ restoreSlow();
711
+ }
712
+ });
713
+
714
+ // MUT:swap-height-teardown-live-leave — the same teardown with a LEAVING
715
+ // copy still parked in the cell (the disabled pin above parks the enter):
716
+ // the release must not wait for a copy whose leave will never settle
717
+ // under the disabled branch.
718
+ it("releases the armed tween when the host flips to disabled while a leaving copy is parked", async () => {
719
+ const restoreSlow = slowSide("leave", 600);
720
+ const restore = stubGrid({ preview: 240, source: 100 });
721
+ try {
722
+ const key = ref("preview");
723
+ const disabled = ref(false);
724
+ const container = document.createElement("div");
725
+ document.body.appendChild(container);
726
+ const app = createApp({
727
+ render: () =>
728
+ h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true, disabled: disabled.value }, () => [h("p", key.value)]),
729
+ });
730
+ app.mount(container);
731
+ mounts.push({ app, container });
732
+ await settle();
733
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
734
+ key.value = "source";
735
+ await nextTick();
736
+ await nextTick();
737
+ const leaving = [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "preview");
738
+ expect(leaving?.classList.contains("hk-dswap-leave-active"), "the leaving copy must still be parked for this pin to mean anything").toBe(true);
739
+ expect(host.hasAttribute("data-height-anim")).toBe(true);
740
+ disabled.value = true;
741
+ await nextTick();
742
+ await nextTick();
743
+ expect(host.hasAttribute("data-swap-disabled")).toBe(true);
744
+ expect(host.hasAttribute("data-height-anim"), "height left armed after the Transition went away").toBe(false);
745
+ expect(host.style.height).toBe("");
746
+ } finally {
747
+ restore();
748
+ restoreSlow();
749
+ }
750
+ });
751
+
752
+ // MUT:swap-height-teardown-unmount — the component's own unmount is
753
+ // the third teardown path, and the one the ref cannot serve: Vue nulls
754
+ // `hostRef` on the host vnode BEFORE the Transition child's
755
+ // `onVnodeBeforeUnmount` runs, so a release keyed on the ref alone is a
756
+ // no-op exactly here (R6 measured the detached host still armed). The
757
+ // release must land on the element the tween was armed on, so a node a
758
+ // host re-inserts never comes back frozen.
759
+ it("releases the armed tween when the component unmounts mid-exchange", async () => {
760
+ const restoreSlow = slowSide("enter", 600);
761
+ const restore = stubGrid({ preview: 240, source: 100 });
762
+ try {
763
+ const key = ref("preview");
764
+ const container = document.createElement("div");
765
+ document.body.appendChild(container);
766
+ const app = createApp({
767
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
768
+ });
769
+ app.mount(container);
770
+ await settle();
771
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
772
+ key.value = "source";
773
+ await nextTick();
774
+ await nextTick();
775
+ expect(host.hasAttribute("data-height-anim")).toBe(true);
776
+ expect(host.style.height).toBe("100px");
777
+ app.unmount();
778
+ mounts.splice(mounts.findIndex((m) => m.app === app), 1);
779
+ await nextTick();
780
+ expect(host.isConnected).toBe(false);
781
+ expect(host.hasAttribute("data-height-anim"), "the detached host came out still armed").toBe(false);
782
+ expect(host.style.height).toBe("");
783
+ } finally {
784
+ restore();
785
+ restoreSlow();
786
+ }
787
+ });
788
+
789
+ // MUT:swap-height-release-own-write-only — the release undoes THIS
790
+ // component's inline height and nothing else: a host that sized itself
791
+ // mid-exchange keeps its own number through the release (the arm is
792
+ // still undone — the attribute goes). R7: the attribute removal was
793
+ // guarded but the height write was not, so the "only ever unwinds this
794
+ // component's own writes" promise held for one of the two writes.
795
+ it("leaves a host-set inline height alone when it changed mid-exchange", async () => {
796
+ const restoreSlow = slowSide("enter", 600);
797
+ const restore = stubGrid({ preview: 240, source: 100 });
798
+ try {
799
+ const key = ref("preview");
800
+ const container = document.createElement("div");
801
+ document.body.appendChild(container);
802
+ const app = createApp({
803
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
804
+ });
805
+ app.mount(container);
806
+ mounts.push({ app, container });
807
+ await settle();
808
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
809
+ key.value = "source";
810
+ await nextTick();
811
+ await nextTick();
812
+ expect(host.hasAttribute("data-height-anim")).toBe(true);
813
+ expect(host.style.height).toBe("100px");
814
+ // The host writes its own height while the tween is armed.
815
+ host.style.height = "77px";
816
+ await vi.waitFor(() => expect(host.hasAttribute("data-height-anim")).toBe(false), { timeout: 1000 });
817
+ expect(host.style.height, "a host height written mid-exchange must survive the release").toBe("77px");
818
+ } finally {
819
+ restore();
820
+ restoreSlow();
821
+ }
822
+ });
823
+
355
824
  // MUT:swap-height-zero-skips — an unreadable height (hidden host, or
356
825
  // an incoming pane not yet laid out reports 0) skips the tween: a
357
826
  // 0px freeze would collapse the surface for the whole exchange and
@@ -435,16 +904,26 @@ describe("HkDirectionalSwap", () => {
435
904
  });
436
905
 
437
906
  // MUT:swap-height-followup-current — a faster follow-up swap arrives
438
- // AFTER Vue cancelled the interrupted enter (the release runs, then the
439
- // fresh exchange begins): its freeze must read the cell's CURRENT
440
- // rendered height via offsetHeight — never a remembered start value.
441
- // The moving `current` stub stands in for the live layout height this
442
- // DOM cannot compute; the write sequence pins it exactly: the second
443
- // exchange writes [150px (its own freeze), 240px (its target)] — a
444
- // regression that remembers the old start writes [100px, ...] first.
907
+ // while the first exchange is mid-tween: Vue cancels the interrupted
908
+ // enter, and the fresh exchange must freeze at the cell's CURRENT
909
+ // RENDERED height — the animated mid-state — never the row and never a
910
+ // remembered start. What this pin defends is the ABSENCE of a release
911
+ // on cancel: while the tween stays armed, both the before-leave read and
912
+ // the armed fallback at enter resolve to the animated value (they agree
913
+ // by construction — `restHeight` itself is pinned by the growing test
914
+ // above), whereas a cancel that releases drops `data-height-anim`, the
915
+ // host reads `auto` — the ROW, the taller copy still in the cell — and
916
+ // the follow-up freezes there: 240px → 240px, the tween dead (R2 of the
917
+ // first cut measured exactly that). `stubGrid`'s `rendered` models the
918
+ // animated value a browser renders while armed, and only while armed.
919
+ // comment-gate-ok: the armed gating is the `stubGrid` self-test's own
920
+ // pin (MUT:swap-height-stubgrid-model); the no-release sequence below is
921
+ // this pin's (MUT:swap-height-followup-current, above).
922
+ // The sequence pins it exactly: freeze, target, re-freeze at the
923
+ // mid-state, re-target — with no release in between.
445
924
  it("a follow-up exchange freezes at the current rendered height, not the old start", async () => {
446
- let current = 100;
447
- const restore = stubLayout(() => current, () => 240);
925
+ let current: number | null = null;
926
+ const restore = stubGrid({ preview: 240, source: 100 }, () => current);
448
927
  const writes: string[] = [];
449
928
  try {
450
929
  const key = ref("preview");
@@ -465,22 +944,134 @@ describe("HkDirectionalSwap", () => {
465
944
  descSet.call(host.style, v);
466
945
  },
467
946
  });
468
- // First exchange.
947
+ // First exchange: from rest (240) down to the source pane (100).
469
948
  key.value = "source";
470
949
  await nextTick();
471
950
  await nextTick();
472
- // Follow-up swap: Vue cancels the interrupted enter — the release
473
- // writes "" — then the fresh exchange freezes at the CURRENT
474
- // stubbed height before its target. The full sequence pins all of
475
- // it: freeze, target, cancel-release, re-freeze, re-target.
476
- current = 150;
951
+ // Follow-up swap mid-tween: the host is armed and rendering 170.
952
+ // Vue cancels the interrupted enter (the tween stays armed), the
953
+ // before-leave hook reads the animated 170, and the fresh exchange
954
+ // freezes there before its target (the preview pane's own 240).
955
+ current = 170;
477
956
  key.value = "preview";
478
957
  await nextTick();
479
958
  await nextTick();
480
959
  expect(host.hasAttribute("data-height-anim")).toBe(true);
481
- expect(writes).toEqual(["100px", "240px", "", "150px", "240px"]);
960
+ expect(writes).toEqual(["240px", "100px", "170px", "240px"]);
961
+ } finally {
962
+ restore();
963
+ }
964
+ });
965
+
966
+ // MUT:swap-height-back-and-forth — the reader toggles back to the
967
+ // pane they just left before the exchange settles. Vue cancels the
968
+ // interrupted enter, then FINISHES the first copy's leave (a keyed swap
969
+ // mounts a fresh element for the returning key; its old copy is removed
970
+ // at once — `leave-cancelled` never fires here, measured) and enters
971
+ // the fresh one. The cancel must not release: the second freeze has to
972
+ // read the animated mid-state. `slowSide("enter")` keeps the source
973
+ // pane's enter running so the exchange is genuinely mid-flight when the
974
+ // toggle returns; the sequence pins no release between the exchanges.
975
+ it("a back-and-forth toggle re-freezes at the mid-state through a cancelled enter", async () => {
976
+ const restoreSlow = slowSide("enter", 600);
977
+ let current: number | null = null;
978
+ const restore = stubGrid({ preview: 240, source: 100 }, () => current);
979
+ try {
980
+ const key = ref("preview");
981
+ const container = document.createElement("div");
982
+ document.body.appendChild(container);
983
+ const app = createApp({
984
+ render: () => h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true }, () => [h("p", key.value)]),
985
+ });
986
+ app.mount(container);
987
+ mounts.push({ app, container });
988
+ await settle();
989
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
990
+ const { writes, restore: unspy } = spyHeightWrites(host);
991
+ key.value = "source";
992
+ await nextTick();
993
+ await nextTick();
994
+ expect(writes).toEqual(["240px", "100px"]);
995
+ current = 170;
996
+ key.value = "preview";
997
+ await nextTick();
998
+ await nextTick();
999
+ expect(host.hasAttribute("data-height-anim"), "armed across the cancelled leave").toBe(true);
1000
+ expect(writes).toEqual(["240px", "100px", "170px", "240px"]);
1001
+ // The returning pane (a fresh element) is live: never inert, never
1002
+ // hidden — the leaving copy's dead-content marks die with it.
1003
+ // comment-gate-ok: asserted on the two lines after the lookup
1004
+ // (MUT:swap-height-back-and-forth, above).
1005
+ const back = [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")].find((el) => el.dataset.swapKey === "preview")!;
1006
+ expect(back.hasAttribute("inert")).toBe(false);
1007
+ expect(back.getAttribute("aria-hidden")).toBeNull();
1008
+ unspy();
1009
+ } finally {
1010
+ restore();
1011
+ restoreSlow();
1012
+ }
1013
+ });
1014
+
1015
+ // MUT:swap-height-teardown-releases — the other half of the bargain:
1016
+ // a tween kept armed through cancelled hooks must release when the
1017
+ // Transition itself goes away, or a host that flips to `disabled`
1018
+ // mid-exchange freezes the height for good (no exchange hook ever
1019
+ // runs again in that branch). Vue's `onVnodeBeforeUnmount` on the
1020
+ // Transition is the signal.
1021
+ it("releases the armed tween when the host flips to disabled mid-exchange", async () => {
1022
+ const restoreSlow = slowSide("enter", 600);
1023
+ const restore = stubGrid({ preview: 240, source: 100 });
1024
+ try {
1025
+ const key = ref("preview");
1026
+ const disabled = ref(false);
1027
+ const container = document.createElement("div");
1028
+ document.body.appendChild(container);
1029
+ const app = createApp({
1030
+ render: () =>
1031
+ h(HkDirectionalSwap, { swapKey: key.value, animateHeight: true, disabled: disabled.value }, () => [h("p", key.value)]),
1032
+ });
1033
+ app.mount(container);
1034
+ mounts.push({ app, container });
1035
+ await settle();
1036
+ const host = container.querySelector<HTMLElement>(".hk-dswap")!;
1037
+ key.value = "source";
1038
+ await nextTick();
1039
+ await nextTick();
1040
+ expect(host.hasAttribute("data-height-anim")).toBe(true);
1041
+ expect(host.style.height).toBe("100px");
1042
+ disabled.value = true;
1043
+ await nextTick();
1044
+ await nextTick();
1045
+ expect(host.hasAttribute("data-swap-disabled")).toBe(true);
1046
+ expect(host.hasAttribute("data-height-anim"), "height left armed after the Transition went away").toBe(false);
1047
+ expect(host.style.height).toBe("");
1048
+ // And the disabled branch still swaps synchronously at auto height
1049
+ // (no hooks there, so nothing arms again).
1050
+ key.value = "preview";
1051
+ await nextTick();
1052
+ expect(host.hasAttribute("data-height-anim")).toBe(false);
1053
+ expect(host.style.height).toBe("");
1054
+ // Re-enabling the Transition mid-interrupted-exchange and swapping
1055
+ // again must arm and release normally (a real host path: flip
1056
+ // `disabled`, flip it back, toggle). The teardown's settle-state
1057
+ // reset is defensive here — Vue's own leave flow still fires the
1058
+ // interrupted copy's after-leave once its frame callback resolves,
1059
+ // so the counter balances with or without the reset (R3: deleting
1060
+ // the reset lines left the suite green); this pins the path, not
1061
+ // the reset.
1062
+ disabled.value = false;
1063
+ await nextTick();
1064
+ await nextTick();
1065
+ expect(host.hasAttribute("data-swap-disabled")).toBe(false);
1066
+ key.value = "source";
1067
+ await nextTick();
1068
+ await nextTick();
1069
+ expect(host.hasAttribute("data-height-anim"), "the re-enabled exchange must arm").toBe(true);
1070
+ await vi.waitFor(() => expect(host.hasAttribute("data-height-anim")).toBe(false), { timeout: 1000 });
1071
+ expect(host.style.height).toBe("");
482
1072
  } finally {
483
1073
  restore();
1074
+ restoreSlow();
484
1075
  }
485
1076
  });
486
1077