offPeakDispatchPlan.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { describe, expect, it } from "vitest";
  2. import { OffPeakPermanentDispatchError } from "@zcode/services/node";
  3. import {
  4. assertBoundSessionDispatchable,
  5. OffPeakBoundSessionBusyError,
  6. OffPeakBoundSessionDeletedError,
  7. resolveOffPeakDispatchKind,
  8. } from "../src/host/offPeakDispatchPlan.js";
  9. describe("resolveOffPeakDispatchKind(D50 绑定首跑分支)", () => {
  10. it("conversationId 已回填 → 续跑;仅 sessionId → 绑定首跑;两者皆空 → 新建 session", () => {
  11. expect(resolveOffPeakDispatchKind({ conversationId: "sess", sessionId: "sess" })).toBe("resume");
  12. expect(resolveOffPeakDispatchKind({ sessionId: "sess" })).toBe("bound-first-run");
  13. expect(resolveOffPeakDispatchKind({})).toBe("init");
  14. expect(resolveOffPeakDispatchKind({ sessionId: " " })).toBe("init");
  15. });
  16. });
  17. describe("assertBoundSessionDispatchable", () => {
  18. it("会话已删除 → permanent(不再退避重试)", () => {
  19. expect(() =>
  20. assertBoundSessionDispatchable({ sessionId: "sess", deleted: true, running: false }),
  21. ).toThrow(OffPeakBoundSessionDeletedError);
  22. expect(new OffPeakBoundSessionDeletedError("sess")).toBeInstanceOf(OffPeakPermanentDispatchError);
  23. });
  24. it("会话正忙 → transient busy,且不是 permanent", () => {
  25. expect(() =>
  26. assertBoundSessionDispatchable({ sessionId: "sess", deleted: false, running: true }),
  27. ).toThrow(OffPeakBoundSessionBusyError);
  28. expect(new OffPeakBoundSessionBusyError("sess")).not.toBeInstanceOf(
  29. OffPeakPermanentDispatchError,
  30. );
  31. });
  32. it("空闲且存在 → 放行", () => {
  33. expect(() =>
  34. assertBoundSessionDispatchable({ sessionId: "sess", deleted: false, running: false }),
  35. ).not.toThrow();
  36. });
  37. });