file-summary-checks.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import assert from "node:assert/strict";
  2. import { writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import type { BrowserWindow } from "electron";
  5. export async function checkFileSummary(
  6. win: BrowserWindow,
  7. root: string,
  8. waitHost: (predicate: string) => Promise<unknown>,
  9. ) {
  10. for (const width of [1100, 390]) {
  11. win.setSize(width, 820);
  12. for (const theme of ["light", "dark"]) {
  13. await win.webContents.executeJavaScript(
  14. `document.documentElement.className=${JSON.stringify(theme === "dark" ? "dark theme-zai-dark" : "theme-zai-light")}; document.body.className='bg-background text-foreground'`,
  15. );
  16. // 先证明普通 HTML 可以正常出现,再验证 visualize 去重,避免空组件假阳性。
  17. await win.webContents.executeJavaScript("window.showFileSummary('ordinary')");
  18. await waitHost(
  19. `document.querySelector('#file-preview-cards')?.textContent.includes('demo.html')`,
  20. );
  21. await win.webContents.executeJavaScript("window.showFileSummary('mixed')");
  22. await waitHost(
  23. `document.querySelector('#file-summary-panel')?.textContent.includes('1 file changed')`,
  24. );
  25. await waitHost(
  26. `document.querySelector('#file-preview-cards')?.textContent.includes('demo.html')`,
  27. );
  28. const text = await win.webContents.executeJavaScript(
  29. "document.querySelector('#file-summary-fixture').textContent",
  30. );
  31. assert.ok(text.includes("+7") && text.includes("-2"));
  32. assert.ok(!text.includes("+77") && !text.includes("calendar.html"));
  33. await win.webContents.executeJavaScript(
  34. "window.summaryTrigger=document.querySelector('#file-summary-panel button'); window.summaryTrigger.focus(); window.summaryTrigger.click()",
  35. );
  36. await waitHost(
  37. `document.querySelector('#file-summary-panel')?.textContent.includes('demo.html')`,
  38. );
  39. assert.equal(
  40. await win.webContents.executeJavaScript(
  41. "window.summaryTrigger.isConnected && document.activeElement === window.summaryTrigger",
  42. ),
  43. true,
  44. );
  45. assert.equal(
  46. await win.webContents.executeJavaScript(
  47. "document.querySelectorAll('#file-preview-cards [data-zcode-stream-animate]').length",
  48. ),
  49. 1,
  50. );
  51. await win.webContents.executeJavaScript(
  52. "document.querySelector('#file-summary-fixture').scrollIntoView({block:'center'})",
  53. );
  54. await win.webContents.executeJavaScript(`Promise.all(
  55. document.querySelector('#file-summary-fixture').getAnimations({subtree:true})
  56. .map(animation => animation.finished.catch(() => {})))`);
  57. await writeFile(
  58. join(root, `file-summary-${width}-${theme}.png`),
  59. (await win.webContents.capturePage()).toPNG(),
  60. );
  61. await win.webContents.executeJavaScript("window.showFileSummary('only')");
  62. await waitHost(`document.querySelector('#file-summary-fixture')?.textContent === ''`);
  63. assert.ok(
  64. await win.webContents.executeJavaScript(
  65. "Boolean(document.querySelector('[data-gen-ui-phase=ready]'))",
  66. ),
  67. );
  68. }
  69. }
  70. await win.webContents.executeJavaScript("window.showFileSummary('mixed', true)");
  71. await waitHost(
  72. `document.querySelector('#file-summary-panel')?.textContent.includes('1 file changed')`,
  73. );
  74. assert.equal(
  75. await win.webContents.executeJavaScript(
  76. "document.querySelector('#file-preview-cards').textContent",
  77. ),
  78. "",
  79. );
  80. await win.webContents.executeJavaScript(
  81. "window.showFileSummary(null); document.documentElement.className=''; document.body.className=''; document.querySelector('#gen-ui-scroll').scrollTop=0",
  82. );
  83. win.setSize(1100, 820);
  84. }