updateStatusButton.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. import { describe, expect, it } from "vitest";
  2. import { createElement, isValidElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { UpdateStatusButton } from "@/UpdateStatusButton.js";
  5. import { UpdateStatusDialog } from "@/UpdateStatusDialog.js";
  6. import {
  7. deriveUpdateStatusViewModel,
  8. isUpdateActionCompleted,
  9. type UpdateStatusDialogPhase,
  10. } from "@/updateStatusModel.js";
  11. import {
  12. formatUpdateReleaseDate,
  13. getLocalizedUpdateReleaseNotes,
  14. } from "@/updateReleaseNotes.js";
  15. import enUS from "@/i18n/locales/en-US.js";
  16. import zhCN from "@/i18n/locales/zh-CN.js";
  17. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  18. import type { IntlInstance } from "@/i18n/IntlProvider.js";
  19. import { TooltipProvider } from "@/components/ui/tooltip.js";
  20. import { PlatformProvider } from "@/hooks/usePlatform.js";
  21. import { ServiceProvider } from "@/hooks/useServices.js";
  22. import type { IServiceAccessor } from "@zcode/services";
  23. import type { IPlatformService, UpdateStatePayload } from "@zcode/shared";
  24. const noopPlatform = {
  25. openExternal: () => undefined,
  26. downloadUpdate: async () => undefined,
  27. cancelUpdateDownload: async () => undefined,
  28. getAutoUpdatePreferences: async () => ({
  29. autoDownloadAndInstallUpdates: false,
  30. }),
  31. setAutoDownloadAndInstallUpdates: async () => undefined,
  32. skipUpdateVersion: async () => undefined,
  33. quitAndInstallUpdate: async () => undefined,
  34. } as unknown as IPlatformService;
  35. const noopServices = {
  36. settingService: {
  37. get: async () => ({}),
  38. update: async () => undefined,
  39. },
  40. } as unknown as IServiceAccessor;
  41. function renderUpdateStatusButton(
  42. updateState: UpdateStatePayload | null,
  43. version: string | null = null,
  44. ) {
  45. return renderToStaticMarkup(
  46. createElement(
  47. TooltipProvider,
  48. null,
  49. createElement(
  50. ServiceProvider,
  51. { services: noopServices },
  52. createElement(
  53. PlatformProvider,
  54. { platform: noopPlatform },
  55. createElement(
  56. ZCodeIntlProvider,
  57. { initialLocale: "zh-CN" },
  58. createElement(UpdateStatusButton, {
  59. platform: noopPlatform,
  60. version,
  61. updateState,
  62. }),
  63. ),
  64. ),
  65. ),
  66. ),
  67. );
  68. }
  69. const testIntl: IntlInstance = {
  70. formatMessage({ id }, values) {
  71. if (values?.version) {
  72. return `更新 ${values.version}`;
  73. }
  74. return id;
  75. },
  76. };
  77. function updateStatusDialogElementContainsText(
  78. phase: UpdateStatusDialogPhase,
  79. text: string,
  80. isUpdateActionPending = false,
  81. ) {
  82. return elementContainsText(
  83. renderUpdateStatusDialogElement(
  84. phase,
  85. isUpdateActionPending,
  86. ),
  87. text,
  88. );
  89. }
  90. function updateStatusDialogElementContainsPropText(
  91. phase: UpdateStatusDialogPhase,
  92. text: string,
  93. edgeToEdge = false,
  94. ) {
  95. return elementContainsPropText(
  96. renderUpdateStatusDialogElement(phase, false, null, edgeToEdge),
  97. text,
  98. );
  99. }
  100. function updateStatusDialogElementContainsMatchingPropText(
  101. phase: UpdateStatusDialogPhase,
  102. matcher: (className: string) => boolean,
  103. edgeToEdge = false,
  104. ) {
  105. return elementContainsMatchingElement(
  106. renderUpdateStatusDialogElement(phase, false, null, edgeToEdge),
  107. (_type, props) =>
  108. typeof props.className === "string" && matcher(props.className),
  109. );
  110. }
  111. function updateStatusDialogUsesStackedReleaseDate(
  112. phase: UpdateStatusDialogPhase,
  113. ) {
  114. return elementContainsMatchingElement(
  115. renderUpdateStatusDialogElement(phase, false, "2026-07-09"),
  116. (type, props) =>
  117. type === "div" &&
  118. typeof props.className === "string" &&
  119. props.className.includes("inline-flex") &&
  120. props.className.includes("max-w-full") &&
  121. !props.className.includes("ml-auto") &&
  122. !props.className.includes("text-right") &&
  123. elementContainsText(props.children, "2026-07-09"),
  124. );
  125. }
  126. function updateStatusDialogUsesCenteredLogoTitleLayout(
  127. phase: UpdateStatusDialogPhase,
  128. ) {
  129. return elementContainsMatchingElement(
  130. renderUpdateStatusDialogElement(phase, false, "2026-07-09"),
  131. (type, props) =>
  132. type === "div" &&
  133. typeof props.className === "string" &&
  134. props.className.includes("flex") &&
  135. props.className.includes("items-center") &&
  136. elementContainsMatchingElement(
  137. props.children,
  138. (childType, childProps) =>
  139. childType === "img" &&
  140. typeof childProps.className === "string" &&
  141. childProps.className.includes("size-12"),
  142. ) &&
  143. elementContainsText(props.children, "2026-07-09"),
  144. );
  145. }
  146. function updateStatusDialogUsesMacDockProductLogo(
  147. phase: UpdateStatusDialogPhase,
  148. ) {
  149. return elementContainsMatchingElement(
  150. renderUpdateStatusDialogElement(phase),
  151. (type, props) =>
  152. type === "img" &&
  153. typeof props.src === "string" &&
  154. props.src.includes("icon_512@2x.png") &&
  155. props.alt === "" &&
  156. props.draggable === false &&
  157. typeof props.className === "string" &&
  158. props.className.includes("size-12") &&
  159. props.className.includes("-ml-[5px]") &&
  160. props.className.includes("shadow-none") &&
  161. props.className.includes("drop-shadow-none"),
  162. );
  163. }
  164. function countUpdateStatusDialogElementPropText(
  165. phase: UpdateStatusDialogPhase,
  166. text: string,
  167. isUpdateActionPending = false,
  168. ) {
  169. return countElementPropText(
  170. renderUpdateStatusDialogElement(
  171. phase,
  172. isUpdateActionPending,
  173. ),
  174. text,
  175. );
  176. }
  177. function countUpdateStatusDialogBooleanProp(
  178. phase: UpdateStatusDialogPhase,
  179. propName: string,
  180. isUpdateActionPending = false,
  181. edgeToEdge = false,
  182. ) {
  183. return countElementBooleanProp(
  184. renderUpdateStatusDialogElement(
  185. phase,
  186. isUpdateActionPending,
  187. null,
  188. edgeToEdge,
  189. ),
  190. propName,
  191. );
  192. }
  193. function countUpdateStatusDialogPropValue(
  194. phase: UpdateStatusDialogPhase,
  195. propName: string,
  196. propValue: unknown,
  197. ) {
  198. return countElementPropValue(
  199. renderUpdateStatusDialogElement(phase),
  200. propName,
  201. propValue,
  202. );
  203. }
  204. function renderUpdateStatusDialogElement(
  205. phase: UpdateStatusDialogPhase,
  206. isUpdateActionPending = false,
  207. releaseDateLabel: string | null = null,
  208. edgeToEdge = false,
  209. ) {
  210. return UpdateStatusDialog({
  211. autoDownloadAndInstallUpdates: false,
  212. displayVersion: "3.3.3",
  213. edgeToEdge,
  214. intl: testIntl,
  215. isUpdateActionPending,
  216. localizedUpdateReleaseNotes: {
  217. markdown: "Visible before downloading",
  218. title: "Release v3.3.3",
  219. },
  220. onAutoDownloadAndInstallUpdatesChange: async () => undefined,
  221. onCancelDownload: async () => undefined,
  222. onDownloadUpdate: async () => undefined,
  223. onOpenChange: () => undefined,
  224. onOpenReleaseNotesExternalUrl: () => undefined,
  225. onRestartUpdate: async () => undefined,
  226. onSkipUpdate: async () => undefined,
  227. open: true,
  228. phase,
  229. progressLabel: "4.2MB / 10.0MB",
  230. progressValue: 42,
  231. releaseDateLabel,
  232. skippableVersion: phase === "downloaded" ? null : "3.3.3",
  233. });
  234. }
  235. function elementContainsText(node: unknown, text: string): boolean {
  236. if (node == null || typeof node === "boolean") {
  237. return false;
  238. }
  239. if (typeof node === "string" || typeof node === "number") {
  240. return String(node).includes(text);
  241. }
  242. if (Array.isArray(node)) {
  243. return node.some((child) => elementContainsText(child, text));
  244. }
  245. if (isValidElement(node)) {
  246. const props = node.props as { children?: unknown };
  247. return elementContainsText(props.children, text);
  248. }
  249. return false;
  250. }
  251. function countElementPropText(node: unknown, text: string): number {
  252. if (node == null || typeof node === "boolean") {
  253. return 0;
  254. }
  255. if (Array.isArray(node)) {
  256. return node.reduce(
  257. (count, child) => count + countElementPropText(child, text),
  258. 0,
  259. );
  260. }
  261. if (isValidElement(node)) {
  262. const props = node.props as Record<string, unknown>;
  263. const matchingPropCount = Object.entries(props).filter(
  264. ([key, value]) =>
  265. key !== "children" &&
  266. typeof value === "string" &&
  267. value.includes(text),
  268. ).length;
  269. return matchingPropCount + countElementPropText(props.children, text);
  270. }
  271. return 0;
  272. }
  273. function countElementBooleanProp(node: unknown, propName: string): number {
  274. if (node == null || typeof node === "boolean") {
  275. return 0;
  276. }
  277. if (Array.isArray(node)) {
  278. return node.reduce(
  279. (count, child) => count + countElementBooleanProp(child, propName),
  280. 0,
  281. );
  282. }
  283. if (isValidElement(node)) {
  284. const props = node.props as Record<string, unknown>;
  285. const selfCount = props[propName] === true ? 1 : 0;
  286. return selfCount + countElementBooleanProp(props.children, propName);
  287. }
  288. return 0;
  289. }
  290. function countElementPropValue(
  291. node: unknown,
  292. propName: string,
  293. propValue: unknown,
  294. ): number {
  295. if (node == null || typeof node === "boolean") {
  296. return 0;
  297. }
  298. if (Array.isArray(node)) {
  299. return node.reduce(
  300. (count, child) => count + countElementPropValue(child, propName, propValue),
  301. 0,
  302. );
  303. }
  304. if (isValidElement(node)) {
  305. const props = node.props as Record<string, unknown>;
  306. const selfCount = props[propName] === propValue ? 1 : 0;
  307. return selfCount + countElementPropValue(props.children, propName, propValue);
  308. }
  309. return 0;
  310. }
  311. function elementContainsPropText(node: unknown, text: string): boolean {
  312. if (node == null || typeof node === "boolean") {
  313. return false;
  314. }
  315. if (Array.isArray(node)) {
  316. return node.some((child) => elementContainsPropText(child, text));
  317. }
  318. if (isValidElement(node)) {
  319. const props = node.props as Record<string, unknown>;
  320. const hasMatchingStringProp = Object.entries(props).some(
  321. ([key, value]) =>
  322. key !== "children" &&
  323. typeof value === "string" &&
  324. value.includes(text),
  325. );
  326. return (
  327. hasMatchingStringProp || elementContainsPropText(props.children, text)
  328. );
  329. }
  330. return false;
  331. }
  332. function elementContainsMatchingElement(
  333. node: unknown,
  334. predicate: (type: unknown, props: Record<string, unknown>) => boolean,
  335. ): boolean {
  336. if (node == null || typeof node === "boolean") {
  337. return false;
  338. }
  339. if (Array.isArray(node)) {
  340. return node.some((child) =>
  341. elementContainsMatchingElement(child, predicate),
  342. );
  343. }
  344. if (isValidElement(node)) {
  345. const props = node.props as Record<string, unknown>;
  346. return (
  347. predicate(node.type, props) ||
  348. elementContainsMatchingElement(props.children, predicate)
  349. );
  350. }
  351. return false;
  352. }
  353. describe("UpdateStatusButton release notes", () => {
  354. it("selects release notes by current locale", () => {
  355. const payload = {
  356. version: "3.1.3",
  357. title: "Release v3.1.3",
  358. markdown: "- 默认说明",
  359. releaseNotesByLocale: {
  360. "zh-CN": {
  361. title: "ZCode v3.1.3",
  362. markdown: "- 中文说明",
  363. },
  364. "en-US": {
  365. title: "ZCode v3.1.3",
  366. markdown: "- English notes",
  367. },
  368. },
  369. };
  370. expect(getLocalizedUpdateReleaseNotes(payload, "en-US")).toEqual({
  371. title: "ZCode v3.1.3",
  372. markdown: "- English notes",
  373. });
  374. expect(getLocalizedUpdateReleaseNotes(payload, "zh-CN")).toEqual({
  375. title: "ZCode v3.1.3",
  376. markdown: "- 中文说明",
  377. });
  378. });
  379. it("falls back to default release notes for legacy update feeds", () => {
  380. expect(
  381. getLocalizedUpdateReleaseNotes(
  382. {
  383. version: "3.1.3",
  384. title: "Release v3.1.3",
  385. markdown: "- default notes",
  386. },
  387. "en-US",
  388. ),
  389. ).toEqual({
  390. title: "Release v3.1.3",
  391. markdown: "- default notes",
  392. });
  393. });
  394. it("keeps default release notes for zh-CN when only an english sibling exists", () => {
  395. expect(
  396. getLocalizedUpdateReleaseNotes(
  397. {
  398. version: "3.1.4",
  399. title: "Release v3.1.4",
  400. markdown: "- 中文说明",
  401. releaseNotesByLocale: {
  402. "en-US": {
  403. title: "Release v3.1.4",
  404. markdown: "- English notes",
  405. },
  406. },
  407. },
  408. "zh-CN",
  409. ),
  410. ).toEqual({
  411. title: "Release v3.1.4",
  412. markdown: "- 中文说明",
  413. });
  414. });
  415. it("formats release dates by locale", () => {
  416. expect(formatUpdateReleaseDate("2026-06-23T13:48:54.438Z", "en-US")).toBe(
  417. "June 23, 2026",
  418. );
  419. expect(formatUpdateReleaseDate("2026-06-23T13:48:54.438Z", "zh-CN")).toBe(
  420. "2026年6月23日",
  421. );
  422. });
  423. it("formats UTC midnight release dates without shifting to local previous day", () => {
  424. expect(formatUpdateReleaseDate("2026-04-15T00:00:00.000Z", "en-US")).toBe(
  425. "April 15, 2026",
  426. );
  427. });
  428. it("shows dialog release date as a plain date without suffix text", () => {
  429. const formattedDate = formatUpdateReleaseDate(
  430. "2026-06-23T13:48:54.438Z",
  431. "zh-CN",
  432. );
  433. expect(zhCN["updateDialog.releaseDate"].replace("{date}", formattedDate!)).toBe(
  434. "2026年6月23日",
  435. );
  436. });
  437. it("labels the available update action as download update", () => {
  438. expect(zhCN["updateDialog.downloadAndUpdate"]).toBe("下载更新");
  439. expect(enUS["updateDialog.downloadAndUpdate"]).toBe("Download update");
  440. });
  441. it("shows the auto download checkbox before downloading", () => {
  442. expect(
  443. updateStatusDialogElementContainsText(
  444. "before-download",
  445. "updateDialog.autoDownloadAndInstall",
  446. ),
  447. ).toBe(true);
  448. expect(
  449. updateStatusDialogElementContainsText(
  450. "downloading",
  451. "updateDialog.autoDownloadAndInstall",
  452. ),
  453. ).toBe(false);
  454. });
  455. it("omits release notes from the update dialog", () => {
  456. expect(
  457. updateStatusDialogElementContainsText(
  458. "before-download",
  459. "Visible before downloading",
  460. ),
  461. ).toBe(false);
  462. expect(
  463. updateStatusDialogElementContainsText(
  464. "downloading",
  465. "Visible before downloading",
  466. ),
  467. ).toBe(false);
  468. expect(
  469. updateStatusDialogElementContainsText(
  470. "downloaded",
  471. "Visible before downloading",
  472. ),
  473. ).toBe(false);
  474. });
  475. it("omits update dialog title descriptions", () => {
  476. expect(
  477. updateStatusDialogElementContainsText(
  478. "before-download",
  479. "updateDialog.availableDescription",
  480. ),
  481. ).toBe(false);
  482. expect(
  483. updateStatusDialogElementContainsText(
  484. "downloading",
  485. "updateDialog.downloadingDescription",
  486. ),
  487. ).toBe(false);
  488. expect(
  489. updateStatusDialogElementContainsText(
  490. "downloaded",
  491. "updateDialog.readyDescription",
  492. ),
  493. ).toBe(false);
  494. });
  495. it.each<UpdateStatusDialogPhase>([
  496. "before-download",
  497. "downloading",
  498. "downloaded",
  499. ])("shows the macOS Dock product logo in the %s title", (phase) => {
  500. expect(updateStatusDialogUsesMacDockProductLogo(phase)).toBe(true);
  501. expect(
  502. updateStatusDialogElementContainsPropText(phase, "scale-105"),
  503. ).toBe(false);
  504. expect(
  505. updateStatusDialogElementContainsPropText(phase, "rounded-xl"),
  506. ).toBe(false);
  507. });
  508. it.each<UpdateStatusDialogPhase>([
  509. "before-download",
  510. "downloading",
  511. "downloaded",
  512. ])("centers the %s title metadata beside the logo", (phase) => {
  513. expect(updateStatusDialogUsesStackedReleaseDate(phase)).toBe(true);
  514. expect(updateStatusDialogUsesCenteredLogoTitleLayout(phase)).toBe(true);
  515. });
  516. it("uses standard dialog interior padding in the update dialog", () => {
  517. expect(
  518. updateStatusDialogElementContainsPropText("before-download", "p-5"),
  519. ).toBe(true);
  520. expect(
  521. updateStatusDialogElementContainsPropText("before-download", "px-5"),
  522. ).toBe(false);
  523. });
  524. it("uses theme color treatment for downloading progress", () => {
  525. expect(
  526. updateStatusDialogElementContainsPropText("downloading", "bg-primary"),
  527. ).toBe(true);
  528. expect(
  529. updateStatusDialogElementContainsPropText("downloading", "bg-success"),
  530. ).toBe(false);
  531. });
  532. it("shows download size text in a button-aligned progress row", () => {
  533. expect(
  534. updateStatusDialogElementContainsText("downloading", "4.2MB / 10.0MB"),
  535. ).toBe(true);
  536. expect(
  537. updateStatusDialogElementContainsPropText("downloading", "ml-2"),
  538. ).toBe(false);
  539. });
  540. it("uses the same secondary treatment for skip and later actions", () => {
  541. expect(countUpdateStatusDialogPropValue("before-download", "variant", "secondary")).toBe(2);
  542. expect(
  543. updateStatusDialogElementContainsPropText(
  544. "before-download",
  545. "text-foreground-subtle",
  546. ),
  547. ).toBe(false);
  548. expect(
  549. updateStatusDialogElementContainsPropText("before-download", "-ml-2"),
  550. ).toBe(false);
  551. expect(
  552. updateStatusDialogElementContainsPropText("before-download", "ml-2"),
  553. ).toBe(false);
  554. });
  555. it("uses standard control height across the update dialog actions", () => {
  556. expect(
  557. countUpdateStatusDialogElementPropText("before-download", "h-9"),
  558. ).toBe(3);
  559. });
  560. it("hides skip and later actions while downloading", () => {
  561. expect(
  562. updateStatusDialogElementContainsText(
  563. "downloading",
  564. "updateDialog.skipVersion",
  565. ),
  566. ).toBe(false);
  567. expect(
  568. updateStatusDialogElementContainsText("downloading", "updateDialog.later"),
  569. ).toBe(false);
  570. expect(
  571. updateStatusDialogElementContainsText(
  572. "downloading",
  573. "updateDialog.cancelDownload",
  574. ),
  575. ).toBe(true);
  576. expect(countUpdateStatusDialogElementPropText("downloading", "h-9")).toBe(
  577. 1,
  578. );
  579. expect(
  580. updateStatusDialogElementContainsPropText("downloading", "-mt-2"),
  581. ).toBe(true);
  582. });
  583. it("shows later and restart actions after download finishes", () => {
  584. expect(
  585. updateStatusDialogElementContainsText(
  586. "downloaded",
  587. "updateDialog.skipVersion",
  588. ),
  589. ).toBe(false);
  590. expect(
  591. updateStatusDialogElementContainsText("downloaded", "updateDialog.later"),
  592. ).toBe(true);
  593. expect(
  594. updateStatusDialogElementContainsText(
  595. "downloaded",
  596. "updateDialog.restartToUpdate",
  597. ),
  598. ).toBe(true);
  599. expect(countUpdateStatusDialogElementPropText("downloaded", "h-9")).toBe(
  600. 2,
  601. );
  602. });
  603. it("disables visible update actions while a command is in flight", () => {
  604. expect(
  605. countUpdateStatusDialogBooleanProp("before-download", "disabled", true),
  606. ).toBe(4);
  607. expect(
  608. countUpdateStatusDialogBooleanProp("downloading", "disabled", true),
  609. ).toBe(1);
  610. expect(
  611. countUpdateStatusDialogBooleanProp("downloaded", "disabled", true),
  612. ).toBe(2);
  613. });
  614. it("uses the standard large desktop dialog width", () => {
  615. expect(
  616. updateStatusDialogElementContainsPropText(
  617. "before-download",
  618. "max-w-[calc(100vw-2rem)]",
  619. ),
  620. ).toBe(true);
  621. expect(
  622. updateStatusDialogElementContainsPropText(
  623. "before-download",
  624. "sm:max-w-lg",
  625. ),
  626. ).toBe(true);
  627. expect(
  628. updateStatusDialogElementContainsPropText(
  629. "before-download",
  630. "sm:max-w-[28rem]",
  631. ),
  632. ).toBe(false);
  633. });
  634. it("can remove the dialog shell for the standalone update window", () => {
  635. expect(
  636. updateStatusDialogElementContainsPropText(
  637. "before-download",
  638. "border-0",
  639. true,
  640. ),
  641. ).toBe(true);
  642. expect(
  643. updateStatusDialogElementContainsPropText(
  644. "before-download",
  645. "shadow-none",
  646. true,
  647. ),
  648. ).toBe(true);
  649. expect(
  650. updateStatusDialogElementContainsPropText(
  651. "before-download",
  652. "max-w-none",
  653. true,
  654. ),
  655. ).toBe(true);
  656. expect(
  657. updateStatusDialogElementContainsPropText(
  658. "before-download",
  659. "data-open:animate-in",
  660. true,
  661. ),
  662. ).toBe(false);
  663. });
  664. it("keeps desktop footer action layout in the standalone update window", () => {
  665. expect(
  666. updateStatusDialogElementContainsPropText(
  667. "before-download",
  668. "flex-row items-center gap-3",
  669. true,
  670. ),
  671. ).toBe(true);
  672. expect(
  673. updateStatusDialogElementContainsPropText(
  674. "before-download",
  675. "justify-between",
  676. true,
  677. ),
  678. ).toBe(true);
  679. expect(
  680. updateStatusDialogElementContainsPropText(
  681. "before-download",
  682. "w-auto flex-row justify-end",
  683. true,
  684. ),
  685. ).toBe(true);
  686. expect(
  687. updateStatusDialogElementContainsPropText(
  688. "downloading",
  689. "justify-end",
  690. true,
  691. ),
  692. ).toBe(true);
  693. });
  694. it("does not reserve an empty content row in the standalone update window before download", () => {
  695. expect(
  696. updateStatusDialogElementContainsPropText(
  697. "before-download",
  698. "grid-rows-[auto_auto]",
  699. true,
  700. ),
  701. ).toBe(true);
  702. expect(
  703. updateStatusDialogElementContainsPropText(
  704. "before-download",
  705. "grid-rows-[auto_minmax(0,1fr)_auto]",
  706. true,
  707. ),
  708. ).toBe(false);
  709. expect(
  710. updateStatusDialogElementContainsPropText(
  711. "downloading",
  712. "grid-rows-[auto_minmax(0,1fr)_auto]",
  713. true,
  714. ),
  715. ).toBe(true);
  716. });
  717. it("keeps the standalone update window titlebar area draggable", () => {
  718. expect(
  719. updateStatusDialogElementContainsMatchingPropText(
  720. "before-download",
  721. (className) =>
  722. className.includes("p-5") &&
  723. className.includes("pt-10") &&
  724. !className.includes("[app-region:no-drag]"),
  725. true,
  726. ),
  727. ).toBe(true);
  728. expect(
  729. updateStatusDialogElementContainsMatchingPropText(
  730. "before-download",
  731. (className) =>
  732. className.includes("items-center") &&
  733. className.includes("[app-region:no-drag]"),
  734. true,
  735. ),
  736. ).toBe(true);
  737. });
  738. it("uses the original color treatment whenever the main update entry is visible", () => {
  739. const availableHtml = renderUpdateStatusButton({
  740. kind: "update-available",
  741. version: "3.3.3",
  742. channel: "stable",
  743. } as UpdateStatePayload);
  744. const downloadingHtml = renderUpdateStatusButton({
  745. kind: "download-progress",
  746. version: "3.3.3",
  747. progress: "42",
  748. channel: "stable",
  749. } as UpdateStatePayload);
  750. const downloadedHtml = renderUpdateStatusButton({
  751. kind: "update-downloaded",
  752. version: "3.3.3",
  753. channel: "stable",
  754. } as UpdateStatePayload);
  755. expect(availableHtml).toContain("bg-success");
  756. expect(availableHtml).toContain("text-success-foreground");
  757. expect(availableHtml).toContain("topoverlayer:w-auto");
  758. expect(availableHtml).toContain("h-auto");
  759. expect(availableHtml).toContain("min-h-5");
  760. expect(availableHtml).toContain("py-0.5");
  761. expect(availableHtml).toContain("font-medium");
  762. expect(availableHtml).toContain("text-ui-xs");
  763. expect(availableHtml).not.toContain("text-ui-sm");
  764. expect(availableHtml).not.toContain("text-ui-base");
  765. expect(downloadingHtml).toContain("bg-success");
  766. expect(downloadingHtml).toContain("text-success-foreground");
  767. expect(downloadingHtml).toContain("animate-spin");
  768. expect(downloadingHtml).not.toContain('disabled=""');
  769. expect(downloadingHtml).not.toContain("下载中");
  770. expect(downloadingHtml).not.toContain("topoverlayer:w-auto");
  771. expect(downloadingHtml).not.toContain("topoverlayer:hidden");
  772. expect(downloadedHtml).toContain("bg-success");
  773. expect(downloadedHtml).toContain("text-success-foreground");
  774. expect(downloadedHtml).toContain("topoverlayer:w-auto");
  775. });
  776. it("keeps the downloading entry visible when progress state omits version", () => {
  777. const html = renderUpdateStatusButton({
  778. kind: "download-progress",
  779. progress: "42",
  780. channel: "stable",
  781. } as UpdateStatePayload);
  782. // Bugfix: download-progress.version 是协议可选字段。不能因为某一帧缺少版本号
  783. // 就把更新入口卸载,否则用户打开的下载弹窗会跟着消失。
  784. expect(html).toContain("bg-success");
  785. expect(html).toContain("animate-spin");
  786. expect(html).not.toContain("topoverlayer:w-auto");
  787. });
  788. it("derives downloading dialog model from state kind instead of version", () => {
  789. const model = deriveUpdateStatusViewModel({
  790. legacyReadyVersion: "3.3.2",
  791. updateState: {
  792. kind: "download-progress",
  793. enabled: false,
  794. progress: "42",
  795. transferredBytes: 4.2 * 1024 * 1024,
  796. totalBytes: 10 * 1024 * 1024,
  797. },
  798. });
  799. expect(model.dialogPhase).toBe("downloading");
  800. expect(model.displayVersion).toBe("…");
  801. expect(model.progressLabel).toBe("4.2MB / 10.0MB");
  802. expect(model.progressValue).toBe(42);
  803. expect(model.skippableVersion).toBe(null);
  804. });
  805. it("keeps action completion rules in the update status model", () => {
  806. expect(
  807. isUpdateActionCompleted("download", { kind: "checking", enabled: true }),
  808. ).toBe(false);
  809. expect(
  810. isUpdateActionCompleted("download", { kind: "idle", enabled: true }),
  811. ).toBe(false);
  812. expect(
  813. isUpdateActionCompleted("download", {
  814. kind: "download-progress",
  815. enabled: false,
  816. progress: "1",
  817. }),
  818. ).toBe(true);
  819. expect(
  820. isUpdateActionCompleted("cancel", {
  821. kind: "download-progress",
  822. enabled: false,
  823. progress: "1",
  824. }),
  825. ).toBe(false);
  826. expect(
  827. isUpdateActionCompleted("skip", { kind: "idle", enabled: true }),
  828. ).toBe(true);
  829. });
  830. it("ignores stale legacy ready version after state reports idle", () => {
  831. const html = renderUpdateStatusButton(
  832. { kind: "idle", enabled: true },
  833. "3.3.2",
  834. );
  835. // Bugfix: main 侧 staging error 后会广播 idle,但旧 UpdateReady 缓存仍可能残留。
  836. // UI 此时不能继续展示“重启以更新”,否则点击只会命中一个已失效的安装上下文。
  837. expect(html).not.toContain("bg-success");
  838. expect(html).not.toContain("updateReady.shortTitle");
  839. });
  840. it("keeps legacy ready version when no update state API exists", () => {
  841. const html = renderUpdateStatusButton(null, "3.3.2");
  842. // 平滑迁移:旧包只会发 UpdateReady,不一定有持续状态流。
  843. // 只有新状态明确同步成 idle/error/checking 后,才清掉 legacy ready。
  844. expect(html).toContain("bg-success");
  845. expect(html).toContain("topoverlayer:w-auto");
  846. });
  847. });