conversationShareContent.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { readFile } from "node:fs/promises";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { expect, it } from "vitest";
  5. import { conversationShareContentBlockSchema } from "@zcode/shared";
  6. import { ConversationShareReadonlyTimeline } from "@/v4/ConversationShareReadonlyTimeline.js";
  7. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  8. const blocks = JSON.parse(
  9. await readFile(
  10. new URL("../../services/test/fixtures/share-content-blocks.json", import.meta.url),
  11. "utf8",
  12. ),
  13. );
  14. const base = { turnId: "t1", productTurnId: "share-product-turn-1", createdAt: 1 };
  15. const rows: ConversationRow[] = [
  16. {
  17. ...base,
  18. rowId: 1,
  19. createdAtSeq: 1,
  20. kind: "turnHeader",
  21. origin: "userInput",
  22. state: "completedSuccess",
  23. startedAt: 1,
  24. },
  25. {
  26. ...base,
  27. rowId: 2,
  28. createdAtSeq: 2,
  29. kind: "assistantText",
  30. state: "complete",
  31. text: "ORIGINAL_FINAL_TEXT",
  32. },
  33. {
  34. ...base,
  35. rowId: 3,
  36. createdAtSeq: 3,
  37. kind: "assistantText",
  38. state: "complete",
  39. text: "WORKFLOW_FALLBACK",
  40. },
  41. ];
  42. it("完整内容不依赖桌面宿主,正文不被摘要挤进折叠历史", () => {
  43. expect(conversationShareContentBlockSchema.safeParse(blocks[0]).success).toBe(true);
  44. const html = renderToStaticMarkup(
  45. createElement(ConversationShareReadonlyTimeline, { rows, contentBlocks: blocks }),
  46. );
  47. expect(html).toContain("ORIGINAL_FINAL_TEXT");
  48. expect(html).toContain("军棋项目进度看板");
  49. expect(html).toContain("军棋项目交付报告");
  50. expect(html).not.toContain("WORKFLOW_FALLBACK");
  51. expect(html).not.toContain('data-history-open="false"');
  52. });
  53. it("不展示空的不可用工作流卡及本地网站卡,也不恢复其占位摘要", () => {
  54. const unavailable = {
  55. ...blocks[0],
  56. payload: {
  57. name: "Missing workflow",
  58. completion: false,
  59. unavailable: "source_unavailable",
  60. artifacts: [],
  61. },
  62. };
  63. const input = rows.map((row) =>
  64. row.rowId === 2 ? { ...row, text: "ORIGINAL_FINAL_TEXT http://127.0.0.1:8080" } : row,
  65. );
  66. const html = renderToStaticMarkup(
  67. createElement(ConversationShareReadonlyTimeline, { rows: input, contentBlocks: [unavailable] }),
  68. );
  69. expect(html).toContain("ORIGINAL_FINAL_TEXT");
  70. expect(html).not.toContain("WORKFLOW_FALLBACK");
  71. expect(html).not.toContain("data-share-content-block");
  72. expect(html).not.toContain("data-share-local-website");
  73. expect(html).not.toContain("Snapshot at sharing");
  74. });
  75. it("未知类型、版本及损坏块不吞掉原位摘要", () => {
  76. for (const block of [
  77. { ...blocks[0], type: "future" },
  78. { ...blocks[0], version: 99 },
  79. { ...blocks[0], payload: null },
  80. ]) {
  81. const html = renderToStaticMarkup(
  82. createElement(ConversationShareReadonlyTimeline, { rows, contentBlocks: [block] }),
  83. );
  84. expect(html).toContain("WORKFLOW_FALLBACK");
  85. expect(html).toContain("ORIGINAL_FINAL_TEXT");
  86. }
  87. });