{"version":3,"file":"secretmanager-5olfnI1b.mjs","names":[],"sources":["../src/vitest/mocks/shared.ts","../src/vitest/mocks/secretmanager.ts"],"sourcesContent":["// ---------------------------------------------------------------------------\n// Shared helpers\n// ---------------------------------------------------------------------------\n\n// Attach a non-enumerable `Symbol.dispose` to a facade so it works with `using`.\nexport function withDispose<T extends object>(facade: T, dispose: () => void): T & Disposable {\n  Object.defineProperty(facade, Symbol.dispose, {\n    value: dispose,\n    enumerable: false,\n    writable: true,\n    configurable: true,\n  });\n  return facade as T & Disposable;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tailorRoot(): Record<string, any> {\n  const g = globalThis as Record<string, unknown>;\n  if (!g.tailor) {\n    // Ensure the container (and the always-present context stub) exists even if\n    // the base globals were not installed (e.g. a unit test that only acquires\n    // a single mock without the tailor-runtime environment).\n    g.tailor = { context: { getInvoker: () => null } };\n  }\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  return g.tailor as Record<string, any>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function tailordbRoot(): Record<string, any> {\n  const g = globalThis as Record<string, unknown>;\n  if (!g.tailordb) {\n    g.tailordb = {};\n  }\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  return g.tailordb as Record<string, any>;\n}\n","import { vi } from \"vitest\";\nimport { assertDefined } from \"#/utils/assert\";\nimport { tailorRoot, withDispose } from \"./shared\";\n\ninterface SecretCall {\n  method: \"getSecret\" | \"getSecrets\";\n  vault: string;\n  name?: string;\n  names?: readonly string[];\n}\n\n/** Initial fixtures for a Secret Manager mock. */\nexport interface MockSecretmanagerOptions {\n  /** Secrets to merge over fixtures inherited from the currently installed mock. */\n  secrets?: Record<string, Record<string, string>>;\n}\n\n// ---------------------------------------------------------------------------\n// SecretManager Mock\n// ---------------------------------------------------------------------------\n\n// Hidden accessor key used to inherit the previous scope's secret store on\n// acquisition (so secrets seeded once outside tests — e.g. from tailor.config.ts\n// via setup.ts — remain visible) while still isolating per-test overrides.\nconst SECRET_STORE = Symbol(\"tailorSecretStore\");\n\n/**\n * Acquire a disposable mock for `tailor.secretmanager`. The secret store is\n * inherited (cloned) from the currently-installed mock on acquisition and\n * restored on dispose, so secrets seeded outside the test survive across\n * `using` scopes while per-test `setSecrets()` overrides stay isolated.\n * @param options - Initial Secret Manager fixtures\n * @returns Disposable SecretManager mock control object\n * @example\n * ```typescript\n * import { mockSecretmanager } from \"@tailor-platform/sdk/vitest\";\n *\n * test(\"reads secrets from vault\", async () => {\n *   using sm = mockSecretmanager({ secrets: { \"my-vault\": { API_KEY: \"sk-123\" } } });\n *   sm.setSecret(\"my-vault\", \"API_KEY\", \"replacement\");\n *   // …\n * });\n * ```\n */\nexport function mockSecretmanager(options: MockSecretmanagerOptions = {}) {\n  const root = tailorRoot();\n  const prev = root.secretmanager;\n\n  const holder: { store: Record<string, Record<string, string>> } = {\n    // prior mock state may be absent\n    // oxlint-disable-next-line typescript/no-unnecessary-condition\n    store: structuredClone((prev?.[SECRET_STORE]?.store as typeof holder.store) ?? {}),\n  };\n  for (const [vault, secrets] of Object.entries(options.secrets ?? {})) {\n    holder.store[vault] = { ...holder.store[vault], ...secrets };\n  }\n\n  async function defaultGetSecret(vault: string, name: string): Promise<string | undefined> {\n    return holder.store[vault]?.[name];\n  }\n\n  async function defaultGetSecrets<const T extends readonly string[]>(\n    vault: string,\n    names: T,\n  ): Promise<Partial<Record<T[number], string>>> {\n    const vaultData = holder.store[vault] ?? {};\n    const result: Record<string, string> = {};\n    for (const name of names) {\n      if (name in vaultData) {\n        result[name] = assertDefined(vaultData[name], `vault entry missing for: ${name}`);\n      }\n    }\n    return result as Partial<Record<T[number], string>>;\n  }\n\n  const getSecret = vi.fn(defaultGetSecret);\n  const getSecrets = vi.fn(defaultGetSecrets);\n\n  root.secretmanager = { getSecret, getSecrets, [SECRET_STORE]: holder };\n\n  const facade = {\n    /** The `getSecret` `vi.fn`. */\n    getSecret,\n    /** The `getSecrets` `vi.fn`. */\n    getSecrets,\n\n    setSecrets(secrets: Record<string, Record<string, string>>): void {\n      holder.store = secrets;\n    },\n\n    setSecret(vault: string, name: string, value: string): void {\n      holder.store = {\n        ...holder.store,\n        [vault]: { ...holder.store[vault], [name]: value },\n      };\n    },\n\n    mergeSecrets(vault: string, secrets: Record<string, string>): void {\n      holder.store = {\n        ...holder.store,\n        [vault]: { ...holder.store[vault], ...secrets },\n      };\n    },\n\n    get calls(): SecretCall[] {\n      // Merge both methods' calls back into chronological order via vi.fn's\n      // global invocationCallOrder, so a test mixing getSecret/getSecrets sees\n      // them in the order they actually ran (not all getSecret, then all getSecrets).\n      const entries: { order: number; call: SecretCall }[] = [\n        ...getSecret.mock.calls.map((args, i) => ({\n          order: getSecret.mock.invocationCallOrder[i] ?? 0,\n          call: { method: \"getSecret\" as const, vault: args[0], name: args[1] },\n        })),\n        ...getSecrets.mock.calls.map((args, i) => ({\n          order: getSecrets.mock.invocationCallOrder[i] ?? 0,\n          call: {\n            method: \"getSecrets\" as const,\n            vault: args[0],\n            names: args[1],\n          },\n        })),\n      ];\n      return entries.toSorted((a, b) => a.order - b.order).map((e) => e.call);\n    },\n\n    clear(): void {\n      getSecret.mockClear();\n      getSecrets.mockClear();\n    },\n\n    reset(): void {\n      holder.store = {};\n      getSecret.mockReset();\n      getSecret.mockImplementation(defaultGetSecret);\n      getSecrets.mockReset();\n      getSecrets.mockImplementation(defaultGetSecrets);\n    },\n  };\n\n  return withDispose(facade, () => {\n    root.secretmanager = prev;\n  });\n}\n"],"mappings":"sEAKA,SAAgB,YAA8B,EAAW,EAAqC,CAO5F,OANA,OAAO,eAAe,EAAQ,OAAO,QAAS,CAC5C,MAAO,EACP,WAAY,GACZ,SAAU,GACV,aAAc,EAChB,CAAC,EACM,CACT,CAGA,SAAgB,YAAkC,CAChD,IAAM,EAAI,WAQV,MAPA,CAIE,EAAE,SAAS,CAAE,QAAS,CAAE,eAAkB,IAAK,CAAE,EAG5C,EAAE,MACX,CAGA,SAAgB,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