groupQueueCard.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createGroupQueueCardSync } from "#src/bots/groupQueueCard.js";
  3. import type { BotGroupInputProgress } from "@zcode/shared";
  4. function setup(initial: BotGroupInputProgress = { status: "waiting" }) {
  5. let saved: BotGroupInputProgress | undefined = initial;
  6. const render = vi.fn(async () => {});
  7. const sync = createGroupQueueCardSync({
  8. read: async () => saved,
  9. mutate: async (_key, update) => (saved ? (saved = update(saved)) : undefined),
  10. render,
  11. });
  12. return {
  13. sync,
  14. render,
  15. read: () => saved,
  16. disable: () => {
  17. saved = undefined;
  18. },
  19. };
  20. }
  21. describe("queue card lifecycle", () => {
  22. it("updates the same card after queued input starts and completes", async () => {
  23. const { sync, render, read } = setup();
  24. await sync.attach("input", "om_card");
  25. await sync.update("input", "working");
  26. await sync.update("input", "done");
  27. expect(render.mock.calls).toEqual([
  28. ["input", "om_card", "working"],
  29. ["input", "om_card", "done"],
  30. ]);
  31. expect(read()?.cardStatus).toBe("done");
  32. });
  33. it("catches up when card creation finishes after completion and ignores late queue acknowledgement", async () => {
  34. const { sync, render } = setup();
  35. await sync.update("input", "working");
  36. await sync.update("input", "done");
  37. await sync.update("input", "waiting");
  38. await sync.attach("input", "om_late");
  39. await sync.update("input", "done");
  40. expect(render.mock.calls).toEqual([["input", "om_late", "done"]]);
  41. });
  42. it.each(["failed", "stopped", "cancelled", "discarded"] as const)(
  43. "renders terminal %s and rejects late working state",
  44. async (state) => {
  45. const { sync, render } = setup();
  46. await sync.attach("input", "om_card");
  47. await sync.update("input", state);
  48. await sync.update("input", "working");
  49. expect(render.mock.calls).toEqual([["input", "om_card", state]]);
  50. },
  51. );
  52. it("does not update a revoked group or lose the persisted target after recreation", async () => {
  53. const { sync, render, disable } = setup({
  54. status: "working",
  55. cardMessageId: "om_saved",
  56. cardStatus: "waiting",
  57. });
  58. await sync.update("input", "done");
  59. expect(render).toHaveBeenCalledWith("input", "om_saved", "done");
  60. disable();
  61. await sync.update("input", "done");
  62. expect(render).toHaveBeenCalledTimes(1);
  63. });
  64. });