providerProvisioningSource.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { ModelConfigRules, parsePersonalProviderConfigMap } from "@zcode/provider";
  5. import {
  6. NodePersonalProviderConfigRepository,
  7. encodeProviderConfigFile,
  8. } from "@zcode/provider-node";
  9. import { afterEach, describe, expect, it, vi } from "vitest";
  10. import { createCredentialCipherProvider } from "../src/credential/providers/credentialCipherProvider.js";
  11. import {
  12. listProviderProvisioningCredentialKeys,
  13. createProviderProvisioningSource,
  14. } from "../src/model-provider/providerProvisioningSource.js";
  15. const cleanups: (() => Promise<void>)[] = [];
  16. afterEach(async () => {
  17. for (const cleanup of cleanups.splice(0)) await cleanup();
  18. });
  19. function personalRepository(root: string) {
  20. const repo = new NodePersonalProviderConfigRepository({
  21. filePath: join(root, "provider_config.json"),
  22. pollingIntervalMs: false,
  23. });
  24. cleanups.push(async () => {
  25. repo.dispose();
  26. await rm(root, { recursive: true, force: true });
  27. });
  28. return repo;
  29. }
  30. describe("Provider Provisioning Source", () => {
  31. it("exports sparse Personal Config, default, account settings and only provider credentials", async () => {
  32. const root = await mkdtemp(join(tmpdir(), "zcode-provider-provisioning-source-"));
  33. const credentialsPath = join(root, "credentials.json");
  34. const cipher = createCredentialCipherProvider({ env: { ZCODE_CREDENTIAL_SECRET: "source" } });
  35. await writeFile(
  36. credentialsPath,
  37. JSON.stringify({
  38. "oauth:active_provider": cipher.encrypt("zai"),
  39. "oauth:zai:access_token": cipher.encrypt("oauth-token"),
  40. "account-provider:individual-coding-plan:provider:account:user:api-key":
  41. cipher.encrypt("coding-plan-key"),
  42. "account-provider:custom-provider:identity": cipher.encrypt("account-user-id"),
  43. "ssh:password": cipher.encrypt("must-not-export"),
  44. "ssh:malformed": { unexpected: true },
  45. "account-provider:coding-plan:legacy:account:user:api-key": "enc:v1:broken",
  46. "account-provider:coding-plan:malformed:account:user:api-key": { invalid: true },
  47. }),
  48. "utf8",
  49. );
  50. const repository = personalRepository(root);
  51. await repository.update(() => ({
  52. providers: parsePersonalProviderConfigMap({
  53. providerRules: [
  54. {
  55. providerId: "custom",
  56. config: {
  57. group: "standard-personal",
  58. access: { type: "api-key", apiKey: "provider-key" },
  59. api: { type: "openai-chat-completions", baseUrl: "https://example.com/v1" },
  60. personalModelIds: ["model-a"],
  61. },
  62. },
  63. ],
  64. }),
  65. models: ModelConfigRules.empty(),
  66. providerOrder: ["custom"],
  67. defaultModelSelection: { providerId: "custom", modelId: "model-a" },
  68. }));
  69. const source = createProviderProvisioningSource({
  70. personalRepository: repository,
  71. settingService: {
  72. get: vi.fn(async () => ({
  73. providerFamilyDomain: "zai",
  74. providerFamilyConnectionSelections: { zai: { kind: "individual-coding-plan" } },
  75. })),
  76. } as never,
  77. credentialFilePath: credentialsPath,
  78. personalConfigFilePath: join(root, "provider_config.json"),
  79. cipherProvider: cipher,
  80. });
  81. const snapshot = await source.read("sync-source");
  82. expect(await listProviderProvisioningCredentialKeys(credentialsPath)).toEqual([
  83. "oauth:active_provider",
  84. "oauth:zai:access_token",
  85. ]);
  86. expect(snapshot.schemaVersion).toBe(2);
  87. expect(snapshot.personalConfig).toEqual(
  88. encodeProviderConfigFile(await repository.read()).config,
  89. );
  90. expect(snapshot.personalConfig.defaultModelSelection).toEqual({
  91. providerId: "custom",
  92. modelId: "model-a",
  93. });
  94. expect(snapshot.credentials).toEqual([
  95. { scope: "oauth-session", key: "oauth:active_provider", value: "zai" },
  96. { scope: "oauth-session", key: "oauth:zai:access_token", value: "oauth-token" },
  97. ]);
  98. expect(snapshot).not.toHaveProperty("sourceRevision");
  99. repository.dispose();
  100. });
  101. it("still rejects malformed allowlist credential records", async () => {
  102. const root = await mkdtemp(join(tmpdir(), "zcode-provider-provisioning-source-"));
  103. const credentialsPath = join(root, "credentials.json");
  104. await writeFile(
  105. credentialsPath,
  106. JSON.stringify({ "oauth:active_provider": { malformed: true } }),
  107. "utf8",
  108. );
  109. const source = createProviderProvisioningSource({
  110. personalRepository: personalRepository(root),
  111. settingService: { get: vi.fn(async () => ({})) } as never,
  112. credentialFilePath: credentialsPath,
  113. personalConfigFilePath: join(root, "provider_config.json"),
  114. });
  115. await expect(source.read("sync-malformed-allowlist")).rejects.toThrow(
  116. "Credential allowlist value must be a string",
  117. );
  118. });
  119. });