providerProvisioningSource.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.credentials.some((entry) => entry.key === "ssh:password")).toBe(false);
  99. expect(snapshot.credentials.some((entry) => entry.key.endsWith(":identity"))).toBe(false);
  100. expect(snapshot).not.toHaveProperty("sourceRevision");
  101. repository.dispose();
  102. });
  103. it("does not let malformed non-allowlist credential records block sync", async () => {
  104. const root = await mkdtemp(join(tmpdir(), "zcode-provider-provisioning-source-"));
  105. const credentialsPath = join(root, "credentials.json");
  106. const cipher = createCredentialCipherProvider({ env: { ZCODE_CREDENTIAL_SECRET: "source" } });
  107. await writeFile(
  108. credentialsPath,
  109. JSON.stringify({
  110. "oauth:active_provider": cipher.encrypt("zai"),
  111. "ssh:malformed": { unexpected: true },
  112. "account-provider:coding-plan:legacy:account:user:api-key": "enc:v1:broken",
  113. "account-provider:coding-plan:malformed:account:user:api-key": { invalid: true },
  114. }),
  115. "utf8",
  116. );
  117. const source = createProviderProvisioningSource({
  118. personalRepository: personalRepository(root),
  119. settingService: { get: vi.fn(async () => ({})) } as never,
  120. credentialFilePath: credentialsPath,
  121. personalConfigFilePath: join(root, "provider_config.json"),
  122. cipherProvider: cipher,
  123. });
  124. await expect(source.read("sync-malformed-unrelated")).resolves.toMatchObject({
  125. credentials: [{ scope: "oauth-session", key: "oauth:active_provider", value: "zai" }],
  126. });
  127. });
  128. it("still rejects malformed allowlist credential records", async () => {
  129. const root = await mkdtemp(join(tmpdir(), "zcode-provider-provisioning-source-"));
  130. const credentialsPath = join(root, "credentials.json");
  131. await writeFile(
  132. credentialsPath,
  133. JSON.stringify({ "oauth:active_provider": { malformed: true } }),
  134. "utf8",
  135. );
  136. const source = createProviderProvisioningSource({
  137. personalRepository: personalRepository(root),
  138. settingService: { get: vi.fn(async () => ({})) } as never,
  139. credentialFilePath: credentialsPath,
  140. personalConfigFilePath: join(root, "provider_config.json"),
  141. });
  142. await expect(source.read("sync-malformed-allowlist")).rejects.toThrow(
  143. "Credential allowlist value must be a string",
  144. );
  145. });
  146. });