botTopicPreparationState.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { expect, it } from "vitest";
  2. import type { BotInboundMessage } from "@zcode/shared";
  3. import { createTopicPreparationState } from "../src/bots/topicPreparationState.js";
  4. it("isolates topic preparation and exposes no attachment payload in its UI snapshot", () => {
  5. const state = createTopicPreparationState();
  6. const message = {
  7. text: "request",
  8. actor: { providerMessageId: "m", providerUserId: "u", displayName: "User" },
  9. attachments: [{ dataBase64: "private-file" }],
  10. } as BotInboundMessage;
  11. state.add("topic-a", message);
  12. expect(state.snapshot("topic-b")).toEqual([]);
  13. expect(JSON.stringify(state.snapshot("topic-a"))).not.toContain("private-file");
  14. expect(state.retry("topic-a", "m")).toBeUndefined();
  15. state.update("topic-a", ["m"], "failed", "Download failed");
  16. expect(state.retry("topic-a", "m")).toBe(message);
  17. state.clear("topic-a");
  18. expect(state.retry("topic-a", "m")).toBeUndefined();
  19. expect(state.snapshot("topic-a")).toEqual([]);
  20. });
  21. it("keeps messages arriving during stop in waitingStop until stop completes", () => {
  22. const state = createTopicPreparationState();
  23. const message = {
  24. text: "request",
  25. actor: { providerMessageId: "m", providerUserId: "u" },
  26. } as BotInboundMessage;
  27. state.setWaitingStop("topic", true);
  28. state.add("topic", message);
  29. expect(state.snapshot("topic")[0]?.status).toBe("waitingStop");
  30. state.setWaitingStop("topic", false);
  31. expect(state.snapshot("topic")[0]?.status).toBe("preparing");
  32. state.update("topic", ["m"], "failed", "error");
  33. state.setWaitingStop("topic", true);
  34. state.setWaitingStop("topic", false);
  35. expect(state.snapshot("topic")[0]?.status).toBe("failed");
  36. state.clear("topic");
  37. state.add("topic", message);
  38. expect(state.snapshot("topic")[0]?.status).toBe("preparing");
  39. });