modelProviderRestartRuntime.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { mkdtemp, readFile, stat } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. describe("model provider restart runtime", () => {
  6. afterEach(() => {
  7. vi.unstubAllEnvs();
  8. vi.unstubAllGlobals();
  9. vi.resetModules();
  10. });
  11. function installStorage(records: Record<string, string> = {}) {
  12. const scope: { clientNamespace: string; hostId: string; projectId: string | null } = {
  13. clientNamespace: "desktop",
  14. hostId: "local",
  15. projectId: "project-a",
  16. };
  17. const recentKey = 'zcode-composer-recent:v2:["desktop","local","project-a"]';
  18. vi.stubGlobal("browser", {
  19. execute: async (script: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  20. Reflect.apply(script, undefined, args),
  21. });
  22. return import("./e2e/helpers/desktop-app.js").then(({ getE2EAppDataPaths }) => {
  23. const mappingKey = `zcode-composer-workspace-scope:v1:${JSON.stringify(["desktop", getE2EAppDataPaths().workspace])}`;
  24. const values = { [mappingKey]: JSON.stringify(scope), ...records };
  25. vi.stubGlobal("window", {
  26. __zcodeSessionStoreE2E: {
  27. getState: () => ({
  28. workspaces: { [getE2EAppDataPaths().workspace]: { draftProjectId: scope.projectId } },
  29. }),
  30. },
  31. localStorage: {
  32. getItem: (key: string) => values[key] ?? null,
  33. setItem: (key: string, value: string) => {
  34. values[key] = value;
  35. },
  36. },
  37. });
  38. return { values, recentKey, scope };
  39. });
  40. }
  41. it("读取当前项目的 Recent,忽略其他项目和旧 workspace 选择", async () => {
  42. const { values, recentKey } = await installStorage({
  43. 'zcode-composer-recent:v2:["desktop","local","project-b"]': JSON.stringify({
  44. modelSelection: { providerId: "wrong", modelId: "wrong" },
  45. }),
  46. });
  47. const { getE2EAppDataPaths } = await import("./e2e/helpers/desktop-app.js");
  48. values[`zcode-model-selection-recent-v1:${getE2EAppDataPaths().workspace}`] = JSON.stringify({
  49. providerId: "legacy",
  50. modelId: "legacy",
  51. });
  52. values[recentKey] = JSON.stringify({
  53. modelSelection: {
  54. providerId: "provider-a",
  55. modelId: "model-a",
  56. options: { reasoningLevel: "high" },
  57. },
  58. mode: "yolo",
  59. });
  60. const { readLastSelectedAgentConfig } = await import("./e2e/helpers/model-provider-restart.js");
  61. expect(await readLastSelectedAgentConfig()).toEqual({
  62. schemaVersion: 1,
  63. model: "provider-a/model-a",
  64. thoughtLevel: "high",
  65. });
  66. });
  67. it("播种当前项目结构化 Recent,保留已有权限且不覆盖其他项目", async () => {
  68. const { values, recentKey } = await installStorage();
  69. const otherKey = 'zcode-composer-recent:v2:["desktop","local","project-b"]';
  70. const otherSelection = JSON.stringify({
  71. modelSelection: { providerId: "other", modelId: "other" },
  72. });
  73. values[otherKey] = otherSelection;
  74. values[recentKey] = JSON.stringify({ mode: "yolo" });
  75. const { seedPersistedModelSelection, readLastSelectedAgentConfig } =
  76. await import("./e2e/helpers/model-provider-restart.js");
  77. await seedPersistedModelSelection({
  78. providerId: "provider-a",
  79. modelId: "model-a",
  80. reasoningLevel: "max",
  81. });
  82. expect(JSON.parse(values[recentKey]!)).toMatchObject({
  83. mode: "yolo",
  84. modelSelection: {
  85. providerId: "provider-a",
  86. modelId: "model-a",
  87. options: { reasoningLevel: "max" },
  88. },
  89. });
  90. expect((await readLastSelectedAgentConfig())?.thoughtLevel).toBe("max");
  91. expect(values[otherKey]).toBe(otherSelection);
  92. expect(
  93. Object.keys(values).some((key) => key.startsWith("zcode-model-selection-recent-v1")),
  94. ).toBe(false);
  95. });
  96. it("首次未选项目草稿没有导航缓存时,使用真实草稿的 null 项目作用域", async () => {
  97. const { values, scope } = await installStorage();
  98. scope.projectId = null;
  99. for (const key of Object.keys(values)) delete values[key];
  100. const { seedPersistedModelSelection } = await import("./e2e/helpers/model-provider-restart.js");
  101. await seedPersistedModelSelection({
  102. providerId: "provider-a",
  103. modelId: "model-a",
  104. reasoningLevel: "high",
  105. });
  106. expect(
  107. JSON.parse(values['zcode-composer-recent:v2:["desktop","local",null]']!).modelSelection
  108. .modelId,
  109. ).toBe("model-a");
  110. });
  111. it("Recent 只有 mode 时不伪造模型配置", async () => {
  112. const { values, recentKey } = await installStorage();
  113. values[recentKey] = JSON.stringify({ mode: "yolo" });
  114. const { readLastSelectedAgentConfig } = await import("./e2e/helpers/model-provider-restart.js");
  115. expect(await readLastSelectedAgentConfig()).toBeNull();
  116. });
  117. it("通过正式 Model Selection Config 设置冷启动默认模型", async () => {
  118. const homeDir = await mkdtemp(join(tmpdir(), "zcode-restart-selection-"));
  119. vi.stubEnv("ZCODE_E2E_HOME_DIR", homeDir);
  120. const { seedTurboAgentStartupModel } =
  121. await import("./e2e/helpers/model-provider-restart-runtime.js");
  122. await seedTurboAgentStartupModel("provider-a", "model-a");
  123. expect(
  124. JSON.parse(await readFile(join(homeDir, ".zcode", "v2", "provider_config.json"), "utf-8")),
  125. ).toEqual({
  126. schemaVersion: 1,
  127. config: {
  128. providerOrder: [],
  129. providerConfigRules: { providerRules: [] },
  130. modelConfigRules: { providerModelRules: [], manualProviderModelRules: [] },
  131. defaultModelSelection: { providerId: "provider-a", modelId: "model-a" },
  132. },
  133. });
  134. await expect(
  135. stat(join(homeDir, "ZCodeProject", ".zcode", "config.json")),
  136. ).rejects.toMatchObject({ code: "ENOENT" });
  137. });
  138. });