upstreamProviderPreferenceSeed.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import { ZCODE_AGENT_PROVIDER } from "@zcode/shared";
  3. import {
  4. UPSTREAM_MODEL,
  5. UPSTREAM_PROVIDER_ID,
  6. UPSTREAM_THOUGHT_LEVEL,
  7. ensureUpstreamProviderForE2E,
  8. } from "./e2e/helpers/upstream-provider.js";
  9. const desktopAppMocks = vi.hoisted(() => ({
  10. clickTestIdByDom: vi.fn(),
  11. getE2EAppDataPaths: vi.fn(),
  12. readModelProvider: vi.fn(),
  13. readModelProviders: vi.fn(),
  14. setInputValueByTestIdDom: vi.fn(),
  15. waitForDefaultWorkspaceReady: vi.fn(),
  16. waitForTestIdByDom: vi.fn(),
  17. waitForWorkspaceApp: vi.fn(),
  18. }));
  19. vi.mock("./e2e/helpers/desktop-app.js", () => ({
  20. DEFAULT_WORKSPACE: "/tmp/zcode-e2e-workspace",
  21. ...desktopAppMocks,
  22. }));
  23. function createStorage() {
  24. const values = new Map<string, string>();
  25. return {
  26. getItem: (key: string) => values.get(key) ?? null,
  27. removeItem: (key: string) => values.delete(key),
  28. setItem: (key: string, value: string) => values.set(key, value),
  29. };
  30. }
  31. describe("Upstream E2E 工具栏偏好种子", () => {
  32. beforeEach(() => {
  33. desktopAppMocks.readModelProvider.mockResolvedValue({
  34. apiKey: "e2e-fixture-key",
  35. enabled: true,
  36. models: [{ id: UPSTREAM_MODEL }],
  37. });
  38. });
  39. afterEach(() => {
  40. vi.clearAllMocks();
  41. vi.unstubAllGlobals();
  42. });
  43. it("按 workspace App Recent 协议写入结构化稀疏选择", async () => {
  44. const storage = createStorage();
  45. vi.stubGlobal("window", { localStorage: storage });
  46. vi.stubGlobal("browser", {
  47. execute: vi.fn(async (script: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  48. Reflect.apply(script, undefined, args),
  49. ),
  50. });
  51. await ensureUpstreamProviderForE2E({ skipToolbarSelection: true });
  52. expect(storage.getItem("zcode-model-selection-recent-v1:/tmp/zcode-e2e-workspace")).toBe(
  53. JSON.stringify({
  54. providerId: UPSTREAM_PROVIDER_ID,
  55. modelId: UPSTREAM_MODEL,
  56. ...(UPSTREAM_THOUGHT_LEVEL.trim()
  57. ? { options: { reasoningLevel: UPSTREAM_THOUGHT_LEVEL.trim() } }
  58. : {}),
  59. }),
  60. );
  61. expect(storage.getItem("zcode-last-agent-provider")).toBe(ZCODE_AGENT_PROVIDER);
  62. });
  63. });