v4DraftModelReadinessGate.test.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // @vitest-environment jsdom
  2. import { act, renderHook, waitFor } from "@testing-library/react";
  3. import { Emitter } from "@zcode/rpc";
  4. import type { IModelSelectionService, ModelSelectionView } from "@zcode/services";
  5. import { describe, expect, it, vi } from "vitest";
  6. import {
  7. resolveModelSelectionReadinessStatus,
  8. useDraftModelReadinessGate,
  9. } from "@/v4/composer/useDraftModelReadinessGate.js";
  10. function selectionView(revision: number, withModel: boolean): ModelSelectionView {
  11. return {
  12. revision,
  13. providers: withModel
  14. ? [
  15. {
  16. providerId: "provider-a",
  17. config: { kind: "api" },
  18. models: [{ modelId: "model-a", config: {} }],
  19. },
  20. ]
  21. : [],
  22. };
  23. }
  24. function selectionService(params: {
  25. getView: () => Promise<ModelSelectionView>;
  26. emitter?: Emitter<ModelSelectionView>;
  27. }): Pick<IModelSelectionService, "getView" | "onDidChange"> {
  28. const emitter = params.emitter ?? new Emitter<ModelSelectionView>();
  29. return {
  30. getView: params.getView,
  31. onDidChange: emitter.event,
  32. };
  33. }
  34. describe("draft model readiness gate", () => {
  35. it("uses published Model Selection candidates as the only readiness fact", () => {
  36. expect(resolveModelSelectionReadinessStatus(selectionView(1, false))).toBe("missing");
  37. expect(resolveModelSelectionReadinessStatus(selectionView(2, true))).toBe("ready");
  38. });
  39. it("blocks a new draft when the Selection View confirms there are no models", async () => {
  40. const service = selectionService({ getView: vi.fn(async () => selectionView(1, false)) });
  41. const { result } = renderHook(() =>
  42. useDraftModelReadinessGate({
  43. workspacePath: "/workspace/app",
  44. sessionId: null,
  45. modelSelectionService: service,
  46. }),
  47. );
  48. await waitFor(() => expect(result.current.error).not.toBeNull());
  49. expect(result.current.agentStartupAllowed).toBe(false);
  50. await expect(result.current.ensureReadyForSend()).resolves.toBe(false);
  51. });
  52. it("does not let a stale initial read overwrite a newer Selection event", async () => {
  53. const emitter = new Emitter<ModelSelectionView>();
  54. let resolveInitial!: (view: ModelSelectionView) => void;
  55. const initial = new Promise<ModelSelectionView>((resolve) => {
  56. resolveInitial = resolve;
  57. });
  58. const service = selectionService({ getView: vi.fn(() => initial), emitter });
  59. const { result } = renderHook(() =>
  60. useDraftModelReadinessGate({
  61. workspacePath: "/workspace/app",
  62. sessionId: null,
  63. modelSelectionService: service,
  64. }),
  65. );
  66. act(() => emitter.fire(selectionView(2, true)));
  67. await waitFor(() => expect(result.current.agentStartupAllowed).toBe(true));
  68. await act(async () => resolveInitial(selectionView(1, false)));
  69. expect(result.current.agentStartupAllowed).toBe(true);
  70. expect(result.current.error).toBeNull();
  71. });
  72. it("leaves the Host admission gate in charge when Selection View reading fails", async () => {
  73. const service = selectionService({
  74. getView: vi.fn(async () => {
  75. throw new Error("registry unavailable");
  76. }),
  77. });
  78. const { result } = renderHook(() =>
  79. useDraftModelReadinessGate({
  80. workspacePath: "/workspace/app",
  81. sessionId: null,
  82. modelSelectionService: service,
  83. }),
  84. );
  85. await waitFor(() => expect(result.current.agentStartupAllowed).toBe(true));
  86. expect(result.current.error).toBeNull();
  87. });
  88. });