conversationShareContext.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseConversationShareContext,
  4. resolveAttachableShareContext,
  5. } from "@/lib/conversationShareContext.js";
  6. /** 历史消息里可能残留的 share URL 尾块;写入端已删除,这里只验证读取端能剥干净。 */
  7. function withShareContextBlock(text: string, payload: Record<string, unknown>): string {
  8. const block = [
  9. "# zcode-share-context:",
  10. "```zcode-share-context",
  11. JSON.stringify(payload),
  12. "```",
  13. ].join("\n");
  14. return text ? `${text}\n\n${block}` : block;
  15. }
  16. describe("conversation share context reference", () => {
  17. it("把历史消息里的 share URL 尾块从可见正文剥掉", () => {
  18. const encoded = withShareContextBlock("继续分析", {
  19. contextId: "context-1",
  20. shareUrl: "https://zcode.z.ai/cn/share/share-1",
  21. });
  22. expect(parseConversationShareContext(encoded)).toEqual({
  23. visibleContent: "继续分析",
  24. reference: {
  25. contextId: "context-1",
  26. shareUrl: "https://zcode.z.ai/cn/share/share-1",
  27. },
  28. });
  29. });
  30. it("rejects signed/download URLs and unknown fields", () => {
  31. const parsed = parseConversationShareContext(
  32. [
  33. "继续分析",
  34. "",
  35. "# zcode-share-context:",
  36. "```zcode-share-context",
  37. JSON.stringify({
  38. contextId: "context-1",
  39. shareUrl: "https://signed.example/download?token=secret",
  40. markdown: "must-not-cross-ui",
  41. }),
  42. "```",
  43. ].join("\n"),
  44. );
  45. expect(parsed.reference).toBeNull();
  46. expect(parsed.visibleContent).toContain("# zcode-share-context:");
  47. });
  48. });
  49. /**
  50. * 线上故障回归:composer 原本从一个平行的 sharedContextImport prop 读 handover context,
  51. * 而 SessionPane 从没传过它。结果首条消息永远不带 sharedContextRefs,CLI 侧
  52. * pending→reserved→attached 一步都走不了,shared_context 消息被 hydrator 永久跳过——
  53. * 会话顶部的只读块照常显示,模型却全程没拿到分享内容,肉眼完全看不出来。
  54. *
  55. * 现在改成从 snapshot 推导。这组用例钉住「什么情况下应该带 refs」这个判定本身。
  56. */
  57. describe("resolveAttachableShareContext", () => {
  58. const pending = {
  59. contextId: "context-1",
  60. title: "来自分享:X",
  61. shareUrl: "https://zcode.z.ai/cn/share/share-1",
  62. status: "pending",
  63. };
  64. it("pending 必须可 attach —— 否则模型永远拿不到分享内容", () => {
  65. expect(resolveAttachableShareContext(pending)).toEqual(pending);
  66. });
  67. it("reserved(已进队列但还没消费)仍然可 attach", () => {
  68. expect(resolveAttachableShareContext({ ...pending, status: "reserved" })?.status).toBe(
  69. "reserved",
  70. );
  71. });
  72. it("discarded 不再 attach", () => {
  73. expect(resolveAttachableShareContext({ ...pending, status: "discarded" })).toBeNull();
  74. });
  75. it("legacy 形状(只有 title、没有 contextId)不 attach:构不出 refs", () => {
  76. expect(resolveAttachableShareContext({ title: "来自分享:X" })).toBeNull();
  77. });
  78. it("没有 handover context 时返回 null", () => {
  79. expect(resolveAttachableShareContext(null)).toBeNull();
  80. expect(resolveAttachableShareContext(undefined)).toBeNull();
  81. });
  82. });