offPeakTaskListRender.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { TID_OFFPEAK_CARD, TID_OFFPEAK_CARD_SESSION, type ZCodeOffPeakTask } from "@zcode/shared";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { OffPeakTaskList } from "@/settings/OffPeakTaskList.js";
  8. afterEach(cleanup);
  9. function task(overrides: Partial<ZCodeOffPeakTask> & { offPeakTaskId: string }): ZCodeOffPeakTask {
  10. return {
  11. title: overrides.offPeakTaskId,
  12. prompt: "prompt",
  13. status: "queued",
  14. createdAt: 1,
  15. ...overrides,
  16. } as ZCodeOffPeakTask;
  17. }
  18. function renderList(tasks: ZCodeOffPeakTask[]) {
  19. const noop = vi.fn();
  20. return render(
  21. createElement(
  22. ZCodeIntlProvider,
  23. { initialLocale: "zh-CN" },
  24. createElement(OffPeakTaskList, {
  25. tasks,
  26. busyOperationId: null,
  27. onOpen: noop,
  28. onPause: noop,
  29. onContinue: noop,
  30. onCancel: noop,
  31. onDelete: noop,
  32. onOpenSession: noop,
  33. }),
  34. ),
  35. );
  36. }
  37. describe("OffPeakTaskList render", () => {
  38. it("失败卡片的状态徽章不可收缩,长会话标题只截断自己", () => {
  39. const view = renderList([
  40. task({
  41. offPeakTaskId: "failed-1",
  42. status: "failed",
  43. sessionTitle:
  44. "扫描最近的 CI 运行,列出失败和不稳定测试及其可能原因,并按影响范围给出修复建议",
  45. }),
  46. ]);
  47. const card = view.getByTestId(TID_OFFPEAK_CARD);
  48. const statusLabel = view.getByText("失败");
  49. const badge = statusLabel.parentElement!;
  50. // Bug 回归:徽章曾是 min-w-0,会被右侧会话标题压到只剩一个字。
  51. expect(badge.className).toContain("shrink-0");
  52. expect(badge.className).not.toContain("min-w-0");
  53. const session = view.getByTestId(TID_OFFPEAK_CARD_SESSION);
  54. expect(session.className).toContain("truncate");
  55. expect(session.className).toContain("min-w-0");
  56. expect(card.contains(session)).toBe(true);
  57. });
  58. it("超过 8 张卡片时 grid 自身滚动,8 张及以内不锁高度", () => {
  59. const eight = Array.from({ length: 8 }, (_, i) => task({ offPeakTaskId: `t-${i}` }));
  60. const view = renderList(eight);
  61. const grid = view.getAllByTestId(TID_OFFPEAK_CARD)[0]!.parentElement!;
  62. expect(grid.className).not.toContain("overflow-y-auto");
  63. view.rerender(
  64. createElement(
  65. ZCodeIntlProvider,
  66. { initialLocale: "zh-CN" },
  67. createElement(OffPeakTaskList, {
  68. tasks: [...eight, task({ offPeakTaskId: "t-8" })],
  69. busyOperationId: null,
  70. onOpen: vi.fn(),
  71. onPause: vi.fn(),
  72. onContinue: vi.fn(),
  73. onCancel: vi.fn(),
  74. onDelete: vi.fn(),
  75. onOpenSession: vi.fn(),
  76. }),
  77. ),
  78. );
  79. const scrollGrid = view.getAllByTestId(TID_OFFPEAK_CARD)[0]!.parentElement!;
  80. expect(scrollGrid.className).toContain("overflow-y-auto");
  81. expect(scrollGrid.className).toContain("lg:max-h-[606px]");
  82. });
  83. });