independentPlanSupport.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. ensureIndependentPlanSupport,
  4. supportsHistoryFirst,
  5. } from "../src/zcode-agent/independentPlanSupport.js";
  6. describe("独立 Plan 执行端能力", () => {
  7. it("复用同一 CLI 的成功检查,重建 CLI 后重新检查", async () => {
  8. const first = { request: vi.fn().mockResolvedValue({ independentPlanState: true }) };
  9. await ensureIndependentPlanSupport(first as never);
  10. await ensureIndependentPlanSupport(first as never);
  11. expect(first.request).toHaveBeenCalledTimes(1);
  12. const second = { request: vi.fn().mockResolvedValue({ independentPlanState: true }) };
  13. await ensureIndependentPlanSupport(second as never);
  14. expect(second.request).toHaveBeenCalledTimes(1);
  15. });
  16. it("旧 CLI 不认识能力查询时拒绝,不能只相信新 Host 的版本", async () => {
  17. const client = { request: vi.fn().mockRejectedValue(new Error("Method not found")) };
  18. await expect(ensureIndependentPlanSupport(client as never)).rejects.toThrow(
  19. "proto.independentPlanUnsupported",
  20. );
  21. });
  22. });
  23. describe("历史先显示执行端能力", () => {
  24. it("同一 CLI 只查询一次,只有显式 true 才开启", async () => {
  25. const client = { request: vi.fn().mockResolvedValue({ historyFirst: true }) };
  26. expect(
  27. await Promise.all([
  28. supportsHistoryFirst(client as never),
  29. supportsHistoryFirst(client as never),
  30. ]),
  31. ).toEqual([true, true]);
  32. expect(client.request).toHaveBeenCalledTimes(1);
  33. expect(
  34. await supportsHistoryFirst({
  35. request: vi.fn().mockResolvedValue({ independentPlanState: true }),
  36. } as never),
  37. ).toBe(false);
  38. });
  39. it("旧方法或临时请求失败不发送新订阅字段,下一次可重新协商", async () => {
  40. const client = {
  41. request: vi
  42. .fn()
  43. .mockRejectedValueOnce(new Error("Method not found"))
  44. .mockResolvedValue({ historyFirst: true }),
  45. };
  46. expect(await supportsHistoryFirst(client as never)).toBe(false);
  47. expect(await supportsHistoryFirst(client as never)).toBe(true);
  48. });
  49. });