providerRefreshWorkspace.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, expect, it, vi } from "vitest";
  3. import { refreshSeededOpenAIProvidersThroughSettings } from "#e2e/helpers/custom-openai-provider";
  4. import { waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  5. vi.mock("#e2e/helpers/desktop-app", () => ({
  6. DEFAULT_WORKSPACE: "/fixture/ZCodeProject",
  7. clickTestIdByDom: vi.fn(),
  8. waitForWorkspaceApp: vi.fn(),
  9. }));
  10. vi.mock("#e2e/helpers/custom-openai-provider-store", () => ({}));
  11. vi.mock("#e2e/helpers/upstream-provider", () => ({}));
  12. vi.mock("#e2e/helpers/e2e-electron-reload", () => ({}));
  13. vi.mock("#e2e/helpers/personal-provider-settings", () => ({}));
  14. vi.mock("#e2e/helpers/provider-model-metadata-dialog", () => ({}));
  15. afterEach(() => {
  16. vi.unstubAllGlobals();
  17. vi.clearAllMocks();
  18. });
  19. function install() {
  20. const document = {
  21. body: { innerText: "Fixture provider" },
  22. querySelectorAll: () => [
  23. {
  24. getAttribute: (name: string) => (name === "aria-label" ? "Refresh" : null),
  25. disabled: false,
  26. click: vi.fn(),
  27. },
  28. ],
  29. };
  30. const window = {
  31. __zcodeTabStoreE2E: { getState: () => ({ activeWorkspacePath: "/fixture/unclassified" }) },
  32. };
  33. vi.stubGlobal("$", () => ({ waitForDisplayed: vi.fn() }));
  34. vi.stubGlobal("browser", {
  35. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  36. runInNewContext(`(${fn.toString()})(...args)`, { document, window, args }),
  37. waitUntil: async (predicate: () => Promise<boolean>) => {
  38. expect(await predicate()).toBe(true);
  39. },
  40. });
  41. }
  42. it("刷新 Personal provider 后返回原未分类工作区,不等待未激活的默认项目", async () => {
  43. install();
  44. await refreshSeededOpenAIProvidersThroughSettings(["Fixture provider"]);
  45. expect(waitForWorkspaceApp).toHaveBeenCalledWith("/fixture/unclassified", 30_000);
  46. });
  47. it("显式指定的返回工作区保持优先", async () => {
  48. install();
  49. await refreshSeededOpenAIProvidersThroughSettings(["Fixture provider"], {
  50. workspacePath: "/explicit",
  51. });
  52. expect(waitForWorkspaceApp).toHaveBeenCalledWith("/explicit", 30_000);
  53. });