conversationShareContent.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. conversationShareContentBlockSchema,
  4. decodeConversationShareContentBlocks,
  5. } from "../src/conversation-share-content.js";
  6. const block = {
  7. id: "share-content-1",
  8. type: "workflow",
  9. version: 1,
  10. product_turn_id: "share-product-turn-1",
  11. fallback_row_id: 3,
  12. captured_at: 1,
  13. resource_ids: [],
  14. payload: { name: "军棋", completion: true, artifacts: [] },
  15. };
  16. describe("公开内容块", () => {
  17. it("v2 文档正文有字节上限,v1 不接受该扩展", () => {
  18. const artifact = {
  19. id: "doc",
  20. resource_id: "share-artifact-1",
  21. kind: "markdown",
  22. title: "报告",
  23. version: 1,
  24. content_type: "text/markdown",
  25. preview_text: "# Frozen report",
  26. };
  27. const extended = { ...block, version: 2, payload: { ...block.payload, artifacts: [artifact] } };
  28. expect(conversationShareContentBlockSchema.safeParse(extended).success).toBe(true);
  29. expect(conversationShareContentBlockSchema.safeParse({ ...extended, version: 1 }).success).toBe(
  30. false,
  31. );
  32. expect(
  33. conversationShareContentBlockSchema.safeParse({
  34. ...extended,
  35. payload: {
  36. ...extended.payload,
  37. artifacts: [{ ...artifact, preview_text: "中".repeat(25000) }],
  38. },
  39. }).success,
  40. ).toBe(false);
  41. });
  42. it("已知块严格校验,未知块独立降级", () => {
  43. expect(conversationShareContentBlockSchema.parse(block)).toEqual(block);
  44. expect(decodeConversationShareContentBlocks([block, { ...block, version: 99 }])).toEqual([
  45. block,
  46. ]);
  47. expect(
  48. conversationShareContentBlockSchema.safeParse({
  49. ...block,
  50. payload: { ...block.payload, sessionId: "private" },
  51. }).success,
  52. ).toBe(false);
  53. });
  54. it("看板禁止可执行字段和本地引用", () => {
  55. const artifact = {
  56. id: "board",
  57. title: "进度",
  58. kind: "board",
  59. version: 1,
  60. spec: { key: "id", status: "status", columns: ["done"] },
  61. items: [{ sequence: 1, siteId: "report", ordinal: 1, item: { id: "a", status: "done" } }],
  62. };
  63. expect(
  64. conversationShareContentBlockSchema.safeParse({
  65. ...block,
  66. payload: { ...block.payload, artifacts: [artifact] },
  67. }).success,
  68. ).toBe(true);
  69. expect(
  70. conversationShareContentBlockSchema.safeParse({
  71. ...block,
  72. payload: {
  73. ...block.payload,
  74. artifacts: [{ ...artifact, spec: { ...artifact.spec, onClick: "execute" } }],
  75. },
  76. }).success,
  77. ).toBe(false);
  78. expect(
  79. conversationShareContentBlockSchema.safeParse({
  80. ...block,
  81. payload: {
  82. ...block.payload,
  83. artifacts: [
  84. {
  85. ...artifact,
  86. items: [{ sequence: 1, siteId: "report", ordinal: 1, item: "file:///secret" }],
  87. },
  88. ],
  89. },
  90. }).success,
  91. ).toBe(false);
  92. });
  93. });