start-plan-selection.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { fileURLToPath } from "node:url";
  5. import {
  6. AccountProviderService,
  7. ProviderConfig,
  8. ProviderConfigMap,
  9. ZhipuAccountAccessConfig,
  10. } from "@zcode/provider";
  11. import { BUILTIN_MODEL_PROVIDER_IDS as ids } from "@zcode/shared";
  12. import { expect, it } from "vitest";
  13. import { NodeProviderRegistryRuntime } from "../src/index.js";
  14. import { createNodeModelSelectionFacade } from "../src/model-selection-facade.js";
  15. it("Host/Worker 共用 facade:Start 与付费并存,Start 不映射或迁移,付费仍按当前连接解析", async () => {
  16. const root = await mkdtemp(join(tmpdir(), "start154-selection-"));
  17. let available = true;
  18. let account!: AccountProviderService;
  19. const runtime = new NodeProviderRegistryRuntime({
  20. zcodeBuiltinFilePath: fileURLToPath(
  21. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  22. ),
  23. personalFilePath: join(root, "personal.json"),
  24. personalPollingIntervalMs: false,
  25. createAccountSource(configSource) {
  26. account = new AccountProviderService({
  27. configSource,
  28. resolve: async () => ({
  29. providers: new ProviderConfigMap(
  30. [ids.bigmodelStartPlan, ids.bigmodelTeamCodingPlan].map((id) => [
  31. id,
  32. new ProviderConfig({
  33. access: new ZhipuAccountAccessConfig({
  34. entitled: id !== ids.bigmodelStartPlan || available,
  35. }),
  36. builtinModelIds: ["GLM-5.2"],
  37. }),
  38. ]),
  39. ),
  40. states: Object.fromEntries(
  41. [ids.bigmodelStartPlan, ids.bigmodelTeamCodingPlan].map((id) => [
  42. id,
  43. {
  44. current: true,
  45. availability:
  46. id === ids.bigmodelStartPlan && !available ? "unavailable" : "available",
  47. entitled: id !== ids.bigmodelStartPlan || available,
  48. },
  49. ]),
  50. ),
  51. }),
  52. });
  53. return account;
  54. },
  55. });
  56. try {
  57. await runtime.start();
  58. const facade = createNodeModelSelectionFacade(runtime.registryService);
  59. const start = {
  60. providerId: ids.bigmodelStartPlan,
  61. modelId: "GLM-5.2",
  62. options: {
  63. reasoningLevel: facade
  64. .getView()
  65. .providers.find((p) => p.providerId === ids.bigmodelStartPlan)!
  66. .models.find((m) => m.modelId === "GLM-5.2")!.config.optionSpecs.reasoningLevel
  67. .values[0]!,
  68. },
  69. };
  70. const first = facade.getView(undefined, undefined, { selection: start });
  71. expect(first.providers.map((p) => p.providerId)).toEqual(
  72. expect.arrayContaining([ids.bigmodelStartPlan, ids.bigmodelTeamCodingPlan]),
  73. );
  74. expect(first.effectiveSelection).toEqual(start);
  75. expect({
  76. result: facade.getView(undefined, undefined, {
  77. selection: { ...start, providerId: ids.bigmodelIndividualCodingPlan },
  78. }),
  79. states: runtime.registryService.getSnapshot()?.account.states,
  80. }).toMatchObject({
  81. result: { effectiveSelection: { providerId: ids.bigmodelTeamCodingPlan } },
  82. });
  83. expect(
  84. facade.getView(undefined, undefined, {
  85. selection: { ...start, providerId: ids.zaiStartPlan },
  86. }).effectiveSelection,
  87. ).toBeNull();
  88. available = false;
  89. await account.refresh("start-expired");
  90. await runtime.registryService.refresh("start-expired");
  91. expect(
  92. facade.getView(undefined, undefined, { selection: start }).effectiveSelection,
  93. ).toBeNull();
  94. expect(start.providerId).toBe(ids.bigmodelStartPlan);
  95. } finally {
  96. runtime.dispose();
  97. account?.dispose();
  98. await rm(root, { recursive: true, force: true });
  99. }
  100. });