useProviderModelDraft.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @vitest-environment jsdom
  2. import type { ModelConfigResolution } from "@zcode/provider";
  3. import { act, renderHook } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { useProviderModelDraft } from "@/settings/model-provider-section/useProviderModelDraft.js";
  6. import { smartDraftModel } from "./providerModelSmartDraftFixture.js";
  7. afterEach(() => vi.useRealTimers());
  8. describe("Todo90 shared draft lifecycle", () => {
  9. it("关闭模式拒绝在途推荐;重新开启不复用旧模式的回包", async () => {
  10. const model = smartDraftModel();
  11. const pending: ((value: ModelConfigResolution) => void)[] = [];
  12. const resolve = vi.fn(
  13. () => new Promise<ModelConfigResolution>((finish) => pending.push(finish)),
  14. );
  15. const { result } = renderHook(() =>
  16. useProviderModelDraft({ model, open: true, scopeKey: "p", resolve }),
  17. );
  18. const value = { inheritedConfig: model.config, effectiveConfig: model.config, issues: [] };
  19. act(() => result.current.change({ idValue: "b" }));
  20. act(() => {
  21. void result.current.flush();
  22. });
  23. await act(async () => pending[0]!(value));
  24. act(() => {
  25. void result.current.flush();
  26. });
  27. act(() => result.current.change({ useRecommendedConfigValue: false }));
  28. const fixed = result.current.draft;
  29. await act(async () => pending[1]!({ ...value, inheritedConfig: {} }));
  30. expect(result.current.draft).toEqual(fixed);
  31. act(() => result.current.change({ useRecommendedConfigValue: true }));
  32. expect(result.current.inheritedConfig).toBeUndefined();
  33. act(() => {
  34. void result.current.flush();
  35. });
  36. await act(async () => pending[2]!(value));
  37. expect(result.current.inheritedConfig).toEqual(model.config);
  38. });
  39. it("does not resolve by ID while fixed, including save; re-enable resolves current ID", async () => {
  40. const model = smartDraftModel();
  41. const resolve = vi.fn(async () => ({
  42. inheritedConfig: model.config,
  43. effectiveConfig: model.config,
  44. issues: [],
  45. }));
  46. const { result } = renderHook(() =>
  47. useProviderModelDraft({ model, open: true, scopeKey: "p", resolve }),
  48. );
  49. act(() => result.current.change({ useRecommendedConfigValue: false }));
  50. act(() => result.current.change({ idValue: "b" }));
  51. await act(async () => {
  52. expect((await result.current.commit()).status).toBe("commit");
  53. });
  54. expect(resolve).not.toHaveBeenCalled();
  55. act(() => result.current.change({ useRecommendedConfigValue: true }));
  56. await act(async () => {
  57. await result.current.flush();
  58. });
  59. expect(resolve).toHaveBeenCalledWith("b", {});
  60. });
  61. it("discards A→B→A late responses, close and scope changes", async () => {
  62. const model = { ...smartDraftModel(), modelId: "initial" };
  63. const pending: { id: string; finish: (value: ModelConfigResolution) => void }[] = [];
  64. const resolve = vi.fn(
  65. (id: string) => new Promise<ModelConfigResolution>((finish) => pending.push({ id, finish })),
  66. );
  67. const { result, rerender } = renderHook(
  68. ({ open, scope }) => useProviderModelDraft({ model, open, scopeKey: scope, resolve }),
  69. { initialProps: { open: true, scope: "p" } },
  70. );
  71. const request = async (id: string) => {
  72. act(() => result.current.change({ idValue: id }));
  73. act(() => {
  74. void result.current.flush();
  75. });
  76. };
  77. await request("a");
  78. await request("b");
  79. await request("a");
  80. const finish = (index: number, contextWindow: number) => {
  81. const config = structuredClone(model.config);
  82. config.properties!.contextWindow = contextWindow;
  83. pending[index]!.finish({ inheritedConfig: config, effectiveConfig: config, issues: [] });
  84. };
  85. await act(async () => finish(2, 333333));
  86. expect(result.current.inheritedConfig?.properties?.contextWindow).toBe(333333);
  87. await act(async () => {
  88. finish(0, 111111);
  89. finish(1, 222222);
  90. });
  91. expect(result.current.inheritedConfig?.properties?.contextWindow).toBe(333333);
  92. await request("c");
  93. rerender({ open: false, scope: "p" });
  94. await act(async () => finish(3, 444444));
  95. rerender({ open: true, scope: "other" });
  96. expect(result.current.inheritedConfig?.properties?.contextWindow).not.toBe(444444);
  97. expect(result.current.draft.idValue).toBe("initial");
  98. });
  99. it("failed recommendation never commits stale old-model values", async () => {
  100. const model = smartDraftModel();
  101. const resolve = vi.fn(async () => {
  102. throw new Error("recommendations offline");
  103. });
  104. const { result } = renderHook(() =>
  105. useProviderModelDraft({ model, open: true, scopeKey: "p", resolve }),
  106. );
  107. act(() => result.current.change({ idValue: "b", contextWindowValue: "123456" }));
  108. await act(async () => {
  109. await expect(result.current.commit()).rejects.toThrow("recommendations offline");
  110. });
  111. expect(result.current.draft.contextWindowValue).toBe("123456");
  112. });
  113. });