legacyAccountConnectionCompletion.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { mkdtemp, mkdir, readFile, writeFile, rm } 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. const originalHome = process.env.HOME;
  6. const homes: string[] = [];
  7. const oldFields = {
  8. providerFamilyDomain: "bigmodel",
  9. modelProviderFamilyModes: { bigmodel: "oauth" },
  10. modelProviderFamilySelectedKeys: {
  11. bigmodel: "team-plan:builtin:bigmodel-coding-plan:product:project",
  12. },
  13. };
  14. async function setup(raw: unknown = oldFields) {
  15. const home = await mkdtemp(join(tmpdir(), "zcode-team-migration-"));
  16. homes.push(home);
  17. process.env.HOME = home;
  18. const dir = join(home, ".zcode", "v2");
  19. await mkdir(dir, { recursive: true });
  20. const file = join(dir, "setting.json");
  21. await writeFile(file, JSON.stringify(raw));
  22. vi.resetModules();
  23. const { createSettingServiceWithMigrations } = await import("../src/setting/settingService.js");
  24. return {
  25. ...createSettingServiceWithMigrations(),
  26. read: async () => JSON.parse(await readFile(file, "utf8")),
  27. };
  28. }
  29. afterEach(async () => {
  30. if (originalHome === undefined) delete process.env.HOME;
  31. else process.env.HOME = originalHome;
  32. await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true })));
  33. });
  34. describe("旧 Team 连接组织补齐的实际设置边界", () => {
  35. it("普通设置读取和保存不把待迁移连接写成新版空选择", async () => {
  36. const { service, read } = await setup();
  37. await service.get();
  38. await service.update({ locale: "en-US" });
  39. expect(await read()).toMatchObject({ ...oldFields, locale: "en-US" });
  40. expect(await read()).not.toHaveProperty("providerFamilyConnectionSelections");
  41. });
  42. it("迁移查询可以读取代理设置并保存普通偏好,完成后保存完整身份且旧字段仍在", async () => {
  43. const { service, prepareLegacyAccountConnections, read } = await setup();
  44. const resolve = vi.fn(async () => {
  45. await service.get();
  46. await service.update({ locale: "en-US" });
  47. return "organization";
  48. });
  49. expect(await prepareLegacyAccountConnections(resolve)).toEqual([]);
  50. expect(resolve).toHaveBeenCalledWith({
  51. family: "bigmodel",
  52. productId: "product",
  53. projectId: "project",
  54. });
  55. expect(await read()).toMatchObject({
  56. ...oldFields,
  57. locale: "en-US",
  58. providerFamilyConnectionSelections: {
  59. bigmodel: {
  60. kind: "team-coding-plan",
  61. productId: "product",
  62. projectId: "project",
  63. organizationId: "organization",
  64. },
  65. },
  66. });
  67. expect(await prepareLegacyAccountConnections(resolve)).toEqual([]);
  68. expect(resolve).toHaveBeenCalledOnce();
  69. });
  70. it.each(["unknown", "failed"])("%s 保留待迁移记录,下一次现有刷新可重新尝试", async (outcome) => {
  71. const { prepareLegacyAccountConnections, read } = await setup();
  72. expect(
  73. await prepareLegacyAccountConnections(async () => {
  74. if (outcome === "failed") throw new Error("offline");
  75. return null;
  76. }),
  77. ).toEqual(["bigmodel"]);
  78. expect(await read()).not.toHaveProperty("providerFamilyConnectionSelections");
  79. expect(await prepareLegacyAccountConnections(async () => "organization")).toEqual([]);
  80. });
  81. it.each(["connection", "domain"])("等待期间用户修改 %s,迟到迁移不能覆盖", async (field) => {
  82. const { service, prepareLegacyAccountConnections, read } = await setup();
  83. await prepareLegacyAccountConnections(async () => {
  84. const before = await service.get();
  85. await service.update(
  86. field === "connection"
  87. ? { providerFamilyConnectionSelections: { bigmodel: { kind: "individual-coding-plan" } } }
  88. : { providerFamilyDomain: "zai" },
  89. {
  90. providerFamilyDomain: before.providerFamilyDomain,
  91. providerFamilyConnectionSelections: before.providerFamilyConnectionSelections,
  92. providerFamilyDomainUpdatedAt: before.providerFamilyDomainUpdatedAt ?? 0,
  93. },
  94. );
  95. return "old-organization";
  96. });
  97. const stored = await read();
  98. if (field === "connection")
  99. expect(stored.providerFamilyConnectionSelections.bigmodel).toEqual({
  100. kind: "individual-coding-plan",
  101. });
  102. else {
  103. expect(stored.providerFamilyDomain).toBe("zai");
  104. expect(stored).not.toHaveProperty("providerFamilyConnectionSelections");
  105. }
  106. });
  107. it.each([
  108. { ...oldFields, providerFamilyConnectionSelections: {} },
  109. { ...oldFields, modelProviderFamilyModes: { bigmodel: "apiKey" } },
  110. {
  111. ...oldFields,
  112. modelProviderFamilySelectedKeys: {
  113. bigmodel: "team-plan:builtin:bigmodel-coding-plan:product:org:project",
  114. },
  115. },
  116. ])("不需要组织补齐的记录不发查询", async (raw) => {
  117. const { prepareLegacyAccountConnections } = await setup(raw);
  118. const resolve = vi.fn(async () => "organization");
  119. expect(await prepareLegacyAccountConnections(resolve)).toEqual([]);
  120. expect(resolve).not.toHaveBeenCalled();
  121. });
  122. });