v4-native-assistant-feedback.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { CommandEnvelope } from "@zcode/shared/zcode-protocol-v4";
  3. import { V4CommandExecutor } from "../src/zcode-protocol-v4/commands/executor.js";
  4. import type { V4CommandCoreHost } from "../src/zcode-protocol-v4/commands/types.js";
  5. function envelope(feedback: "like" | "dislike" | null): CommandEnvelope {
  6. return {
  7. commandId: "feedback-command",
  8. clientId: "desktop-client",
  9. sessionId: "session-1",
  10. baseRevision: 4,
  11. baseLogEpoch: "epoch-1",
  12. type: "setAssistantFeedback",
  13. payload: {
  14. target: { rowId: 9, entityId: "assistant-1" },
  15. feedback,
  16. },
  17. issuedAt: 1_700_000_000_000,
  18. };
  19. }
  20. function host(setAssistantFeedback = vi.fn().mockResolvedValue(undefined)): V4CommandCoreHost {
  21. return {
  22. getRecord: () => ({ app: { sessionId: "session-1" } }) as never,
  23. resolveRowActionTarget: () =>
  24. ({
  25. ok: true,
  26. action: "setAssistantFeedback",
  27. messageId: "assistant-1",
  28. row: { kind: "assistantText" },
  29. }) as never,
  30. setAssistantFeedback,
  31. } as V4CommandCoreHost;
  32. }
  33. describe("v4 setAssistantFeedback", () => {
  34. it("把 row CAS 解析出的 message/entity 与 nullable feedback 交给持久化 host", async () => {
  35. const persist = vi.fn().mockResolvedValue(undefined);
  36. const executor = new V4CommandExecutor(host(persist));
  37. await executor.execute(envelope(null));
  38. expect(persist).toHaveBeenCalledWith("session-1", {
  39. entityId: "assistant-1",
  40. messageId: "assistant-1",
  41. feedback: null,
  42. });
  43. });
  44. it("stale row target 不写持久化", async () => {
  45. const persist = vi.fn().mockResolvedValue(undefined);
  46. const staleHost = host(persist);
  47. staleHost.resolveRowActionTarget = () =>
  48. ({ ok: false, status: "stale", reasonCode: "proto.staleTarget" }) as never;
  49. await expect(new V4CommandExecutor(staleHost).execute(envelope("like"))).rejects.toThrow();
  50. expect(persist).not.toHaveBeenCalled();
  51. });
  52. });