assistant-feedback-persistence.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect, it, vi } from "vitest";
  2. import type {
  3. MessageInfo,
  4. SessionEvent,
  5. SessionEventStorePort,
  6. SessionStorePort,
  7. } from "@zcode/contracts";
  8. import { persistAssistantFeedback } from "../src/zcode-protocol-v4/assistant-feedback-persistence.js";
  9. function assistantInfo(): MessageInfo {
  10. return {
  11. id: "assistant-1" as never,
  12. sessionID: "session-1" as never,
  13. role: "assistant",
  14. time: { created: 1 },
  15. parentID: "user-1" as never,
  16. modelID: "model-1" as never,
  17. providerID: "provider-1" as never,
  18. mode: "default",
  19. agent: "default",
  20. path: { cwd: "/repo", root: "/repo" },
  21. cost: 0,
  22. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  23. metadata: { retained: "yes" },
  24. };
  25. }
  26. describe("assistant feedback persistence", () => {
  27. it("like 写 metadata;null 只删除 feedback 并保留其它 metadata,随后发布事件", async () => {
  28. let current = assistantInfo();
  29. const saveMessage = vi.fn(async (info: MessageInfo) => {
  30. current = info;
  31. });
  32. const sessionStore = {
  33. messages: vi.fn(async () => [{ info: current, parts: [] }]),
  34. saveMessage,
  35. } as unknown as SessionStorePort;
  36. const persistedEvents: SessionEvent[] = [];
  37. const eventStore = {
  38. getLatestSequenceNumber: vi.fn(async () => persistedEvents.length),
  39. append: vi.fn(async (event: SessionEvent) => {
  40. persistedEvents.push(event);
  41. return event;
  42. }),
  43. } as unknown as SessionEventStorePort;
  44. const projected: SessionEvent[] = [];
  45. const common = {
  46. sessionStore,
  47. eventStore,
  48. sessionId: "session-1",
  49. messageId: "assistant-1",
  50. entityId: "assistant-1",
  51. traceId: "trace-1",
  52. now: () => 100,
  53. onPersistedEvent: (event: SessionEvent) => projected.push(event),
  54. };
  55. await persistAssistantFeedback({ ...common, feedback: "like" });
  56. expect(current.metadata).toEqual({ retained: "yes", assistantFeedback: "like" });
  57. expect(projected.at(-1)?.payload).toEqual({ entityId: "assistant-1", feedback: "like" });
  58. await persistAssistantFeedback({ ...common, feedback: null });
  59. expect(current.metadata).toEqual({ retained: "yes" });
  60. expect(projected.at(-1)?.payload).toEqual({ entityId: "assistant-1", feedback: null });
  61. expect(saveMessage).toHaveBeenCalledTimes(2);
  62. expect(eventStore.append).toHaveBeenCalledTimes(2);
  63. });
  64. it("event append 失败时补偿 transcript;durable 后 projection 失败不返回失败 ACK", async () => {
  65. let current = assistantInfo();
  66. const saveMessage = vi.fn(async (info: MessageInfo) => {
  67. current = info;
  68. });
  69. const sessionStore = {
  70. messages: vi.fn(async () => [{ info: current, parts: [] }]),
  71. saveMessage,
  72. } as unknown as SessionStorePort;
  73. const appendError = new Error("append failed");
  74. const eventStore = {
  75. getLatestSequenceNumber: vi.fn(async () => 0),
  76. append: vi.fn(async () => {
  77. throw appendError;
  78. }),
  79. } as unknown as SessionEventStorePort;
  80. await expect(
  81. persistAssistantFeedback({
  82. sessionStore,
  83. eventStore,
  84. sessionId: "session-1",
  85. messageId: "assistant-1",
  86. entityId: "assistant-1",
  87. traceId: "trace-1",
  88. feedback: "like",
  89. onPersistedEvent: () => undefined,
  90. }),
  91. ).rejects.toBe(appendError);
  92. expect(current).toEqual(assistantInfo());
  93. expect(saveMessage).toHaveBeenCalledTimes(2);
  94. const persistedEventStore = {
  95. getLatestSequenceNumber: vi.fn(async () => 0),
  96. append: vi.fn(async (event: SessionEvent) => event),
  97. } as unknown as SessionEventStorePort;
  98. const onLiveProjectionError = vi.fn();
  99. await expect(
  100. persistAssistantFeedback({
  101. sessionStore,
  102. eventStore: persistedEventStore,
  103. sessionId: "session-1",
  104. messageId: "assistant-1",
  105. entityId: "assistant-1",
  106. traceId: "trace-1",
  107. feedback: "dislike",
  108. onPersistedEvent: () => {
  109. throw new Error("live projection failed");
  110. },
  111. onLiveProjectionError,
  112. }),
  113. ).resolves.toBeUndefined();
  114. expect(current.metadata).toEqual({
  115. retained: "yes",
  116. assistantFeedback: "dislike",
  117. });
  118. expect(onLiveProjectionError).toHaveBeenCalledOnce();
  119. });
  120. });