botTopicContinuation.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createTopicContinuation } from "../src/bots/topicContinuation.js";
  3. function deferred<T>() {
  4. let resolve!: (value: T) => void;
  5. const promise = new Promise<T>((done) => {
  6. resolve = done;
  7. });
  8. return { promise, resolve };
  9. }
  10. const flush = async () => {
  11. for (let i = 0; i < 12; i++) await Promise.resolve();
  12. };
  13. function fixture() {
  14. const stopped = deferred<void>();
  15. const submit = vi.fn(async (_messages: string[]) => undefined);
  16. const stopAndWait = vi.fn(async (_runId: string) => stopped.promise);
  17. const readRunningRun = vi.fn(async (): Promise<string | undefined> => "old-run");
  18. const failed = vi.fn();
  19. const controller = createTopicContinuation({ readRunningRun, stopAndWait, submit, failed });
  20. return { controller, submit, stopped, stopAndWait, readRunningRun, failed };
  21. }
  22. describe("topic stop before continuation", () => {
  23. it("requests stop before attachments finish and waits for authoritative completion", async () => {
  24. const f = fixture();
  25. const material = deferred<string>();
  26. f.controller.receive("b", () => material.promise);
  27. await flush();
  28. expect(f.stopAndWait).toHaveBeenCalledWith("old-run");
  29. material.resolve("B");
  30. await flush();
  31. expect(f.submit).not.toHaveBeenCalled();
  32. f.stopped.resolve();
  33. await f.controller.settled();
  34. expect(f.submit).toHaveBeenCalledWith(["B"]);
  35. });
  36. it("combines all arrivals during stop in arrival order even when preparation completes backwards", async () => {
  37. const f = fixture();
  38. const b = deferred<string>();
  39. f.controller.receive("b", () => b.promise);
  40. f.controller.receive("c", async () => "C");
  41. await flush();
  42. f.stopped.resolve();
  43. await flush();
  44. expect(f.submit).not.toHaveBeenCalled();
  45. b.resolve("B");
  46. await f.controller.settled();
  47. expect(f.stopAndWait).toHaveBeenCalledTimes(1);
  48. expect(f.submit).toHaveBeenCalledExactlyOnceWith(["B", "C"]);
  49. });
  50. it("cancel discards pending material and cannot submit after its late completion", async () => {
  51. const f = fixture();
  52. const b = deferred<string>();
  53. f.controller.receive("b", () => b.promise);
  54. await flush();
  55. f.controller.cancel();
  56. f.stopped.resolve();
  57. b.resolve("B");
  58. await f.controller.settled();
  59. expect(f.submit).not.toHaveBeenCalled();
  60. });
  61. it("can accept a fresh message after cancelling a still-pending attachment", async () => {
  62. const f = fixture();
  63. f.readRunningRun.mockResolvedValue(undefined);
  64. const old = deferred<string>();
  65. f.controller.receive("old", () => old.promise);
  66. await flush();
  67. f.controller.cancel();
  68. f.controller.receive("fresh", async () => "fresh");
  69. await flush();
  70. expect(f.submit).toHaveBeenCalledExactlyOnceWith(["fresh"]);
  71. old.resolve("old");
  72. await f.controller.settled();
  73. expect(f.submit).toHaveBeenCalledTimes(1);
  74. });
  75. it("does not execute an incomplete batch when one required material fails", async () => {
  76. const f = fixture();
  77. f.controller.receive("b", async () => {
  78. throw new Error("download failed");
  79. });
  80. f.controller.receive("c", async () => "C");
  81. f.stopped.resolve();
  82. await f.controller.settled();
  83. expect(f.submit).not.toHaveBeenCalled();
  84. expect(f.failed).toHaveBeenCalledWith(["b", "c"], expect.any(Error));
  85. });
  86. it("deduplicates delivery and admits idle input without stopping", async () => {
  87. const f = fixture();
  88. f.readRunningRun.mockResolvedValue(undefined);
  89. f.controller.receive("b", async () => "B");
  90. f.controller.receive("b", async () => "duplicate");
  91. await f.controller.settled();
  92. expect(f.stopAndWait).not.toHaveBeenCalled();
  93. expect(f.submit).toHaveBeenCalledExactlyOnceWith(["B"]);
  94. });
  95. it("reports admission failure against the submitted batch, not later arrivals", async () => {
  96. const f = fixture();
  97. f.readRunningRun.mockResolvedValue(undefined);
  98. f.submit.mockRejectedValueOnce(new Error("admission failed"));
  99. f.controller.receive("b", async () => "B");
  100. await f.controller.settled();
  101. expect(f.failed).toHaveBeenCalledWith(["b"], expect.any(Error));
  102. });
  103. it("stops the new authoritative run when a message arrives during admission", async () => {
  104. const f = fixture();
  105. const accepted = deferred<void>();
  106. f.readRunningRun.mockResolvedValueOnce(undefined).mockResolvedValue("new-run");
  107. f.submit.mockImplementationOnce(async () => accepted.promise);
  108. f.controller.receive("b", async () => "B");
  109. await flush();
  110. f.controller.receive("c", async () => "C");
  111. accepted.resolve();
  112. await flush();
  113. expect(f.stopAndWait).toHaveBeenCalledWith("new-run");
  114. f.stopped.resolve();
  115. await f.controller.settled();
  116. expect(f.submit.mock.calls).toEqual([[["B"]], [["C"]]]);
  117. });
  118. });