runtime-checks.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import assert from "node:assert/strict";
  2. import type { BrowserWindow } from "electron";
  3. type Read = (code: string) => Promise<any>;
  4. const waitFor = (read: Read, predicate: string) =>
  5. read(`new Promise((resolve,reject)=>{
  6. const end=Date.now()+4000; const tick=()=>{
  7. if(${predicate})resolve(true);else if(Date.now()>end)reject(new Error(${JSON.stringify(predicate)} + ' ' + JSON.stringify({focus:document.activeElement?.outerHTML,tip:document.getElementById('tip')?.matches(':focus-visible'),popup:document.querySelector('[role=tooltip]')?.outerHTML})));else requestAnimationFrame(tick);
  8. };tick();
  9. })`);
  10. export async function checkRuntimeWidgets(read: Read) {
  11. await read("document.querySelector('#second-tab').click()");
  12. assert.equal(await read("document.querySelector('#second').hidden"), false);
  13. assert.equal(await read("document.querySelector('#first').hidden"), true);
  14. assert.ok(await read("Boolean(document.querySelector('svg.lucide'))"));
  15. await read(
  16. "document.querySelector('#second-tab').dispatchEvent(new KeyboardEvent('keydown',{key:'Home',bubbles:true}))",
  17. );
  18. assert.equal(await read("document.querySelector('#first').hidden"), false);
  19. assert.equal(await read("typeof FloatingUIDOM.computePosition"), "function");
  20. assert.equal(await read("typeof lucide.createIcons"), "function");
  21. await read(
  22. `document.body.insertAdjacentHTML('beforeend','<div id="runtime-probe"><span id="described">Existing description</span><button id="tip" data-tooltip="Runtime tooltip" aria-describedby="described">Tooltip</button><div id="shadow"></div><i id="dynamic-icon" data-lucide="search"></i><div class="nav" role="tablist"><button id="shared-a" role="tab" aria-controls="shared-panel">A</button><button id="shared-b" role="tab" aria-controls="shared-panel">B</button></div><div id="shared-panel">Shared panel</div></div>')`,
  23. );
  24. await read("new Promise(requestAnimationFrame)");
  25. assert.equal(await read("document.getElementById('dynamic-icon').tagName"), "I");
  26. await read("lucide.createIcons({attrs:{width:16,height:16}})");
  27. assert.equal(await read("document.getElementById('dynamic-icon').tagName.toLowerCase()"), "svg");
  28. await read("document.getElementById('shared-a').click()");
  29. assert.equal(await read("document.getElementById('shared-panel').hidden"), false);
  30. await read(
  31. "document.getElementById('shared-a').dispatchEvent(new KeyboardEvent('keydown',{key:'ArrowRight',bubbles:true}))",
  32. );
  33. assert.equal(
  34. await read("document.getElementById('shared-b').getAttribute('aria-selected')"),
  35. "true",
  36. );
  37. assert.equal(await read("document.getElementById('shared-panel').hidden"), false);
  38. // 上游只为 focus-visible 自动打开;脚本 focus 不等于用户键盘输入,悬停走实际延迟路径。
  39. await read(
  40. "document.getElementById('tip').dispatchEvent(new PointerEvent('pointerover',{pointerType:'mouse',bubbles:true}))",
  41. );
  42. await waitFor(read, "document.querySelector('[role=tooltip]')?.style.visibility === 'visible'");
  43. assert.ok(
  44. (await read("document.getElementById('tip').getAttribute('aria-describedby')")).startsWith(
  45. "described ",
  46. ),
  47. );
  48. await read("document.dispatchEvent(new KeyboardEvent('keydown',{key:'Escape',bubbles:true}))");
  49. assert.equal(await read("document.querySelector('[role=tooltip]') === null"), true);
  50. assert.equal(
  51. await read("document.getElementById('tip').getAttribute('aria-describedby')"),
  52. "described",
  53. );
  54. for (const placement of ["top", "right", "bottom", "left"]) {
  55. await read(`(() => {
  56. const tip=document.getElementById('tip');tip.dataset.tooltipPlacement=${JSON.stringify(placement)};
  57. tip.style.cssText='position:fixed;top:150px;left:250px';
  58. tip.dispatchEvent(new PointerEvent('click',{pointerType:'touch',detail:1,bubbles:true}));
  59. })()`);
  60. await waitFor(read, "document.querySelector('[role=tooltip]')?.style.visibility === 'visible'");
  61. const bounds = await read(
  62. `(() => {const t=document.getElementById('tip').getBoundingClientRect(),p=document.querySelector('[role=tooltip]').getBoundingClientRect();return {tip:t.toJSON(),popup:p.toJSON(),width:innerWidth,height:innerHeight}})()`,
  63. );
  64. assert.ok(bounds.popup.left >= 0 && bounds.popup.right <= bounds.width + 1);
  65. assert.ok(bounds.popup.top >= 0 && bounds.popup.bottom <= bounds.height + 1);
  66. if (placement === "top") assert.ok(bounds.popup.bottom <= bounds.tip.top);
  67. if (placement === "bottom") assert.ok(bounds.popup.top >= bounds.tip.bottom);
  68. if (placement === "left") assert.ok(bounds.popup.right <= bounds.tip.left);
  69. if (placement === "right") assert.ok(bounds.popup.left >= bounds.tip.right);
  70. await read(
  71. "document.getElementById('tip').dispatchEvent(new PointerEvent('pointerout',{pointerType:'touch',bubbles:true}))",
  72. );
  73. assert.equal(await read("document.querySelectorAll('[role=tooltip]').length"), 1);
  74. await read(
  75. "document.body.dispatchEvent(new PointerEvent('click',{pointerType:'touch',detail:1,bubbles:true}))",
  76. );
  77. assert.equal(await read("document.querySelector('[role=tooltip]') === null"), true);
  78. }
  79. await read(
  80. `document.getElementById('shadow').attachShadow({mode:'open'}).innerHTML='<button data-tooltip="Shadow tooltip">Shadow</button>'`,
  81. );
  82. await read("new Promise(requestAnimationFrame)");
  83. await read(
  84. "document.getElementById('shadow').shadowRoot.querySelector('button').dispatchEvent(new PointerEvent('click',{pointerType:'touch',detail:1,bubbles:true,composed:true}))",
  85. );
  86. await waitFor(read, "document.querySelector('[role=tooltip]')?.style.visibility === 'visible'");
  87. assert.equal(
  88. await read("document.querySelector('[role=tooltip]').textContent"),
  89. "Shadow tooltip",
  90. );
  91. await read("document.getElementById('shadow').remove()");
  92. await waitFor(read, "!document.querySelector('[role=tooltip]')");
  93. await read("document.getElementById('runtime-probe').remove()");
  94. }
  95. export async function checkCalendarAndCarousel(read: Read) {
  96. assert.equal(await read("window.calendarWasReady"), true);
  97. assert.equal(
  98. await read("document.querySelector('.viz-carousel').dataset.activeVariant"),
  99. "First",
  100. );
  101. await read("document.querySelector('.viz-carousel-next').click()");
  102. assert.equal(
  103. await read("document.querySelector('.viz-carousel').dataset.activeVariant"),
  104. "Second",
  105. );
  106. await read("document.querySelector('.viz-carousel-previous').click()");
  107. assert.equal(
  108. await read("document.querySelector('.viz-carousel').dataset.activeVariant"),
  109. "First",
  110. );
  111. await read(`(() => {
  112. const calendar=document.createElement('viz-calendar');calendar.id='calendar-probe';calendar.setAttribute('date','2026-09-29');calendar.setAttribute('start','09:00');calendar.setAttribute('end','12:00');calendar.setAttribute('interactive','');
  113. calendar.events=[{title:'One',start:'09:00',end:'10:00'},{title:'Two',start:'09:30',end:'10:30'}];
  114. calendar.addEventListener('eventselect',event=>window.calendarSelection=event.detail.index);document.body.append(calendar);
  115. })()`);
  116. const readCalendar = (expression: string) =>
  117. read(`(() => {const c=document.getElementById('calendar-probe');return ${expression}})()`);
  118. assert.equal(await readCalendar("c.shadowRoot.querySelectorAll('.event').length"), 2);
  119. assert.equal(
  120. await readCalendar(
  121. "c.shadowRoot.querySelectorAll('.event')[1].style.getPropertyValue('--lanes')",
  122. ),
  123. "2",
  124. );
  125. await readCalendar("c.shadowRoot.querySelector('button[data-event-index]').click()");
  126. assert.equal(await read("window.calendarSelection"), 0);
  127. await readCalendar("c.setAttribute('date','2026-02-30')");
  128. assert.ok(await readCalendar("c.shadowRoot.querySelector('[role=alert]') !== null"));
  129. await readCalendar("c.setAttribute('date','2026-09-29')");
  130. assert.equal(await readCalendar("c.shadowRoot.querySelectorAll('.event').length"), 2);
  131. await readCalendar("c.remove()");
  132. }
  133. export async function checkTweakLifecycle(read: Read, win: BrowserWindow) {
  134. const host = (code: string) => win.webContents.executeJavaScript(code);
  135. await read(`(() => {
  136. const container=document.createElement('div');container.id='tweak-probe';document.body.append(container);
  137. window.probeState={size:4};window.probeRenders=0;
  138. window.probeTweak=new Tweak({container,onChange:()=>window.probeRenders++});
  139. window.probeTweak.addSlider(window.probeState,'size',{min:0,max:10,label:'Probe'});
  140. })()`);
  141. await waitFor(host, "window.inspect().snapshot.groups.length === 3");
  142. const id = await host("window.inspect().snapshot.groups[2].controls[0].id");
  143. await host(`window.setProbeTweaks(${JSON.stringify({ [id]: 9 })})`);
  144. await waitFor(read, "window.probeState.size === 9");
  145. await read("window.probeTweak.dispose()");
  146. await waitFor(host, "window.inspect().snapshot.groups.length === 2");
  147. assert.equal(await read("window.probeState.size"), 4);
  148. await read(
  149. "window.probeTweak=new Tweak({container:document.getElementById('tweak-probe'),onChange:()=>{}});window.probeTweak.addToggle({enabled:true},'enabled')",
  150. );
  151. await waitFor(host, "window.inspect().snapshot.groups.length === 3");
  152. await read("document.getElementById('tweak-probe').remove()");
  153. await waitFor(host, "window.inspect().snapshot.groups.length === 2");
  154. }