workspaceLastActivity.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { ZCodeTaskMeta } from "@zcode/shared";
  5. import zh from "@/i18n/locales/zh-CN.js";
  6. import { WorkspaceLastActivity } from "@/WorkspaceHeaderSections/WorkspaceLastActivity.js";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. intl: {
  10. formatMessage: ({ id }: { id: string }, values: Record<string, unknown> = {}) =>
  11. zh[id]!.replace(/\{(\w+)\}/g, (_, key) => String(values[key])),
  12. },
  13. }),
  14. }));
  15. afterEach(() => vi.useRealTimers());
  16. describe("current task last activity", () => {
  17. it("uses current task updatedAt", () => {
  18. vi.useFakeTimers();
  19. vi.setSystemTime(1_000_000);
  20. expect(
  21. renderToStaticMarkup(
  22. createElement(WorkspaceLastActivity, { task: { updatedAt: 880_000 } as ZCodeTaskMeta }),
  23. ),
  24. ).toContain("最近活动 2 分钟前");
  25. });
  26. it("prefers live activity over stored metadata", () => {
  27. vi.useFakeTimers();
  28. vi.setSystemTime(1_000_000);
  29. const task = {
  30. updatedAt: 1,
  31. __zcodeSessionActivity: { lastActivityAt: 999_000 },
  32. } as unknown as ZCodeTaskMeta;
  33. expect(renderToStaticMarkup(createElement(WorkspaceLastActivity, { task }))).toContain(
  34. "最近活动 刚刚",
  35. );
  36. });
  37. it("does not invent a timestamp for a missing task", () => {
  38. expect(renderToStaticMarkup(createElement(WorkspaceLastActivity, { task: null }))).toBe("");
  39. });
  40. });