sharedContextFormatter.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect, it } from "vitest";
  2. import { formatSharedContextV1 } from "../src/conversation-share/sharedContextFormatter.js";
  3. describe("SharedContextFormatterV1", () => {
  4. it("稳定格式化 Rows 并把 artifact ref 改写为 workspace 相对路径", () => {
  5. const input = {
  6. share: { shareId: "share-1", title: "Research" },
  7. rows: [
  8. {
  9. rowId: 1,
  10. turnId: "turn-1",
  11. productTurnId: "product-1",
  12. createdAt: 1,
  13. createdAtSeq: 1,
  14. kind: "turnHeader" as const,
  15. origin: "userInput" as const,
  16. state: "completedSuccess" as const,
  17. startedAt: 1,
  18. },
  19. {
  20. rowId: 2,
  21. turnId: "turn-1",
  22. productTurnId: "product-1",
  23. createdAt: 2,
  24. createdAtSeq: 2,
  25. kind: "userInput" as const,
  26. origin: "realUser" as const,
  27. text: "Make a report",
  28. },
  29. {
  30. rowId: 3,
  31. turnId: "turn-1",
  32. productTurnId: "product-1",
  33. createdAt: 3,
  34. createdAtSeq: 3,
  35. kind: "artifact" as const,
  36. artifactVersionId: "artifact-1",
  37. logicalArtifactKey: "report",
  38. displayName: "report.pdf",
  39. artifactType: "pdf" as const,
  40. mimeType: "application/pdf",
  41. sizeBytes: 3,
  42. sha256: "a".repeat(64),
  43. ref: "zcode-artifact://share/artifact-1",
  44. state: "current" as const,
  45. },
  46. ],
  47. installedArtifacts: [
  48. {
  49. artifactId: "artifact-1",
  50. workspaceRelativePath: "shared-artifacts/report.pdf",
  51. displayName: "report.pdf",
  52. mimeType: "application/pdf",
  53. sha256: "a".repeat(64),
  54. },
  55. ],
  56. };
  57. const first = formatSharedContextV1(input);
  58. expect(formatSharedContextV1(input)).toEqual(first);
  59. expect(first.markdown).toContain("shared-artifacts/report.pdf");
  60. expect(first.markdown).not.toContain("zcode-artifact://");
  61. });
  62. it("把 userInput 附件写入导入后的模型上下文", () => {
  63. const result = formatSharedContextV1({
  64. share: { shareId: "share-text", title: "Pasted text" },
  65. rows: [
  66. {
  67. rowId: 1,
  68. turnId: "turn-1",
  69. productTurnId: "product-1",
  70. createdAt: 1,
  71. createdAtSeq: 1,
  72. kind: "userInput" as const,
  73. origin: "realUser" as const,
  74. text: "请分析粘贴内容",
  75. attachments: [
  76. {
  77. ref: "zcode-artifact://share/text-1",
  78. fileName: "pasted-text.txt",
  79. mime: "text/plain",
  80. bytes: 4,
  81. },
  82. ],
  83. },
  84. ],
  85. installedArtifacts: [
  86. {
  87. artifactId: "text-1",
  88. workspaceRelativePath: "shared-artifacts/pasted-text.txt",
  89. displayName: "pasted-text.txt",
  90. mimeType: "text/plain",
  91. sha256: "b".repeat(64),
  92. },
  93. ],
  94. });
  95. expect(result.markdown).toContain("shared-artifacts/pasted-text.txt");
  96. expect(result.markdown).toContain("text/plain");
  97. });
  98. });